Assignment #103: Keychains for Sale, part 3
Code
import java.util.Scanner;
public class KeychainsIII {
public static void main (String[] args) {
Scanner kb = new Scanner(System.in);
int menuSelect, price = 10, total = 0, shipping = 5, shipunit = 1;
double tax = 8.25;
System.out.println("1. Add Keychains");
System.out.println("2. Remove Keychains");
System.out.println("3. View Order");
System.out.println("4. Checkout");
System.out.println("Please enter your choice: ");
menuSelect = kb.nextInt();
while (menuSelect < 5) {
if (menuSelect == 1) {
total = addKeychains(total);
System.out.println("1. Add Keychains");
System.out.println("2. Remove Keychains");
System.out.println("3. View Order");
System.out.println("4. Checkout");
System.out.println("Please enter your choice: ");
menuSelect = kb.nextInt();
}
if (menuSelect == 2) {
total = removeKeychains(total);
System.out.println("1. Add Keychains");
System.out.println("2. Remove Keychains");
System.out.println("3. View Order");
System.out.println("4. Checkout");
System.out.println("Please enter your choice: ");
menuSelect = kb.nextInt();
}
if (menuSelect == 3) {
viewOrder(total, price, shipping, shipunit, tax);
System.out.println("1. Add Keychains");
System.out.println("2. Remove Keychains");
System.out.println("3. View Order");
System.out.println("4. Checkout");
System.out.println("Please enter your choice: ");
menuSelect = kb.nextInt();
}
if (menuSelect == 4) {
checkoutCounter(total, price, shipping, shipunit, tax);
menuSelect = 5;
}
}
}
public static int addKeychains(int totalkc) {
Scanner kb = new Scanner(System.in);
int add;
System.out.println("Add keychains:");
add = kb.nextInt();
if (add <= 0) {
System.out.println("Please submit a valid order.");
System.out.println("Add keychains:");
add = kb.nextInt();
}
if (add > 0) {
totalkc = totalkc + add;
}
return totalkc;
}
public static int removeKeychains(int totalkc) {
Scanner kb = new Scanner(System.in);
int rem;
System.out.println("Remove keychains:");
rem = kb.nextInt();
if (rem <= 0 || rem > totalkc) {
System.out.println("Please submit a valid order.");
System.out.println("Remove keychains:");
rem = kb.nextInt();
}
if (rem > 0) {
totalkc = totalkc - rem;
}
return totalkc;
}
public static void viewOrder(int totalkc, int pricekc, int shiptotal, int shipkc, double taxorder) {
System.out.println("View order:");
System.out.println("You have ordered " + totalkc + " keychains.");
System.out.println("Keychains cost $" + pricekc + " per keychain.");
System.out.println("There is a $" + shiptotal + " shipping cost plus a $" + shipkc + " shipping cost per unit.");
System.out.println("There is a " + taxorder + "% sales tax on the order.");
System.out.println("The total cost is $" + (((totalkc * pricekc) + shiptotal + (shipkc * totalkc)) * (1 + (taxorder / 100))) + ".");
}
public static void checkoutCounter(int totalkc, int pricekc, int shiptotal, int shipkc, double taxorder) {
Scanner kb = new Scanner(System.in);
String name;
System.out.println("Checkout:");
System.out.println("What's your name?");
name = kb.next();
System.out.println(name + ", you have bought " + totalkc + " keychains.");
System.out.println("Keychains cost $" + pricekc + " per keychain.");
System.out.println("There is a $" + shiptotal + " shipping cost plus a $" + shipkc + " shipping cost per unit.");
System.out.println("There is a " + taxorder + "% sales tax on the order.");
System.out.println("The total cost is $" + (((totalkc * pricekc) + shiptotal + (shipkc * totalkc)) * (1 + (taxorder / 100))) + ".");
}
}
Picture of the output