Write a program which consists of a single class called BuyStuff that asks the user for two amounts, adds them, calculates tax at 15%, shows the result to the user, and asks for money. It then compares if the person gave enough money. If so, it displays the amount of change to return otherwise it displays a message asking for more money
public class BuyStuff {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Amount 1: ");
double amount1 = scanner.nextDouble();
System.out.println("Amount 2: ");
double amount2 = scanner.nextDouble();
double sum = addAmounts(amount1, amount2) + tax(addAmounts(amount1, amount2));
double depMoney = 0;
double count = 0;
System.out.println("result: " + sum + " Deposit money: ");
while (depMoney < sum) {
count = scanner.nextDouble();
depMoney = depMoney + count;
if (depMoney >= sum) {
break;
}
System.out.println("result: " + depMoney + " left to deposit: " + (sum - depMoney) + " Deposit more money: ");
}
System.out.println("Ok!");
scanner.close();
}
private static double addAmounts(double a, double b) {
return a + b;
}
private static double tax(double a) {
return a * 15 / 100;
}
}
Comments
Leave a comment