Programming Assignment #103
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;
double tax = .0825;
int baseShipping = 5;
int perKeychainShipping = 1;
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, tax, baseShipping, perKeychainShipping);
break;
case 4: checkout(price, keyNum, tax, baseShipping, perKeychainShipping);
break;
default: System.out.println("That's not a valid choice."); 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();
if(currentNum - removed >= 0){
return currentNum - removed;
}
else{
System.out.println("You can't have negative keychains...");
return currentNum;
}
}
public static void viewOrder(int kPrice, int currentNum, double tax, int baseShipping, int pKShipping){
int shipPrice = currentNum * pKShipping + baseShipping;
int total = shipPrice + kPrice * currentNum;
Scanner k = new Scanner(System.in);
System.out.println("Keychains: " + currentNum);
System.out.println("Price: $" + kPrice);
System.out.println("Shipping: $" + (currentNum * pKShipping + baseShipping));
System.out.println("Price no tax: $" + total);
System.out.println("Total Price: $" + (total * tax));
}
public static void checkout(int kPrice, int currentNum, double tax, int baseShipping, int pKShipping){
int shipPrice = currentNum * pKShipping + baseShipping;
int total = shipPrice + kPrice * 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("Shipping: $" + (currentNum * pKShipping + baseShipping));
System.out.println("Price no tax: $" + total);
System.out.println("Total Price: $" + (total * tax));
System.out.println("Thanks for your order, " + name + "!");
}
}
Outputs