Advertisement
Boyan5

09-11

Nov 9th, 2021
633
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.93 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Car {
  4.     static Scanner scan = new Scanner(System.in);
  5.  
  6.     String model;
  7.     double price;
  8.     int year;
  9.  
  10.     public Car() {
  11.         this.model = "";
  12.         this.price = 0;
  13.         this.year = 0;
  14.     }
  15.  
  16.     public Car(String model, double price, int year) {
  17.         this.model = model;
  18.         this.price = price;
  19.         this.year = year;
  20.     }
  21.  
  22.     public void setModel(String model) {
  23.         this.model = model;
  24.     }
  25.  
  26.     public String getModel() {
  27.         return this.model;
  28.     }
  29.  
  30.     public void setPrice(double price) {
  31.         this.price = price;
  32.     }
  33.  
  34.     public double getPrice() {
  35.         return price;
  36.     }
  37.  
  38.     public void setYear(int year) {
  39.         this.year = year;
  40.     }
  41.  
  42.     public int getYear() {
  43.         return year;
  44.     }
  45.  
  46.     public static Car input(){
  47.         Scanner scan = new Scanner(System.in);
  48.         System.out.println("Enter model: ");
  49.         String model = scan.nextLine();
  50.         System.out.println("Enter price: ");
  51.         double price = Double.parseDouble(scan.nextLine());
  52.         System.out.println("Enter year: ");
  53.         int year = Integer.parseInt(scan.nextLine());
  54.  
  55.         return new Car(model, price, year);
  56.     }
  57.  
  58.     @Override
  59.     public String toString() {
  60.         return String.format("Model: %s%nPrice: %.2f%nYear: %d%n", this.model, this.price, this.year);
  61.     }
  62. }
  63.  
  64.  
  65.  
  66.  
  67. import java.util.Scanner;
  68.  
  69. public class Cars {
  70.     private Car[] arr;
  71.     public Cars() {
  72.         arr = null;
  73.     }
  74.     public void Add(Car c) {
  75.         for (int i = 0; i < this.arr.length; i++) {
  76.             if (this.arr[i] == null) {
  77.                 this.arr[i] = c;
  78.                 System.out.println("Added car successfully");
  79.                 return;
  80.             }
  81.         }
  82.         System.out.println("There is no space");
  83.         return;
  84.     }
  85.  
  86.     public void input() {
  87.         Scanner scan = new Scanner(System.in);
  88.         System.out.println("Enter max: ");
  89.         this.arr = new Car[Integer.parseInt(scan.nextLine())];
  90.         for (int i = 0; i < this.arr.length; i++) {
  91.             System.out.println("CAR:" + i );
  92.             Add(Car.input());
  93.         }
  94.     }
  95.  
  96.     public String toString() {
  97.         for (int i = 0; i < this.arr.length; i++) {
  98.             arr[i].toString();
  99.             System.out.println();
  100.         }
  101.         return null;
  102.     }
  103.  
  104.     public void delete(String model) {
  105.         for (int i = 0; i < this.arr.length; i++) {
  106.             if (this.arr[i].getModel().equals(model)) {
  107.                 arr[i] = null;
  108.                 System.out.println("Deleted! " + model);
  109.             }
  110.         }
  111.     }
  112.  
  113. }
  114.  
  115.  
  116.  
  117.  
  118. public class CarTest {
  119.     public static void main(String[] args) {
  120.         Cars cars = new Cars();
  121.         cars.input();
  122.         cars.toString();
  123.         cars.Add(Car.input());
  124.         cars.delete("I8");
  125.         cars.Add(Car.input());
  126.         cars.toString();
  127.     }
  128. }
  129.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement