Part 1

class Book (Part 1) Close Book

  1. Define 1 argument constructor for car lot,
  2. Define toString method for id and car lot.
  3. Generate unique id for each vehicle
  4. Create a public getter that has vehicleCount
  5. 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);
Vehicle count: 2
Car 1 id: 8cb1f5e3-e7fa-4036-8671-1078b1aa0934
Car 1 year: 2020
Car 2 id: be41845c-7b19-4742-a6eb-526b4f2be355
Car 2 year: 2019

Cars in CarLot:
Car ID: 8cb1f5e3-e7fa-4036-8671-1078b1aa0934, Year: 2020
Car ID: be41845c-7b19-4742-a6eb-526b4f2be355, Year: 2019

Part 2

extended Classes (Part 2) Try to use alternate forms of loops and techniques for construction.

  1. Ensure Motorcycle and Coupe run the Constructor from Car.
  2. Create instance variables unique to Motorcycle has 2 wheels, Coupe has 4 wheels. New items are not required by Constructor.
  3. Make Getters and Setters for all new items. You can add your own.
  4. Add a time when vehicle entered the lot. This should be same for Parent and Subclasses.
  5. Make sure there are getters and setters for items as needed. For instance, be able to set items not required by constructor.
  6. 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);
Vehicle count: 2
a863c72a-c509-4cd7-a876-a3f5dcab08bc 2021 2
3197190c-127e-412c-b3cc-555ac3283024 2022 4 4
Cars in CarLot:
Car ID: a863c72a-c509-4cd7-a876-a3f5dcab08bc, Year: 2021, Entry Time: 2023-04-26T21:11:06.089458, Number of Wheels: 2
Car ID: 3197190c-127e-412c-b3cc-555ac3283024, Year: 2022, Entry Time: 2023-04-26T21:11:06.089504, Number of Wheels: 4, Number of Doors: 4

Part 3

Simulation (Part 3)

  1. Build a Tester Method that does a Simulation.
  2. Define a default method in Vehicle that returns "current shelfLife" from date/time of construction. (Hint, think about capturing time in sorts)
  3. 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)
  4. Use a sleep in Java to assist with simulation
  5. 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);
Adding vehicles to the lot...
Car Lot:
Vehicle ID: 5d4270c0-445c-4f68-bc31-3d23c6d3510f, Year: 2023, Entry Time: 2023-04-26T21:24:00.893494
Vehicle ID: 523be08c-3cbe-4e7e-8ac3-a5a1cfdb6b13, Year: 2015, Entry Time: 2023-04-26T21:24:00.893551

Vehicle count: 2
Simulating passage of time...
Motorcycle with ID 5d4270c0-445c-4f68-bc31-3d23c6d3510f is expired.
Coupe with ID 523be08c-3cbe-4e7e-8ac3-a5a1cfdb6b13 is not yet expired.
Motorcycle with ID 5d4270c0-445c-4f68-bc31-3d23c6d3510f is expired.
Coupe with ID 523be08c-3cbe-4e7e-8ac3-a5a1cfdb6b13 is not yet expired.
Motorcycle with ID 5d4270c0-445c-4f68-bc31-3d23c6d3510f is expired.
Coupe with ID 523be08c-3cbe-4e7e-8ac3-a5a1cfdb6b13 is not yet expired.
Removing vehicles from the lot...
Car Lot:
Vehicle ID: 5d4270c0-445c-4f68-bc31-3d23c6d3510f, Year: 2023, Entry Time: 2023-04-26T21:24:00.893494

Vehicle count: 1