Java Restaurant Menu, calculation issue [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am trying to calculate the total for a menu item, I cannot quite figure out how to get the code to take the proper inputs and read them as item values. I need it to register that the user wants say 2 pizzas that are 12$ each for a total of 24, however I cannot figure it out. Here is what I have.
package javaapplication4;
/**
*
* #author Travis
*/
import java.util.Scanner;
public class JavaApplication4 {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
float Chicken = 9;
float Pizza = 12;
float Veal = 15;
float Alfredo = 9;
float Garlic = 8;
float Veggie = 7;
float Spaghetti = 9;
float Raviolli = 7;
float Meat = 8;
float Canolli = 5;
// Display menu and prices
System.out.println("1. Chicken $9");
System.out.println("2. Pizza $12");
System.out.println("3. Veal $15");
System.out.println("4. Alfredo $9");
System.out.println("5. Garlic $8");
System.out.println("6. Veggie $7");
System.out.println("7. Spaghetti $9");
System.out.println("8. Raviolli $7");
System.out.println("9. Meat $8");
System.out.println("10. Canolli 5$");
System.out.print("Enter your menu choice name: ");
Scanner input = new Scanner(System.in);
int orderNumber = input.nextInt();
//prompt user for quantity
System.out.print("How many would you like?: ");
input = new Scanner(System.in);
int orderAmount = input.nextInt();
//Switch to determine price
double price = orderNumber + orderAmount;
switch (orderNumber) {
case 1: price = Chicken ;
break;
case 2: price = Pizza;
break;
case 3: price = Veal;
break;
case 4: price = Alfredo;
break;
case 5: price = Garlic;
break;
case 6: price = Veggie;
break;
case 7: price = Spaghetti;
break;
case 8: price = Raviolli;
break;
case 9: price = Meat;
break;
case 10: price = Canolli;
break;
}
System.out.println("Your total is " + price);
System.out.println("Please enter your payment amount");
int payment = input.nextInt();
double total = payment - price;
System.out.println ("Thank you your change is $" + total);
}

Take a look at your switch statement (you're never really using it).
You should have something like this:
double price = 0.0; //You will reassign this
switch (orderNumber) {
case 1: price = Chicken ;
break;
case 2: price = Pizza;
break;
case 3: price = Veal;
break;
case 4: price = Alfredo;
break;
case 5: price = Garlic;
break;
case 6: price = Veggie;
break;
case 7: price = Spaghetti;
break;
case 8: price = Raviolli;
break;
case 9: price = Meat;
break;
case 10: price = Canolli;
break;
double total_price = price * orderAmount; //You currently have this as an addition

You wrote
double price = orderNumber + orderAmount;
I think it should be:
double price = orderNumber * orderAmount;

Related

I writting a program for this plan but i want to know did program have any answer and different my writting?

A hotel has a pricing policy as follows:
2 people: 80000
3 people: 90000
4 people: 95000
additional people: 6000 per person
If a customer is staying on a company business, there is a 20% discount.
In addition:
If a customer is over 60 years of age, there is a 15% discount.
A customer does not receive both discounts.
Given the above data, write a solution to find cost of a room.
import javax.swing.JOptionPane;
public class NewClass {
public static void main(String[] args) {
String num = JOptionPane.showInputDialog(null, "Enter a number of people ?");
int number = Integer.parseInt(num);
String Inputage = JOptionPane.showInputDialog(null, "Enter your age ?");
int age = Integer.parseInt(Inputage);
String set = JOptionPane.showInputDialog(null, "You staying on a company business ? answer , yes or no !");
String no = "no";
if (age < 60 && set.equals(no)) {
switch (number) {
case 2:
JOptionPane.showMessageDialog(null, "80000");
break;
case 3:
JOptionPane.showMessageDialog(null, "90000");
break;
case 4:
JOptionPane.showMessageDialog(null, "95000");
break;
default:
System.out.println("Error");
}
}
if (age >= 60 && set.equals(no)) {
switch (number) {
case 2:
JOptionPane.showMessageDialog(null, "68000");
break;
case 3:
JOptionPane.showMessageDialog(null, "76500");
break;
case 4:
JOptionPane.showMessageDialog(null, "80750");
break;
default:
System.out.println("Error");
}
}
String yes = "yes";
if (age < 60 && set.equals(yes)) {
switch (number) {
case 2:
JOptionPane.showMessageDialog(null, "64000");
break;
case 3:
JOptionPane.showMessageDialog(null, "72000");
break;
case 4:
JOptionPane.showMessageDialog(null, "76000");
break;
default:
System.out.println("Error");
}
}
if (age >= 60 && set.equals(yes)) {
switch (number) {
case 2:
JOptionPane.showMessageDialog(null, "You can't have both of offer !");
break;
default:
System.out.println("Error");
}
}
}
}
Possibly, a separate method to calculate the price should be implemented:
public static int getPrice(int people, int age, boolean onBusiness) {
int price;
switch (people) {
case 1: // assuming the same price as for 2 people
case 2:
price = 80_000; break;
case 3:
price = 90_000; break;
case 4:
price = 95_000; break;
default:
price = 95_000 + 6_000 * (people - 4);
break;
}
// applying only one discount, checking if the larger is applicable first
if (onBusiness) {
price = price * 80 / 100; // discount 20%
} else if (age >= 60) {
price = price * 85 / 100; // discount 15%
}
return price;
}
If Java 12+ is used, more concise switch syntax may be used:
public static int getPrice(int people, int age, boolean onBusiness) {
int price = switch (people) {
case 1, 2 -> 80_000;
case 3 -> 90_000;
case 4 -> 95_000;
default -> 95_000 + 6_000 * (people - 4);
};
// applying only one discount, checking if the larger is applicable first
if (onBusiness) {
price = price * 80 / 100; // discount 20%
} else if (age >= 60) { // discount for people at age 60 or older
price = price * 85 / 100; // discount 15%
}
return price;
}

Cannot be resolved error in java, any fixes? [duplicate]

This question already has answers here:
Java: Unresolved compilation problem
(10 answers)
Closed 3 years ago.
This is the error I am getting and I cant seem to figure out how to fix it.
I was trying to make it so that once you have finished your calculations, the program asks you if you want to perform them again.
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
retry cannot be resolved
yes cannot be resolved to a variable
My code(don't judge, this is my first program in java):
package calculatorpls;
import java.util.Scanner;
public class calc {
public static void main(String[]Args)
{
do {
Scanner num = new Scanner(System.in);
System.out.println("Please enter the first number."+"\n");
int no1 = num.nextInt();
System.out.println("\n"+"The number you have entered is "+no1+"."+"\n"+"\n"+"Please enter the second number now."+"\n");
int no2 = num.nextInt();
System.out.println("\n"+"The number you have entered is "+no2+"."+"\n"+"\n"+"Please choose what you would like to do from the following options:"+"\n"+
"1)Addition"+"\n"+"2}Subtraction(1st Number-2nd Number)"+"\n"+"3)Subtraction(2nd Number-1st Number)"+"\n"+"4)Multiplication"+"\n"+"5)Division(1st Number divided by 2nd Number)"+"\n"+"6)Division(2nd Number divided by 1st Number)"
+ ""+"\n"+"7)Multiply by an exponent(1st Number)"+"\n"+"8)Multiply by an exponent(2nd Number)"+"\n"+"\n"+"Type any number from 1-8 to select your option."+"\n");
//String Addition;
//String Subtraction(1st Number-2nd Number);
//String Subtraction(2nd Number-1st Number);
//String Multiplication;
//String Division(1st Number divided by 2nd Number);
//String Division(2nd Number divided by 1st Number);
//String Multiply by an exponent(1st Number);
//String Multiply by an exponent(2nd Number);
int choice = num.nextInt();
System.out.println("\n"+"You have chosen "+choice +"\n");
switch (choice)
{
case 1:
float addition = no1+no2;
System.out.println("\n"+ addition);
break;
case 2:
float subtraction1 = no1-no2;
System.out.println("\n"+ subtraction1);
break;
case 3:
float subtraction2 = no2-no1;
System.out.println("\n"+ subtraction2);
break;
case 4:
float multiplication = no1*no2;
System.out.println("\n"+ multiplication);
break;
case 5:
double division1 = no1/no2;
System.out.println("\n"+ division1);
break;
case 6:
double division2 = no2/no1;
System.out.println("\n"+ division2);
break;
case 7:
System.out.println("\n"+ "Please enter the power."+"\n");
int exponent = num.nextInt();
double exponent1 = (int) Math.pow(no1, exponent);
System.out.println("\n"+ exponent1);
break;
case 8:
System.out.println("\n"+ "Please enter the power."+"\n");
int exponenttwo = num.nextInt();
double exponent2 = (int) Math.pow(no2, exponenttwo);
System.out.println("\n"+ exponent2);
break;
default:
System.out.println("\n"+ "There isnt any such option matching your entry!"+"\n");
break;
}
System.out.println("\n"+ "Would you like to perform more calculations? Respond with yes or no."+"\n");
String retry = num.nextLine();
String again = "yes";
}while(retry.equalsIgnoreCase(again));
}
}
I fixed it for you.
String retry;
String again;
do {
Scanner num = new Scanner(System.in);
System.out.println("Please enter the first number."+"\n");
int no1 = num.nextInt();
System.out.println("\n"+"The number you have entered is "+no1+"."+"\n"+"\n"+"Please enter the second number now."+"\n");
int no2 = num.nextInt();
System.out.println("\n"+"The number you have entered is "+no2+"."+"\n"+"\n"+"Please choose what you would like to do from the following options:"+"\n"+
"1)Addition"+"\n"+"2}Subtraction(1st Number-2nd Number)"+"\n"+"3)Subtraction(2nd Number-1st Number)"+"\n"+"4)Multiplication"+"\n"+"5)Division(1st Number divided by 2nd Number)"+"\n"+"6)Division(2nd Number divided by 1st Number)"
+ ""+"\n"+"7)Multiply by an exponent(1st Number)"+"\n"+"8)Multiply by an exponent(2nd Number)"+"\n"+"\n"+"Type any number from 1-8 to select your option."+"\n");
//String Addition;
//String Subtraction(1st Number-2nd Number);
//String Subtraction(2nd Number-1st Number);
//String Multiplication;
//String Division(1st Number divided by 2nd Number);
//String Division(2nd Number divided by 1st Number);
//String Multiply by an exponent(1st Number);
//String Multiply by an exponent(2nd Number);
int choice = num.nextInt();
System.out.println("\n"+"You have chosen "+choice +"\n");
switch (choice)
{
case 1:
float addition = no1+no2;
System.out.println("\n"+ addition);
break;
case 2:
float subtraction1 = no1-no2;
System.out.println("\n"+ subtraction1);
break;
case 3:
float subtraction2 = no2-no1;
System.out.println("\n"+ subtraction2);
break;
case 4:
float multiplication = no1*no2;
System.out.println("\n"+ multiplication);
break;
case 5:
double division1 = no1/no2;
System.out.println("\n"+ division1);
break;
case 6:
double division2 = no2/no1;
System.out.println("\n"+ division2);
break;
case 7:
System.out.println("\n"+ "Please enter the power."+"\n");
int exponent = num.nextInt();
double exponent1 = (int) Math.pow(no1, exponent);
System.out.println("\n"+ exponent1);
break;
case 8:
System.out.println("\n"+ "Please enter the power."+"\n");
int exponenttwo = num.nextInt();
double exponent2 = (int) Math.pow(no2, exponenttwo);
System.out.println("\n"+ exponent2);
break;
default:
System.out.println("\n"+ "There isnt any such option matching your entry!"+"\n");
break;
}
System.out.println("\n"+ "Would you like to perform more calculations? Respond with yes or no."+"\n");
retry = num.next();
again = "yes";
}while(retry.equalsIgnoreCase(again));
I'm answering just to expand on nusaK's answer.
You should declare utilities like Scanner which may be used multiple times outside of for loops.
When you put the Scanner num = new Scanner(System.in); inside a loop, a new Scanner Object will be created every time the loop runs. Since we're always scanning from System.in, we can use the same object to scan for all iterations.
Since Java manages memory by itself, it isn't much of a problem but it might be in other languages.
Scanner num = new Scanner(System.in);
String again = "yes"; // you can initialize again here
String retry=""; // always initialize in languages like Java to avoid errors.
do {
...
}
while((retry=num.next()).equalsIgnoreCase(again));
// u can also use this, i.e. assign and evaluate at the same time but it's harder to read.

Using intAt for a Switch

I'm struggling to understand why intAt will not work in this program. The goal of program is to simply convert weights for a specific planet.
package weightonotherplanets;
import java.util.Scanner;
public class WeightonOtherPlanets
{
public static void main(String args[]){
System.out.println("What is your weight on the Earth?");
Scanner weightInput = new Scanner(System.in); // Enter your weight
int weight = weightInput.nextInt();
System.out.println("1. Voltar\n2. Krypton\n3. Fertos\n4. Servontos\n"); // Choice of planets
System.out.println(" Selection?");
Scanner selectionChoice = new Scanner(System.in);
int selection = selectionChoice.nextInt();
int select = selection.intAt(0); // This is the problem in the code
switch (select)
{
case '1':
System.out.println("Your weight on Voltor would be " + weight * 0.091);
break;
case '2':
System.out.println("Your weight on Krypton would be " + weight * 0.720);
break;
case '3':
System.out.println("Your weight on Fertos would be " + weight * 0.865);
break;
case '4':
System.out.println("Your weight on Servontos would be " + weight * 4.612);
break;
default:
System.out.println("Please make a selection.");
}
}
}

Program breaks from switch java

for some reason, when I enter '1' on the switch menu, nothing happens, but the program doesn't terminate. It's the same with options 2-5. The default option works just fine. Can anyone help me with this? Thanks
Code:
import java.util.Scanner;
public class ConversionTrial {
public static void main(String[] args) {
double pound;
double euro;
double dollars;
double yen;
double rupees;
double poundEuro;
double poundDollars;
double poundYen;
double poundRupees;
int choice;
Scanner input = new Scanner(System.in);
Scanner exchange = new Scanner(System.in);
menu: while(true) {
System.out.println("Please choose an option:");
System.out.println("1. Enter values");
System.out.println("2. Euros (1GBP = 1.28EUR)");
System.out.println("3. Dollars (1GBP = 1.51USD)");
System.out.println("4. Yen (1GBP = 179.80JPY)");
System.out.println("5. Rupees (1GBP = 95.60INR)");
System.out.println("6. Exit");
choice = input.nextInt();
switch(choice){
case -1:
case 6:
break menu;
case 1:
pound = exchange.nextDouble();
System.out.print("Please enter your values you would like to exchange:");
break;
case 2:
pound = exchange.nextDouble();
euro = 1.28;
poundEuro = pound * euro;
System.out.println("Your amounts in Euros are" + poundEuro);
case 3:
pound = exchange.nextDouble();
dollars = 1.51;
poundDollars = pound * dollars;
System.out.println("Your amounts in Dollars are" + poundDollars);
case 4:
pound = exchange.nextDouble();
yen = 1.28;
poundYen = pound * yen;
System.out.println("Your amounts in Yen are" + poundYen);
case 5:
pound = exchange.nextDouble();
rupees = 1.28;
poundRupees = pound * rupees;
System.out.println("Your amounts in Rupees are" + poundRupees);
default:
System.out.println("You must enter an option between 1 and 6!");
}
}
input.close();
exchange.close();
}
}
First, don't create two scanner objects. Just create the one, and use that.
Second, in you options 1-5, you are waiting for input before outputting anything to the user, so that is likely why it seems to not be working. You should add a prompt for the value expected.
Third, you are missing break; at the end of cases 2-5.
Fourth, using a label is generally not the best way to do things. It can end up with some hard to read code. A better way would be to do it would be to have a flag variable, boolean exit = false;. Then, your while loop will loop based on it not being true, while(!exit). And in your case 6:, exit = true;
Fifth, why do you have -1 exit, when it isn't an option given the user? I would remove that.
import java.util.Scanner;
public class ConversionTrial{
public static void main(String[] args) {
double pound;
double euro;
double dollars;
double yen;
double rupees;
double poundEuro;
double poundDollars;
double poundYen;
double poundRupees;
int choice;
Scanner input = new Scanner(System.in);
boolean exit = false;
while(!exit) {
System.out.println("Please choose an option:");
System.out.println("1. Enter values");
System.out.println("2. Euros (1GBP = 1.28EUR)");
System.out.println("3. Dollars (1GBP = 1.51USD)");
System.out.println("4. Yen (1GBP = 179.80JPY)");
System.out.println("5. Rupees (1GBP = 95.60INR)");
System.out.println("6. Exit");
choice = input.nextInt();
switch(choice){
case 6:
exit = true;
break;
case 1:
System.out.print("Please enter your values you would like to exchange: ");
pound = input.nextDouble();
break;
case 2:
System.out.print("Please enter your values you would like to exchange: ");
pound = input.nextDouble();
euro = 1.28;
poundEuro = pound * euro;
System.out.println("Your amounts in Euros are " + poundEuro);
break;
case 3:
System.out.print("Please enter your values you would like to exchange: ");
pound = input.nextDouble();
dollars = 1.51;
poundDollars = pound * dollars;
System.out.println("Your amounts in Dollars are " + poundDollars);
break;
case 4:
System.out.print("Please enter your values you would like to exchange: ");
pound = input.nextDouble();
yen = 1.28;
poundYen = pound * yen;
System.out.println("Your amounts in Yen are " + poundYen);
break;
case 5:
System.out.print("Please enter your values you would like to exchange: ");
pound = input.nextDouble();
rupees = 1.28;
poundRupees = pound * rupees;
System.out.println("Your amounts in Rupees are " + poundRupees);
break;
default:
System.out.println("You must enter an option between 1 and 6!");
break;
}
}
input.close();
}
}
Edit: As a bonus, I also noticed that option 1 doesn't actually do anything. Is that intentional? And for cleaner code, I would initialize the values of your conversion variables when you define the variables, instead of each time they are used. You could also use those values in your menu, so they only need to be changed once if the values change.
The menu label is'nt really necessary. Get rid of it, that is bad code smell.
Additionally you miss the break; in all other cases.
Add breaks for every case to exit the switch statement. Not sure if this is the answer you wanted, so let me know if you are looking for something else.
It's like PeterMmm said. You forget the break; in your cases.
Your code is working but i think it does not what u want it to do.
If i press 1 i can type in a double and afterwards it tells me i can type in a value.
But with this value from switch 1 happens just nothing.
The other options i can type in a double and get the exchange in a foreign currency.
What are u planning to do?
just ran your code and I have figured out what you want to do. I am getting the following:
> Please enter your values you would like to exchange:Please choose an
> option:
> 1. Enter values
> 2. Euros (1GBP = 1.28EUR)
> 3. Dollars (1GBP = 1.51USD)
> 4. Yen (1GBP = 179.80JPY)
> 5. Rupees (1GBP = 95.60INR)
> 6. Exit 2
> 1.2 Your amounts in Euros are1.536
But pay attention that you must enter double values.
Also, I insist that you use break to exit the switch for every case existing in your switch.
Regards
When you select an option 1-5, the program then waits for another double (the amount you want to convert), only then does it respond. So select an option, then give it another double value and it should tell you the conversion amount
import java.util.Scanner;
public class ConversionTrial {
public static void main(String[] args) {
double pound;
double euro;
double dollars;
double yen;
double rupees;
double poundEuro;
double poundDollars;
double poundYen;
double poundRupees;
int choice;
Scanner input = new Scanner(System.in);
Scanner exchange = new Scanner(System.in);
menu: while(true) {
System.out.println("Please choose an option:");
System.out.println("1. Enter values");
System.out.println("2. Euros (1GBP = 1.28EUR)");
System.out.println("3. Dollars (1GBP = 1.51USD)");
System.out.println("4. Yen (1GBP = 179.80JPY)");
System.out.println("5. Rupees (1GBP = 95.60INR)");
System.out.println("6. Exit");
choice = input.nextInt();
switch(choice){
case 1:
pound = exchange.nextDouble();
System.out.print("Please enter your values you would like to exchange:");
break;
case 2:
pound = exchange.nextDouble();
euro = 1.28;
poundEuro = pound * euro;
System.out.println("Your amounts in Euros are" + poundEuro);
break;
case 3:
pound = exchange.nextDouble();
dollars = 1.51;
poundDollars = pound * dollars;
System.out.println("Your amounts in Dollars are" + poundDollars);
break;
case 4:
pound = exchange.nextDouble();
yen = 1.28;
poundYen = pound * yen;
System.out.println("Your amounts in Yen are" + poundYen);
break;
case 5:
pound = exchange.nextDouble();
rupees = 1.28;
poundRupees = pound * rupees;
System.out.println("Your amounts in Rupees are" + poundRupees);
break;
case -1:
case 6:
break menu;
default:
System.out.println("You must enter an option between 1 and 6!");
}
}
input.close();
exchange.close();
}
}

Why is case 2 printing when I enter 2?

I am working on a program that tracks items and their costs as you add them to you bag. I have switch statement to give options for 1. adding new items, 2. printing the totals, and 3. ending the program.
For some reason when I select case 1 it also prints the totals using my toString method. But I only have the toString method in case 2.
Can anyone explain why this would be happening?
Here is my main
import java.util.Scanner;
public class ShoppingBagTracker {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
float taxRate, cost;
int items, newItems, choice;
String receipt;
String menu = ("1. Add items" +
"2. Get receipt"
+ "3. Exit");
System.out.print("Enter the sales tax rate: ");
taxRate = in.nextFloat();
ShoppingBag myBag = new ShoppingBag(taxRate);
items = 0;
do{
System.out.println("What would you like to do?");
System.out.println(menu);
choice = in.nextInt();
switch(choice){
case 1:
System.out.print("Enter cost of item: ");
cost = in.nextFloat();
System.out.print("Enter number of items: ");
newItems = in.nextInt();
items = items + newItems;
myBag.place(items, cost);
myBag.getItems();
myBag.getCost();
myBag.getTotal();
myBag.getAverage();
case 2:
receipt = myBag.toString();
System.out.println(receipt);
case 3:
break;
default:
System.out.println("That is not an option");
}
}while(choice != 3);
}
}
Here is my Shopping Bag class
public class ShoppingBag {
public float taxRate;
public int items;
public float cost;
public float average;
public float totalCost;
public float finalCost;
public ShoppingBag(float taxRate)
{
this.taxRate = taxRate;
}
public void place(int newitems, float newcost)
{
items = newitems;
cost = newcost;
cost = items * cost;
}
public int getItems()
{
return items;
}
public float getCost()
{
return cost;
}
public float getTotal()
{
finalCost = cost + (cost * taxRate);
return finalCost;
}
public float getAverage()
{
average = finalCost/items;
return average;
}
#Override
public String toString()
{
return("Items: " + items + " Cost: " + cost + " Total cost: " + finalCost + " Average cost: " + average);
}
}
A switch case isn't restrict to execute one 'case'. It execute all code from the matching case down to a break; or to the end of the switch.
In your case add a break; before the case 2
You need to have break in switch-cases.
switch(something){
case 1:
// do something
break;
case 2:
//do something
break;
default:
// do something
break;
}
If there is no break, that will execute all cases here.
switch-cases in Java.
Conclusion.
If there is no return in cases, you should use break for every case.
Following case you don't need break.
switch (cases){
case "a":
return "a";
case "b":
return "b";
default:
return "default";
}
You need to add a break statement inside the case 1 body
case 1:
System.out.print("Enter cost of item: ");
cost = in.nextFloat();
System.out.print("Enter number of items: ");
newItems = in.nextInt();
items = items + newItems;
myBag.place(items, cost);
myBag.getItems();
myBag.getCost();
myBag.getTotal();
myBag.getAverage();
break;
Here is the official tutorial
You should either have a break for all the cases or have a return statements in all cases. Otherwise when Java will execute all the cases which are after the matching case.
swicth(urSwitch){
case 1:// logic
break; // or return something
case 2:// logic
break; // or return something
case 3:// logic
break; // or return something
}

Categories