Ошибка Logi c в проекте - PullRequest
       1

Ошибка Logi c в проекте

0 голосов
/ 08 июля 2020

Цель проекта - собрать данные, введенные пользователем, и вывести определенные цены на основе ваших данных. При вводе данных кажется, что ни один из моих logi c не работает и возвращает значение 0.

PS. Проблема в том, что общая стоимость тарифного плана сотового телефона является неправильной. Когда я вызываю свои методы из основного метода, все работает, но я предполагаю, что мои уравнения неверны.

import java.util.Scanner; // gathers user input

public class Exam2Hassan {

   public static void main(String [] args) {
         
      Scanner keyboard = new Scanner(System.in);   
  
      int numOfPhones = 0, choice = 0; 
      double finalCost = 0, basePrice = 0, cellPlanCost = 0;
      char goAgain;
      
      
      // holds menu selection
        final int MENU_OPTION_ONE = 1;
        final int MENU_OPTION_TWO = 2;
        final int MENU_OPTION_THREE = 3;
        final int MENU_OPTION_FOUR = 4;

      // calls all methods
      displayData();
      basePrice = basePricing(choice);
      cellPlanCost = calculateCost(numOfPhones, basePrice);  
      finalCost = displayFinalCost(cellPlanCost);
      

   } // end main
   
   
   
   
   
   /**
   This method displays the data plan menu, should not accept any arguments or return any value
   */
   public static void displayData() {
        Scanner keyboard = new Scanner(System.in); // allows user input
        int choice, numOfPhones;
        double cellPlanCost = 0;
        double totalCost = 0;
        char goAgain;
        
        System.out.print("Data Plan Menu: ");
        System.out.println();
        System.out.println();
        System.out.println("\t1. 1 GB");
        System.out.println("\t2. 2 GB");
        System.out.println("\t3. 3 GB");
        System.out.println("\t4. 4 GB");
        
        System.out.println();
         
         do { // allows user to input how many plans they want to process
         System.out.print("Select a plan from the menu above: ");
         choice = keyboard.nextInt();
      
         while(choice < 1 || choice > 4)//validate the user's input
         {
            System.out.print("Selection must be between 1 and 4.  Select a plan from the menu above: ");
            choice = keyboard.nextInt();
         }
      
         System.out.print("Enter the number of phones: ");
         numOfPhones = keyboard.nextInt();
      
         while(numOfPhones < 1) //validate the user's input
         {
            System.out.print("Number of phones must be at least 1.  Enter the Number of phones: ");
            numOfPhones = keyboard.nextInt();
         }
         
         System.out.printf("The total price of the cell phone plan (before tax and fees) is: $%.2f\n", totalCost);
                
         System.out.println();
         keyboard.nextLine();
         System.out.println();
         System.out.print("Do you wish to calculate the price of another plan (Y/N)? ");
         goAgain = keyboard.nextLine().charAt(0);
         
         System.out.println();
      }
      while(goAgain == 'Y' || goAgain == 'y');
      }
         
   /**
   This method accepts the menu selection as an argument and shows which data plan was selected. should return base pricing   
   */
   public static double basePricing(int choice){        
        
        final double ONE_GB = 34.99;
        final double TWO_GB = 49.99;
        final double FOUR_GB = 64.99;
        final double UNLIMITED_GB = 74.99;
        double basePrice = 0;

                
      
      if(choice == 1) {
         basePrice = 34.99;
      }   
      else if(choice == 2) {
         basePrice = 49.99;
      }
      else if(choice == 3) {
         basePrice = 64.99;
      }
      else if(choice == 4) {
         basePrice = 74.99;

      }
         return basePrice;
      }

   /**
   This method calculates the cost of a cell plan. Accepts number of lines and base pricing. returns the cost of the plan
   */
   public static double calculateCost(int numOfPhones, double basePrice) {
      double cellPlanCost = 0;
      cellPlanCost = basePrice + (10.00 * numOfPhones);
      return cellPlanCost;
       
   }

   /**
   This method should display the final cost. accepts the final cost as argument
   */
    public static double displayFinalCost(double cellPlanCost) {
      
      return cellPlanCost;
    }  
  

} // end class   

Ответы [ 2 ]

0 голосов
/ 08 июля 2020

Ниже приведен код того же.

 import java.util.Scanner; // gathers user input

 public class Exam2Hassan {

  public static void main(String [] args) {
     
  // calls all methods
   displayData();

 } // end main
  
 /**
  This method displays the data plan menu, should not accept any arguments or return 
 any value
 */
 public static void displayData() {
    Scanner keyboard = new Scanner(System.in); // allows user input
    int choice, numOfPhones;
    double cellPlanCost = 0;
    double basePrice=0;
    char goAgain;
    
    System.out.print("Data Plan Menu: ");
    System.out.println();
    System.out.println();
    System.out.println("\t1. 1 GB");
    System.out.println("\t2. 2 GB");
    System.out.println("\t3. 3 GB");
    System.out.println("\t4. 4 GB");
    
    System.out.println();
     
     do { // allows user to input how many plans they want to process
     System.out.print("Select a plan from the menu above: ");
     choice = keyboard.nextInt();
     
     while(choice < 1 || choice > 4)//validate the user's input
     {
        System.out.print("Selection must be between 1 and 4.  Select a plan from the menu above: ");
        choice = keyboard.nextInt();
     }
     basePrice=basePricing(choice);
  
     System.out.print("Enter the number of phones: ");
     numOfPhones = keyboard.nextInt();
  
     while(numOfPhones < 1) //validate the user's input
     {
        System.out.print("Number of phones must be at least 1.  Enter the Number of phones: ");
        numOfPhones = keyboard.nextInt();
     }
     cellPlanCost = calculateCost(numOfPhones, basePrice); 
     System.out.printf("The total price of the cell phone plan (before tax and fees) is: $%.2f\n", cellPlanCost);
            
     System.out.println();
     keyboard.nextLine();
     System.out.println();
     System.out.print("Do you wish to calculate the price of another plan (Y/N)? ");
     goAgain = keyboard.nextLine().charAt(0);
     
     System.out.println();
  }
  while(goAgain == 'Y' || goAgain == 'y');
  }
     
 /**
 This method accepts the menu selection as an argument and shows which data plan was 
 selected. should return base pricing   
 */
 public static double basePricing(int choice){        
    
    double basePrice = 0;
  if(choice == 1) {
     basePrice = 34.99;
  }   
  else if(choice == 2) {
     basePrice = 49.99;
  }
  else if(choice == 3) {
     basePrice = 64.99;
  }
  else if(choice == 4) {
     basePrice = 74.99;

  }
     return basePrice;
  }

 /**
  This method calculates the cost of a cell plan. Accepts number of lines and base 
 pricing. returns the cost of the plan
 */
 public static double calculateCost(int numOfPhones, double basePrice) {
  double cellPlanCost = 0;
  cellPlanCost = basePrice + (10.00 * numOfPhones);
  return cellPlanCost;
   
 }

 /**
  This method should display the final cost. accepts the final cost as argument
  */
  public static double displayFinalCost(double cellPlanCost) {
  
  return cellPlanCost;
  }  


 } // end class   
0 голосов
/ 08 июля 2020

Сразу после ввода количества телефонов , нет logi c для расчета стоимости перед печатью.

Я бы посоветовал вам вызвать следующие методы после ввода number телефонов внутри метода displayData() и используйте finalCost или соответствующую переменную в зависимости от ваших требований.

basePrice = basePricing(choice);
cellPlanCost = calculateCost(numOfPhones, basePrice);  
finalCost = displayFinalCost(cellPlanCost);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...