Skip to content

Commit

Permalink
Merge branch 'AY2425S1-CS2113-T11-3:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronZZ10 authored Oct 17, 2024
2 parents bce4f38 + 256457e commit 487984c
Show file tree
Hide file tree
Showing 17 changed files with 350 additions and 119 deletions.
11 changes: 9 additions & 2 deletions src/main/java/car/Car.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package car;

import exceptions.CarException;

public class Car {

private String model;
private String licensePlateNumber;
private double price;

public Car(String model, String licensePlateNumber, double price) {
public Car(String model, String licensePlateNumber, double price) throws CarException{
this.model = model;
this.licensePlateNumber = licensePlateNumber;
this.price = Double.parseDouble(String.format("%.2f", price));
if (price < 0.00) {
throw CarException.invalidPrice();
} else {
this.price = Double.parseDouble(String.format("%.2f", price));
}

}

public void setModel(String model) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/customer/CustomerList.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public static void addCustomer(Customer customer){
System.out.println(customer.toString());
}

public static boolean removeCustomer(String Username){
public static boolean removeCustomer(String username){
for(Customer customer : customers){
if(customer.getUsername().equals(Username)){
if(customer.getUsername().equals(username)){
customers.remove(customer);
return true;
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/exceptions/CarException.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ public static CarException addCarException() {
return new CarException(message);
}

public static CarException invalidPrice() {
String message = "Unable to add car.. Price cannot be negative!!";
return new CarException(message);
}

}
2 changes: 1 addition & 1 deletion src/main/java/parser/CarParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,6 @@ private static String extractLicensePlateForRemoval(String userInput) {
if (splitInput.length < 2) {
throw new CarException("Invalid format for removing a car. Use: remove-car /i [CAR_ID]");
}
return splitInput[1]; // Expecting the license plate number to be the second argument
return splitInput[2]; // Expecting the license plate number to be the second argument
}
}
10 changes: 10 additions & 0 deletions src/main/java/parser/CustomerParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import customer.Customer;
import exceptions.CustomerException;
import exceptions.CliRentalException;


public class CustomerParser {

Expand Down Expand Up @@ -73,4 +75,12 @@ private static boolean isValidSequence(String[] parameters, String userInput){

return true;
}

public static String parseUsernameForRemoval(String userInput) throws CliRentalException {
String[] words = userInput.split(" ");
if (words.length < 2) {
throw new CliRentalException("Please provide the username to remove.");
}
return words[2]; // assuming input format is: remove-user <username>
}
}
34 changes: 28 additions & 6 deletions src/main/java/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import car.CarList;
import customer.Customer;
import customer.CustomerList;
import rental.RentalTransaction;
import transcation.Transaction;
import exceptions.CliRentalException;
import transcation.TransactionList;

Expand All @@ -15,11 +15,15 @@ public class Parser {
public static Scanner scanner = new Scanner(System.in);
private static final String HELP_COMMAND = "help";
private static final String ADD_CUSTOMER_COMMAND = "add-user";
private static final String REMOVE_CUSTOMER_COMMAND = "remove-user";
private static final String LIST_USERS_COMMAND = "list-users";
private static final String ADD_CAR_COMMAND = "add-car";
private static final String ADD_TRANSACTION_COMMAND = "add-tx";
private static final String REMOVE_CAR_COMMAND = "remove-car";
private static final String LIST_USERS_COMMAND = "list-users";
private static final String ADD_TRANSACTION_COMMAND = "add-tx";
private static final String LIST_CARS_COMMAND = "list-cars";
private static final String REMOVE_TRANSACTION_COMMAND = "remove-tx";
private static final String LIST_ALL_TRANSACTIONS = "list-tx";
private static final String EXIT_COMMAND = "exit";

public static String getUserInput(){
System.out.println("What would you like to do?");
Expand All @@ -46,10 +50,13 @@ public static boolean parse(String userInput) throws CliRentalException {
Car car = CarParser.parseIntoCar(userInput);
CarList.addCar(car);
return false;
case LIST_CARS_COMMAND:
CarList.printCarList();
return false;
case ADD_TRANSACTION_COMMAND:
try {
RentalTransaction transaction = RentalParser.parseIntoRentalTransaction(userInput);
System.out.println("Rental transaction added: " + transaction);
Transaction transaction = TransactionParser.parseIntoTransaction(userInput);
System.out.println("Rental transaction added: " + transaction.toString());
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
Expand All @@ -62,13 +69,28 @@ public static boolean parse(String userInput) throws CliRentalException {
System.out.println(e.getMessage());
}
return false;
case REMOVE_CUSTOMER_COMMAND:
try {
String username = CustomerParser.parseUsernameForRemoval(userInput);
if (CustomerList.removeCustomer(username)) {
System.out.println("Customer " + username + " has been removed.");
} else {
System.out.println("Customer " + username + " not found.");
}
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
return false;
case LIST_USERS_COMMAND:
CustomerList.printCustomers();
return false;
case REMOVE_TRANSACTION_COMMAND:
TransactionList.removeTransaction(userInput);
return false;
case "exit":
case LIST_ALL_TRANSACTIONS:
TransactionList.printAllTransactions();
return false;
case EXIT_COMMAND:
return true;
default:
throw CliRentalException.unknownCommand();
Expand Down
86 changes: 0 additions & 86 deletions src/main/java/parser/RentalParser.java

This file was deleted.

50 changes: 50 additions & 0 deletions src/main/java/parser/TransactionParser.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,55 @@
package parser;

import transcation.Transaction;

import java.time.LocalDate;

public class TransactionParser {

private static final String ADD_TRANSACTION_COMMAND = "add-tx";

public static Transaction parseIntoTransaction(String userInput) throws IllegalArgumentException {
userInput = userInput.substring(ADD_TRANSACTION_COMMAND.length()).trim();
String[] parameters = { "/l", "/b", "/d", "/c" };
String[] parameterContents;

if (isValidSequence(parameters, userInput)) {
parameterContents = parseParameterContents(parameters, userInput);
} else {
throw new IllegalArgumentException("Invalid command format for adding a transaction.");
}

String carLicensePlate = parameterContents[0];
String borrowerName = parameterContents[1];
int duration = Integer.parseInt(parameterContents[2]);
LocalDate startDate = LocalDate.parse(parameterContents[3]);
String transactionId = Transaction.getTransactionId();

return new Transaction(carLicensePlate, borrowerName, String.valueOf(duration),
startDate.toString(), transactionId);
}

private static boolean isValidSequence(String[] parameters, String userInput) {
for (String parameter : parameters) {
if (!userInput.contains(parameter)) {
return false;
}
}
return true;
}

private static String[] parseParameterContents(String[] parameters, String userInput) {
String[] contents = new String[parameters.length];
String[] words = userInput.split(" ");

for (int i = 0; i < parameters.length; i++) {
for (int j = 0; j < words.length; j++) {
if (words[j].equals(parameters[i]) && j + 1 < words.length) {
contents[i] = words[j + 1];
break;
}
}
}
return contents;
}
}
6 changes: 3 additions & 3 deletions src/main/java/seedu/clirental/CliRental.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ public static void main(String[] args) {
isExit = true;
}

} catch(CustomerException exception){
} catch (CustomerException exception){
exception.printErrorMessage();
} catch(NumberFormatException exception){
System.out.println("Unable to parse customer");
} catch (NumberFormatException exception){
System.out.println("Unable to parse input");
} catch (CarException e) {
System.out.println(e.getMessage());
} catch (CliRentalException e) {
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/transcation/Transaction.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package transcation;

public class Transaction {
private final String transactionId;
public static String transactionId;
private String carLicensePlate;
private String borrowerName;
private String duration;
Expand All @@ -15,10 +15,20 @@ public Transaction(String carLicensePlate, String borrowerName, String duration,
this.createdAt = createdAt;
}

public String getTransactionId() {
public static String getTransactionId() {
return transactionId;
}

@Override
public String toString() {
return "Transaction{" +
"carLicensePlate='" + carLicensePlate + '\'' +
", borrowerName='" + borrowerName + '\'' +
", duration='" + duration + '\'' +
", createdAt='" + createdAt + '\'' +
'}';
}

public String getCreatedAt() {
return createdAt;
}
Expand Down
Loading

0 comments on commit 487984c

Please sign in to comment.