Skip to main content
Rollback to Revision 3
Source Link
AJNeufeld
  • 34k
  • 5
  • 39
  • 101
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;
                }
            }public catchvoid showMenu(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 voidwhile addCar(loopVariable) {
        String make, model;
        int year;
        double price;
        try {
            System.out.println("What's"1: priceAdd ofcar.\n2: yourRemove car?.\n3: "List cars.\n4: Exiting");
            priceint userInput = console.nextDoublenextInt();
            validateInputswitch (priceuserInput); {
            System.out.println("What year was your carcase made?");1:
            year = console.nextInt      addCar();
            validateInputYear(year);        break;
            System.out.println("What's the make of yourcase car?");2:
            make = console.next      removeCar();
            System.out.println("What's the model of your car?");   break;
            model = console.next  case 3:
                    listCars();
            Car car       break;
                case 4:
                    loopVariable = newfalse;
 Car(price, year, make, model);                break;
            carsList.addCar(car);}
        } catch (InputMismatchException e) {
            console.nextLine();
            System.out.println("\n" + e + "\nhas" \nhas happened, makechoose surevalue tofrom input1 correcto values4.\n Click enter to continue...");
            console.nextLine();
        }
    }
}

    private void removeCaraddCar() {
        ifString (carsList.isEmpty())make, {model;
      int year;
    double console.nextLine();price;
    try {
        System.out.println("There are"What's noprice carsof inyour thecar? list"");
          price = console.nextLinenextDouble();
        } else {
            try {validateInput(price);
                System.out.println("Type"What idyear ofwas theyour car you want to remove: made?");
                int userInputIDyear = console.nextInt();
                carsList.removeCarvalidateInputYear(userInputIDyear);
                System.out.println("Car with ID:"What's "the +make userInputIDof +your car?" has been removed.\n");
              make = console.nextLinenext();
          System.out.println("What's the }model catchof (IndexOutOfBoundsExceptionyour ecar?") {;
              model = console.nextLinenext();
            Car car = new System.out.printlnCar("\n" + e + "\nhas happenedprice, you have to pick ID that EXISTS.\n Click enteryear, tomake, continue..."model);
                consolecarsList.nextLineaddCar(car);
            } catch (InputMismatchException e) {
                console.nextLine();
                System.out.println("\n" + e + "\nhas happened, make sure to input ancorrect integervalues.\n Click enter to continue...");
                console.nextLine();
            }
        }
    }
}

private void removeCar() {
 private void listCars if (carsList.isEmpty()) {
        console.nextLine();
        System.out.println("List of cars: ");
        printCarDetails();
     "There are no System.out.println("\nPresscars anythingin tothe continue..."list");
        console.nextLine();
    } else {
        try {
    private void printCarDetails      System.out.println()"Type {id of the car you want to remove: ");
         if   int userInputID = console.nextInt();
            carsList.isEmptyremoveCar()userInputID) {;
            System.out.println("Empty..");
"Car with ID: " + userInputID + " }elsehas {been removed.\n");
            forconsole.nextLine(Car);
 c : carsList.getCars     } catch ()IndexOutOfBoundsException e) {
            console.nextLine();
            System.out.println("ID:""\n" + c.getCarID()e + ""\nhas "happened, +you c.getMake()have +to "pick "
ID that EXISTS.\n Click enter to continue...");
           + cconsole.getModelnextLine();
 + " is " + c.getYearsOld() + "} "
catch (InputMismatchException e) {
            console.nextLine();
 + "years old and has price of " + c  System.getPriceout.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.");
        }
    }
}

}
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 NoSuchElementExceptionIndexOutOfBoundsException();
        }
    }

    public void printCars() {
        if (isEmpty()) {
            System.out.println("Empty..");
        } else {
            for (Car c : carsArray) {
                c.printDetails();
            }
        }
    }

    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 = year2018 - this.year_of_production;
        this.carID = CAR_ID;
        CAR_ID++;

    }

    public int getYearsOldgetCarID() {
        return yearsOld;this.carID;
    }

    public intvoid getCarIDsetPrice(int price) {
        returnthis.price carID;= price;
    }

    public void setPricesetYear_of_production(int priceyear_of_production) {
        this.priceyear_of_production = price;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);
    }

}
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();
            }
        }
    }

    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: ");
        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 carsArray;
    }

    public int size() {
        return carsArray.size();
    }

    public void addCar(Car element) {
        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 NoSuchElementException();
        }
    }

    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 = year - this.year_of_production;
        this.carID = CAR_ID;
        CAR_ID++;

    }

    public int getYearsOld() {
        return yearsOld;
    }

    public int getCarID() {
        return carID;
    }

    public void setPrice(int price) {
        this.price = price;
    }
    
    public double getPrice() {
        return price;
    }

    public int getYear_of_production() {
        return year_of_production;
    }

    public String getMake() {
        return make;
    }

    public String getModel() {
        return model;
    }

}
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.");
    }
}

}
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();
    }

}
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);
    }

}
Posted new code after suggested changes.
Source Link
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);
    }

}
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.");
    }
}

}
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();
    }

}
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);
    }

}
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();
            }
        }
    }

    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: ");
        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 carsArray;
    }

    public int size() {
        return carsArray.size();
    }

    public void addCar(Car element) {
        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 NoSuchElementException();
        }
    }

    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 = year - this.year_of_production;
        this.carID = CAR_ID;
        CAR_ID++;

    }

    public int getYearsOld() {
        return yearsOld;
    }

    public int getCarID() {
        return carID;
    }

    public void setPrice(int price) {
        this.price = price;
    }
    
    public double getPrice() {
        return price;
    }

    public int getYear_of_production() {
        return year_of_production;
    }

    public String getMake() {
        return make;
    }

    public String getModel() {
        return model;
    }

}
deleted 6 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

JAVA - Starting with OOP - simple Simple console based car array

Hey I have recently started coding in Java and I had to do this assignment in college.

Develop a menu driven application called TestCarPartcTestCarPartc that allows the user to add, remove and list Car objects. For this use an ArrayListArrayList of CarCar. What are the advantages of using an ArrayListArrayList 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

How does it look like in a real programming job? Would Would my code be considered "well written" for its purposes? What are the things I should look out for?

ArrayArray Class:

CarCar Class:

JAVA - Starting with OOP - simple console based car array

Hey 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 an ArrayList of Car. What are the advantages of using an ArrayList 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?

Array Class:

Car Class:

Simple console based car array

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 an ArrayList of Car. What are the advantages of using an ArrayList 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?

Array Class:

Car Class:

Fix possessive its; use Ctrl+K to include entire code in code block.
Source Link
mdfst13
  • 21.7k
  • 6
  • 33
  • 68
Loading
Source Link
Loading