Пользователь может добавить новый автомобиль или просмотреть все добавленные автомобили в один список.
Сначала класс MainVehicle:
Код: Выделить всё
package classes;
import java.util.ArrayList;
public class MainVehicle {
private String make;
private String model;
private int yearOfManufacture;
public MainVehicle(String make, String model, int yearOfManufacture) {
this.make = make;
this.model = model;
this.yearOfManufacture = yearOfManufacture;
}
public MainVehicle() {
}
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public int getYearOfManufacture() {
return yearOfManufacture;
}
public void setYearOfManufacture(int yearOfManufacture) {
this.yearOfManufacture = yearOfManufacture;
}
}
Код: Выделить всё
package classes;
import interfaces.MotorVehicle;
public class MotorCycle extends MainVehicle implements MotorVehicle {
private int numberOfWheels;
private String motorCycleType;
// Constructor and other methods (getters and setters)
public MotorCycle(String make, String model, int yearOfManufacture, int numberOfWheels, String motorCycleType) {
super(make, model, yearOfManufacture);
this.numberOfWheels = numberOfWheels;
this.motorCycleType = motorCycleType;
}
public MotorCycle() {
}
@Override
public int getNumberOfWheels() {
return numberOfWheels;
}
public void setNumberOfWheels(int numberOfWheels) {
this.numberOfWheels = numberOfWheels;
}
@Override
public String getMotorcycleType() {
return motorCycleType;
}
public void setMotorCycleType(String motorCycleType) {
this.motorCycleType = motorCycleType;
}
}
Код: Выделить всё
package classes;
import interfaces.CarVehicle;
public class Car extends MainVehicle implements CarVehicle {
private int numberOfDoors;
private String fuelType;
public Car(String make, String model, int yearOfManufacture, int numberOfDoors, String fuelType) {
super(make, model, yearOfManufacture);
this.numberOfDoors = numberOfDoors;
this.fuelType = fuelType;
}
public Car() {
}
@Override
public int getNumberOfDoors() {
return numberOfDoors;
}
public void setNumberOfDoors(int numberOfDoors) {
this.numberOfDoors = numberOfDoors;
}
@Override
public String getFuelType() {
return fuelType;
}
public void setFuelType(String fuelType) {
this.fuelType = fuelType;
}
}
Код: Выделить всё
package classes;
import java.util.ArrayList;
public class MainVehicle {
private String make;
private String model;
private int yearOfManufacture;
public MainVehicle(String make, String model, int yearOfManufacture) {
this.make = make;
this.model = model;
this.yearOfManufacture = yearOfManufacture;
}
public MainVehicle() {
}
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public int getYearOfManufacture() {
return yearOfManufacture;
}
public void setYearOfManufacture(int yearOfManufacture) {
this.yearOfManufacture = yearOfManufacture;
}
}
The interfaces:
First interface Vehicle
```java
package interfaces;
public interface Vehicle {
String getMake();
String getModel();
int getYearOfManufacture();
}
Код: Выделить всё
package interfaces;
public interface TruckVehicle extends Vehicle {
double getCargoCapacityTons();
String getTransmissionType();
}
Код: Выделить всё
package interfaces;
public interface MotorVehicle extends Vehicle {
int getNumberOfWheels();
String getMotorcycleType();
}
Код: Выделить всё
package interfaces;
public interface CarVehicle extends Vehicle {
int getNumberOfDoors();
String getFuelType();
}
Код: Выделить всё
package main;
import classes.Car;
import classes.MainVehicle;
import classes.MotorCycle;
import classes.Truck;
import com.sun.tools.javac.Main;
import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.Scanner;
public class VehicleApp {
private static ArrayList carList = new ArrayList();
private static ArrayList motorCyclesList = new ArrayList();
private static ArrayList truckList = new ArrayList();
// private static ArrayList truckList = new ArrayList();
// private static String truckList[] = {"Volvo", "Renault", "Citroen"};
public static void addNewVehicle(MainVehicle vehicle, String type){
Scanner reader = new Scanner(System.in);
System.out.println("Please, enter the make of the " + type + ":");
vehicle.setMake(reader.next());
System.out.println("Please, enter the model of the " + type + ":");
vehicle.setModel(reader.next());
System.out.println("Please, enter the year of manufacture:");
vehicle.setYearOfManufacture(reader.nextInt());
if (vehicle instanceof Car){
System.out.println("Pleas, enter the number of doors:");
((Car) vehicle).setNumberOfDoors(reader.nextInt());
System.out.println("Please, enter the fuel type:");
((Car) vehicle).setFuelType(reader.next());
carList.add((Car) vehicle);
} else if (vehicle instanceof MotorCycle) {
System.out.println("Pleas, enter the number of wheels:");
((MotorCycle) vehicle).setNumberOfWheels(reader.nextInt());
System.out.println("Please, enter the type of the motorcycle");
((MotorCycle) vehicle).setMotorCycleType(reader.next());
motorCyclesList.add((MotorCycle) vehicle);
} else if (vehicle instanceof Truck) {
System.out.println("Pleas, enter the cargo capacity in tons:");
((Truck) vehicle).setCargoCapacityTons(reader.nextInt());
System.out.println("Please, enter the transmission type:");
((Truck) vehicle).setTransmissionType(reader.next());
truckList.add((Truck) vehicle);
System.out.println(truckList.get(0).getMake());
}
System.out.println("You have successfully added a new " + type +" to your data");
System.out.println("****************************\n\n\n\n\n");
}
public static void main(String[] args) {
int userChoice;
Scanner reader = new Scanner(System.in);
Car car = new Car();
MotorCycle motorCycle = new MotorCycle();
Truck truck = new Truck();
do{
System.out.println("Weclome to Vehicle App!\nWhat do you want to do?\n1. Add a new car.\n2. Show all the cars.\n3. Add a new motorcycle.\n" +
"4. Show all motorcycles.\n5. Add a new truck.\n6. Show all trucks.\n7. Exit\nPlease inter the number of your choice");
userChoice = reader.nextInt();
switch (userChoice){
case 1:
addNewVehicle(car, "car");
break;
case 2:
for (Car c : carList) {
System.out.println(c.getMake() + " " + c.getModel() + " " + c.getYearOfManufacture() + " " + c.getNumberOfDoors()+ " " + c.getFuelType());
}
System.out.println("****************************\n\n\n\n\n");
break;
case 3:
addNewVehicle(motorCycle, "motorcyle");
break;
case 4:
for (MotorCycle c : motorCyclesList) {
System.out.println(c.getMake() + " " + c.getModel() + " " + c.getYearOfManufacture() + " " + c.getNumberOfWheels() + " " + c.getMotorcycleType() );
}
System.out.println("****************************\n\n\n\n\n");
break;
case 5:
addNewVehicle(truck, "truck");
break;
case 6:
for (Truck c : truckList) {
System.out.println(c.getMake() + " " + c.getModel() + " " + c.getYearOfManufacture() + " " + c.getCargoCapacityTons() + " " + c.getTransmissionType());
}
System.out.println("****************************\n\n\n\n\n");
break;
}
}while (
userChoice != 7
);
}
}
Код: Выделить всё
Weclome to Vehicle App!
What do you want to do?
1. Add a new car.
2. Show all the cars.
3. Add a new motorcycle.
4. Show all motorcycles.
5. Add a new truck.
6. Show all trucks.
7. Exit
Please inter the number of your choice
1
Please, enter the make of the car:
1
Please, enter the model of the car:
1
Please, enter the year of manufacture:
1
Pleas, enter the number of doors:
1
Please, enter the fuel type:
1
You have successfully added a new car to your data
****************************
Weclome to Vehicle App!
What do you want to do?
1. Add a new car.
2. Show all the cars.
3. Add a new motorcycle.
4. Show all motorcycles.
5. Add a new truck.
6. Show all trucks.
7. Exit
Please inter the number of your choice
1
Please, enter the make of the car:
2
Please, enter the model of the car:
2
Please, enter the year of manufacture:
2
Pleas, enter the number of doors:
2
Please, enter the fuel type:
2
You have successfully added a new car to your data
****************************
Weclome to Vehicle App!
What do you want to do?
1. Add a new car.
2. Show all the cars.
3. Add a new motorcycle.
4. Show all motorcycles.
5. Add a new truck.
6. Show all trucks.
7. Exit
Please inter the number of your choice
1
Please, enter the make of the car:
3
Please, enter the model of the car:
3
Please, enter the year of manufacture:
3
Pleas, enter the number of doors:
3
Please, enter the fuel type:
3
You have successfully added a new car to your data
****************************
Weclome to Vehicle App!
What do you want to do?
1. Add a new car.
2. Show all the cars.
3. Add a new motorcycle.
4. Show all motorcycles.
5. Add a new truck.
6. Show all trucks.
7. Exit
Please inter the number of your choice
2
3 3 3 3 3
3 3 3 3 3
3 3 3 3 3
****************************
Я ожидаю выполнения, когда выбираю вариант 2:
Код: Выделить всё
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
Код: Выделить всё
3 3 3 3 3
3 3 3 3 3
3 3 3 3 3
- Я пытался изменить метод add() на addFirst() или addLast ().
- Я попробовал изменить метод get() на getLast() или getFirst().
Подробнее здесь: https://stackoverflow.com/questions/788 ... -arraylist