Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JUnit Test for Member Class Constructor #35

Merged
merged 8 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 0 additions & 36 deletions src/main/java/longah/GroupList.java

This file was deleted.

200 changes: 66 additions & 134 deletions src/main/java/longah/LongAh.java
Original file line number Diff line number Diff line change
@@ -1,172 +1,104 @@
package longah;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.ArrayList;

import longah.util.MemberList;
import longah.util.TransactionList;
import longah.util.Subtransaction;
import longah.exception.LongAhException;

/**
* LongAh class manages debts between members.
*/
public class LongAh {
private Map<String, Member> members;
private TransactionList transactions;
private static MemberList members = new MemberList();
private static TransactionList transactions = new TransactionList();
private Scanner scanner;

/**
* Constructs a new LongAh instance.
*/
public LongAh() {
this.members = new HashMap<>();
this.transactions = new TransactionList();
this.scanner = new Scanner(System.in);
}

/**
* Adds a debt between two members.
*
* @param person1 The name of the first person.
* @param amount The amount of the debt.
* @param person2 The name of the second person.
*/
public void addDebt(String person1, double amount, String person2) {
Member from = getOrCreateMember(person1);
Member to = getOrCreateMember(person2);
transactions.add(new Transaction(from, to, amount));
}

/**
* Lists all debts between members.
*/
public void listAllDebts() {
Map<String, Double> balances = transactions.calculateBalances();

// Display simplified debts
boolean hasDebts = false;
for (String name : balances.keySet()) {
double amount = balances.get(name);
if (amount != 0) {
String formattedAmount = String.format("%.2f", Math.abs(amount));
String otherPerson = getOtherPerson(name);
if (amount > 0 && balances.containsKey(otherPerson) && balances.get(otherPerson) == -amount) {
System.out.println(otherPerson + " owes " + name + " $" + formattedAmount);
hasDebts = true;
} else if (amount < 0 && balances.containsKey(otherPerson) && balances.get(otherPerson) == -amount) {
// Skip, as the other person already owes this person
} else {
System.out.println(name + " owes " + otherPerson + " $" + formattedAmount);
hasDebts = true;
}
}
}

if (!hasDebts) {
System.out.println("No debts, Huat Ah!");
}
}

/**
* Gets the other person involved in a transaction.
*
* @param name The name of the person.
* @return The name of the other person.
*/
private String getOtherPerson(String name) {
return transactions.getOtherPerson(name);
}

/**
* Deletes a debt transaction by index.
*
* @param index The index of the transaction to delete.
*/
public void deleteDebt(int index) {
transactions.remove(index);
}
ArrayList<Subtransaction> subtransactions = members.solveTransactions();

/**
* Finds debts involving a specific person.
*
* @param person The name of the person to find debts for.
*/
public void findDebts(String person) {
for (Transaction transaction : transactions.getTransactions()) {
if (transaction.getInvolves(person)) {
System.out.println(transaction);
}
System.out.println("Best Way to Solve Debts:");
for (Subtransaction subtransaction : subtransactions) {
System.out.println(subtransaction);
}
}

/**
* Clears all debts.
*/
public void clearAllDebts() {
transactions.clear();
}

/**
* Gets or creates a member with the given name.
*
* @param name The name of the member.
* @return The Member object.
*/
private Member getOrCreateMember(String name) {
return members.computeIfAbsent(name, Member::new);
}

/**
* The main method to run the LongAh application.
*
* @param args The command-line arguments.
* @param args
* The command-line arguments.
*/
public static void main(String[] args) {
System.out.println("Welcome to LongAh!");
LongAh app = new LongAh();
while (true) {
System.out.println("Enter command:");
if (!app.scanner.hasNextLine()) {
return;
}
String command = app.scanner.nextLine();
String[] parts = command.split(" ");
switch (parts[0]) {
case "add":
if (parts.length == 4 && parts[1].startsWith("p/")
&& parts[2].startsWith("a/") && parts[3].startsWith("p/")) {
String person1 = parts[1].substring(2);
double amount = Double.parseDouble(parts[2].substring(2));
String person2 = parts[3].substring(2);
app.addDebt(person1, amount, person2);
} else {
System.out.println("Invalid command format. Use 'add p/PERSON1 a/AMOUNT p/PERSON2'");
}
break;
case "list":
app.listAllDebts();
break;
case "delete":
if (parts.length == 2) {
int index = Integer.parseInt(parts[1]);
app.deleteDebt(index);
} else {
System.out.println("Invalid command format. Use 'delete INDEX'");
try {
System.out.print("Enter command: ");
if (!app.scanner.hasNextLine()) {
return;
}
break;
case "find":
if (parts.length == 2) {
String person = parts[1];
app.findDebts(person);
} else {
System.out.println("Invalid command format. Use 'find PERSON'");
String command = app.scanner.nextLine();
String[] parts = command.split(" ", 2);
switch (parts[0]) {
case "add":
transactions.add(parts[1], members);
break;
case "list":
app.listAllDebts();
break;
// case "delete":
// if (parts.length == 2) {
// int index = Integer.parseInt(parts[1]);
// app.deleteDebt(index);
// } else {
// System.out.println("Invalid command format. Use 'delete
// INDEX'");
// }
// break;
// case "find":
// if (parts.length == 2) {
// String person = parts[1];
// app.findDebts(person);
// } else {
// System.out.println("Invalid command format. Use 'find
// PERSON'");
// }
// break;
// case "clear":
// app.clearAllDebts();
// break;
case "addmember":
if (parts.length == 2) {
String name = parts[1];
members.addMember(name);
} else {
System.out.println("Invalid command format. Use 'addmember NAME'");
}
break;
case "listmembers":
members.listMembers();
break;
case "exit":
System.exit(0);
return;
default:
System.out.println("Invalid command. Use 'add', 'list', 'delete', 'find', 'clear', or 'exit'.");
}
break;
case "clear":
app.clearAllDebts();
break;
case "exit":
System.exit(0);
return;
default:
System.out.println("Invalid command. Use 'add', 'list', 'delete', 'find', 'clear', or 'exit'.");
} catch (LongAhException e) {
LongAhException.printException(e);
}
}
}
Expand Down
72 changes: 0 additions & 72 deletions src/main/java/longah/Transaction.java

This file was deleted.

Loading
Loading