#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2015
    Posts
    3
    Rep Power
    0

    Inputing Decimals into Cashier Program.


    Hey everyone, I'm fairly new to JAVA so this question might come across as easy but i've been scratching my head as to how to complete this for the last hour or so. My issue is that I cannot input a digit with a decimal value. E.g. Total is: 57.695. If I (being the customer) were to input 57.695 when paying for the bill, the program would crash.

    The original question, which I believe my issue is derived from: What if the customer doesn't pay enough? Use "if" statements to assist you. Cont. What if the customer now pays the exact correct amount? Does your program still work? Use another If statement to assist you.

    I believe the error in my code originates from the "if" statements but at this point I'm not sure. Here is the program in it's entirety:

    Code:
    package ben_kinsella_e2_q1;
    
    import java.util.Scanner;
    
    public class Ben_Kinsella_E2_Q1 {
    
        public static void main(String[] args) {
            
            
            double popcorn= 4.75;
            double pop= 2.00;
            double candy= 3.25; 
            double variable1;
            double variable2;
            double variable3;
            double variable4;
            double subTotal;
            double taxes;
            double total;
            double gst;
            double change;
            Scanner myKeyboard; 
           
            myKeyboard= new Scanner(System.in);
            System.out.println("Please enter the amount of popcorn: ");
            variable1= myKeyboard.nextInt();
            
            System.out.println("Please enter the amount of pop: ");
            variable2= myKeyboard.nextInt();
            
            System.out.println("Please enter the amount of candy: ");
            variable3= myKeyboard.nextInt();
            
            subTotal= popcorn *variable1 + pop *variable2 + candy *variable3; 
            taxes= subTotal * 0.13; 
            gst= subTotal * 0.13;
            total= subTotal + gst;
            
            
            System.out.println("Subtotal: " + subTotal);
            System.out.println("taxes: " + taxes);
            System.out.println("Total: " + total);
            
            System.out.println("Amount given to cashier: ");
            variable4= myKeyboard.nextInt();
            
            if (variable4 > total){
            change= variable4 - total;
            System.out.println("Change: " + change);
            
            
            if (variable4 < total){
                System.out.println("Don't make me take the money from your wallet!");
            }
            else{
            System.out.println("No change");
            } 
            }
                            
     }           
    }
    Any suggestions are greatly appreciated and will result in a bout of profusely thanking the individual that solves my dilemma.
  2. #2
  3. Lord of the Dance
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Oct 2003
    Posts
    4,118
    Rep Power
    2010
    You should try to give better and more meaningful names to your variable than just variable1, variable2 etc.

    To make sure that enough is payed, you actually have to use a loop.
    In this case, a do-while loop would be a good choice, as you need to run it at least once and don't know how many times need to run it.

    java Code:
     
     
    // let user pay the bill
    double needToPay = total;
    do{
            System.out.println("Please pay: " + needToPay);
            System.out.println("Enter amount given to cashier: ");
            userIsPaying= myKeyboard.nextInt();
            needToPay = needToPay - userIsPaying;
     
    } while(needToPay < total)
     
    //let user get money back if payment is higher than total
    if(needToPay > total) {
            System.out.println("Money change back: " + (needToPay-total)); 
    }



    I hope this was helping you. Just ask if there is something you are in doubt about.
    (Btw, this has not been tested.)
    Last edited by MrFujin; September 26th, 2015 at 08:01 AM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2015
    Posts
    3
    Rep Power
    0
    Thanks for the reply. I'll give your solution a try, but I was wondering if there is a way of doing this strictly using "if" statements. Feel free to change anything if need be.

    Thanks again everyone.
  6. #4
  7. Lord of the Dance
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Oct 2003
    Posts
    4,118
    Rep Power
    2010
    If you want to keep asking for a price, then you would need some kind of a loop.

    If you only want to tell whether enough has been paid, then you can keep use the 'if -else' statements.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2015
    Posts
    3
    Rep Power
    0
    Hey, I figured out what was wrong with the program and it turns out the if statements were fine except for an issue with nesting. The issue was that I declared nextInt instead of nextDouble when inputing my variables. This made it so I couldn't input any numbers with decimal points. Thanks for the help!

    Comments on this post

    • MrFujin agrees : Can see I made same mistake in my code. :)
  10. #6
  11. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,803
    Rep Power
    508
    Now modify your program to handle soda.
    Next change the price of candy.

    Is the user operation experience at all reasonable?
    Is reprogramming each change reasonable?
    (no, and no)

    Read items and prices from a file.

    Comments on this post

    • MrFujin agrees : Now imagine doing this without any loops. :p
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo