TsetsoP

tema 9

Dec 8th, 2021
1,036
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Car
  2. -----------
  3. public class Car {
  4.     private String brand;
  5.     private String model;
  6.  
  7.     public Car(String brand, String model) {
  8.         this.brand = brand;
  9.         this.model = model;
  10.     }
  11.  
  12.     public String getBrand() {
  13.         return brand;
  14.     }
  15.  
  16.     public void setBrand(String brand) {
  17.         this.brand = brand;
  18.     }
  19.  
  20.     public String getModel() {
  21.         return model;
  22.     }
  23.  
  24.     public void setModel(String model) {
  25.         this.model = model;
  26.     }
  27. }
  28. ===================================
  29. App
  30. -----------
  31. import java.util.Arrays;
  32. import java.util.Scanner;
  33.  
  34. public class App{
  35.  
  36.     public static void main(String[] args) {
  37.         Car[] cars = new Car[3];
  38.         Car car = new Car("Peugeot", "206");
  39.         Car car1 = new Car("Seat", "Ibiza");
  40.         Car car2 = new Car("Chevrolet", "Lacetti");
  41.         cars[0] = car;
  42.         cars[1] = car1;
  43.         cars[2] = car2;
  44.  
  45.         boolean check = true;
  46.  
  47.         while(check) {
  48.         Scanner scanner = new Scanner(System.in);
  49.         System.out.println();
  50.         System.out.println("1 add");
  51.         System.out.println("2 remove");
  52.         System.out.println("3 print");
  53.         System.out.println("4 exit");
  54.         System.out.println("Choose an option: ");
  55.  
  56.         String option = scanner.nextLine();
  57.  
  58.         String brand;
  59.         String model;
  60.  
  61.           switch (option) {
  62.               case "1":
  63.                   System.out.println("Please input the brand of the car: ");
  64.                   brand = scanner.nextLine();
  65.                   System.out.println("Please input the model of the car: ");
  66.                   model = scanner.nextLine();
  67.                   Car[] carsOneAdded = new Car[cars.length + 1];
  68.                   for (int i = 0; i < cars.length; i++) {
  69.                       carsOneAdded[i] = cars[i];
  70.                   }
  71.                   carsOneAdded[cars.length] = new Car(brand, model);
  72.                   System.out.println("The car was added.");
  73.                   System.out.println(Arrays.toString(carsOneAdded));
  74.                   break;
  75.  
  76.               case "2":
  77.                   System.out.println("Input the index of car you want to remove(0, 1, 2): ");
  78.                   int index = Integer.parseInt(scanner.nextLine());
  79.                   Car[] carsOneRemoved = new Car[cars.length - 1];
  80.                   for (int i = 0, k = 0; i < cars.length; i++) {
  81.                       if (i == index) {
  82.                           continue;
  83.                       }
  84.                       carsOneRemoved[k++] = cars[i];
  85.                   }
  86.                   System.out.println("The car was removed.");
  87.                   System.out.println(Arrays.toString(carsOneRemoved));
  88.                   break;
  89.  
  90.               case "3":
  91.                   System.out.println(car.getBrand() + " " + car.getModel() + "\n" + car1.getBrand() + " " + car1.getModel() + "\n" + car2.getBrand() + " " + car2.getModel());
  92.                    break;
  93.  
  94.               case "4":
  95.                   System.out.println("Goodbye");
  96.                   check = false;
  97.                   break;
  98.           }
  99.           }
RAW Paste Data