Assignment #102: Keychains for Sale, part II
Code
import java.util.Scanner;
public class KeychainsII {
public static void main (String[] args) {
Scanner kb = new Scanner(System.in);
int menuSelect, price = 10, total = 0;
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);
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);
menuSelect = 5;
}
}
}
public static int addKeychains(int totalkc) {
Scanner kb = new Scanner(System.in);
int add;
System.out.println("Add keychains:");
add = kb.nextInt();
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();
totalkc = totalkc - rem;
return totalkc;
}
public static void viewOrder(int totalkc, int pricekc) {
System.out.println("View order:");
System.out.println("You have ordered " + totalkc + " keychains at $10 per keychain for a total price of $" + (totalkc * pricekc) + ".");
}
public static void checkoutCounter(int totalkc, int pricekc) {
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 bought " + totalkc + " keychains at $10 per keychain for a total price of $" + (totalkc * pricekc) + ".");
System.out.println("Thank you for shopping at the Java Keychain Store!");
}
}
Picture of the output