Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. package VehicleCatalogue;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Scanner;
  6.  
  7. public class Main {
  8. public static void main(String[] args) {
  9. Scanner sc = new Scanner(System.in);
  10. String i = sc.nextLine();
  11. String type = "";
  12. String model = "";
  13. String color = "";
  14. Double horsePower = 0.0;
  15. List <Vehicle> vehicles = new ArrayList<>();
  16. double sum = 0;
  17. double sum1 = 0;
  18. double counter = 0;
  19. double counter1 = 0.0;
  20. while (!i.equals("End")){
  21. String [] input = i.split("\\s+");
  22. type = input [0];
  23. model = input [1];
  24. color = input [2];
  25. horsePower = Double.parseDouble(input [3]);
  26. Vehicle vehicle = new Vehicle(type, model, color, horsePower);
  27. if (vehicle.getType().equals("truck")){
  28. sum += horsePower;
  29. counter++;
  30. }
  31. else if (vehicle.getType().equals("car")){
  32. sum1 += horsePower;
  33. counter1++;
  34. }
  35. vehicles.add(vehicle);
  36. i = sc.nextLine();
  37. }
  38. String m = sc.nextLine();
  39. while (!m.equals("Close the Catalogue")){
  40. for (Vehicle v: vehicles) {
  41. if (v.getModel().equals(m)){
  42. if (v.getType().equals("car")){
  43. v.setType("Car");
  44. }
  45. else if (v.getType().equals("truck")){
  46. v.setType("Truck");
  47. }
  48. System.out.print(v);
  49. }
  50. }
  51. m = sc.nextLine();
  52. }
  53.  
  54. double print = sum/counter;
  55. double print1 = sum1/counter1;
  56. String cars = "Cars";
  57. String truck = "Trucks";
  58. System.out.printf("%s have average horsepower of: %.2f.", cars, print1);
  59. System.out.println();
  60. System.out.printf("%s have average horsepower of: %.2f.", truck, print);
  61.  
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement