Assignemnt #98 Keychain Store For Real

Code


    /// Name: JJ Deng
    /// Period: 6
    /// Program Name: Chains For Real
    /// File Name: ChainsReal.java
    /// Date Finished: 3/26/2016
    
    import java.util.Scanner;
import java.util.InputMismatchException;

public class ChainsReal{
    static Scanner keyboard = new Scanner(System.in);
    static int currentKey = 0;
    public static void main(String[] args){
        System.out.println("WELCOME TO GOOD OLD JJ'S KEYCHAIN");
        
        int choice = 0;
        
        do{
            showMenu();
            
            do choice = choice();
            while (choice ==0);
            
            if (choice == 1) addKey();
            if ( choice == 2) removeKey();
            if (choice == 3) viewOrder();
            
        }
        while (choice!=4);
        
        checkOut();
        
        System.out.println();
    }
    
    public static void showMenu(){
        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) Check Out");
        System.out.println("");
    }
    
    public static int choice() {
        int choice = 0;
        System.out.print("Choice: ");
        choice = keyboard.nextInt();
                System.out.println();

        
        if (choice < 1 || choice > 4) choice = 0;
        
        return choice;
    }
    
    public static void addKey(){
        System.out.print("How many?: ");
        try{
            int add = keyboard.nextInt();
            if ( add>0)
            {
             currentKey += add;
                System.out.println();
             System.out.println(add + " KEYCHAIN ADDED");
            System.out.println("");
                Chains();
            }
            else System.out.println("ERROR");
        }
        catch (InputMismatchException e)
        {
            System.out.println("ERROR");
        }
        
        
        
        
    }
    
    public static void removeKey(){
        System.out.print("How many?: ");
        try {
            int del = keyboard.nextInt();
            if ( del > 0 && del <= currentKey){
                currentKey -= del;
                System.out.println();
                System.out.println(del + " KEYCHAIN REMOVED");
                System.out.println("***********************");
                Chains();
            }
            else System.out.println("ERROR");
        }
        catch (InputMismatchException e){
            System.out.println("ERROR");
        }

        
    
    }
    public static void viewOrder(){
        
        System.out.println("YOU HAVE "+currentKey+" KEY CHAINS.");
        int cost = (10*currentKey);
        
        System.out.println("");
        System.out.println("TOTAL COST:$"+cost);
    }
    public static void checkOut(){
        System.out.print("What is your name? ");
        String name = keyboard.next();
        Chains();
        System.out.println("Keychains cost $10 each.");
        int cost = (10*currentKey);
        System.out.println("Total cost is $"+cost);
        System.out.println("Thanks for your order, "+name+" !");
    
        
        
        
    }
    
    public static void Chains() {
        
        System.out.println("YOU HAVE "+currentKey+" KEYCHAINS.");
    }
}

    

Picture of the output

Assignment 102