Guest User

Untitled

a guest
Oct 28th, 2019
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. package L06_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 scanner = new Scanner(System.in);
  10. String input;
  11. List<Vehicle> trucks = new ArrayList<>();
  12. List<Vehicle> cars = new ArrayList<>();
  13. List<Vehicle> vehicles = new ArrayList<>();
  14.  
  15. while (!"End".equals(input = scanner.nextLine())) {
  16. String[] tokens = input.split(" ");
  17. String type = tokens[0];
  18. String model = tokens[1];
  19. String color = tokens[2];
  20. int horsePower = Integer.parseInt(tokens[3]);
  21.  
  22. Vehicle vehicle = new Vehicle(type, model, color, horsePower);
  23.  
  24. if (type.equals("car")){
  25. cars.add(vehicle);
  26. }else {
  27. trucks.add(vehicle);
  28. }
  29. vehicles.add(vehicle);
  30. }
  31.  
  32. while (!"Close the Catalogue".equals(input = scanner.nextLine())){
  33. String model = input;
  34. for (Vehicle vehicle : vehicles) {
  35. if (vehicle.model.equals(model)){
  36. System.out.print(vehicle);
  37. break;
  38. }
  39. }
  40. }
  41. int avgCars = 0;
  42. for (Vehicle car : cars) {
  43. avgCars += car.horsePower;
  44. }
  45. System.out.printf("Cars have average horsepower of: %.2f.", avgCars * 1.0 / cars.size());
  46. int avgTrucks = 0;
  47. for (Vehicle truck : trucks) {
  48. avgTrucks += truck.horsePower;
  49. }
  50. System.out.println();
  51. System.out.printf("Trucks have average horsepower of: %.2f.", avgTrucks * 1.0 / trucks.size());
  52. }
  53. }
  54. -----------------------------------------------------------------------
  55. package L06_VehicleCatalogue;
  56.  
  57. public class Vehicle {
  58. String type;
  59. String model;
  60. String color;
  61. int horsePower;
  62.  
  63. public Vehicle(String type, String model, String color, int horsePower) {
  64. this.type = type;
  65. this.model = model;
  66. this.color = color;
  67. this.horsePower = horsePower;
  68. }
  69.  
  70. @Override
  71. public String toString() {
  72. return String.format("Type: %s%nModel: %s%nColor: %s%nHorsepower: %d%n", (this.type.substring(0,1).toUpperCase()+this.type.substring(1)), model, color, horsePower);
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment