Assignment # 103 and Keychains ULtimate Power
Code
/// Name: Tommy Oyuntseren
/// Period: 7
/// Program Name: KeychainsUltimatePower
/// File Name: KeychainsUltimatePower.java
/// Date: 4/12/2016
import java.util.Scanner;
public class KeychainsUltimatePower
{
public static void main ( String[] args )
{
Scanner keyboard = new Scanner(System.in);
int choice, numKeychains = 0, pricePerKeychain = 10, baseShipping = 5, perKeychainShipping = 1;
double tax = .0825;
System.out.println("Unique Keychain Shop");
do
{
System.out.println("\n1. Add Keychains to Order");
System.out.println("2. Remove Keychains from Order");
System.out.println("3. View Current Order");
System.out.println("4. Checkout");
System.out.print("\nPlease enter your choice: ");
choice = keyboard.nextInt();
while (choice> 4 || choice< 1)
{
System.out.print("Error. Please choose a number between one and four. Try again: ");
choice = keyboard.nextInt();
}
if (choice== 1)
{
numKeychains= addKeychains(numKeychains);
}
else if (choice== 2)
{
numKeychains= removeKeychains(numKeychains);
}
else if (choice== 3)
{
viewOrder(numKeychains, pricePerKeychain, tax, baseShipping, perKeychainShipping);
}
else if (choice== 4)
{
checkout(numKeychains, pricePerKeychain, tax, baseShipping, perKeychainShipping );
}
}while (choice!= 4);
}
public static int addKeychains(int numKeychains)
{
Scanner keyboard= new Scanner(System.in);
System.out.print("\nYou have " + numKeychains + " keychains. How many to add? ");
int add= keyboard.nextInt();
while (add< 0)
{
System.out.print( "You must add a positive number of keychains. Try again: how many would you like to add? ");
add = keyboard.nextInt();
}
numKeychains= numKeychains + add;
System.out.print("You now have " + numKeychains + " keychains.\n");
return numKeychains;
}
public static int removeKeychains(int numKeychains)
{
Scanner keyboard= new Scanner(System.in);
System.out.print("\nYou have " + numKeychains + " keychains. How many would you like to remove? ");
int remove = keyboard.nextInt();
while (remove> numKeychains)
{
System.out.println( "Error. The number of keychains to remove exceeds number of keychains in cart.");
System.out.print( "How many would you like to remove? " );
remove= keyboard.nextInt();
}
numKeychains= numKeychains - remove;
System.out.print("You now have " + numKeychains + " keychains.\n");
return numKeychains;
}
public static void viewOrder( int numKeychains, int pricePerKeychain, double tax, int baseShipping, int perKeychainShipping )
{
System.out.println("\nYou have " + numKeychains + " keychains. " );
System.out.println( "Keychains cost $10 each. The cost of " + numKeychains + " keychains is $" + ( numKeychains * pricePerKeychain ) + "." );
System.out.println( "The shipping charges are $" + ( baseShipping + (numKeychains * perKeychainShipping) ) + "." );
int subtotal = (numKeychains * pricePerKeychain) + baseShipping + (numKeychains * perKeychainShipping);
System.out.println( "The subtotal is $" + subtotal + "." );
System.out.println("The tax is $" + ( tax * subtotal ) + "." );
System.out.println( "The final cost is $" + ((tax * subtotal) + subtotal) + ".");
}
public static void checkout(int numKeychains, int pricePerKeychain, double tax, int baseShipping, int perKeychainShipping)
{
Scanner keyboard= new Scanner(System.in);
System.out.println("\nCHECKOUT\n");
System.out.print("What is your name? " );
String name = keyboard.next();
viewOrder(numKeychains, pricePerKeychain, tax, baseShipping, perKeychainShipping);
System.out.println("Thanks for your order, " + name + "!");
}
}
Picture of the output