Question: design and code 4 additional methods for this solution:- to the existing code 1. a method which displays the menu, prompts for and validates the

design and code 4 additional methods for this solution:- to the existing code
1. a method which displays the menu, prompts for and validates the menu choice, and returns it
2. a method which prompts for and validates the quantity (minimum 1, maximum 200) and returns it
3. a method which prompts for and validates the weight (minimum 1.0, maximum 400.0) and returns it
4. a void method which calculates and displays the average weight
• Use class constants for the apple types and for the maximum quantity and weight values

 

 

 

// AppleLoggerMethods.java
// Iam Aprogrammer
// 5/17/21
// Log apple shipments and calculate average weights

import java.util.Scanner;
import java.io.*;

public class AppleLoggerMethods {
           
   
       // constants for apple types
       static final int NUM_TYPES = 4;
       static final String APPLE1 = "Granny Smith";
       static final String APPLE2 = "Paula Red";
       static final String APPLE3 = "Golden Delicious";
       static final String APPLE4 = "Pink Pearl";
       static final String APPLE_UNKNOWN = "Unknown Apple Type";
       
   
       
       public static void main(String[] args) throws IOException {
       
       
       // variables for quantity, weight, average, and apple type
       int quantity = 0;
       double weight = 0.0;
       double average = 0.0;
       
       // menu selections
       String appleType = APPLE_UNKNOWN;
       
       int menuChoice = 0;
       Scanner input = new Scanner(System.in);
       String wantsToContinue = "y";
       
               
       // accumulator
       double totalWeight = 0.0;
       
       // output file
       PrintWriter outFile = new PrintWriter("apples.txt");

       // prompt the user for apple information until they are done
       while (wantsToContinue.equalsIgnoreCase("y")) {        

           menuChoice = getMenuChoice(input);
           
           // set apple type based on menu choice
           switch (menuChoice) {
               case 1:  
                    appleType = APPLE1;
                    break;
               case 2:  
                    appleType = APPLE2;
                    break;
               case 3:  
                    appleType = APPLE3;
                    break;
               case 4:  
                    appleType = APPLE4;
                    break;
               default:
                    appleType = APPLE_UNKNOWN;
                    break;
           }
           
           if (appleType == APPLE_UNKNOWN)
               System.out.println("Sorry, that is an invalid choice.");
               
           else {
               System.out.printf("Please enter the quantity of %s apples received [1-200]: ",
                                                                                  appleType);
               quantity = input.nextInt();
               System.out.printf("Please enter the total weight of the %s apples, in lbs. [1.0-400.0]: ",
                                                                                              appleType);
               weight = input.nextDouble();
               average = weight / quantity;
               System.out.printf("The average weight of your %s apples is %.2f lbs.",
                                                                   appleType, average);
                   
               outFile.printf("%-20s %-4d %-4.2f", appleType, quantity, weight);    
                   
               totalWeight += weight; // add weight to accumulator
           }
           System.out.print("Would you like to log more apples? (y/n): ");
           
           // flush the keyboard buffer, then read the next line
           input.nextLine();
           wantsToContinue = input.nextLine();
       }
       System.out.printf("Your total shipment weighs %.2f lbs.", totalWeight);
       outFile.close();
   }
      // get and validate menu choice
      static int getMenuChoice(Scanner input) {

            int menuChoice = 0;
           
            do { // nested loop
           
               // display menu
               System.out.println(1 + ". " + APPLE1);
               System.out.println(2 + ". " + APPLE2);
               System.out.println(3 + ". " + APPLE3);
               System.out.println(4 + ". " + APPLE4);  
               System.out.print(
                       "Please enter the apple type from the choices displayed above: ");
   
               menuChoice = input.nextInt();
               if (menuChoice < 1 || menuChoice > NUM_TYPES)
                   System.out.println("Sorry, that is an invalid choice.");
                   
           } while (menuChoice < 1 || menuChoice > NUM_TYPES);
           
             return menuChoice;
        }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

1 Displaying the menu and getting validated choice Java static int displayMenuAndGetChoiceScanner in... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!