CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2011
    Posts
    4

    General Java Programming

    Okay hi guys. I'm new here, but I have a question which my whole class can't solve.

    I don't know if outside links are allowed but
    Code:
    Introduction:
    
    SP Cake Shop sells five flavors of cake -- Strawberry, Chocolate, Vanilla, Almond and Durian. The shop has approached you to write a program for one of their newly designed self-service machine.
    
    The customer will first choose a cake and pay for it. The machine will dispense 1 dollar, 50 cents, 20 cents and 10 cents coins depending on the price of the cake and the payment received from the customer.
    
    1.	Overview 
    
    You are required to write Java classes that compute the change to be dispensed based on customer’s choice and the payment.
    
    The screenshots below illustrate the samples of the program output:
    
    
    Figure1.1: The program displays the 5 types of cakes, their prices and the number of cakes available respectively. If the user input is invalid, it displays an error message and prompts for re-input. You may assume there are 50 cakes available for each type of the cakes when the application is launches.
    
    Figure1.2: When the user input is valid, it displays the price for the selected cake.
    
    Figure1.3: The program prompts user to enter the payment, the payment should be more than or equal to the price. If the input is invalid, it prompts for the error and ask for the re-input.
                                    
    Figure1.4:  The program computes the change and then displays the change.
    
    Figure1.5:  The program displays the number of coins to be dispensed for each category of 1 dollar, 50 cents, 20 cents and 10 cents respectively. (Assume that the machine only accepts and dispenses the above types of coins).
    
    Figure1.6:  The program continues and asks the user whether he wants more cake. The program displays the menu if the user’s input is ‘1’, the number of cakes available is updated in the menu.
    
    Figure1.7:  The program displays the price when the user enters a valid input.
                                                      
    Figure1.8:  The program reads the payment and displays the change.
    
    Figure1.9:  The program displays the number of coins to be dispensed.
                                  
    Figure1.10:  The program continues and asks the user whether he wants more cake. If the user’s input is not ‘1’, it displays the "Thank you" message as shown and terminates.
    <-- this is my assignment.

    Now problem is. I've coded all the way up to figure 1.5 but starting from 1.6, I'm stuck. I know nobody is going to help me do my homework but can someone explain to me how should I continue?


    So this is what I've got up till now, if it can be made more efficient, please advice.
    Code:
    public class Cake1 {
        public static void main(String[] args) {
            int price, change, c1, c2, c3, c4;
            int nCakes=50;
    
            String type=JOptionPane.showInputDialog
                        (null,
                        "Enter your choice of cake:\n\n"
                        + "1. Strawberry(90cents),   no. of cakes available: "+nCakes+"\n"
                        + "2. Chocolate(80cents),     no. of cakes available: "+nCakes+"\n"
                        + "3. Vanilla(70cents),           no. of cakes available: "+nCakes+"\n"
                        + "4. Almond(100cents),       no. of cakes available: "+nCakes+"\n"
                        + "5. Durian(120cents),         no. of cakes available: "+nCakes+"\n","SP cake shop",
                        JOptionPane.QUESTION_MESSAGE);
    
           int readChoice=Integer.parseInt(type);
    
                    //Prompt error message if its not 1 to 5
                    if((readChoice < 1) || (readChoice > 5)){
                     JOptionPane.showMessageDialog(null,"Invalid input! Please enter in the range from 1 to 5.","Error",
                             JOptionPane.ERROR_MESSAGE);
                    }
    
        int cost;
          switch(readChoice){
    
          case 1: JOptionPane.showMessageDialog(null, "The price is 90 cents" );
                      cost = 90;
                      do{
                     price = Integer.parseInt(JOptionPane.showInputDialog(null,"Please enter your payment (The price is "
                             +cost+ " cents)","SP cake shop",JOptionPane.QUESTION_MESSAGE));
                        
    
                      //Prompt error message if the payment is less than cost
                     if(price < cost){
                     JOptionPane.showMessageDialog(null,"Invalid input! Please enter a minimum payment of "+cost+ " cents",
                             "Error", JOptionPane.ERROR_MESSAGE);
                              }
                          } while(price < cost);
                     change = price - cost;
                     JOptionPane.showMessageDialog(null, "The change is "+change+ " cents.");
    
                      c1 = (change / 100);
                      c2 = (change &#37; 100/50);
                      c3 = (change % 100%50/20);
                      c4 = (change % 100%50%20/10);
    
                     JOptionPane.showMessageDialog(null,
                        "Dispensing.......\n "
                        + c1 +" x One dollar\n"
                        + c2 +" x 50c coin\n"
                        + c3 +" x 20c coin\n"
                        + c4 +" x 10c coin\n");
                      break;
    
         case 2: JOptionPane.showMessageDialog(null, "The price is 80 cents");
                      cost = 80;
                      do{
                      price = Integer.parseInt(JOptionPane.showInputDialog(null,"Please enter your payment (The price is " 
                              +cost+ "cents)","SP cake shop",JOptionPane.QUESTION_MESSAGE));
    
                     //Prompt error message if the payment is less than cost
                     if(price < cost){
                     JOptionPane.showMessageDialog(null,"Invalid input! Please enter a minimum payment of "+cost+ " cents",
                             "Error", JOptionPane.ERROR_MESSAGE);
                                }
                         }while(price < cost);
                      change = price - cost;
                     JOptionPane.showMessageDialog(null, "The change is "+change+ " cents.");
    
                      c1 = (change / 100);
                      c2 = (change % 100/50);
                      c3 = (change % 100%50/20);
                      c4 = (change % 100%50%20/10);
                     
                     
                     JOptionPane.showMessageDialog(null,
                        "Dispensing.......\n "
                        + c1 +" x One dollar\n"
                        + c2 +" x 50c coin\n"
                        + c3 +" x 20c coin\n"
                        + c4 +" x 10c coin\n");
                     break;
    
          case 3: JOptionPane.showMessageDialog(null, "The price is 70 cents");
                     cost = 70;
                     do{
                     price = Integer.parseInt(JOptionPane.showInputDialog(null,"Please enter your payment (The price is " 
                             +cost+ "cents)","SP cake shop",JOptionPane.QUESTION_MESSAGE));
    
                    //Prompt error message if the payment is less than cost
                    if(price < cost){
                     JOptionPane.showMessageDialog(null,"Invalid input! Please enter a minimum payment of "+cost+ " cents",
                             "Error", JOptionPane.ERROR_MESSAGE);
                            }
                         }while(price < cost);
                     change = price - cost;
                     JOptionPane.showMessageDialog(null, "The change is "+change+ " cents.");
    
                      c1 = (change / 100);
                      c2 = (change % 100/50);
                      c3 = (change % 100%50/20);
                      c4 = (change % 100%50%20/10);
    
    
                     JOptionPane.showMessageDialog(null,
                        "Dispensing.......\n "
                        + c1 +" x One dollar\n"
                        + c2 +" x 50c coin\n"
                        + c3 +" x 20c coin\n"
                        + c4 +" x 10c coin\n");
                     break;
    
          case 4: JOptionPane.showMessageDialog(null, "The price is 100 cents");
                     cost = 100;
                     do{
                 price = Integer.parseInt(JOptionPane.showInputDialog(null,"Please enter your payment (The price is " 
                         +cost+ "cents)","SP cake shop",JOptionPane.QUESTION_MESSAGE));
                       
                    //Prompt error message if the payment is less than cost
                     if(price < cost){
                     JOptionPane.showMessageDialog(null,"Invalid input! Please enter a minimum payment of "+cost+ " cents",
                             "Error", JOptionPane.ERROR_MESSAGE);
                             }
                         }while(price < cost);
                     change = price - cost;
                     JOptionPane.showMessageDialog(null, "The change is "+change+ " cents.");
    
                      c1 = (change / 100);
                      c2 = (change % 100/50);
                      c3 = (change % 100%50/20);
                      c4 = (change % 100%50%20/10);
    
    
                        JOptionPane.showMessageDialog(null,
                        "Dispensing.......\n "
                        + c1 +" x One dollar\n"
                        + c2 +" x 50c coin\n"
                        + c3 +" x 20c coin\n"
                        + c4 +" x 10c coin\n");
                     break;
    
          case 5: JOptionPane.showMessageDialog(null, "The price is 120 cents");
                     cost = 120;
                     do{
                     price = Integer.parseInt(JOptionPane.showInputDialog(null,"Please enter your payment (The price is " 
                             +cost+ "cents)","SP cake shop",JOptionPane.QUESTION_MESSAGE));
    
                     //Prompt error message if the payment is less than cost
                     if(price < cost){
                     JOptionPane.showMessageDialog(null,"Invalid input! Please enter a minimum payment of "+cost+ " cents",
                             "Error", JOptionPane.ERROR_MESSAGE);
                            }
                       }while(price < cost);
    
                     change = price - cost;
                     JOptionPane.showMessageDialog(null, "The change is "+change+ " cents.");
    
                      c1 = (change / 100);
                      c2 = (change % 100/50);
                      c3 = (change % 100%50/20);
                      c4 = (change % 100%50%20/10);
    
    
                        JOptionPane.showMessageDialog(null,
                        "Dispensing.......\n "
                        + c1 +" x One dollar\n"
                        + c2 +" x 50c coin\n"
                        + c3 +" x 20c coin\n"
                        + c4 +" x 10c coin\n");
                     break;
    
            }       
          
              int contCake1=Integer.parseInt(JOptionPane.showInputDialog(null, "Do you want mor drinks?\n(Enter 1 for yes, other "
                  + "numbers for no","Input",JOptionPane.QUESTION_MESSAGE));
    
    
    
           
        }
    }
    This is not supposed to be the main class btw, just that I have no idea how to continue, so I had to leave it as that for the time being.
    Last edited by TM101; January 31st, 2011 at 11:46 PM.

  2. #2
    Join Date
    Jan 2011
    Posts
    4

    Re: General Java Programming

    Somebody please help. If I don't get it out by wednesday, I'm screwed.

  3. #3
    Join Date
    Jan 2011
    Location
    Tacoma, Washington
    Posts
    31

    Re: General Java Programming

    Good afternoon.

    Now you can't just pop on and ask "what next?". You should show exactly what you need to do, what you have done to do it, and why you think it might not be working. I'm sure you will find many willing to help you if you are more specific with your requirements. Tell us what you would like to try and we can tell you if it is flawed, doomed to fail, or a plausible idea.

    Plus, you could always ask your instructor. Their job is to instruct after all.

    Good luck =D

  4. #4
    Join Date
    Jan 2011
    Posts
    4

    Re: General Java Programming

    Quote Originally Posted by djgarrison View Post
    Good afternoon.

    Now you can't just pop on and ask "what next?". You should show exactly what you need to do, what you have done to do it, and why you think it might not be working. I'm sure you will find many willing to help you if you are more specific with your requirements. Tell us what you would like to try and we can tell you if it is flawed, doomed to fail, or a plausible idea.

    Plus, you could always ask your instructor. Their job is to instruct after all.

    Good luck =D
    Well the thing is, I have absolutely no idea what I need to do. Can't think of anything that might help me continue the question.

    And I can't ask my instructor. Asking them reduces the marks I'm gonna get.

  5. #5
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: General Java Programming

    If you post the text of your assignment you are more likely to get help. Many people helping here won't readily click on links.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  6. #6
    Join Date
    Jan 2011
    Posts
    4

    Re: General Java Programming

    Quote Originally Posted by keang View Post
    If you post the text of your assignment you are more likely to get help. Many people helping here won't readily click on links.
    Oh okay thanks. But the problem is, I have pictures as well. How do I put up pictures here?
    Last edited by TM101; January 31st, 2011 at 11:39 PM.

  7. #7
    Join Date
    Jan 2011
    Location
    Tacoma, Washington
    Posts
    31

    Re: General Java Programming

    sounds like what you need is a while loop that runs until the user presses 1. Put your code in the while loop with the last instruction being getting the input from the user.

  8. #8
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: General Java Programming

    Please don't go back and edit earlier posts in the thread it makes the whole thread impossible to read as the reponses to your earlier post no longer make sense.

    If people ask for more info etc please just add another post.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured