Jump to content

I don't understand on how to store user data and for my project i have to make a receipt of what the user ordered at the end. How do i do that? Do i use arrays?

 

package project.pkg1;
import java.util.Scanner;
import java.text.DecimalFormat;
 
public class Project1 {
 
 
    public static void main(String[] args) {
     String choice;
     boolean doneOrdering = false;
     boolean yes = false;
     String order, A, B, D;
     Scanner Keyboard = new Scanner(System.in);
               
     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;
     int C = 0;
     double tip = 0.015;
     final double salestax = 0.082;
          
     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 $8.00");
     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 'Done' when finsished. \n");
     System.out.println("What would you like?");
         
     while(!doneOrdering){
        choice = Keyboard.nextLine();
        if(choice.equalsIgnoreCase("GoodBurger")){        
        subtotal = subtotal + 8.00;
        }
        else if(choice.equalsIgnoreCase("GoodSalad")){        
        subtotal= subtotal += 7.00;
        }    
        else if(choice.equalsIgnoreCase("Soda")){        
        subtotal= subtotal + 2.00;             
        } 
        else if(choice.equalsIgnoreCase("KidsMeal")){        
        subtotal = subtotal + 3.00;
        }
        
        else if(choice.equalsIgnoreCase("Done")){
        doneOrdering = true;
        System.out.println("\nDo you want to tip? (Yes/No)");
        A = Keyboard.next();
        
        if(A.equalsIgnoreCase("yes")){
        System.out.print("What percentage would you like to tip? ");
        C = Keyboard.nextInt();        
        double tiptotal = C*tip*subtotal;
        double taxtotal = subtotal*salestax;
        System.out.println("\nSubTotal $" +subtotal);
        System.out.println("Tax " +salestax);
        System.out.println("Tip $" +tiptotal);
        System.out.println("-------------------------");
        System.out.println(subtotal+tiptotal+taxtotal);
        }
        if(A.equalsIgnoreCase("no")){  
        double tiptotal = C*tip*subtotal;
        double taxtotal = subtotal*salestax;
        System.out.println("\nSubTotal $" +subtotal);
        System.out.println("Tax " +salestax);
        System.out.println("-------------------------");
        System.out.println(subtotal+tiptotal+taxtotal);
       }           
     }else{   
      System.out.println("Invalid");
      }
          
  }
Link to comment
https://linustechtips.com/topic/244391-how-do-i-store-user-data-for-a-receipt/
Share on other sites

Link to post
Share on other sites

Didn't you create these variables for storing the number of ordered items?

double num, GoodBurger = 0, GoodSalad = 0, Soda = 0, KidsMeal = 0;

just increment them each time you order one of them. (dunno what num is for though)

 

Also you can do subtotal += 8 which is the same as subtotal = subtotal + 8 for incrementing. (works with other operations too)

(also GoodBurger++ would increase the value by 1, -- would decrease by 1) Just a few tips there :)

Aragorn (WS): 250D | 6800k | 840 Pro 512GB | Intel 530 480GB  | Asus X99-M WS | 64GB DDR4 | Corsair HX720i | GTX 1070 | Corsair H115i | Philips BDM4350UC 43" 3840x2160 IPS

Gimli (server):  Node 304 | G4560 | ADATA XPG SX8000 128GB | 2x 5TB WD Red | ASROCK H270M-ITX/AC  | 8GB DDR4 | Seasonic 400FL

 Omega (server):                 Fractal Arc Mini R2 | i3 4130 | 500GB Maxtor | 2TB WD Red : Raid 1 | 3TB Seagate Barracuda | 16GB RAM | Seasonic G-450w
Alpha (WS): 900D | 4770k | GTX 780  | 840 Pro 512GB  | GA-Z87X-OC | Corsair RM 850 | 24GB 2400mhz | Samsung S27B970D 2560x1440

                              ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

Link to post
Share on other sites

Using code tags makes your code a lot easier to read: 

package project.pkg1;import java.util.Scanner;import java.text.DecimalFormat; public class Project1 {    public static void main(String[] args) {     String choice;     boolean doneOrdering = false;     boolean yes = false;     String order, A, B, D;     Scanner Keyboard = new Scanner(System.in);                    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;     int C = 0;     double tip = 0.015;     final double salestax = 0.082;               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 $8.00");     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 'Done' when finsished. \n");     System.out.println("What would you like?");              while(!doneOrdering){        choice = Keyboard.nextLine();        if(choice.equalsIgnoreCase("GoodBurger")){                subtotal = subtotal + 8.00;        }        else if(choice.equalsIgnoreCase("GoodSalad")){                subtotal= subtotal += 7.00;        }            else if(choice.equalsIgnoreCase("Soda")){                subtotal= subtotal + 2.00;                     }         else if(choice.equalsIgnoreCase("KidsMeal")){                subtotal = subtotal + 3.00;        }                else if(choice.equalsIgnoreCase("Done")){        doneOrdering = true;        System.out.println("\nDo you want to tip? (Yes/No)");        A = Keyboard.next();                if(A.equalsIgnoreCase("yes")){        System.out.print("What percentage would you like to tip? ");        C = Keyboard.nextInt();                double tiptotal = C*tip*subtotal;        double taxtotal = subtotal*salestax;        System.out.println("\nSubTotal $" +subtotal);        System.out.println("Tax " +salestax);        System.out.println("Tip $" +tiptotal);        System.out.println("-------------------------");        System.out.println(subtotal+tiptotal+taxtotal);        }        if(A.equalsIgnoreCase("no")){          double tiptotal = C*tip*subtotal;        double taxtotal = subtotal*salestax;        System.out.println("\nSubTotal $" +subtotal);        System.out.println("Tax " +salestax);        System.out.println("-------------------------");        System.out.println(subtotal+tiptotal+taxtotal);       }                }else{         System.out.println("Invalid");      }            }

Do you know much about Object Oriented programming? I would recommend you make an object to represent each of the items or a generic item that can represent all items. You could also make a ShoppingBasket object that could contain a list of shopping items.

This might be difficult if you are quite new to programming.
Is this for a school assignment?

Link to post
Share on other sites

Using code tags makes your code a lot easier to read: 

package project.pkg1;import java.util.Scanner;import java.text.DecimalFormat; public class Project1 {    public static void main(String[] args) {     String choice;     boolean doneOrdering = false;     boolean yes = false;     String order, A, B, D;     Scanner Keyboard = new Scanner(System.in);                    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;     int C = 0;     double tip = 0.015;     final double salestax = 0.082;               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 $8.00");     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 'Done' when finsished. \n");     System.out.println("What would you like?");              while(!doneOrdering){        choice = Keyboard.nextLine();        if(choice.equalsIgnoreCase("GoodBurger")){                subtotal = subtotal + 8.00;        }        else if(choice.equalsIgnoreCase("GoodSalad")){                subtotal= subtotal += 7.00;        }            else if(choice.equalsIgnoreCase("Soda")){                subtotal= subtotal + 2.00;                     }         else if(choice.equalsIgnoreCase("KidsMeal")){                subtotal = subtotal + 3.00;        }                else if(choice.equalsIgnoreCase("Done")){        doneOrdering = true;        System.out.println("\nDo you want to tip? (Yes/No)");        A = Keyboard.next();                if(A.equalsIgnoreCase("yes")){        System.out.print("What percentage would you like to tip? ");        C = Keyboard.nextInt();                double tiptotal = C*tip*subtotal;        double taxtotal = subtotal*salestax;        System.out.println("\nSubTotal $" +subtotal);        System.out.println("Tax " +salestax);        System.out.println("Tip $" +tiptotal);        System.out.println("-------------------------");        System.out.println(subtotal+tiptotal+taxtotal);        }        if(A.equalsIgnoreCase("no")){          double tiptotal = C*tip*subtotal;        double taxtotal = subtotal*salestax;        System.out.println("\nSubTotal $" +subtotal);        System.out.println("Tax " +salestax);        System.out.println("-------------------------");        System.out.println(subtotal+tiptotal+taxtotal);       }                }else{         System.out.println("Invalid");      }            }

Do you know much about Object Oriented programming? I would recommend you make an object to represent each of the items or a generic item that can represent all items. You could also make a ShoppingBasket object that could contain a list of shopping items.

This might be difficult if you are quite new to programming.

Is this for a school assignment?

yes it is. I'm pretty much a noob at this stuff, but ill get the hang of it. 

Link to post
Share on other sites

Didn't you create these variables for storing the number of ordered items?

double num, GoodBurger = 0, GoodSalad = 0, Soda = 0, KidsMeal = 0;

just increment them each time you order one of them. (dunno what num is for though)

 

Also you can do subtotal += 8 which is the same as subtotal = subtotal + 8 for incrementing. (works with other operations too)

(also GoodBurger++ would increase the value by 1, -- would decrease by 1) Just a few tips there :)

How would that look like? (sorry i'm pretty new to this.) 

Link to post
Share on other sites

How would that look like? (sorry i'm pretty new to this.) 

If you are looking for a quick and simple solution, you could create a int variable for counting how many of each product has been brought.

For example:

int numberOfBurgersPurchased = 0;

Then change:

 

 if(choice.equalsIgnoreCase("GoodBurger")){            subtotal = subtotal + 8.00; }

to:

 

 if(choice.equalsIgnoreCase("GoodBurger")){           subtotal = subtotal + 8.00;   numberOfBurgersPurchased++;}

Then at the end you can add:

 

if (numberOfBurgersPurchased != 0) {   System.out.println(numberOfBurgersPurchased + "x GoodBurgers: $" + (numberOfGoodBurgers * GoodBurgers));}

Which will print something like:

5x GoodBurgers: $38.75

Link to post
Share on other sites

If you are looking for a quick and simple solution, you could create a int variable for counting how many of each product has been brought.

For example:

int numberOfBurgersPurchased = 0;

Then change:

 

 if(choice.equalsIgnoreCase("GoodBurger")){            subtotal = subtotal + 8.00; }

to:

 

 if(choice.equalsIgnoreCase("GoodBurger")){           subtotal = subtotal + 8.00;   numberOfBurgersPurchased++;}

Then at the end you can add:

 

if (numberOfBurgersPurchased != 0) {   System.out.println(numberOfBurgersPurchased + "x GoodBurgers: $" + (numberOfGoodBurgers * GoodBurgers));}

Which will print something like:

5x GoodBurgers: $38.75

okay, ill try this. do i have to do this to the other 3 too?

Link to post
Share on other sites

okay, ill try this. do i have to do this to the other 3 too?

I just noticed that you have a variable called "GoodBurger" which is set to 0. I guess this is what you were going to use to count the number of burgers purchased?

Yes, adding the other 3 will be the simplest solution for you.

Link to post
Share on other sites

I just noticed that you have a variable called "GoodBurger" which is set to 0. I guess this is what you were going to use to count the number of burgers purchased?

Yes, adding the other 3 will be the simplest solution for you.

So do should i keep goodburger =0? When i was testing it out this occured:

 

What would you like?

soda
goodburger
1x GoodBurgers: $7.75
goodsalad
1x GoodBurgers: $7.75
soda
1x GoodBurgers: $7.75
kidsmeal
1x GoodBurgers: $7.75
 
how do i fix this?
Link to post
Share on other sites

 

So do should i keep goodburger =0? When i was testing it out this occured:

 

What would you like?

soda
goodburger
1x GoodBurgers: $7.75
goodsalad
1x GoodBurgers: $7.75
soda
1x GoodBurgers: $7.75
kidsmeal
1x GoodBurgers: $7.75
 
how do i fix this?

 

Right now goodBurger = 0 is not used so it can be removed.

You need to change the print statement for each shopping item. For example the GoodSalad print statement should be something like:

 

if (numberOfSaladsPurchased != 0) {   System.out.println(numberOfSaladsPurchased + "x GoodSalads: $" + (numberOfGoodSalads * GoodSalads));}
Link to post
Share on other sites

 

Right now goodBurger = 0 is not used so it can be removed.

You need to change the print statement for each shopping item. For example the GoodSalad print statement should be something like:

 

if (numberOfSaladsPurchased != 0) {   System.out.println(numberOfSaladsPurchased + "x GoodSalads: $" + (numberOfGoodSalads * GoodSalads));}

you're a lifesaver. thanks!

Link to post
Share on other sites

Just gonna throw this in here, but database on this situation will help immensely.

 

Since it's a school assignment and guessing based on the code, but I'm thinking a database is too advanced right now. At most they'll only want in memory or simple text file.

Link to post
Share on other sites

Pretty easy, just write to a text file.

 

Heres how to do it Java. Tinker with it a bit to understand how it works, pretty straightforward.

 

This demo hows how to read and write from a file, so just ask the user for the filepath and  your good to go. Writing back to the file is simple as well but watch for the boolean at the end of the write or you will delete your original entry. ( True is append, false is overwrite)

 

WriteFile_Run.gif

Additional Info:

http://www.homeandlearn.co.uk/java/write_to_textfile.html

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

×