Advertisement
desislava_topuzakova

05. Vehicle Catalog

Oct 29th, 2022
770
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. package VehicleCatalogue_06;
  2.  
  3. public class Vehicle {
  4. //{typeOfVehicle} {model} {color} {horsepower}
  5. private String type;
  6. private String model;
  7.  
  8. public void setType(String type) {
  9. this.type = type;
  10. }
  11.  
  12. public void setModel(String model) {
  13. this.model = model;
  14. }
  15.  
  16. public void setColor(String color) {
  17. this.color = color;
  18. }
  19.  
  20. public void setHorsePower(int horsePower) {
  21. this.horsePower = horsePower;
  22. }
  23.  
  24. public String getType() {
  25. return type;
  26. }
  27.  
  28. public String getModel() {
  29. return model;
  30. }
  31.  
  32. public String getColor() {
  33. return color;
  34. }
  35.  
  36. public int getHorsePower() {
  37. return horsePower;
  38. }
  39.  
  40. private String color;
  41. private int horsePower;
  42.  
  43. public Vehicle(String type, String model, String color, int horsePower) {
  44. this.type = type;
  45. this.model = model;
  46. this.color = color;
  47. this.horsePower = horsePower;
  48. }
  49.  
  50. @Override
  51. public String toString() {
  52. String typeVeh = type.equals("car") ? "Car" : "Truck";
  53. return "Type: " + typeVeh + "\n" +
  54. "Model: " + model + "\n" +
  55. "Color: " + color + "\n" +
  56. "Horsepower: " + horsePower;
  57. }
  58. }
  59.  
  60. package VehicleCatalogue_06;
  61.  
  62. import java.util.ArrayList;
  63. import java.util.List;
  64. import java.util.Scanner;
  65.  
  66. public class Main {
  67. public static void main(String[] args) {
  68. Scanner scanner = new Scanner(System.in);
  69. String input = scanner.nextLine();
  70. List<Vehicle> vehicles = new ArrayList<>();
  71.  
  72. while (!input.equals("End")) {
  73. String[] tokens = input.split("\\s+");
  74. vehicles.add(new Vehicle(tokens[0], tokens[1], tokens[2], Integer.parseInt(tokens[3])));
  75. input = scanner.nextLine();
  76. }
  77.  
  78. String model = scanner.nextLine();
  79. double sumCar = 0;
  80. double sumTruck = 0;
  81. int countCar = 0;
  82. int countTruck = 0;
  83. while (!model.equals("Close the Catalogue")) {
  84. for (Vehicle vehicle : vehicles) {
  85. if (vehicle.getModel().equals(model)) {
  86. System.out.println(vehicle);
  87.  
  88. }
  89. }
  90. model = scanner.nextLine();
  91. }
  92.  
  93. for (Vehicle vehicle : vehicles) {
  94. if (vehicle.getType().equals("car")) {
  95. sumCar += vehicle.getHorsePower();
  96. countCar++;
  97. } else if (vehicle.getType().equals("truck")) {
  98. sumTruck += vehicle.getHorsePower();
  99. countTruck++;
  100. }
  101. }
  102.  
  103. double averageCars = sumCar / countCar;
  104. if(countCar == 0) {averageCars = 0;}
  105. System.out.printf("Cars have average horsepower of: %.2f.%n", averageCars);
  106. double averageTrucks = sumTruck / countTruck;
  107. if(countTruck == 0) {averageTrucks = 0;}
  108. System.out.printf("Trucks have average horsepower of: %.2f.%n", averageTrucks);
  109.  
  110. }
  111. }
  112.  
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement