Fast Food Java Program - java

I have to write a program application that allows an employee to enter the number of burgers, fries, and soft drinks that a customer orders.
After my "final total", I should have another part that says "Enter Amount tendered:" and "Change:" from the customer. I have the code written out, I'm not sure what to write out for the amount tendered and the change?
public class FastFood {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
final double pburgers=2.49;
final double pfries=1.89;
final double psodas=0.99;
final double ptax=0.13;
double burgers;
double fries;
double sodas;
double totaltax;
double total;
double tax;
Scanner input = new Scanner(System.in);
System.out.print("Enter the amount of burgers: ");
burgers = input.nextDouble();
System.out.print("Enter the amount of fries: ");
fries = input.nextDouble();
System.out.print("Enter the amount of soft drinks: ");
sodas = input.nextDouble();
totaltax = (burgers*pburgers)+(fries*pfries)+(sodas*psodas);
tax = totaltax*ptax;
total = totaltax + tax;
System.out.format("Total before tax is: $%-5.2f\n", totaltax);
System.out.format("Tax: $%-5.2f\n", tax);
System.out.format("Your final total is: $%-5.2f\n", total);
}
}

import java.util.Scanner;
public class FastFood {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
final double pburgers=2.49;
final double pfries=1.89;
final double psodas=0.99;
final double ptax=0.13;
double burgers;
double fries;
double sodas;
double totaltax;
double total;
double tax;
double tender;
double change;
Scanner input = new Scanner(System.in);
System.out.print("Enter the amount of burgers: ");
burgers = input.nextDouble();
System.out.print("Enter the amount of fries: ");
fries = input.nextDouble();
System.out.print("Enter the amount of soft drinks: ");
sodas = input.nextDouble();
totaltax = (burgers*pburgers)+(fries*pfries)+(sodas*psodas);
tax = totaltax*ptax;
total = totaltax + tax;
System.out.format("Total before tax is: $%-5.2f\n", totaltax);
System.out.format("Tax: $%-5.2f\n", tax);
System.out.format("Your final total is: $%-5.2f\n", total);
System.out.print("Enter amount tendered: " );
tender = input.nextDouble();
change = tender - total;
System.out.format("Change: $%-5.2f\n", change);
}
}

Related

Compute Total Tip and Tax for Restaurant Bill

I need to use two methods. One is getMealCharge() and needs to return a mealCharge and no argument. The second is computeAndPrintTotalBill() and needs to do calculation in that method.
My question is- when I get the user input from first method, how do I get that number to apply to the second method so it can calculate.
If I put everything on the first method, it will work. But, for some reasons in the second method it won't show up. If anyone can please help me finding what I am doing wrong. Thank you.
import java.util.Scanner;
public class ComputeTip{
final double taxRate = 0.0725;
final double tipRate = 0.15;
double mealCharge;
double tax;
double tip;
double total;
public double getMealCharge(){
System.out.println("Enter meal charge: ");
Scanner keyboard = new Scanner(System.in);
mealCharge = keyboard.nextDouble();
return mealCharge;
}
public void computeAndPrintTotalBill(double getMealCharge, double mealCharge){
Scanner keyboard = new Scanner(System.in);
tax = mealCharge * taxRate;
tip = mealCharge * tipRate;
total = mealCharge + tax + tip;
Test.println("charge: " + mealCharge);
Test.println("tax: " + tax);
Test.println("tip: " + tip);
Test.println("total: " + total);
}
}
You are using the parameters wrong.
Try this:
public void computeAndPrintTotalBill(){
double mealCharge = getMealCharge();
tax = mealCharge * taxRate;
tip = mealCharge * tipRate;
total = mealCharge + tax + tip;
Test.println("charge: " + mealCharge);
Test.println("tax: " + tax);
Test.println("tip: " + tip);
Test.println("total: " + total);
}
You can use only computeAndPrintTotalBill method to get your work done by modifying your methods and variable as below:
final static double taxRate = 0.0725;
final static double tipRate = 0.15;
public static void computeAndPrintTotalBill(double mealCharge) {
double tax= mealCharge * taxRate;
double tip = mealCharge * tipRate;
double total= mealCharge + tax + tip;
System.out.println("charge: " + mealCharge);
System.out.println("tax: " + tax);
System.out.println("tip: " + tip);
System.out.println("total: " + total);
}
public static void main(String...args){
Scanner keyboard = new Scanner(System.in);
Double mealCharge = keyboard.nextDouble();
computeAndPrintTotalBill(mealCharge);
}
OUTPUT:
21.5
charge: 21.5
tax: 1.5587499999999999
tip: 3.225
total: 26.28375
Note: You can also do
ComputeTip computeTip = new ComputeTip();
double mealCharge = computeTip.getMealCharge();
computeTip.computeAndPrintTotalBill(mealCharge);
In your original program (Need to remove double getMealCharge from your method signature). This also works fine.

Basic Java, passing data between methods

I am new to Java and trying to make a basic body mass calculator.
My problem is I need to ask the questions, convert the measurements and then pass it to another method then display the results in a separate method.
I've added my code below but keep getting a 1.0 returned as the answer each time.
import java.util.Scanner;
public class calcBMI {
public static void main(String[] args)
{
Scanner keyboard = new Scanner( System.in );
System.out.print("Enter weight in pounds: ");
double weightInPounds = keyboard.nextDouble();
double weightInKg = (weightInPounds / 2.2);
System.out.print("Enter height in inches: ");
double heightInInches = keyboard.nextDouble();
double heightInMeters = (heightInInches / 0.254);
double resultBMI = 1;
displayResults(resultBMI);
}
public static double bodyMassIndex(double weightInKg, double
heightInMeters)
{
double resultBMI = weightInKg / Math.pow(heightInMeters, 2) * 1.375;
return resultBMI;
}
public static void displayResults(double resultBMI)
{
System.out.printf("The calculated body mass index was: " + resultBMI);
System.out.println();
}
}
Updated code, now getting;
Enter weight in pounds: 180
Enter height in inches: 68
The calculated body mass index was: 1.1415618118905313E-5
BUILD SUCCESSFUL (total time: 3 seconds)
import java.util.Scanner;
public class calcBMI {
public static void main(String[] args)
{
Scanner keyboard = new Scanner( System.in );
System.out.print("Enter weight in pounds: ");
double weightInPounds = keyboard.nextDouble();
double weightInKg = (weightInPounds / 2.2);
System.out.print("Enter height in inches: ");
double heightInInches = keyboard.nextDouble();
double heightInMeters = (heightInInches / 0.0254);
displayResults(bodyMassIndex(weightInKg, heightInMeters));
}
public static double bodyMassIndex(double weightInKg, double heightInMeters)
{
return (weightInKg / Math.pow(heightInMeters, 2));
}
public static void displayResults(double resultBMI)
{
System.out.printf("The calculated body mass index was: " + resultBMI);
System.out.println();
}
}
You are not calling the bodyMassIndex method in your code at all. Change
displayResults(resultBMI);
to
displayResults(bodyMassIndex(weightInKg, heightInMeters));
resultBMI equals 1, so of course the output would always be :
"The calculated body mass index was: 1"
Full code:
public static void main(String[] args) {
System.out.print("Enter weight in pounds: ");
double weightInPounds = keyboard.nextDouble();
double weightInKg = (weightInPounds / 2.2);
System.out.print("Enter height in inches: ");
double heightInInches = keyboard.nextDouble();
double heightInMeters = (heightInInches / 0.254);
// You can get rid of the resultBMI variable
displayResults(bodyMassIndex(weightInKg, heightInMeters));
}
You get 1.0 because you hard coded it as such.
Change this:
double resultBMI = 1;
To:
double resultBMI = bodyMassIndex(weightInKG, heightInMeters);
By the way, you could also have returned BMI directly in the method and there is no need to multiply by 1.375 anymore since you are already supplying weight in KG:
public static double bodyMassIndex(double weightInKg, double heightInMeters)
{
return (weightInKg / (heightInMeters*heightInMeters));
}
Add on:
Your conversion from inches to meters is wrong as well. It should be:
double heightInMeters = (heightInInches * 0.0254);

Can't get correct output from created class (wage + commission calculator)

So I'm writing a program that is supposed to take a person's wage ($7.25) and add their commission, then spit out the total. I input all the requested info and I get 0.0 for the result, which is obviously wrong. Any help is appreciated, thanks!!
HERE'S WHAT I HAVE IN THE DRIVER CLASS:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double hours, sales, total;
DecimalFormat df = new DecimalFormat("$0.00");
Wages emp1 = new Wages();
System.out.println("Please enter the hours worked: ");
hours = input.nextDouble();
System.out.println("Please the amount of sales: ");
sales = input.nextDouble();
System.out.println("Your total pay check wages are: " + df.format(emp1.getEverything()));
}
AND HERE'S THE WAGES CLASS I CREATED:
public class Wages {
private double PAY_RATE = 7.25;
public double sales, commission, total;
public int hours;
public Wages(){
}
public Wages(double rate,double sales, double total, double commission, int hours){
this.sales = sales;
this.total = total;
this.PAY_RATE = rate;
this.commission = commission;
this.hours = hours;
}
public double getSales(){
return sales;
}
public double getCommission(){
if(sales >= 1.00 && sales <= 99.99){
commission = (sales * 0.05);
return commission;
}
else if(sales >= 100.00 && sales <= 299.99){
commission = (sales * 0.10);
return commission;
}
else if(sales >= 300.00){
commission = (sales * 0.15);
return commission;
}
return commission;
}
public double getEverything(){
total = (PAY_RATE * hours) + commission;
return total;
}
}
You have created a constructor that takes sales and hours, but you didn't use that constructor and instead called default.
So sales and hours have no values.
If I were to do it, I'd change up your Wages class to include this:
public double hours; //You're taking a double for their input, so replace the int
public Wages(double hours, double sales){
this.sales = sales;
this.hours = hours;
}
//...
public double getEverything(){
total = (PAY_RATE * hours) + getCommission();
return total;
}
and use this in the driver:
System.out.println("Please enter the hours worked: ");
hours = input.nextDouble();
System.out.println("Please the amount of sales: ");
sales = input.nextDouble();
Wages emp1 = new Wages(hours, sales);

restaurant bill, initializing error

import java.util.Scanner;
import javax.swing.JOptionPane;
public class RestaurantBill3
{
public static void main(String [] args)
{
//Constant
final double TAX_RATE = 0.0675;
final double TIP_PERCENT = 0.15;
//Variables
double cost;
double taxAmount = TAX_RATE * cost; //Tax amount
double totalWTax = taxAmount + cost; //Total with tax
double tipAmount = TIP_PERCENT * totalWTax; //Tip amount
double totalCost = taxAmount + tipAmount + totalWTax; //Total cost of meal
Scanner keyboard = new Scanner(System.in);
System.out.print("What is the cost of your meal? ");
cost = keyboard.nextDouble();
System.out.println("Your meal cost $" +cost);
System.out.println("Your Tax is $" + taxAmount);
System.out.println("Your Tip is $" + tipAmount);
System.out.println("The total cost of your meal is $" + totalCost);
System.exit(0); //End program
}
}
/*
I keep receiving the error that cost has evidently not been initialized, but if it's waiting on input, how is it supposed to do that?*/
You are referring to the value of cost before it's initialized here:
double taxAmount = TAX_RATE * cost;
double totalWTax = taxAmount + cost;
Move the initialization of those variables after the initialization of cost, so cost will have a value when it's referenced.
Look at how you declare the variable cost. You are declaring a variable but you are not assigning it a value, hence it being uninitialized. I think there is a bigger conceptual problem though. Let's look at your code:
double cost; // this is uninitialized because it has not been assigned a value yet
double taxAmount = TAX_RATE * cost; //Tax amount
double totalWTax = taxAmount + cost; //Total with tax
double tipAmount = TIP_PERCENT * totalWTax; //Tip amount
double totalCost = taxAmount + tipAmount + totalWTax; //Total cost of meal
Here, what is happening is you are declaring variables and setting their value to be the result of the expression - the right hand side of the equals sign. The program flow is top down, in this case, and these statements are executed sequentially. When taxAmount and your other variables are declared and assigned, the value of cost is unknown. This results in a compiler error. Try rewriting your code like this, keeping in mind that cost needs be assigned a value before using it.
public static void main(String [] args) {
//Constant
final double TAX_RATE = 0.0675;
final double TIP_PERCENT = 0.15;
//Variables
double cost, taxAmount; // rest of variables
Scanner keyboard = new Scanner(System.in);
System.out.print("What is the cost of your meal? ");
cost = keyboard.nextDouble();
System.out.println("Your meal cost $" +cost);
taxAmount = TAX_RATE * cost;
System.out.println("Your Tax is $" + taxAmount);
// rest of code
System.exit(0);
}

Savings Account Class + How to turn in?

The problem
Design a SavingsAccount class that stores a savings account’s annual interest rate and balance. The class constructor should accept the amount of the savings account’s starting balance. The class should also have methods for subtracting the amount of a withdrawal, adding the amount of a deposit, and adding the amount of monthly interest to the balance. The monthly interest rate is the annual interest rate divided by twelve. To add the monthly interest to the balance, multiply the monthly interest rate by the balance, and add the result to the balance.
Test the class in a program that calculates the balance of a savings account at the end of a period of time. It should ask the user for the annual interest rate, the starting balance, and the number of months that have passed since the account was established. A loop should then iterate once for every month, performing the following:
Ask the user for the amount deposited into the account during the month. Use the class method to add this amount to the account balance.
Ask the user for the amount withdrawn from the account during the month. Use the class method to subtract this amount from the account balance.
Use the class method to calculate the monthly interest.
After the last iteration, the program should display the ending balance, the total amount of deposits, the total amount of withdrawals, and the total interest earned.
I keep getting one issue in the Main Program on line 22, that if I fix, it messes up a bunch of the code. Anyone know how I could go about fixing this?
import java.text.DecimalFormat;
import java.util.Scanner;
import java.io.*;
public class SavingsAccountMainProgram {
public static void main(String args[]) {
Scanner keyboard = new Scanner(System.in);
System.out.print("How much money is in the account?: ");
double startingBalance = keyboard.nextDouble();
System.out.print("Enter the annual interest rate:");
double annualInterestRate = keyboard.nextDouble();
SavingsAccountClass savingAccountClass = new SavingsAccountClass();
SavingsAccount savingsAccountClass = savingAccountClass.new SavingsAccount(
startingBalance, annualInterestRate);
System.out.print("How long has the account been opened? ");
double months = keyboard.nextInt();
double montlyDeposit;
double monthlyWithdrawl;
double interestEarned = 0.0;
double totalDeposits = 0;
double totalWithdrawn = 0;
for (int i = 1; i <= months; i++) {
System.out.print("Enter amount deposited for month: " + i + ": ");
montlyDeposit = keyboard.nextDouble();
totalDeposits += montlyDeposit;
savingsAccountClass.deposit(montlyDeposit);
System.out.print("Enter amount withdrawn for " + i + ": ");
monthlyWithdrawl = keyboard.nextDouble();
totalWithdrawn += monthlyWithdrawl;
savingsAccountClass.withdraw(monthlyWithdrawl);
savingsAccountClass.addInterest();
interestEarned += savingsAccountClass.getLastAmountOfInterestEarned();
}
keyboard.close();
DecimalFormat dollar = new DecimalFormat("#,##0.00");
System.out.println("Total deposited: $" + dollar.format(totalDeposits));
System.out.println("Total withdrawn: $" + dollar.format(totalWithdrawn));
System.out.println("Interest earned: $" + dollar.format(interestEarned));
System.out.println("Ending balance: $"
+ dollar.format(savingsAccountClass.getAccountBalance()));
}}
And the other program for this
import java.util.Scanner;
import java.io.*;
public class SavingsAccountClass {
class SavingsAccount {
private double accountBalance;
private double annualInterestRate;
private double lastAmountOfInterestEarned;
public SavingsAccount(double balance, double interestRate) {
accountBalance = balance;
annualInterestRate = interestRate;
lastAmountOfInterestEarned = 0.0;
}
public void withdraw(double withdrawAmount) {
accountBalance -= withdrawAmount;
}
public void deposit(double depositAmount) {
accountBalance += depositAmount;
}
public void addInterest() {
// Get the monthly interest rate.
double monthlyInterestRate = annualInterestRate / 12;
// Calculate the last amount of interest earned.
lastAmountOfInterestEarned = monthlyInterestRate * accountBalance;
// Add the interest to the balance.
accountBalance += lastAmountOfInterestEarned;
}
public double getAccountBalance() {
return accountBalance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public double getLastAmountOfInterestEarned() {
return lastAmountOfInterestEarned;
}
}
}
Also, would anyone know how I could submit a .class file like the teacher has requested?
Not sure why you have SavingsAccount as inner class. For what purpose?
Here I modified what you have shared and I think it answers the problem statement well
main method
public static void main (String arg[]) {
Scanner keyboard = new Scanner(System.in);
System.out.print("How much money is in the account?: ");
double startingBalance = keyboard.nextDouble();
System.out.print("Enter the annual interest rate:");
double annualInterestRate = keyboard.nextDouble();
SavingsAccount savingsAccount = new SavingsAccount(startingBalance, annualInterestRate);
System.out.print("How long has the account been opened? ");
double months = keyboard.nextInt();
double montlyDeposit;
double monthlyWithdrawl;
double interestEarned = 0.0;
double totalDeposits = 0;
double totalWithdrawn = 0;
for (int i = 1; i <= months; i++) {
System.out.print("Enter amount deposited for month: " + i + ": ");
montlyDeposit = keyboard.nextDouble();
totalDeposits += montlyDeposit;
savingsAccount.deposit(montlyDeposit);
System.out.print("Enter amount withdrawn for " + i + ": ");
monthlyWithdrawl = keyboard.nextDouble();
totalWithdrawn += monthlyWithdrawl;
savingsAccount.withdraw(monthlyWithdrawl);
savingsAccount.addInterest();
interestEarned += savingsAccount.getLastAmountOfInterestEarned();
}
keyboard.close();
DecimalFormat dollar = new DecimalFormat("#,##0.00");
System.out.println("Total deposited: $" + dollar.format(totalDeposits));
System.out.println("Total withdrawn: $" + dollar.format(totalWithdrawn));
System.out.println("Interest earned: $" + dollar.format(interestEarned));
System.out.println("Ending balance: $" + dollar.format(savingsAccount.getAccountBalance()));
}
and, removed SavingsAccountClass and made SavingsAccount as the top level class
class SavingsAccount {
.
.
//everything as it is
}

Categories