Assignment #102 and Keychaisn for sale, for real
Code
///Name: Tim Gibson
///Period: 6
///Project Name: Keychains for sale 2
///File Name: Keychains2.java
///Date Finished: 11/16/15
import java.util.Scanner;
public class Keychains2
{
public static void main ( String[] args )
{
Scanner kb = new Scanner(System.in);
System.out.println( "Keychain Shop" );
int choice, kChains, price;
kChains = 0;
price = 10;
do
{
System.out.print( "\n1. Add Keychains to Order \n2. Remove Keychains from Order \n3. View Current Order \n4. Checkout \n\nPlease Enter your choice: " );
choice = kb.nextInt();
System.out.println();
if ( choice == 1 )
kChains = addKeychains(kChains);
else if ( choice == 2 )
kChains = removeKeychains(kChains);
else if ( choice == 3 )
viewOrder(kChains,price);
else if ( choice == 4 )
checkout(kChains,price);
else
System.out.print( "Error" );
} while ( choice != 4 );
}
public static int addKeychains( int kChains )
{
Scanner kb = new Scanner(System.in);
int add;
System.out.print( "You have " + kChains + " keychains. How many to add? " );
add = kb.nextInt();
kChains += add;
System.out.println( "You now have " + kChains + " keychains.\n" );
return kChains;
}
public static int removeKeychains(int kChains)
{
Scanner kb = new Scanner(System.in);
int remove;
System.out.print( "You have " + kChains + " keychains. How many would you like to remove. " );
remove = kb.nextInt();
kChains -= remove;
System.out.println( "You now have " + kChains + " keychains.\n" );
return kChains;
}
public static void viewOrder(int kChains, int price)
{
int total = price * kChains;
System.out.println( "You have " + kChains + " keychains. \nKechains cost $" + price + " each. \nTotal cost is $" + total + ".\n" );
}
public static void checkout( int kChains, int price )
{
Scanner kb = new Scanner(System.in);
String name;
int total = price * kChains;
System.out.print( "What is your name? " );
name = kb.next();
System.out.println( "You have " + kChains + " keychains. \nKeychains cost $" + price + " each. \nTotal cost is $" + total + ". \nThanks for your order, " + name + "!" );
}
}
Picture of the output