Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Car {
- static Scanner scan = new Scanner(System.in);
- String model;
- double price;
- int year;
- public Car() {
- this.model = "";
- this.price = 0;
- this.year = 0;
- }
- public Car(String model, double price, int year) {
- this.model = model;
- this.price = price;
- this.year = year;
- }
- public void setModel(String model) {
- this.model = model;
- }
- public String getModel() {
- return this.model;
- }
- public void setPrice(double price) {
- this.price = price;
- }
- public double getPrice() {
- return price;
- }
- public void setYear(int year) {
- this.year = year;
- }
- public int getYear() {
- return year;
- }
- public static Car input(){
- Scanner scan = new Scanner(System.in);
- System.out.println("Enter model: ");
- String model = scan.nextLine();
- System.out.println("Enter price: ");
- double price = Double.parseDouble(scan.nextLine());
- System.out.println("Enter year: ");
- int year = Integer.parseInt(scan.nextLine());
- return new Car(model, price, year);
- }
- @Override
- public String toString() {
- return String.format("Model: %s%nPrice: %.2f%nYear: %d%n", this.model, this.price, this.year);
- }
- }
- import java.util.Scanner;
- public class Cars {
- private Car[] arr;
- public Cars() {
- arr = null;
- }
- public void Add(Car c) {
- for (int i = 0; i < this.arr.length; i++) {
- if (this.arr[i] == null) {
- this.arr[i] = c;
- System.out.println("Added car successfully");
- return;
- }
- }
- System.out.println("There is no space");
- return;
- }
- public void input() {
- Scanner scan = new Scanner(System.in);
- System.out.println("Enter max: ");
- this.arr = new Car[Integer.parseInt(scan.nextLine())];
- for (int i = 0; i < this.arr.length; i++) {
- System.out.println("CAR:" + i );
- Add(Car.input());
- }
- }
- public String toString() {
- for (int i = 0; i < this.arr.length; i++) {
- arr[i].toString();
- System.out.println();
- }
- return null;
- }
- public void delete(String model) {
- for (int i = 0; i < this.arr.length; i++) {
- if (this.arr[i].getModel().equals(model)) {
- arr[i] = null;
- System.out.println("Deleted! " + model);
- }
- }
- }
- }
- public class CarTest {
- public static void main(String[] args) {
- Cars cars = new Cars();
- cars.input();
- cars.toString();
- cars.Add(Car.input());
- cars.delete("I8");
- cars.Add(Car.input());
- cars.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement