Skip to content

Commit

Permalink
code cleanup and added mediator design pattern for report generation
Browse files Browse the repository at this point in the history
  • Loading branch information
yuans-dev committed Jul 31, 2024
1 parent 1655e5d commit 83af9dc
Show file tree
Hide file tree
Showing 14 changed files with 108 additions and 645 deletions.
24 changes: 14 additions & 10 deletions src/crms/form/MainForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
package crms.form;

import crms.lib.CarDatabase;
import crms.lib.CarBuilder;
import crms.lib.CarManager;
import crms.lib.CarReport;
import crms.lib.Rental;
import crms.lib.RentalDatabase;
import crms.lib.RentalManager;
import crms.lib.RentalReport;
import crms.lib.RentalReport.RentalStatus;
import crms.lib.Reporter;
import crms.lib.gui.CarTableModel;
import crms.lib.gui.IReportTableModel;
import crms.lib.gui.RentDateVerifier;
Expand Down Expand Up @@ -992,13 +992,11 @@ private void registerCarButtonActionPerformed(java.awt.event.ActionEvent evt) {/
return;
}
try {
var carBuilder = new CarBuilder();
var car = carBuilder.setId(carManager.getDatabase())
.setBrand(brandTextField_add.getText())
.setModel(modelTextField_add.getText())
.setDescription(descriptionTextField_add.getText())
.setPrice(Double.parseDouble(priceTextField_add.getText())).getResult();

var car = carManager.createCar(brandTextField_add.getText(),
modelTextField_add.getText(),
descriptionTextField_add.getText(),
Double.parseDouble(priceTextField_add.getText())
);
carManager.tryAddCar(car);
updateCarTable(carManager.generateReport());
} catch (Exception e) {
Expand Down Expand Up @@ -1185,8 +1183,14 @@ public void warn() {
}

private void initializeProgram() {
carManager = new CarManager(CarDatabase.getInstance(), RentalDatabase.getInstance());
rentalManager = new RentalManager(RentalDatabase.getInstance());
Reporter mediator = new Reporter();

carManager = new CarManager(CarDatabase.getInstance(), mediator);
rentalManager = new RentalManager(RentalDatabase.getInstance(), mediator);

mediator.setCarManager(carManager);
mediator.setRentalManager(rentalManager);

updateCarTable(carManager.generateReport());
}

Expand Down
97 changes: 14 additions & 83 deletions src/crms/lib/Car.java
Original file line number Diff line number Diff line change
@@ -1,107 +1,38 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package crms.lib;

/**
* Represents a unit of a car in the system.
* <p>
* This class contains information about the car, including its brand, model,
* description, price, and a unique ID.
* </p>
*
* @author Yuan Suarez
* @version 1.0
*/
public class Car {

private String description;
private String brand;
private String model;
private double price;
private String id;
private final String id;
private final String description;
private final String brand;
private final String model;
private final double price;

public Car() {
brand = "Unknown brand";
model = "Unknown model";
price = 0;
id = "0000";
description = "No description";
}

public void setBrand(String brand) {
this.brand = brand;
}

public void setModel(String model) {
this.model = model;
}

public void setId(String id) {
public Car(String id, String brand, String model, String description, double price) {
this.id = id;
}

/**
* Sets the price for the {@code Car}.
*
* @param price the price to set
*/
public void setPrice(double price) {
this.description = description.trim();
this.brand = brand.trim();
this.model = model.trim();
this.price = price;
}

/**
* Gets the price of the {@code Car}.
*
* @return the price of the car
*/
public double getPrice() {
return this.price;
}

/**
* Gets the unique ID of the {@code Car}.
*
* @return the ID of the car
*/
public String getId() {
return id;
}

/**
* Gets the description of the {@code Car}.
*
* @return the description of the car
*/
public String getDescription() {
return this.description;
}

/**
* Sets the description of the {@code Car}.
*
* @param description the description to set
*/
public void setDescription(String description) {
this.description = description;
return description;
}

/**
* Gets the brand of the {@code Car}.
*
* @return the brand of the car
*/
public String getBrand() {
return brand;
}

/**
* Gets the model of the {@code Car}.
*
* @return the model of the car
*/
public String getModel() {
return model;
}

public double getPrice() {
return price;
}
}
77 changes: 0 additions & 77 deletions src/crms/lib/CarBuilder.java

This file was deleted.

65 changes: 8 additions & 57 deletions src/crms/lib/CarDatabase.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package crms.lib;

import java.io.BufferedReader;
Expand All @@ -10,49 +6,16 @@
import java.io.IOException;
import java.util.ArrayList;

/**
* The {@code CarDatabase} class manages the collection of recorded {@code Car}
* objects in the local database and performs various operations related to
* them.
*
* <p>
* It implements the singleton pattern to ensure only one instance of the class
* exists.
* </p>
*
* <p>
* It provides methods to add, remove, and retrieve {@code Car} objects,
* generate reports, and handle data persistence to a local file.
* </p>
*
* @author Yuan Suarez
*/
public class CarDatabase extends Database<Car> {

/**
* The filename where the car data is stored.
*/
private final String filename = "cars.txt";

/**
* The single instance of {@code CarDatabase}.
*/
private static CarDatabase instance;

/**
* Private constructor to prevent instantiation. Initializes the
* {@code cars} list.
*/
private CarDatabase() {
dataList = new ArrayList<>();
}

/**
* Returns the singleton instance of {@code CarDatabase}. If the instance
* does not exist, it is created and the cars are fetched from disk.
*
* @return the singleton instance of {@code CarDatabase}.
*/
public static CarDatabase getInstance() {
if (instance == null) {
instance = new CarDatabase();
Expand All @@ -61,13 +24,6 @@ public static CarDatabase getInstance() {
return instance;
}

/**
* Retrieves a {@code Car} object using the given ID.
*
* @param id the ID of the car to be retrieved.
* @return the {@code Car} object with the specified ID, or {@code null} if
* not found.
*/
@Override
public Car getItemById(String id) {
for (Car car : dataList) {
Expand All @@ -78,32 +34,27 @@ public Car getItemById(String id) {
return null;
}

/**
* Fetches the recorded {@code Car} objects from the file {@code cars.txt}.
*/
@Override
public void fetchFromDisk() {
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
while (reader.ready()) {
String text = reader.readLine();
String[] lineComponents = text.split("###");
dataList.add(
new CarBuilder().setId(lineComponents[0])
.setBrand(lineComponents[1])
.setModel(lineComponents[2])
.setDescription(lineComponents[3])
.setPrice(Double.parseDouble(lineComponents[4])).getResult()
);

new Car(
lineComponents[0], // id
lineComponents[1], // brand
lineComponents[2], // model
lineComponents[3], // description
Double.parseDouble(lineComponents[4]) // price
));
}
} catch (IOException | NumberFormatException e) {
System.out.println("An exception has occurred while reading " + filename);
}
}

/**
* Saves the list of {@code Car} objects to the file {@code cars.txt}.
*/
@Override
public void saveToDisk() {
try (FileWriter fileWriter = new FileWriter(filename)) {
StringBuilder toWrite = new StringBuilder();
Expand Down
Loading

0 comments on commit 83af9dc

Please sign in to comment.