Assignment # 102 and Keychains For Real
Code
/// Name: Tommy Oyuntseren
/// Period: 7
/// Program Name: KeychainsForReal
/// File Name: KeychainsForReal.java
/// Date: 4/12/2016
import java.util.Scanner;
public class KeychainsForReal
{
public static void main(String[] args)
{
Scanner keyboard= new Scanner(System.in);
int choice, numKeychains= 0, perKeychain= 10;
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();
if (choice== 1)
{
numKeychains = addKeychains(numKeychains);
}
else if (choice== 2)
{
numKeychains = removeKeychains(numKeychains);
}
else if (choice== 3)
{
viewOrder(numKeychains,perKeychain);
}
else
{
checkout(numKeychains,perKeychain);
}
}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();
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 to remove? ");
int remove = keyboard.nextInt();
numKeychains = numKeychains - remove;
System.out.print("You now have " + numKeychains + " keychains.\n");
return numKeychains;
}
public static void viewOrder(int numKeychains, int perKeychain)
{
System.out.println("\nYou have " + numKeychains + " keychains. ");
System.out.println("Keychains cost $" + perKeychain + " each. ");
System.out.println("Total cost is $" + (numKeychains * perKeychain) + ".");
}
public static void checkout(int numKeychains, int perKeychain)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("\nCHECKOUT\n");
System.out.print("What is your name? ");
String name = keyboard.next();
viewOrder(numKeychains,perKeychain);
System.out.println("Thanks for your order, " + name + "!");
}
}
Picture of the output