Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class AutonomousCarTester {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- System.out.println("How many cars to enter? ");
- int size = scan.nextInt();
- while (size <= 0) {
- System.out.println("Enter a positive non-negative integer!");
- System.out.println("How many cars to enter? ");
- size = scan.nextInt();
- }
- AutonomousCar[] carsArray = new AutonomousCar[size];
- for (int i = 0; i < carsArray.length; i++) {
- System.out.print("Enter car id: ");
- int id = scan.nextInt();
- System.out.print("Enter brand: ");
- String brand = scan.next();
- System.out.print("Enter # of tests conducted on car: ");
- int testsNo = scan.nextInt();
- System.out.print("Enter # of tests passed: ");
- int testsPassedNo = scan.nextInt();
- System.out.print("Enter environment: ");
- String env = scan.next();
- carsArray[i] = new AutonomousCar(id, brand, testsNo, testsPassedNo, env);
- }
- System.out.println("Enter brand to search for: ");
- String brandToSearchFor = scan.next();
- System.out.print("Enter environment to search for: ");
- String environ = scan.next();
- if (findTestPassedByEnv(carsArray,environ) == 0) {
- System.out.println("There are no tests passed in this particular environment.");
- } else {
- System.out.println(findTestPassedByEnv(carsArray,environ));
- }
- AutonomousCar var = updateCarGrade(brandToSearchFor,carsArray);
- if (var != null) {
- System.out.printf("%s::%s\n", var.getBrand(), var.getGrade());
- } else {
- System.out.println("No car is available with specified brand.");
- }
- } // end main()
- public static int findTestPassedByEnv(AutonomousCar[] ar, String environment) {
- int sum = 0;
- if (ar.length == 0) {
- return sum;
- } else {
- int result = 0;
- for (AutonomousCar autonomousCar : ar) {
- if (autonomousCar.getEnvironment().equalsIgnoreCase(environment)) {
- int noOfTestsPassed = autonomousCar.getNoOfTestsPassed();
- result += noOfTestsPassed;
- }
- }
- sum = result;
- }
- return sum;
- } // end findTestPassedByEnv()
- public static AutonomousCar updateCarGrade(String brand, AutonomousCar[] ar) {
- AutonomousCar aCar = null;
- if (ar.length == 0) {
- return null;
- } else {
- for (AutonomousCar autonomousCar : ar) {
- if (autonomousCar != null && autonomousCar.getBrand().equalsIgnoreCase(brand)) {
- int rating =
- (autonomousCar.getNoOfTestsPassed() * 100) / autonomousCar.getNoOfTestsConducted();
- String grade;
- if (rating >= 80) {
- grade = "A1";
- } else {
- grade = "B2";
- }
- autonomousCar.setGrade(grade);
- } // end outer if
- aCar = autonomousCar;
- } // end for
- } // end outer else
- return aCar;
- } // end updateCarGrade()
- } // end class
Add Comment
Please, Sign In to add comment