Assignemnt #103 and Keychains for sale 3
Code
/// Name: Yordan Rashkov
/// Period: 7
/// Program Name: Keychains for sale 3
/// File Name: KFS3.java
/// Date Finished: 3/29/2016
import java.util.Scanner;
public class KFS3
{
public static void main(String[] args)
{
Scanner k = new Scanner(System.in);
int opt;
int numOfKeys = 0;
int priceForKey = 10;
double taxrate = .0825;
int Shipping = 5;
int perKeychainShipping = 1;
System.out.println("Ye Olde Keychain Shoppe");
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: ");
opt = k.nextInt();
System.out.println();
if (opt == 1)
{
numOfKeys = addKeychains( numOfKeys );
System.out.println("You now have " + numOfKeys + " keychains.");
}
else if (opt == 2)
{
numOfKeys = removeKeychains( numOfKeys );
System.out.println("You now have " + numOfKeys + " keychains.");
}
else if (opt == 3)
viewOrder( numOfKeys, priceForKey, taxrate, Shipping, perKeychainShipping );
else if (opt == 4)
checkout( numOfKeys, priceForKey, taxrate, Shipping, perKeychainShipping );
else
System.out.println("ERROR");
} while (opt != 4);
}
public static int addKeychains(int numOfKeys)
{
Scanner k = new Scanner(System.in);
int keyNum;
int add;
do
{
System.out.print("You have " + numOfKeys + " 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 );
keyNum = numOfKeys + add;
return keyNum;
}
public static int removeKeychains(int numOfKeys)
{
Scanner k = new Scanner(System.in);
int keyNum;
int remove;
do
{
System.out.print("You have " + numOfKeys + " keychains. How many to remove? ");
remove = k.nextInt();
if ( numOfKeys < 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 );
keyNum = numOfKeys - remove;
return keyNum;
}
public static void viewOrder( int numOfKeys, int priceForKey, double taxrate, int Shipping, int perKeychainShipping )
{
double totalCost;
System.out.println("You have " + numOfKeys + " keychains.");
System.out.println("Keychains cost $" + pricePerKey + " each.");
System.out.println("Shipping charge is $" + Shipping + " plus $" + perKeychainShipping + " per keychain.");
totalCost = (numOfKeys * priceForKey) + (numOfKeys * perKeychainShipping) + Shipping;
System.out.println("Your subtotal before tax is $" + totalCost + ".");
System.out.println("The tax on your order is " + (taxrate * 100) + "%.");
totalCost = (totalCost * taxrate) + totalCost;
System.out.println("Your final cost is $" + totalCost + ".");
}
public static void checkout( int numOfKeys, int priceForKey, double taxrate, int Shipping, 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( numOfKeys, priceForKey, taxrate, Shipping, perKeychainShipping );
System.out.println("Thanks for your order, " + name + "!");
}
}