Jump to content

One of my mini projects right now is to create a program that takes orders of food and calculates the total of each order.

 

 The program should be able to order multiple numbers of each order and make the
necessary calculations. When the user wishes to complete their order, they should enter choice
4. The program will then display their order (make it look like a receipt!)
 
The only part I am having trouble with right now is the loop part where I have to repeat the process. And from there I might need some help with how to calculate all the orders requested,
 
import java.lang.Math;
import java.util.Scanner;
 
public class Orders
{
   public static void main(String[] args)
   {
      System.out.println("There is the menu and the price of the items.");
      System.out.println("Hot Dog and Drink Combo. $3.99!");
      System.out.println("Hamburger and Drink Combo. $4.99!");
      System.out.println("Pizza and Drink Combo. $5.99!");
      
      
      
      String prices;      
      Scanner keyboard;
      keyboard = new Scanner(System.in);
      
      System.out.println("Give me orders!~");
      int choice = keyboard.nextInt();      
 
      if( choice == 1)
         System.out.println("You ordered a Hot Dog and a Drink Combo. Your total is $3.99!");
             
      else if( choice == 2)
         System.out.println("You ordered a Hamburger and Drink Combo. Your total is $4.99!");
         
      else if( choice == 3)
         System.out.println("You ordered a Pizza and Drink Combo. Your total is $5.99!");
         
      else
         System.out.println("Invalid choice. Error. Please choose 1, 2, or 3!");            
   }
}       
 
So far this is what I have. I am open to constructive comments on my codes. 

Wher should my loop begin and where should it end O_O??

LTT CSGO SERVER! IP 8.12.22.45!~  Connect by connecting on csgo console

Use console command "connect"   --->  connect 8.12.22.45

Link to comment
https://linustechtips.com/topic/231876-do-while-loops-and-while-loops/
Share on other sites

Link to post
Share on other sites

added loop and a bit so you can actually get out of the loop.

import java.lang.Math;import java.util.Scanner;public class AllenDengHW3C{   public static void main(String[] args)   { //while choice is not 4while (choice =! 4){      System.out.println("There is the menu and the price of the items.");      System.out.println("1: Hot Dog and Drink Combo. $3.99!");      System.out.println("2: Hamburger and Drink Combo. $4.99!");      System.out.println("3: Pizza and Drink Combo. $5.99!");      System.out.println("4 to exit");                        String prices;            Scanner keyboard;      keyboard = new Scanner(System.in);            System.out.println("Give me orders!~");      int choice = keyboard.nextInt();            if( choice == 1)         System.out.println("You ordered a Hot Dog and a Drink Combo. Your total is $3.99!");                   else if( choice == 2)         System.out.println("You ordered a Hamburger and Drink Combo. Your total is $4.99!");               else if( choice == 3)         System.out.println("You ordered a Pizza and Drink Combo. Your total is $5.99!");              else         System.out.println("Invalid choice. Error. Please choose 1, 2, or 3!");  }//end loop            }}  

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites

Wouldn't let me compile O_O?

declare choice above the loop

 

int choice=0;

 

and change 

 

int choice = keyboard.nextInt();  to  choice = keyboard.nextInt();  

 

 

 

should work feeding baby daughter atm so let me know ^_^

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites

Pretty sure Java doesn't accept =! as an operator, just != (in the header of the while loop). Posting the compiler errors would be useful...

You should also consider creating a total price count given that with the loop you can select multiple orders. At the moment if I were to select 3 hotdog and drink combos, after the third you'd still be printing "your total is $3.99" rather than $11.97.

 

declare choice above the loop

 

int choice=0;

 

and change 

 

int choice = keyboard.nextInt();  to  choice = keyboard.nextInt();  

 

The above doesn't matter at all. All that declaring int choice = keyboard.nextInt() does is make choice local to the while loop so you can't use it outside and because you don't use it outside the loop it doesn't matter.

Link to post
Share on other sites

 

added loop and a bit so you can actually get out of the loop.

import java.lang.Math;import java.util.Scanner;public class AllenDengHW3C{   public static void main(String[] args)   { //while choice is not 4while (choice =! 4){      System.out.println("There is the menu and the price of the items.");      System.out.println("1: Hot Dog and Drink Combo. $3.99!");      System.out.println("2: Hamburger and Drink Combo. $4.99!");      System.out.println("3: Pizza and Drink Combo. $5.99!");      System.out.println("4 to exit");                        String prices;            Scanner keyboard;      keyboard = new Scanner(System.in);            System.out.println("Give me orders!~");      int choice = keyboard.nextInt();            if( choice == 1)         System.out.println("You ordered a Hot Dog and a Drink Combo. Your total is $3.99!");                   else if( choice == 2)         System.out.println("You ordered a Hamburger and Drink Combo. Your total is $4.99!");               else if( choice == 3)         System.out.println("You ordered a Pizza and Drink Combo. Your total is $5.99!");              else         System.out.println("Invalid choice. Error. Please choose 1, 2, or 3!");  }//end loop            }}  

 

 

You also might want to move the Scanner object outside of the loop so you're not literally creating a new one every time the user selects anything but exit. 

Link to post
Share on other sites

Here this should work

import java.lang.Math;import java.util.Scanner;public class AllenDengHW3C {    public static void main(String[] args) {        // Create scanner object        Scanner keyboard;        // Create integer        int choice = 0;        //while choice is not 4        while (choice != 4) {            System.out.println("There is the menu and the price of the items.");            System.out.println("1: Hot Dog and Drink Combo. $3.99!");            System.out.println("2: Hamburger and Drink Combo. $4.99!");            System.out.println("3: Pizza and Drink Combo. $5.99!");            System.out.println("4 to exit");            // Unused variable?            String prices;            // Initialize scanner object 'keyboard'            keyboard = new Scanner(System.in);            System.out.print("Give me your order: ");            choice = keyboard.nextInt();            if (choice == 1) {                System.out.println("You ordered a Hot Dog and a Drink Combo. Your total is $3.99!");            } else if (choice == 2) {                System.out.println("You ordered a Hamburger and Drink Combo. Your total is $4.99!");            } else if (choice == 3) {                System.out.println("You ordered a Pizza and Drink Combo. Your total is $5.99!");            } else {                System.out.println("Invalid choice. Error. Please choose 1, 2, or 3!");            }        } //end loop             }}
Link to post
Share on other sites

import java.lang.Math;import java.util.Scanner;public class AllenDengHW3C{   public static void main(String[] args)   {////////////Are these your statements or are they given in the assignment? It would be better to store the product name and price in appropriate data structures for re-use later when you make the receipt. (Then use those to output these statements).      System.out.println("There is the menu and the price of the items.");      System.out.println("1: Hot Dog and Drink Combo. $3.99!");      System.out.println("2: Hamburger and Drink Combo. $4.99!");      System.out.println("3: Pizza and Drink Combo. $5.99!");      System.out.println("4 to exit");      String prices;      //////////////////Unused variable. What's it for?      Scanner keyboard;      keyboard = new Scanner(System.in);            System.out.println("Give me orders!~");      int choice = keyboard.nextInt();      ///while loop should start here      if( choice == 1) ///Are you supposed to be printing their choice each time, or after the user has pressed 4?         System.out.println("You ordered a Hot Dog and a Drink Combo. Your total is $3.99!"); ///Total does not take into account purchasing more than item             //////There should be some logic here to calculate the total order price and record the items ordered (call a method)      else if( choice == 2)         System.out.println("You ordered a Hamburger and Drink Combo. Your total is $4.99!");         //////There should be some logic here to calculate the total order price and record the items ordered (call a method)      else if( choice == 3)         System.out.println("You ordered a Pizza and Drink Combo. Your total is $5.99!");//////There should be some logic here to calculate the total order price and record the items ordered (call a method)              else         System.out.println("Invalid choice. Error. Please choose 1, 2, or 3!");             }}  

 

Hard to say what to do without making it too easy, plus we don't know which bits of the code (if any) have been given to you.

 

I'm assuming you are not supposed to be printing to System.out until the user presses 4 (unless they press something other than 1,2,3 or 4. That way you will have a meaningful 'Receipt'.

 

Apologies for my messy comments, I cba.

Link to post
Share on other sites

 

Hard to say what to do without making it too easy, plus we don't know which bits of the code (if any) have been given to you.

 

I'm assuming you are not supposed to be printing to System.out until the user presses 4 (unless they press something other than 1,2,3 or 4. That way you will have a meaningful 'Receipt'.

 

Apologies for my messy comments, I cba.

 

 

 

yeah i would have it to pick an item then how many, however OP didn't really ask that and asked about loops.

 

 

I would also have the print out side the loop.

 

loop 

       ask which meal

       ask how many

/end loop

 

total = (item1 * item1count) + then item2 and 3

 

print item + number

print total

 

 

crude pseudocode but place to start.

 

 

I really need to get eclipse installed on here

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites

import java.text.DecimalFormat;import java.util.Scanner;public class AllenDengHW3C {   public static void main(String[] args){      // Declarative Variables       Scanner keyboard = new Scanner(System.in);      DecimalFormat moneyFormat = new DecimalFormat("$#,###.00");            double num, hotdog = 0, hamburger = 0, pizza = 0;      double hotdogs = 3.99;      double hamburgers = 4.99;      double pizzas = 5.99;      double tax;      double subtotal = 0, total;      final double SALESTAX = 0.09;            //The actual ordering of food         System.out.println("\t----- Menu -----");         System.out.println("Here is the menu and the price of the items.\n");      do      {         System.out.println("1: Hot Dog and Drink Combo. $3.99!");         System.out.println("2: Hamburger and Drink Combo. $4.99!");         System.out.println("3: Pizza and Drink Combo. $5.99!");         System.out.println("4: Exit\n");         System.out.println("Please place your order: ");         num = keyboard.nextInt();               if( num == 1)         {            hotdog++;            subtotal = subtotal + hotdog;            System.out.println("You ordered a Hot Dog and a Drink Combo.\n");         }                              else if( num == 2)         {            hamburger++;            subtotal = subtotal + hamburger;             System.out.println("You ordered a Hamburger and Drink Combo.\n");         }                              else if( num == 3)         {            pizza++;            subtotal = subtotal + pizza;            System.out.println("You ordered a Pizza and Drink Combo.\n");          }                            else if ( num == 4)         {            System.out.println("Here is your receipt!:\n ");          }                     else          {            System.out.println("ERROR: Invalid order\n");         }              }while( num != 4);         tax = subtotal * SALESTAX;         total = tax + subtotal;          hotdog = hotdog * hotdogs;          hamburger = hamburger * hamburgers;         pizza = pizza * pizzas;                  // Check out process is above and below... Receipt print under this comment.             System.out.println("GabeN's Cafe\n");             System.out.println("007 Miku Ave.\n\n");            if(hotdog > 0)         System.out.println(hotdog + "\t" + " Hotdog and Drink \t" + moneyFormat.format(hotdogs) + "\n" );                if(hamburger > 0)         System.out.println(hamburger + "\t" + " Hamburger and Drink \t" + moneyFormat.format(hotdogs) + "\n" );               if(pizza>0)         System.out.println(pizza + "\t" + " Pizza and Drink \t" + moneyFormat.format(pizzas));               System.out.println("______________________________________\n\n"        + "Subtotal\t\t\t" + (moneyFormat.format(subtotal)) + "\n\n"        + "Tax\t\t\t\t" + (moneyFormat.format(tax)) + "\n\n"        + "Total\t\t\t\t" + (moneyFormat.format(total)) + "\n\n");                     }}  // Possible bug when 4 is pressed with no order. Empty receipt = waste of paper. 

 

 

So this is what I finally have currently after all the feedback and my peers. Actually runs. But what can I further improve on this piece of code?

 

 

LTT CSGO SERVER! IP 8.12.22.45!~  Connect by connecting on csgo console

Use console command "connect"   --->  connect 8.12.22.45

Link to post
Share on other sites

 

 

 

So this is what I finally have currently after all the feedback and my peers. Actually runs. But what can I further improve on this piece of code?

 

 

 

 

 

bug is easy 

 

if(total > 0){

 

//print out order 

}else{

 

//error order is empty

}

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites

So this is what I finally have currently after all the feedback and my peers. Actually runs. But what can I further improve on this piece of code?

IMO, move the 2 "---menu--" lines inside the loop so you get full interface each loop cycle - better user experience.

Codewise/Stylistically:

Remove the whitespace between if-elses,

re-align the code after do-while loop,

again remove pointless whitespace, it may look cool now, but it doesn't.

 

as @vorticalbox said, print order only if (total > 0)

...}while(num!=4)if(total>0){  tax = subtotal * SALESTAX;  ...  + "Total\t\t\t\t" + (moneyFormat.format(total)) + "\n\n");}else{  //thank for not shopping ?{

Lastly, just noticed that you keep using single println for each line (e.g. 7 separate calls at the start), but at the end you combine 4 of them into one. When coding, consistency is important. 4 println functions will look cleaner compared to rest of the code IMO.

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

×