Programming Assignment #102
Code
import java.util.Scanner;
public class KeyChains{
public static void main(String[] args){
Scanner k = new Scanner(System.in);
int price = 10;
int keyNum = 0;
int i = 0;
while(i != 4){
System.out.println("");
System.out.println("Keychain shop");
System.out.println("");
System.out.println("1) Add Keychains to your order");
System.out.println("2) Remove Keychains from your order");
System.out.println("3) View your current order");
System.out.println("4) Checkout");
System.out.println("");
System.out.print("Please enter your choice: ");
i = k.nextInt();
System.out.println("");
switch(i){
case 1: keyNum = addKeychains(keyNum);
break;
case 2: keyNum = removeKeychains(keyNum);
break;
case 3: viewOrder(price, keyNum);
break;
case 4: checkout(price, keyNum);
break;
default: break;
}
}
}
public static int addKeychains(int currentNum){
Scanner k = new Scanner(System.in);
System.out.print("You currently have " + currentNum + " keychains in your order. How many do you want to add? ");
int added = k.nextInt();
return currentNum + added;
}
public static int removeKeychains(int currentNum){
Scanner k = new Scanner(System.in);
System.out.println("You currently have " + currentNum + " keychains in your order. How many do you want to remove? ");
int removed = k.nextInt();
return currentNum - removed;
}
public static void viewOrder(int kPrice, int currentNum){
Scanner k = new Scanner(System.in);
System.out.println("Keychains: " + currentNum);
System.out.println("Price: $" + kPrice);
System.out.println("Total Price: $" + (kPrice * currentNum));
}
public static void checkout(int kPrice, int currentNum){
Scanner k = new Scanner(System.in);
System.out.print("What's your name? ");
String name = k.next();
System.out.println("Keychain: " + currentNum);
System.out.println("Price: $" + kPrice);
System.out.println("Total Cost: " + (kPrice * currentNum));
System.out.println("Thanks for your order, " + name + "!");
}
}
Outputs