Advertisement
deyanmalinov

02.Constructors

Apr 25th, 2020
545
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.93 KB | None | 0 0
  1. package DPM;
  2. import java.util.Scanner;
  3. import java.util.stream.IntStream;
  4. public class Main {
  5.     public static void main(String[] args) {
  6.         Scanner scan = new Scanner(System.in);
  7.         int num = Integer.parseInt(scan.nextLine());
  8.         IntStream
  9.                 .rangeClosed(1, num)
  10.                 .boxed()
  11.                 .map(n -> scan.nextLine().split(" "))
  12.                 .map(data -> {
  13.                     Car newCar;
  14.                     if (data.length == 3){
  15.                         newCar = new Car(data[0],data[1],Integer.parseInt(data[2]));
  16.                     }else {
  17.                         newCar = new Car(data[0]);
  18.                     }
  19.                     return newCar;
  20.                 })
  21.                 .forEach(car -> System.out.println(car.getCarInfo()));
  22.     }
  23. }
  24. --------------------------------------------------------------------------
  25. --------------------------------------------------------------------------
  26. package DPM;
  27. public class Car {
  28.     private String make;
  29.     public String getMake() {
  30.         return make;
  31.     }
  32.     public Car(String make){
  33.         this.make=make;
  34.         this.model="unknown";
  35.         this.horsePower= -1;
  36.     }
  37.     public Car (String make, String model, int horsePower){
  38.         this(make);
  39.         this.model=model;
  40.         this.horsePower=horsePower;
  41.     }
  42.     public void setMake(String make) {
  43.         this.make = make;
  44.     }
  45.     public String getModel() {
  46.         return model;
  47.     }
  48.     public void setModel(String model) {
  49.         this.model = model;
  50.     }
  51.     public int getHorsePower() {
  52.         return horsePower;
  53.     }
  54.     public void setHorsePower(int horsePower) {
  55.         this.horsePower = horsePower;
  56.     }
  57.     private String model;
  58.     private int horsePower;
  59.     public String getCarInfo(){
  60.         return String.format("The car is: %s %s - %d HP.",
  61.                 this.getMake(), this.getModel(), this.getHorsePower());
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement