Assignment #103 and Keychains for Sale, real ultimate power
Code
/// Name: Brendan Baird
/// Period: 6
/// Program Name: Keychains for Sale, real ultimate power
/// File Name: KeychainsUltimate.java
/// Date Finished: 1/15/2016
import java.util.Scanner;
public class KeychainsUltimate
{
public static void main(String[] args)
{
Scanner k = new Scanner(System.in);
int choice;
int numberOfKeys = 0;
int pricePerKey = 10;
double tax = .0825;
int baseShipping = 5;
int perKeychainShipping = 1;
System.out.println("Ye Olde Keychain Shoppppe");
do
{
System.out.println();
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");
System.out.println();
System.out.print("Please enter your choice: ");
choice = k.nextInt();
System.out.println();
if (choice == 1)
{
numberOfKeys = addKeychains( numberOfKeys );
System.out.println("You now have " + numberOfKeys + " keychains.");
}
else if (choice == 2)
{
numberOfKeys = removeKeychains( numberOfKeys );
System.out.println("You now have " + numberOfKeys + " keychains.");
}
else if (choice == 3)
viewOrder( numberOfKeys, pricePerKey, tax, baseShipping, perKeychainShipping );
else if (choice == 4)
checkout( numberOfKeys, pricePerKey, tax, baseShipping, perKeychainShipping );
else
System.out.println("ERROR");
} while (choice != 4);
}
public static int addKeychains(int numberOfKeys)
{
Scanner k = new Scanner(System.in);
int keyNumberNow;
int add;
do
{
System.out.print("You have " + numberOfKeys + " keychains. How many to add? ");
add = k.nextInt();
if ( add < 0 )
System.out.println("Sorry, but you cannot ask for a negative number of keychains.");
} while ( add < 0 );
keyNumberNow = numberOfKeys + add;
return keyNumberNow;
}
public static int removeKeychains(int numberOfKeys)
{
Scanner k = new Scanner(System.in);
int keyNumberNow;
int remove;
do
{
System.out.print("You have " + numberOfKeys + " keychains. How many to remove? ");
remove = k.nextInt();
if ( numberOfKeys < remove )
System.out.println("Sorry, but you cannot remove that many keychains.");
if ( remove < 0 )
System.out.println("You cannot remove a negative number of keychains.");
} while ( numberOfKeys < remove || remove < 0 );
keyNumberNow = numberOfKeys - remove;
return keyNumberNow;
}
public static void viewOrder( int numberOfKeys, int pricePerKey, double tax, int baseShipping, int perKeychainShipping )
{
double totalCost;
System.out.println("You have " + numberOfKeys + " keychains.");
System.out.println("Keychains cost $" + pricePerKey + " each.");
System.out.println("Shipping charge is $" + baseShipping + " plus $" + perKeychainShipping + " per keychain.");
totalCost = (numberOfKeys * pricePerKey) + (numberOfKeys * perKeychainShipping) + baseShipping;
System.out.println("Your subtotal before tax is $" + totalCost + ".");
System.out.println("The tax on your order is " + (tax * 100) + "%.");
totalCost = (totalCost * tax) + totalCost;
System.out.println("Your final cost is $" + totalCost + ".");
}
public static void checkout( int numberOfKeys, int pricePerKey, double tax, int baseShipping, int perKeychainShipping )
{
Scanner k = new Scanner(System.in);
System.out.println("CHECKOUT");
System.out.println();
System.out.print("What is your name? ");
String name = k.next();
viewOrder( numberOfKeys, pricePerKey, tax, baseShipping, perKeychainShipping );
System.out.println("Thanks for your order, " + name + "!");
}
}
Picture of the output