Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

import java.util.

Scanner;

public class GarageDoorOpener {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String choice = "Y";

while (choice.equalsIgnoreCase("Y")) {
int quantity = 0;
double unitPrice = 0.0;
double credit = 0.0;

// Ask user for inputs


System.out.print("Enter the quantity of garage door openers ordered:
");
if (input.hasNextInt()) {
quantity = input.nextInt();
} else {
System.out.println("Invalid input. Please enter a valid integer.");
input.next();
continue;
}

System.out.print("Enter the unit price of the garage door openers: ");


if (input.hasNextDouble()) {
unitPrice = input.nextDouble();
} else {
System.out.println("Invalid input. Please enter a valid decimal
number.");
input.next();
continue;
}

System.out.print("Enter the credit that the contractor has with the


company: ");
if (input.hasNextDouble()) {
credit = input.nextDouble();
} else {
System.out.println("Invalid input. Please enter a valid decimal
number.");
input.next();
continue;
}

// Calculate total charge


double totalCharge = calculateOrders(quantity, unitPrice, credit);

// Print total charge


System.out.printf("The total charge is $%.2f%n", totalCharge);

// Ask user if they want to try again


System.out.print("Do you want to try again? (Y/N) ");
choice = input.next();
}
}

public static double calculateOrders(int quantity, double unitPrice, double


credit) {
final double DISCOUNT_RATE = 0.15;
final double TAX_RATE = 0.095;

double totalAmount = quantity * unitPrice;


double discountAmount = totalAmount * DISCOUNT_RATE;
double taxableAmount = totalAmount - discountAmount;
double taxAmount = taxableAmount * TAX_RATE;
double totalCharge = taxableAmount + taxAmount - credit;

return totalCharge;
}
}

You might also like