Car Lot
Redo of the "Random Test" for 1 seed points
Part 1
class Book (Part 1) Close Book
- Define 1 argument constructor for car lot,
- Define toString method for id and car lot.
- Generate unique id for each vehicle
- Create a public getter that has vehicleCount
- Define tester method that initializes at least 2 vehicles, outputs id and year, and provides a count of vehicles in carLot.
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class Car {
private String id;
private int year;
public Car(int year) {
this.year = year;
this.id = UUID.randomUUID().toString();
}
public String getId() {
return id;
}
public int getYear() {
return year;
}
public String toString() {
return "Car ID: " + id + ", Year: " + year;
}
}
public class CarLot {
private List<Car> cars;
private static int vehicleCount = 0;
public CarLot(List<Car> cars) {
this.cars = cars;
vehicleCount += cars.size();
}
public int getVehicleCount() {
return vehicleCount;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Cars in CarLot:\n");
for (Car car : cars) {
sb.append(car.toString() + "\n");
}
return sb.toString();
}
public static void main(String[] args) {
Car car1 = new Car(2020);
Car car2 = new Car(2019);
List<Car> carList = new ArrayList<>();
carList.add(car1);
carList.add(car2);
CarLot carLot = new CarLot(carList);
System.out.println("Vehicle count: " + carLot.getVehicleCount());
System.out.println("Car 1 id: " + car1.getId() + "\nCar 1 year: " + car1.getYear());
System.out.println("Car 2 id: " + car2.getId() + "\nCar 2 year: " + car2.getYear());
System.out.println("\n" + carLot.toString());
}
}
CarLot.main(null);
Part 2
extended Classes (Part 2) Try to use alternate forms of loops and techniques for construction.
- Ensure Motorcycle and Coupe run the Constructor from Car.
- Create instance variables unique to Motorcycle has 2 wheels, Coupe has 4 wheels. New items are not required by Constructor.
- Make Getters and Setters for all new items. You can add your own.
- Add a time when vehicle entered the lot. This should be same for Parent and Subclasses.
- Make sure there are getters and setters for items as needed. For instance, be able to set items not required by constructor.
- Define tester method to test all items.
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class Car {
private String id;
private int year;
private LocalDateTime entryTime;
public Car(int year) {
this.year = year;
this.id = UUID.randomUUID().toString();
this.entryTime = LocalDateTime.now();
}
public String getId() {
return id;
}
public int getYear() {
return year;
}
public LocalDateTime getEntryTime() {
return entryTime;
}
public String toString() {
return "Car ID: " + id + ", Year: " + year + ", Entry Time: " + entryTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
}
}
public class Motorcycle extends Car {
private int numWheels;
public Motorcycle(int year) {
super(year);
this.numWheels = 2;
}
public int getNumWheels() {
return numWheels;
}
public void setNumWheels(int numWheels) {
this.numWheels = numWheels;
}
public String toString() {
return super.toString() + ", Number of Wheels: " + numWheels;
}
}
public class Coupe extends Car {
private int numWheels;
private int numDoors;
public Coupe(int year) {
super(year);
this.numWheels = 4;
this.numDoors = 2;
}
public int getNumWheels() {
return numWheels;
}
public void setNumWheels(int numWheels) {
this.numWheels = numWheels;
}
public int getNumDoors() {
return numDoors;
}
public void setNumDoors(int numDoors) {
this.numDoors = numDoors;
}
public String toString() {
return super.toString() + ", Number of Wheels: " + numWheels + ", Number of Doors: " + numDoors;
}
}
public class CarLot {
private List<Car> cars;
private static int vehicleCount = 0;
public CarLot(List<Car> cars) {
this.cars = cars;
vehicleCount += cars.size();
}
public int getVehicleCount() {
return vehicleCount;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Cars in CarLot:\n");
for (Car car : cars) {
sb.append(car.toString() + "\n");
}
return sb.toString();
}
public static void main(String[] args) {
Motorcycle moto1 = new Motorcycle(2021);
Coupe coupe1 = new Coupe(2022);
moto1.setNumWheels(2);
coupe1.setNumDoors(4);
List<Car> carList = new ArrayList<>();
carList.add(moto1);
carList.add(coupe1);
CarLot carLot = new CarLot(carList);
System.out.println("Vehicle count: " + carLot.getVehicleCount());
System.out.println(moto1.getId() + " " + moto1.getYear() + " " + moto1.getNumWheels());
System.out.println(coupe1.getId() + " " + coupe1.getYear() + " " + coupe1.getNumWheels() + " " + coupe1.getNumDoors());
System.out.println(carLot.toString());
}
}
CarLot.main(null);
Part 3
Simulation (Part 3)
- Build a Tester Method that does a Simulation.
- Define a default method in Vehicle that returns "current shelfLife" from date/time of construction. (Hint, think about capturing time in sorts)
- Define shelfLife expiration methods as needed in Motorcycle and Coupe.
- A Coupe has a fixed shelf life based on the date/time of constructor. (Hint, simulation 3 yrs as 3000 nanoseconds)
- A Motorcycle has a computed shelf life of expiration, the simulation should extend shelf life if a certain number of return stamps where placed in the book. (Hint, 3 return stamps renews for an extra year)
- Use a sleep in Java to assist with simulation
- Make a method that looks at vehicle in lot and determines if they need to get of the shelf, try to have year and on/off status in output.
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public abstract class Vehicle {
private String id;
private int year;
private LocalDateTime entryTime;
private LocalDateTime shelfLifeExpiration;
public Vehicle(int year) {
this.year = year;
this.id = UUID.randomUUID().toString();
this.entryTime = LocalDateTime.now();
this.shelfLifeExpiration = entryTime.plusNanos(1000);
}
public String getId() {
return id;
}
public int getYear() {
return year;
}
public LocalDateTime getEntryTime() {
return entryTime;
}
public LocalDateTime getShelfLifeExpiration() {
return shelfLifeExpiration;
}
public void setShelfLifeExpiration(LocalDateTime shelfLifeExpiration) {
this.shelfLifeExpiration = shelfLifeExpiration;
}
public Duration getCurrentShelfLife() {
LocalDateTime now = LocalDateTime.now();
return Duration.between(entryTime, now);
}
public abstract void checkShelfLifeExpiration();
public String toString() {
return "Vehicle ID: " + id + ", Year: " + year + ", Entry Time: " + entryTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
}
}
public class Motorcycle extends Vehicle {
private int numWheels;
private int numReturnStamps;
public Motorcycle(int year) {
super(year);
this.numWheels = 2;
this.numReturnStamps = 0;
}
public int getNumWheels() {
return numWheels;
}
public void setNumWheels(int numWheels) {
this.numWheels = numWheels;
}
public int getNumReturnStamps() {
return numReturnStamps;
}
public void setNumReturnStamps(int numReturnStamps) {
this.numReturnStamps = numReturnStamps;
}
public void checkShelfLifeExpiration() {
LocalDateTime now = LocalDateTime.now();
if (now.isAfter(getShelfLifeExpiration())) {
System.out.println("Motorcycle with ID " + getId() + " is expired.");
} else {
System.out.println("Motorcycle with ID " + getId() + " is not yet expired.");
}
}
}
public class Coupe extends Vehicle {
private int numWheels;
private int numDoors;
public Coupe(int year) {
super(year);
this.numWheels = 4;
this.numDoors = 2;
this.setShelfLifeExpiration(getEntryTime().plusNanos(Duration.ofDays(1095).toNanos())); // 1095 days = 3 years
}
public int getNumWheels() {
return numWheels;
}
public void setNumWheels(int numWheels) {
this.numWheels = numWheels;
}
public int getNumDoors() {
return numDoors;
}
public void setNumDoors(int numDoors) {
this.numDoors = numDoors;
}
public void checkShelfLifeExpiration() {
LocalDateTime now = LocalDateTime.now();
if (now.isAfter(getShelfLifeExpiration())) {
System.out.println("Coupe with ID " + getId() + " is expired.");
} else {
System.out.println("Coupe with ID " + getId() + " is not yet expired.");
}
}
}
public class CarLot {
private List<Vehicle> lot;
private int vehicleCount;
public CarLot() {
this.vehicleCount = 0;
this.lot = new ArrayList<>();
}
public int getVehicleCount() {
return vehicleCount;
}
public void addVehicle(Vehicle vehicle) {
vehicleCount++;
this.lot.add(vehicle);
}
public void removeVehicle(Vehicle vehicle) {
vehicleCount--;
this.lot.remove(vehicle);
}
public void checkShelfLifeExpiration() {
for (Vehicle vehicle : this.lot) {
vehicle.checkShelfLifeExpiration();
}
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Car Lot:\n");
for (Vehicle vehicle : this.lot) {
sb.append(vehicle.toString() + "\n");
}
return sb.toString();
}
}
public class Tester {
public static void main(String[] args) throws InterruptedException {
CarLot lot = new CarLot();
Motorcycle motorcycle = new Motorcycle(2023);
Coupe coupe = new Coupe(2015);
System.out.println("Adding vehicles to the lot...");
lot.addVehicle(motorcycle);
lot.addVehicle(coupe);
System.out.println(lot.toString());
System.out.println("Vehicle count: " + lot.getVehicleCount());
System.out.println("Simulating passage of time...");
Thread.sleep(2000); // 2 seconds
lot.checkShelfLifeExpiration();
Thread.sleep(2000); // 2 seconds
lot.checkShelfLifeExpiration();
Thread.sleep(2000); // 2 seconds
lot.checkShelfLifeExpiration();
System.out.println("Removing vehicles from the lot...");
lot.removeVehicle(coupe);
System.out.println(lot.toString());
System.out.println("Vehicle count: " + lot.getVehicleCount());
}
}
Tester.main(null);