Extra Car Simulation
import java.util.*;
public class Car {
private String carBrand;
private String modelName;
private String startingYear;
private int carMileage = 0;
private final int CURRENTYEAR = 2023;
private int carId = 0;
private static int carsForSale = 0;
private static ArrayList<Car> carsInMarket = new ArrayList<Car>();
/**
* Constructor with 3 arguments
* @param carBrand
* @param modelName
* @param startingYear
*
* everytime this method is called, car id is added and a new car is added to the array
*/
public Car(String carBrand, String modelName,
String startingYear,
int carMileage){
this.carBrand = carBrand;
this.modelName = modelName;
this.startingYear = startingYear;
this.carMileage = carMileage;
carId = uniqueID();
Car.carsInMarket.add(this);
}
/**
*
* @return number of cars
*/
public static int getCarsCount(){
return Car.carsInMarket.size();
}
/**
*
* @return unique id for each car
*/
private int uniqueID(){
Random randid = new Random();
carId = randid.nextInt(9999);
return carId;
}
/**
*
* @return calling the car array
*/
public static List<Car> getCarsInMarket(){
return Car.carsInMarket;
}
public int getMarketLife(){
return CURRENTYEAR - startingYear;
}
public void ageMarketLife() {
this.life -= Book.YEAR; // remove a year
if (getShelfLife() < 0.1) // small number zero out, this is for double floating point condition
this.life = this.created;
}
public void setMarketLife(int years)
/**
* printing the object
*/
public String toString(){
return "Id: " + carId + " Brand: " + carBrand + " Model: " + modelName +
" Year: " + startingYear +
" Mileage: " + carMileage;
}
public static void main(String[] args){
System.out.println("Car available: " + Car.getCarsCount());
Car BMWX1 = new Car("BMW","X1",30);
Car HondaCRV = new Car("Honda","CRV",40);
for(int i = 0; i < Car.getCarsCount(); i++){
System.out.println((Car.getCarsInMarket().get(i)));
}
System.out.println("Car available: " + Car.getCarsCount());
}
}
Car.main(null);
public class ElectricCar extends Car{
private int numBatteries;
private long chargeTime;
private boolean hasGear;
public ElectricCar(String carBrand, String modelName, String startingYear, int carMileage, int numBatteries, long chargeTime){
super(carBrand, modelName, startingYear, carMileage);
this.numBatteries = numBatteries;
this.chargeTime = chargeTime;
}
public void setHasGear(boolean hasGear){
this.hasGear = hasGear;
}
public boolean getHasGear(){
return hasGear;
}
}