package CarPartsB;
import java.util.InputMismatchException;
import java.util.Scanner;
public class CarPartsMenu {
Scanner console = new Scanner(System.in);
private final CarsArray carsList = new CarsArray();
private boolean loopVariable = true;
public void showMenu() {
while (loopVariable) {
try {
System.out.println("1: Add car.\n2: Remove car.\n3: List cars.\n4: Exiting");
int userInput = console.nextInt();
switch (userInput) {
case 1:
addCar();
break;
case 2:
removeCar();
break;
case 3:
listCars();
break;
case 4:
loopVariable = false;
break;
}
} catch (InputMismatchException e) {
console.nextLine();
System.out.println("\n" + e + " \nhas happened, choose value from 1 o 4.\n Click enter to continue...");
console.nextLine();
}
} catch (InputMismatchException e) {
console.nextLine();
System.out.println("\n" + e + " \nhas happened, choose value from 1 o 4.\n Click enter to continue...");
console.nextLine();
}
}
}
private void addCar() {
String make, model;
int year;
double price;
try {
System.out.println("What's price of your car? ");
price = console.nextDouble();
validateInput(price);
System.out.println("What year was your car made?");
year = console.nextInt();
validateInputYear(year);
System.out.println("What's the make of your car?");
make = console.next();
System.out.println("What's the model of your car?");
model = console.next();
Car car = new Car(price, year, make, model);
carsList.addCar(car);
} catch (InputMismatchException e) {
console.nextLine();
System.out.println("\n" + e + "\nhas happened, make sure to input correct values.\n Click enter to continue...");
console.nextLine();
}
}
private void removeCar() {
private ifvoid (carsList.isEmptyaddCar()) {
console.nextLine();
String make, model;
System.out.println("There are no cars in theint list");year;
console.nextLine();
} elsedouble {price;
try {
System.out.println("Type"What's idprice of theyour car? you");
want to remove: " price = console.nextDouble();
intvalidateInput(price);
userInputID System.out.println("What year was your car made?");
year = console.nextInt();
carsList.removeCarvalidateInputYear(userInputIDyear);
System.out.println("Car"What's withthe ID:make of your car?");
+ userInputID + " has been removed make = console.\n"next();
System.out.println("What's the model of your car?");
model = console.nextLinenext();
Car car = new Car(price, year, make, model);
carsList.addCar(car);
} catch (IndexOutOfBoundsExceptionInputMismatchException e) {
console.nextLine();
System.out.println("\n" + e + "\nhas happened, youmake havesure to pick IDinput thatcorrect EXISTSvalues.\n Click enter to continue...");
console.nextLine();
}
catch }
private void removeCar(InputMismatchException) e{
if (carsList.isEmpty()) {
console.nextLine();
System.out.println("\n" + e + "\nhas happened, input"There anare integer.\nno Clickcars enterin tothe continue..."list");
console.nextLine();
} else {
try {
System.out.println("Type id of the car you want to remove: ");
int userInputID = console.nextInt();
carsList.removeCar(userInputID);
System.out.println("Car with ID: " + userInputID + " has been removed.\n");
console.nextLine();
} catch (IndexOutOfBoundsException e) {
console.nextLine();
System.out.println("\n" + e + "\nhas happened, you have to pick ID that EXISTS.\n Click enter to continue...");
console.nextLine();
} catch (InputMismatchException e) {
console.nextLine();
System.out.println("\n" + e + "\nhas happened, input an integer.\n Click enter to continue...");
console.nextLine();
}
}
}
}
private void listCars() {
console.nextLine();
System.out.println("List of cars: ");
carsList.printCars printCarDetails();
System.out.println("\nPress anything to continue...");
console.nextLine();
}
private void printCarDetails() {
if (carsList.isEmpty()) {
System.out.println("Empty..");
}else {
for(Car c : carsList.getCars()) {
System.out.println("ID:" + c.getCarID() + " " + c.getMake() + " "
+ c.getModel() + " is " + c.getYearsOld() + " "
+ "years old and has price of " + c.getPrice());
}
}
}
private void validateInputYear(int number) {
if (number < 1900 || number > 2018) {
throw new InputMismatchException("\nYou can't add car that was made earlier than 1900\n or after 2018");
}
}
}
private void validateInput(double number) {
if (number <= 0) {
throw new InputMismatchException("\nPrice can't be negative or zero.");
}
}
}
}
package CarPartsB;
import java.util.ArrayList;
import java.util.NoSuchElementException;
public class CarsArray extends ArrayList {
private final ArrayList<Car> carsArray;
public CarsArray() {
this.carsArray = new ArrayList();
}
public ArrayList<Car> getCars() {
return this.carsArray;
}
public int size() {
return this.carsArray.size();
}
public void addCar(Car element) {
this.carsArray.add(element);
}
public void removeCar(int carId) {
Car carToRemove = null;
for (Car c : carsArray) {
if (c.getCarID() == carId) {
carToRemove = c;
}
}
if (carToRemove != null) {
carsArray.remove(carToRemove);
} else {
throw new IndexOutOfBoundsException();
}
}
public void printCars() {
if (isEmpty()) {
System.out.println("Empty..");
} else {
for (Car c : carsArray) {
c.printDetailsNoSuchElementException();
}
}
}
public boolean isEmpty() {
return carsArray.isEmpty();
}
}
package CarPartsB;
import java.time.Year;
public class Car {
private double price;
private final int year_of_production;
private final String make;
private final String model;
private final int yearsOld;
private final int carID;
static int CAR_ID;
int year = Year.now().getValue();
public Car(double price, int year_of_production, String make, String model) {
this.price = price;
this.year_of_production = year_of_production;
this.make = make;
this.model = model;
this.yearsOld = 2018year - this.year_of_production;
this.carID = CAR_ID;
CAR_ID++;
}
public int getCarIDgetYearsOld() {
return this.carID;yearsOld;
}
public void setPrice(int pricegetCarID() {
this.price =return price;carID;
}
public void setYear_of_productionsetPrice(int year_of_productionprice) {
this.year_of_productionprice = year_of_production;price;
}
public void setMake(String make) {
this.make = make;
}
public void setModel(String model) {
this.model = model;
}
public double getPrice() {
return price;
}
public int getYear_of_production() {
return year_of_production;
}
public String getMake() {
return make;
}
public String getModel() {
return model;
}
public void printDetails() {
System.out.println("ID:" + this.carID + " " + this.make + " "
+ this.model + " is " + this.yearsOld + " "
+ "years old and has price of " + this.price);
}
}