Program #103

Code

   
    /// Name: Afaf Nabeeha
    /// Period: 7
    /// Program Name: Keychains for Sale, real ultimate power
    /// File Name: Ultimate.java
    /// Date Finished: 3/8/2016 public class Ultimate

        import java.util.Scanner;
        
            public class Ultimate
            {
                public static void main(String[] args)
                {
                    Scanner k = new Scanner(System.in);
                    int c;
                    int numKeys = 0;
                    int pricePerKey = 10;
                    double tax = .0825;
                    int baseShipping = 5;
                    int perKeychainShipping = 1;
            
                    System.out.println("Ye Olde Keychain Shoppe");
            
                    do
                    {
                        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. Checkout");
                        System.out.println();
                        System.out.print("Please enter your choice: ");
                        c = k.nextInt();
            
                        System.out.println();
            
                        if (c == 1)
                        {
                            numKeys = addKeychains( numKeys );
                            System.out.println("You now have " + numKeys + " keychains.");
                        }
                        else if (c == 2)
                        {
                            numKeys = removeKeychains( numKeys );
                            System.out.println("You now have " + numKeys + " keychains.");
                        }
                        else if (c == 3)
                            viewOrder( numKeys, pricePerKey, tax, baseShipping, perKeychainShipping );
                        else if (c == 4)
                            checkout( numKeys, pricePerKey, tax, baseShipping, perKeychainShipping );
                        else
                            System.out.println("ERROR");
                    } while (c != 4);
                }
            
                public static int addKeychains(int numKeys)
                {
                    Scanner k = new Scanner(System.in);
                    int keyNumberNow;
                    int add;
            
                    do
                    {
                        System.out.print("You have " + numKeys + " keychains. How many to add? ");
                        add = k.nextInt();
                        
                        if ( add < 0 )
                            System.out.println("Sorry, but you cannot ask for a negative number of keychains.");
                    } while ( add < 0 );
                    
                    keyNumberNow = numKeys + add;
                    
                    return keyNumberNow;
                }
            
                public static int removeKeychains(int numKeys)
                {
                    Scanner k = new Scanner(System.in);
                    int keyNumberNow;
                    int subtract;
            
                    do
                    {
                        System.out.print("You have " + numKeys + " keychains. How many to remove? ");
                        subtract = k.nextInt();
            
                        if ( numKeys < subtract )
                            System.out.println("Sorry, but you cannot remove that many keychains.");
                        if ( subtract < 0 )
                            System.out.println("You cannot remove a negative number of keychains.");
                    } while ( numKeys < subtract || subtract < 0 );
                    
                    keyNumberNow = numKeys - subtract;
            
                    return keyNumberNow;
                }
            
                public static void viewOrder( int numKeys, int pricePerKey, double tax, int baseShipping, int perKeychainShipping )
                {
                    double totalCost;
            
                    System.out.println("You have " + numKeys + " keychains.");
                    System.out.println("Keychains cost $" + pricePerKey + " each.");
                    System.out.println("Shipping charge is $" + baseShipping + " plus $" + perKeychainShipping + " per keychain.");
                    
                    totalCost = (numKeys * pricePerKey) + (numKeys * perKeychainShipping) + baseShipping;
                    
                    System.out.println("Your subtotal before tax is $" + totalCost + ".");
                    System.out.println("The tax on your order is " + (tax * 100) + "%.");
                    
                    totalCost = (totalCost * tax) + totalCost;
            
                    System.out.println("Your final cost is $" + totalCost + ".");
                }
            
                public static void checkout( int numKeys, int pricePerKey, double tax, int baseShipping, int perKeychainShipping )
                {
                    Scanner k = new Scanner(System.in);
            
                    System.out.println("CHECKOUT");
                    System.out.println();
            
                    System.out.print("What is your name? ");
                    String name = k.next();
            
                    viewOrder( numKeys, pricePerKey, tax, baseShipping, perKeychainShipping );
                    
                    System.out.println("Thanks for your order, " + name + "!");
                }
    
             
    

Picture of the output

Assignment 103