Assignemnt #103
Code
/// Name: Daniel Nguyen
/// Period: 6
/// Project Name: KeyCh3
/// File Name: KeyCh3.java
/// Date: 3/11/2015
import java.util.Scanner;
import java.util.Scanner;
import java.util.Scanner;
import java.util.InputMismatchException;
public class KeyCh3
{
static int keyChains = 0;
static float tax = 0.0825f;
static int baseShipping = 5;
static int shippingPerKey = 1;
static int pricePerKey = 7;
static Scanner keyboard = new Scanner(System.in);
public static void main( String[] args )
{
System.out.println("Welcome to the keychain store!!!\n");
int choice = 0;
do
{
showMenu();
do choice = askChoice();
while (choice==0);
if (choice == 1) addKey();
if (choice == 2) removeKey();
if (choice == 3) viewOrder();
}
while (choice!=4);
checkout();
}
public static void showMenu()
{
System.out.println("1. Add Keychains to Order");
System.out.println("2. Remove Keychains from Order");
System.out.println("3. View Current Order");
System.out.println("4. Checkout\n");
}
public static int askChoice()
{
int choice = 0;
System.out.print("Enter your choice: ");
try
{
choice = keyboard.nextInt();
}
catch (InputMismatchException e)
{
choice = 0;
System.out.println("WRONG INPUT!!!\n");
}
if (choice < 1 || choice > 4) choice = 0;
return choice;
}
public static void addKey()
{
displayAmount();
System.out.print("How many to add? ");
try
{
int add = keyboard.nextInt();
if (add>0)
{
keyChains += add;
System.out.println("You now have "+keyChains+" keychains. \n");
}
else System.out.println("WRONG INPUT!!!\n");
}
catch (InputMismatchException e)
{
System.out.println("WRONG INPUT!!!\n");
}
}
public static void removeKey()
{
displayAmount();
System.out.print("How many to remove? ");
try
{
int add = keyboard.nextInt();
if (add>0 && add<= keyChains)
{
keyChains -= add;
System.out.println("You now have "+keyChains+" keychains. \n");
}
else System.out.println("WRONG INPUT!!!\n");
}
catch (InputMismatchException e)
{
System.out.println("WRONG INPUT!!!\n");
}
}
public static void viewOrder()
{
int cost = (pricePerKey*keyChains+(baseShipping + keyChains*shippingPerKey));
displayAmount();
System.out.println("\nKeychains each cost 7$.");
System.out.println("Shipping wil cost you " + (baseShipping + keyChains*shippingPerKey) + "$.");
System.out.println("Subtotal before tax is "+cost+"$.\n");
System.out.println("Tax is "+cost*tax+"$.\n");
System.out.println("Total cost is "+cost*(1+tax)+"$.\n");
}
public static void checkout()
{
System.out.println("\nCHECKOUT\n");
System.out.print("What is your name? ");
String name = keyboard.next();
System.out.print("\n\n");
viewOrder();
System.out.print("Thank you for your order, " + name + ".\n");
}
public static void displayAmount() {
System.out.print("You have " + keyChains + " keychains. ");
}
}
Picture of the output