Error "symbol not found" and multiplication error - java

Why do I keep getting the error
symbol not found
for hamburger, cheeseburger, etc.? I also am getting an error for the * sign. I used a template to create this program. I don't understand why it is not compiling.
import java.util.Scanner;
import java.text.NumberFormat;
public class Assignment2
{
public static void main (String[] args)
{
final (double) HAMBURGER_COST = 2.75;
final (double) CHEESBURGER_COST = 3.25;
final (double) FRENCHFRIES_COST = 2.50;
final (double) BEVERAGE_COST = 1.50;
String hamburger, cheeseburger, fries, beverages;
Scanner in = new Scanner(System.in);//allows user input
System.out.println("Welcome to the In-N-Out Burger menu:");
System.out.println("______________________________________");
System.out.println("Hamburger $2.75");
System.out.println("Cheeseburger $3.25");
System.out.println("French Fries $2.50");
System.out.println("Shake & Beverage $1.50");
System.out.print("How many hamburger(s) would you like to buy?");
hamburgers = scan.nextInt();
System.out.print("How many cheeseburger(s) would you like to buy?");
cheeseburgers = scan.nextInt();
System.out.print("How many French fries would you like to buy?");
fries = scan.nextInt();
System.out.print("How many drinks would you like to buy?");
beverages = scan.nextInt();
double hamburgerCost = (double)HAMBURGER_COST * hamburger;
double cheesebugerCost = (double)CHEESBURGER_COST * cheeseburger;
double frenchfriesCost = (double)FRENCHFRIES_COST * fries;
double beverageCost = (double)BEVERAGE_COST * beverages;
double orderCost = (double)hamburgerCost + (double)cheesebugerCost +
(double)frenchfriesCost + (double)beverageCost;
int numberItems = Hamburgers + Cheesburgers + Fries + Beverages;
NumberFormat fmt1 = NumberFormat.getCurrencyInstance();
System.out.println("Hamburger Cost : " + fmt1.format(hamburgerCost));
System.out.println("Cheeseburger Cost : " + fmt1.format(cheesebugerCost));
System.out.println("French Fries Cost : " + fmt1.format(frenchfriesCost));
System.out.println("Order Cost : " + fmt1.format(beverageCost));
System.out.println("Shake & Beverage Cost : " + fmt1.format(beverageCost));
System.out.println("Total number of items ordered : " + numberItems);
}
}

Related

How to cause an error message when a negative number is inputted?

I wrote the code to calculate the total based on the number of seats chosen by the user. The problem is when I enter a negative number for one of the seatings, the total is still calculated. Instead, when a negative number is inputted I want an error message to pop up and not calculate the total.
package javatheatreseating;
import java.util.Scanner;
public class JavaTheatreSeating {
public static final double PREMIUM_PRICE = 45.00;
public static final double STANDARD_PRICE = 30.00;
public static final double ECONOMY_PRICE = 21.00;
public static final double TAX_RATE = 0.0825;
public static final double SURCHARGE = 5.00;
public static void main(String[] args) {
int premiumSeats;
int standardSeats;
int economySeats;
double subTotal;
double salesTax;
double surCharge;
double total;
Scanner stdin = new Scanner(System.in);
//INPUT: number of seats sold
System.out.print ("Enter the number of Premium Seats Sold: ");
premiumSeats = stdin.nextInt();
System.out.print ("Enter the number of Standard Seats Sold: ");
standardSeats = stdin.nextInt();
System.out.print ("Enter the number of Economy Seats Sold: ");
economySeats = stdin.nextInt();
//PROCESS: i calculate the total and add the percent of tax based on the seats added
subTotal = premiumSeats * PREMIUM_PRICE + standardSeats * STANDARD_PRICE + economySeats * ECONOMY_PRICE;
salesTax = TAX_RATE * subTotal;
total = subTotal + salesTax + SURCHARGE;
//OUTPUT:
System.out.println();
System.out.println("Subtotal: " + subTotal);
System.out.println("Tax: " + salesTax);
System.out.println("surCharge: " + SURCHARGE);
System.out.println("Total: " + total);
}
}
put a while loop around each variable input and keep looping until the user gets it right. I didn't check if this compiles though.
while (true) {
try {
System.out.print ("Enter the number of Premium Seats Sold: ");
premiumSeats = stdin.nextInt();
if (premiumSeats >= 0){
break;
} else {
System.out.print ("Please Enter a positive integer.\n");
}
}
catch (Exception e){
System.out.print ("Please Enter a number.\n");
}
}

simple command line stock control

I want to develop a command line solution for a supermarket to calculate the total of the customer's purchase and display it and after every customer's purchase the initial stock of all the items must be updated.(I've used only 3 items).But it's only calculating the total not updating the stock.(And I need to do it by using while loops & for loops only
import java.util.*;
public class newClass {
public static void main(String args[]){
int qty1=100;
int qty2=200;
int qty3=300;
float price1=50.0f;
float price2=50.0f;
float price3=100.0f;
Scanner sc=new Scanner(System.in);
System.out.println("Input item1 quantity");
int no1=sc.nextInt();
System.out.println("Input item2 quantity");
int no2=sc.nextInt();
System.out.println("Input item3 quantity");
int no3=sc.nextInt();
float total=(no1*price1)+(no2*price2)+(no3*price3);
while(qty1>=no1 & qty2>=no2 & qty3>=no3 )
{
qty1=qty1-no1;
qty2=qty2-no2;
qty3=qty3-no3;
}
System.out.println("your total is="+total);
}
}
There are lots of issue with your code. Firstly you intend to loop based on the in-house initial quantity but are not looping it on it. After updating quantity, you need to ask input from the user again if you have given quantity. You are not closing the Scanner object properly.
Below is modified version of your code. Hope this helps
public static void main(String args[]) {
int qty1 = 100;
int qty2 = 200;
int qty3 = 300;
float price1 = 50.0f;
float price2 = 50.0f;
float price3 = 100.0f;
Scanner sc = new Scanner(System.in);
while (qty1 > 0 && qty2 > 0 && qty3 > 0) { // You any of the quantity is out of stock, exit loop
System.out.println("\nCurrent Stock: qty1: " + qty1 + " qty2: " + qty2 + " qty3: " + qty3 + "\n");
System.out.println("Input item1 quantity");
int no1 = sc.nextInt();
System.out.println("Input item2 quantity");
int no2 = sc.nextInt();
System.out.println("Input item3 quantity");
int no3 = sc.nextInt();
if (qty1 < no1) { // making sure that asked quantity is not more that in-house quantity
no1 = qty1;
}
if (qty2 < no2) {
no2 = qty2;
}
if (qty3 < no3) {
no3 = qty3;
}
qty1 = qty1 - no1;
qty2 = qty2 - no2;
qty3 = qty3 - no3;
float total = (no1 * price1) + (no2 * price2) + (no3 * price3);
System.out.println("your total is=" + total);
}
sc.close();
}

Couple of errors

When I call this code in Terminal on my Mac it tells me "could not find or load main class" also it gives me a lot of errors on all of my code regarding printf.
I am new to Java so I assume this code is error ridden.
All I am really asking is how to fix it so it will run in terminal when I type "java itemprice" for it to not show an error.
Also I would like some help with the system.out.printf code.
import java.util.Scanner;
public class itempricecalc {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.print("What is your first name?");
String fname = scanner.nextLine();
String invf = fname.substring(0,2);
System.out.print("What is your last name?");
String lname = scanner.nextLine();
String invl = lname.substring(0,2);
System.out.println("Your name is: " +fname + lname);
System.out.println("What is your zipcode?");
int zipcode = scanner.nextInt();
invoice = (invf + invl + zipcode);
double salestax = 6.25;
double subtotal = 0;
System.out.println("What is item 1 called?")
String itemonename = scanner.nextLine():
System.out.println("Enter price of item 1");
double itemone = scanner.nextDouble();
System.out.println("How many of item 1 did you purchase?");
int itemoneamount = scanner.nextInt();
System.out.println("What is item 2 called?")
String itemtwoname = scanner.nextLine():
System.out.println("Enter price of item 2");
double itemtwo = scanner.nextDouble();
System.out.println("How many of item 2 did you purchase?");
int itemtwoamount = scanner.nextInt();
System.out.println("What is item 3 called?")
String itemthreename = scanner.nextLine():
System.out.println("Enter price of item 3");
double itemthree = scanner.nextDouble();
System.out.println("How many of item 3 did you purchase?");
int itemthreeamount = scanner.nextInt();
double itemonetp = itemoneamount * itemone;
double itemtwotp = itemtwoamount * itemtwo;
double totalprice = itemonetp + itemtwotp;
subtotal = subtotal + totalprice;
double taxtotal = subtotal * salestax;
System.out.print("Your name is: " + fname + " " + lname)
System.out.printf(“Invoice: %.2f”, invoice);
System.out.printf(“Total price of item one is + %.2f”, itemonetp);
System.out.printf(“Total price of item two is + %.2f”, itemtwotp);
System.out.printf(“Total price of your purchase is + %.2f”, totalprice);
System.out.printf(“Your total with tax comes to + %.2f”, taxtotal);
System.out.printf(“You purchased + %.2f + amount of item one”, itemoneamount);
System.out.printf(“You purchased + %.2f + amount of item two”, itemtwoamount);
}
}

Java class to break down change into coins?

I am working on a java assignment where you enter the price of an object and the amount a theoretical customer handed you for the item. Then the program returns how much you owe them, and breaks it down into dollars, quarters, dimes, nickles, and pennies that you should give them.
Basically here's what it would look like when it runs
What was the purchase price? (exclude the decimal in calculation if it helps
you)
$98.50
How much money did you pay with? (exclude the decimal)
$100.00
The purchase price was $98.50
You payed $100.0
You received $1.50 in change.
0 one hundred dollar bill(s)
0 fifty dollar bill(s)
0 twenty dollar bill(s)
0 ten dollar bill(s)
0 five dollar bill(s)
1 one dollar bill(s)
2 quarter(s)
0 dime(s)
0 nickel(s)
0 penny/pennies
I understand most of it, but I cant seem to wrap my mind around the breakdown of the change handed back. Here's my code so far, but if someone could show me how to break down the change.
import java.util.*;
public class ChangeTendered {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("Enter the purchase price: ");
double price = scan.nextDouble();
System.out.println("Enter the amount payed: ");
double ammountPayed = scan.nextDouble();
double changeDue = ammountPayed - price;
int dollars = (int)changeDue;
System.out.println("Return"+ dollars+ "Dollars");
scan.close();
}
}
On a side note, I just cast the changeDue to an int in order to chop off the decimal and get the dollars due, if that caused any confusion.
Here is an initial approach
int change = (int)(Math.ceil(changeDue*100));
int dollars = Math.round((int)change/100);
change=change%100;
int quarters = Math.round((int)change/25);
change=change%25;
int dimes = Math.round((int)change/10);
change=change%10;
int nickels = Math.round((int)change/5);
change=change%5;
int pennies = Math.round((int)change/1);
System.out.println("Dollars: " + dollars);
System.out.println("Quarters: " + quarters);
System.out.println("Dimes: " + dimes);
System.out.println("Nickels: " + nickels);
System.out.println("Pennies: " + pennies);
You can add more code to the do it for currency notes as well.
From what I can understand, you need to break the returned money into different bills: 100, 50, 20, 10, 5 ... etc.
I think you can use 'division' to solve the problem in Java. The following pseudo code is how you might solve the problem:
//For example:
double changeDue = 15.5;
double moneyLeft = changeDue;
int oneHundred = moneyLeft / 100;
moneyLeft -= oneHundred * 100;
int fifty = moneyLeft / 50;
moneyLeft -= fifty*50 ;
...
//Just remember to 'floor' the result when divided by a double value:
int quarter = Math.floor( moneyLeft / 0.25);
moneyLeft -= quarter * 0.25 ;
...//Until your minimum requirement.
//Then print out your results.
Hope it helps.
What I did was convert it to a string then do a decimal split to separate the change and dollars.
import java.util.Scanner;
import java.text.DecimalFormat;
public class register
{
public static void main(String[] args)
{
DecimalFormat decimalFormat = new DecimalFormat("0.00");
Scanner kb = new Scanner(System.in);
System.out.print("How much does this item cose? -> ");
double cost = kb.nextDouble();
System.out.print("How many will be purcased? -> ");
double quanity = kb.nextDouble();
double subtotal = (cost * quanity);
double tax = (subtotal * .06);
double total = (subtotal + tax);
System.out.print("How much money has been tendered? -> ");
double tendered = kb.nextDouble();
double change = (tendered - total);
int dollars = (int)change;
int twenties = dollars / 20;
int dollars1 = dollars % 20;
int tens = dollars1 / 10;
int dollars2 = dollars % 10;
int fives = dollars2 / 5;
int dollars3 = dollars % 5;
int ones = dollars3;
String moneyString = decimalFormat.format(change);
String changeString = Double.toString(change);
String[] parts = moneyString.split("\\.");
String part2 = parts[1];
double cents5 = Double.parseDouble(part2);
int cents = (int)cents5;
int quarters = cents / 25;
int cents1 = cents % 25;
int dimes = cents1 / 10;
int cents2 = cents % 10;
int nickels = cents2 / 5;
int cents3 = cents % 5;
int pennies = cents3;
System.out.println("Cost: " + "$" + decimalFormat.format(cost));
System.out.println("Quanity: " + quanity);
System.out.println("Subtotal: " + "$" + decimalFormat.format(subtotal));
System.out.println("Tax: " + "$" + decimalFormat.format(tax));
System.out.println("Total: " + "$" + decimalFormat.format(total));
System.out.println("Tendered: " + "$" + decimalFormat.format(tendered));
System.out.println("Change: " + "$" + moneyString);
System.out.println(twenties + " Twenties");
System.out.println(tens + " Tens");
System.out.println(fives + " Fives");
System.out.println(ones + " Ones");
System.out.println(quarters + " Quarters");
System.out.println(dimes + " Dimes");
System.out.println(nickels + " Nickels");
System.out.println(pennies + " Pennies");
}
}
import java.math.BigDecimal;
import java.math.MathContext;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;
public class ChangeTenderedWorking {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the purchase price: ");
BigDecimal price = scan.nextBigDecimal();
System.out.println("Enter the amount paid: ");
BigDecimal amountPayed = scan.nextBigDecimal();
Map<Denomination, Integer> changeDue = getDenomination(amountPayed, price);
for(Denomination denomination : changeDue.keySet()) {
System.out.println("Return " + denomination + " bill(s) : "+ changeDue.get(denomination));
}
scan.close();
}
public static Map<Denomination, Integer> getDenomination(BigDecimal amountPayed, BigDecimal price) {
BigDecimal change = amountPayed.subtract(price);
System.out.println("Return change -- "+ change);
Map<Denomination, Integer> changeReturn = new LinkedHashMap<Denomination, Integer>();
for(Denomination denomination : Denomination.values()) {
BigDecimal[] values = change.divideAndRemainder(denomination.value, MathContext.DECIMAL32);
if(!values[0].equals(BigDecimal.valueOf(0.0)) && !values[1].equals(BigDecimal.valueOf(0.0))) {
changeReturn.put(denomination, values[0].intValue());
change = values [1];
}
}
return changeReturn;
}
enum Denomination {
HUNDRED(BigDecimal.valueOf(100)),
FIFTY(BigDecimal.valueOf(50)),
TWENTY(BigDecimal.valueOf(20)),
TEN(BigDecimal.valueOf(10)),
FIVE(BigDecimal.valueOf(5)),
TWO(BigDecimal.valueOf(2)),
ONE(BigDecimal.valueOf(1)),
QUARTER(BigDecimal.valueOf(0.25)),
DIME(BigDecimal.valueOf(0.10)),
NICKEL(BigDecimal.valueOf(0.5)),
PENNY(BigDecimal.valueOf(0.1));
private BigDecimal value;
Denomination(BigDecimal value) {
this.value = value;
}
}
}

Writing for loops/while loops?

I'm in a programming class in high-school, and I was given an assignment to make a basic subtotal and top calculator, but I work at a restaurant, so it seemed a little pointless to make a calculator that only let you read in one food. So I tried to make it able to take in multiple food items and add them to one price variable. Sorry if some of this code may seem inefficient or redundant. It's only high-school of course.
The issue is, when I run it, it gets up to the asking if there was another food item the user would like to add, and when I type in "Yes" or "No", the program does nothing. Keeps running, but goes no further. Any explanations?
import java.text.NumberFormat;
import java.util.Scanner;
public class Price {
/**
* #param args
*/
public static void main(String[] args) {
final double taxRate = .0887; //8.87% Tax Rate
double tipRate;
int quantity1;
Scanner kb = new Scanner(System.in);
double subtotal, tax, tip, totalCost1, unitPrice1 = 0;
String done;
System.out.println ("How many of the first item did you get?: ");
quantity1 = kb.nextInt();
for (int i = 0; i < quantity1; i++)
{
System.out.println ("What was the price of that single item "+(i+1) + ": ");
unitPrice1 = kb.nextDouble();
System.out.println ("Was there another food item you'd like to add?: ");
done=kb.next();
while (done.equalsIgnoreCase("Yes"));
}
System.out.println ("What percent would you like to tip? (Formatted like 0.10 for 10%, 0.20 for 20%, etc.): ");
tipRate = kb.nextDouble();
subtotal= quantity1 * unitPrice1;
tax = subtotal * taxRate;
totalCost1 = subtotal + tax;
tip = totalCost1 * tipRate;
totalCost1 = totalCost1 + tip;
//Formatting
NumberFormat money = NumberFormat.getCurrencyInstance();
NumberFormat tipMoney = NumberFormat.getCurrencyInstance();
NumberFormat taxPercent = NumberFormat.getPercentInstance();
NumberFormat tipPercent = NumberFormat.getPercentInstance();
System.out.println ("Your total before tax is: " + money.format(subtotal));
System.out.println ("The tax is " + money.format(tax) + " at " + tipPercent.format(taxRate));
System.out.println ("The tip at " + tipPercent.format(tipRate) + " is " + tipMoney.format(tip));
}
}
You have an infinite loop here:
while (done.equalsIgnoreCase("Yes"));
Once you enter Yes, it will keep sitting there and doing nothing because the value of done is Yes and never changes.
Also your loop structure is a bit odd. Your outer for loop runs as many times as the quantity of the first item. But shouldn't you only be multiplying that number to the cost? Because you are either running the loop for as long as the number of items the user entered (by asking them up front) or you don't ask them the total number of items and simply ask them to enter Yes if they want to add more items; you can't really do both.
Your loop should probably look something like this:
String input = "Yes";
while(input.equalsIgnoreCase("Yes")) {
System.out.println ("How many of the first item did you get? ");
quantity1 = kb.nextInt();
System.out.println ("What was the price of that single item? ");
unitPrice1 = kb.nextDouble();
//total += unitPrice1 * quantity1 - you don't have this in your code, but this is where you would be calculating the running total
System.out.println("Was there another food item you'd like to add? ");
input = kb.next();
}
you need to exit for loop when user enters yes, so you can use label here like below:
outerloop:
for (int i = 0; i < quantity1; i++)
{
System.out.println ("What was the price of that single item "+(i+1) + ": ");
unitPrice1 = kb.nextDouble();
System.out.println ("Was there another food item you'd like to add?: ");
done=kb.next();
while (done.equalsIgnoreCase("Yes")){
break outerloop;
}
}
Your current code does not do anything inside the while loop if you don't enter yes. And if you enter yes it will be stuck in infinite loop because of your while loop. This is not the efficeint way of looping, but this code will have least change in your current code.
You're while loop is doing nothing, you had given it a condition, but it has no instruction.
Try something like this..(sorry for my rusty java)
'public static void main(String[] args) {
//variable declaration
bool running = true
final double taxRate = .0887; //8.87% Tax Rate
double tipRate;
int quantity1;
Scanner kb = new Scanner(System.in);
double subtotal, tax, tip, totalCost1, unitPrice1 = 0;
String done;
while(running = true){
System.out.println ("How many of the first item did you get?: ");
quantity1 = kb.nextInt();
for (int i = 0; i < quantity1; i++)
{
System.out.println ("What was the price of that single item "+(i+1) + ": ");
unitPrice1 = kb.nextDouble();
System.out.println ("Was there another food item you'd like to add?: ");
done=kb.next();
if(done.equalsIgnoreCase("No")){
running = false
//Allows you to break out of the while loop if the user does not want to add anything else
//DO NOT USE BREAK STATMENTS, IT IS A POOR PROGRAMMING PRACTICE.
};//end if
}//end for
}//end while
System.out.println ("What percent would you like to tip? (Formatted like 0.10 for 10%, 0.20 for 20%, etc.): ");
tipRate = kb.nextDouble();
//You should comment whats going on here
subtotal= quantity1 * unitPrice1;
tax = subtotal * taxRate;
totalCost1 = subtotal + tax;
tip = totalCost1 * tipRate;
totalCost1 = totalCost1 + tip;
//Formatting
NumberFormat money = NumberFormat.getCurrencyInstance();
NumberFormat tipMoney = NumberFormat.getCurrencyInstance();
NumberFormat taxPercent = NumberFormat.getPercentInstance();
NumberFormat tipPercent = NumberFormat.getPercentInstance();
//Output
System.out.println ("Your total before tax is: " + money.format(subtotal));
System.out.println ("The tax is " + money.format(tax) + " at " + tipPercent.format(taxRate));
System.out.println ("The tip at " + tipPercent.format(tipRate) + " is " + tipMoney.format(tip));
}//end main

Categories

Resources