Jump to content

How do i add tip to the program?

airfrog19

I'm having trouble figuring out how to implement the tip to the subtotal. Can anyone help?

 

 
 
package project.pkg1;
import java.util.Scanner;
import java.text.DecimalFormat;
 
public class Project1 {
 
 
    public static void main(String[] args) {
     int choice = 0;
     boolean doneOrdering = false;
     boolean yes = false;
     String order;
     Scanner Keyboard = new Scanner(System.in);
     DecimalFormat moneyFormat = new DecimalFormat("$#,###.00");
     
     
     double num, GoodBurger = 0, GoodSalad = 0, Soda = 0, KidsMeal = 0;
     double GoodBurgers = 7.75;
     double GoodSalads = 7.00;
     double Sodas = 2.00;
     double KidsMeals = 3.00;
     double tax;
     double subtotal = 0, total;
     final double salestax = 0.075;
     final double tip = 0.10;
     
     System.out.println("Welcome to Good Burger!");
     System.out.println("=======================");
     System.out.println("Place your orders and type 'Finish' when you are done");
     System.out.println("--------------------------------------------------------");
    
    
     System.out.println("1. GoodBurger $7.75");
     System.out.println("2. GoodSalad $ 7.00");
     System.out.println("3. Soda $2.00");
     System.out.println("4. KidsMeal $3.00");
     System.out.println("Type '5' if you want to tip. \n");
     System.out.println("What would you like?");
    
     while(!doneOrdering){
         choice = Keyboard.nextInt();
         if( choice == 1){
             System.out.println("GoodBurger $7.75");
             subtotal = subtotal + 7.75;
         }
         else if( choice == 2){
             System.out.println("Good Salad $7.00");
             subtotal= subtotal += 7.00;
         }    
         else if( choice == 3){
             System.out.println("Soda $2.00");
             subtotal= subtotal + 2.00;             
         } 
         else if( choice == 4){
             System.out.println("KidsMeal $3.00");
             subtotal = subtotal + 3.00;
         }
         else if( choice == 5){
             doneOrdering = true;
             System.out.println("SubTotal " +subtotal);
             System.out.println("Tax " +salestax);
             System.out.println("-------------------------");
             System.out.println(subtotal*salestax+subtotal);
             }
         else{
             System.out.println("Invalid");          
         }
         int N = 100, order_size = 0;
         for(int i=0; i<order_size;i++)
     }
Link to comment
Share on other sites

Link to post
Share on other sites

Ask for another input after they've chosen 5, convert the string to a double, then add it to the total.

Link to comment
Share on other sites

Link to post
Share on other sites

Ask for another input after they've chosen 5, convert the string to a double, then add it to the total.

What do i change the double? like Double tip?

Link to comment
Share on other sites

Link to post
Share on other sites

What do i change the double? like Double tip?

I may have misunderstood.

 

If you want to ask people for a tip, then that's what I described above.

 

If instead you just want to use the tip variable you created then I believe it would just be this

//...else if( choice == 5){    doneOrdering = true;    double tipTotal = subtotal*tip;    // Note: If the tip is supposed to be taxed, then you'll want to uncomment the next line and remove tipTotal from the final System.out.println that adds up the totals    // subtotal += tipTotal;    double taxTotal = subtotal*salestax;    System.out.println("SubTotal " +subtotal);    System.out.println("Tax " + salestax);    System.out.println("Tip " + tipTotal);     System.out.println("-------------------------");    System.out.println(subtotal+tipTotal+taxTotal);}//...
Link to comment
Share on other sites

Link to post
Share on other sites

 

I may have misunderstood.

 

If you want to ask people for a tip, then that's what I described above.

 

If instead you just want to use the tip variable you created then I believe it would just be this

//...else if( choice == 5){    doneOrdering = true;    double tipTotal = subtotal*tip;    // Note: If the tip is supposed to be taxed, then you'll want to uncomment the next line and remove tipTotal from the final System.out.println that adds up the totals    // subtotal += tipTotal;    double taxTotal = subtotal*salestax;    System.out.println("SubTotal " +subtotal);    System.out.println("Tax " + salestax);    System.out.println("Tip " + tipTotal);     System.out.println("-------------------------");    System.out.println(subtotal+tipTotal+taxTotal);}//...

Oh okay. I'm having trouble on how to ask the user if they want to tip or not. Do i have to use another loop?

Link to comment
Share on other sites

Link to post
Share on other sites

Oh okay. I'm having trouble on how to ask the user if they want to tip or not. Do i have to use another loop?

No, I believe you can just use your Scanner variable again. This time instead of using nextInt() you can probably just use nextDouble() though as you're asking for money and might want the cents part. Or you can read it as a string with nextLine() and convert the value yourself, but that seems like needless work.

//...else if( choice == 5){    doneOrdering = true;    System.out.println("Please enter the amount of your tip.");    double tip = Keyboard.nextDouble();        // continue as you please}//...
Link to comment
Share on other sites

Link to post
Share on other sites

 

No, I believe you can just use your Scanner variable again. This time instead of using nextInt() you can probably just use nextDouble() though as you're asking for money and might want the cents part. Or you can read it as a string with nextLine() and convert the value yourself, but that seems like needless work.

//...else if( choice == 5){    doneOrdering = true;    System.out.println("Please enter the amount of your tip.");    double tip = Keyboard.nextDouble();        // continue as you please}//...

oh okay. Just one more question: I need to store user input on what the person orders. For example, if i type goodburger, goodsalad, and soda, i want to make a receipt with that towards the end. How do i do that?

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×