I have recently started coding in Java and I had to do this assignment in college.
Develop a menu driven application called
TestCarPartc
that allows the user to add, remove and list Car objects. For this use anArrayList
ofCar
. What are the advantages of using anArrayList
over an array? Place the answer in a comment at the end of your code.
I am trying to wrap my head around the concept of OOP and I am not quite sure when should I make something a new class or if I should implement something as interface or make some classes inherit from the other.
How does it look like in a real programming job? Would my code be considered "well written" for its purposes? What are the things I should look out for?
Console Menu:
package CarPartsB;
import java.util.InputMismatchException;
import java.util.Scanner;
public class CarPartsMenu {
Scanner console = new Scanner(System.in);
private 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();
}
}
}
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() {
if (carsList.isEmpty()) {
console.nextLine();
System.out.println("There are no cars in the 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();
System.out.println("\nPress anything to continue...");
console.nextLine();
}
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.");
}
}
}
Array
Class:
package CarPartsB;
import java.util.ArrayList;
public class CarsArray {
private 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.printDetails();
}
}
}
public boolean isEmpty() {
return carsArray.isEmpty();
}
}
Car
Class:
package CarPartsB;
public class Car {
private double price;
private int year_of_production;
private String make;
private String model;
private int yearsOld;
private int carID;
static int CAR_ID;
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 = 2018 - this.year_of_production;
this.carID = CAR_ID;
CAR_ID++;
}
public int getCarID() {
return this.carID;
}
public void setPrice(int price) {
this.price = price;
}
public void setYear_of_production(int year_of_production) {
this.year_of_production = year_of_production;
}
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);
}
}