binibiningtinamoran

AutonomousCarTester.java

Jun 19th, 2020
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.45 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class AutonomousCarTester {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.         Scanner scan = new Scanner(System.in);
  8.  
  9.         System.out.println("How many cars to enter? ");
  10.         int size = scan.nextInt();
  11.  
  12.         while (size <= 0) {
  13.             System.out.println("Enter a positive non-negative integer!");
  14.             System.out.println("How many cars to enter? ");
  15.             size = scan.nextInt();
  16.         }
  17.  
  18.         AutonomousCar[] carsArray = new AutonomousCar[size];
  19.         for (int i = 0; i < carsArray.length; i++) {
  20.             System.out.print("Enter car id: ");
  21.             int id = scan.nextInt();
  22.             System.out.print("Enter brand: ");
  23.             String brand = scan.next();
  24.             System.out.print("Enter # of tests conducted on car: ");
  25.             int testsNo = scan.nextInt();
  26.             System.out.print("Enter # of tests passed: ");
  27.             int testsPassedNo = scan.nextInt();
  28.             System.out.print("Enter environment: ");
  29.             String env = scan.next();
  30.  
  31.             carsArray[i] = new AutonomousCar(id, brand, testsNo, testsPassedNo, env);
  32.         }
  33.         System.out.println("Enter brand to search for: ");
  34.         String brandToSearchFor = scan.next();
  35.  
  36.         System.out.print("Enter environment to search for: ");
  37.         String environ = scan.next();
  38.  
  39.         if (findTestPassedByEnv(carsArray,environ) == 0) {
  40.             System.out.println("There are no tests passed in this particular environment.");
  41.         } else {
  42.             System.out.println(findTestPassedByEnv(carsArray,environ));
  43.         }
  44.  
  45.         AutonomousCar var = updateCarGrade(brandToSearchFor,carsArray);
  46.         if (var != null) {
  47.             System.out.printf("%s::%s\n", var.getBrand(), var.getGrade());
  48.         } else {
  49.             System.out.println("No car is available with specified brand.");
  50.         }
  51.     } // end main()
  52.  
  53.     public static int findTestPassedByEnv(AutonomousCar[] ar, String environment) {
  54.         int sum = 0;
  55.         if (ar.length == 0) {
  56.             return sum;
  57.         } else {
  58.             int result = 0;
  59.             for (AutonomousCar autonomousCar : ar) {
  60.                 if (autonomousCar.getEnvironment().equalsIgnoreCase(environment)) {
  61.                     int noOfTestsPassed = autonomousCar.getNoOfTestsPassed();
  62.                     result += noOfTestsPassed;
  63.                 }
  64.             }
  65.             sum = result;
  66.         }
  67.         return sum;
  68.     } // end findTestPassedByEnv()
  69.  
  70.     public static AutonomousCar updateCarGrade(String brand, AutonomousCar[] ar) {
  71.         AutonomousCar aCar = null;
  72.  
  73.         if (ar.length == 0) {
  74.             return null;
  75.         } else {
  76.             for (AutonomousCar autonomousCar : ar) {
  77.                 if (autonomousCar != null && autonomousCar.getBrand().equalsIgnoreCase(brand)) {
  78.                     int rating =
  79.                             (autonomousCar.getNoOfTestsPassed() * 100) / autonomousCar.getNoOfTestsConducted();
  80.  
  81.                     String grade;
  82.                     if (rating >= 80) {
  83.                         grade = "A1";
  84.                     } else {
  85.                         grade = "B2";
  86.                     }
  87.                     autonomousCar.setGrade(grade);
  88.                 } // end outer if
  89.                 aCar = autonomousCar;
  90.             } // end for
  91.         } // end outer else
  92.         return aCar;
  93.     } // end updateCarGrade()
  94.  
  95.  
  96. } // end class
Add Comment
Please, Sign In to add comment