Code
/// Name: Afaf Nabeeha
/// Period: 7
/// Program Name: Keychains
/// File Name: Keychains.java
/// Date Finished: 3/8/2016 public class Keychains
import java.util.Scanner;
public class Keychains
{
public static void main ( String[] args )
{
int c, number = 0, price = 10, totalCost = 0;
System.out.println("Ye Olde Keychain Shoppe");
do
{
Scanner keyboard = new Scanner(System.in);
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: ");
c = keyboard.nextInt();
if ( c == 1 )
number = addKeychains(number);
else if ( c == 2 )
number = removeKeychains(number);
else if ( c == 3 )
totalCost = viewOrder(number, price);
else if ( c == 4 )
checkout(number, totalCost);
else
System.out.println("\nERROR");
}while(c != 4);
}
public static int addKeychains(int number)
{
Scanner kb = new Scanner(System.in);
int total, add;
System.out.print("\nYou have " + number + " keychains. How many to add? ");
add = kb.nextInt();
total = number + add;
System.out.println("You now have " + total + " keychains.");
return total;
}
public static int removeKeychains(int number)
{
Scanner kb = new Scanner(System.in);
int total, subtract;
System.out.print("\nYou have " + number + " keychains. How many to remove? ");
subtract = kb.nextInt();
total = number - subtract;
System.out.println("You now hava " + total + " keychains.");
return total;
}
public static int viewOrder(int number, int price)
{
System.out.println("\nYou have " + number + " keychains.");
System.out.println("Keychains cost $" + price + " each.");
int totalCost = price * number;
System.out.println("Total cost is $" + totalCost + ".");
return totalCost;
}
public static void checkout(int number, int totalCost)
{
Scanner kb = new Scanner(System.in);
String name;
int cost = 10;
System.out.println("\nCHECKOUT");
System.out.print("\nWhat is your name? ");
name = kb.next();
System.out.println("You have " + number + " keychains.");
System.out.println("Keychains cost $" + cost + " each.");
System.out.println("Total cost is $" + totalCost + ".");
System.out.println("Thanks for your order, " + name + "!");
}
}
Picture of the output