Assignment 102

Code

    import java.util.Scanner;
    
    public class KeychainsFoReal
    {
        public static void main( String[] args )
        {
        
            Scanner bot = new Scanner(System.in);
            System.out.println("Ye Olde Keychain Shoppe");
            System.out.println();
            
            int selection = 0, keyNum = 0, keyCost = 10;
            
            
            
            while ( selection != 4 )
            {
                System.out.println("1.\tAdd Keychains to Order");
                System.out.println("2.\tRemove Keychains from Order");
                System.out.println("3.\tView Current Order");
                System.out.println("4.\tCheckout");
                System.out.println("");
                
                System.out.println("Please enter your choice: ");
                selection = bot.nextInt();
                
                if ( selection == 1 )
                    keyNum=addKeychains(keyNum);
                else if ( selection == 2 )
                    keyNum=removeKeychains(keyNum);
                else if ( selection == 3 ) 
                    viewOrder(keyNum,keyCost);
                else if ( selection == 4 )
                    checkout(keyNum,keyCost);
                else
                {
                    do
                    {
                        System.out.println("Whoops. You didn't select a valid option. Try again.");
                        
                        System.out.println("");
                        System.out.println("1.\tAdd Keychains to Order");
                        System.out.println("2.\tRemove Keychains from Order");
                        System.out.println("3.\tView Current Order");
                        System.out.println("4.\tCheckout");
                        System.out.println("");
    
                        System.out.println("Please enter your choice: ");
                        selection = bot.nextInt();
                        
                    } while ( selection > 4 || selection < 1 );
                }
                
            }
        
        }
        
        public static int addKeychains( int keys )
        {   
            int keyDiff;
            Scanner bot = new Scanner(System.in);
            System.out.println("ADD KEYCHAINS");
            System.out.println("");
            
            System.out.println("You have " + keys + ". How many would you like to add?");
            keyDiff =  bot.nextInt();
            keys = keys + keyDiff;
            
            System.out.println("");
            System.out.println("You now have " + keys + " keychains.");
            System.out.println("");
            
            return keys;
        }
        
        public static int removeKeychains( int keys )
        {
            int keyDiff;
            ;
            Scanner bot = new Scanner(System.in);
            System.out.println("REMOVE KEYCHAINS");
            System.out.println("");
            
            System.out.println("You have " + keys + ". How many would you like to remove?");
            keyDiff = bot.nextInt();
            
            
            int test = keys - keyDiff;
            
            while ( test < 0 )
            {
                System.out.println("Whoops. Can't have negative keychains.");
                System.out.println("Try again: ");
                keyDiff = bot.nextInt();
            }
            
            keys = keys - keyDiff;
            
            System.out.println("");
            return keys;
        }
        
        public static void viewOrder( int keys, int keyPrice )
        {        
            System.out.println("VIEW ORDER");
            System.out.println("");
            
            int total = keys*keyPrice;
            
            System.out.println("You have " + keys + ".");
            System.out.println("Keychains cost $10 each, meaning your total is $" + total + ".");
            
            System.out.println("");
            
        }
        
        public static void checkout( int keys, int keyPrice )
        {   
            Scanner bot = new Scanner(System.in);
            System.out.println("CHECKOUT");
            System.out.println("");
            
            int total = keys*keyPrice;
            
            System.out.println("What is your name?");
            String name = bot.next();
            System.out.println("You have " + keys + " keychains.");
            System.out.println("Keychains cost $10 each, meaning your total is $" + total + ".");
            System.out.println("Thanks for your order, " + name + "!");
        }
        
    
    }  
    

Picture of the output

Assignment