forked from nus-cs2113-AY2324S2/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from jing-xiang/implement-data-classes-and-pro…
…gram-flow Implemented data classes and program flow Updated tests Resolved scanner bug
- Loading branch information
Showing
7 changed files
with
438 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package longah; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Represents a list of group members. | ||
*/ | ||
public class GroupList { | ||
private List<Member> members; | ||
|
||
/** | ||
* Constructs a new GroupList instance. | ||
*/ | ||
public GroupList() { | ||
this.members = new ArrayList<>(); | ||
} | ||
|
||
/** | ||
* Adds a member to the group. | ||
* | ||
* @param member The member to add. | ||
*/ | ||
public void addMember(Member member) { | ||
members.add(member); | ||
} | ||
|
||
/** | ||
* Gets the list of members in the group. | ||
* | ||
* @return The list of members. | ||
*/ | ||
public List<Member> getMembers() { | ||
return members; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,173 @@ | ||
package longah; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Scanner; | ||
|
||
/** | ||
* LongAh class manages debts between members. | ||
*/ | ||
public class LongAh { | ||
private Map<String, Member> members; | ||
private TransactionList transactions; | ||
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); | ||
} | ||
|
||
/** | ||
* 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); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* 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); | ||
} | ||
|
||
/** | ||
* Main entry-point for the java.duke.Duke application. | ||
* The main method to run the LongAh application. | ||
* | ||
* @param args The command-line arguments. | ||
*/ | ||
public static void main(String[] args) { | ||
String logo = " ____ _ \n" | ||
+ "| _ \\ _ _| | _____ \n" | ||
+ "| | | | | | | |/ / _ \\\n" | ||
+ "| |_| | |_| | < __/\n" | ||
+ "|____/ \\__,_|_|\\_\\___|\n"; | ||
System.out.println("Hello from\n" + logo); | ||
System.out.println("What is your name?"); | ||
|
||
Scanner in = new Scanner(System.in); | ||
System.out.println("Hello " + in.nextLine()); | ||
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'"); | ||
} | ||
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 "exit": | ||
System.exit(0); | ||
return; | ||
default: | ||
System.out.println("Invalid command. Use 'add', 'list', 'delete', 'find', 'clear', or 'exit'."); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package longah; | ||
|
||
/** | ||
* Represents a member in the LongAh application. | ||
*/ | ||
public class Member { | ||
private String name; | ||
private double balance; | ||
|
||
/** | ||
* Constructs a new Member instance with the given name and zero balance. | ||
* | ||
* @param name The name of the member. | ||
*/ | ||
public Member(String name) { | ||
this.name = name; | ||
this.balance = 0.0; | ||
} | ||
|
||
/** | ||
* Adds the specified amount to the member's balance. | ||
* | ||
* @param amount The amount to add to the balance. | ||
*/ | ||
public void addToBalance(double amount) { | ||
balance += amount; | ||
} | ||
|
||
/** | ||
* Subtracts the specified amount from the member's balance. | ||
* | ||
* @param amount The amount to subtract from the balance. | ||
*/ | ||
public void subtractFromBalance(double amount) { | ||
balance -= amount; | ||
} | ||
|
||
/** | ||
* Gets the current balance of the member. | ||
* | ||
* @return The balance of the member. | ||
*/ | ||
public double getBalance() { | ||
return balance; | ||
} | ||
|
||
/** | ||
* Returns a string representation of the member, including name and balance. | ||
* | ||
* @return A string representation of the member. | ||
*/ | ||
@Override | ||
public String toString() { | ||
return name + ": $" + balance; | ||
} | ||
|
||
/** | ||
* Gets the name of the member. | ||
* | ||
* @return The name of the member. | ||
*/ | ||
public String getName() { | ||
return name; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package longah; | ||
|
||
/** | ||
* Represents a transaction between two members. | ||
*/ | ||
public class Transaction { | ||
private Member from; | ||
private Member to; | ||
private double amount; | ||
|
||
/** | ||
* Constructs a new Transaction instance. | ||
* | ||
* @param from The member who owes the amount. | ||
* @param to The member who is owed the amount. | ||
* @param amount The amount of the transaction. | ||
*/ | ||
public Transaction(Member from, Member to, double amount) { | ||
this.from = from; | ||
this.to = to; | ||
this.amount = amount; | ||
from.subtractFromBalance(amount); | ||
to.addToBalance(amount); | ||
} | ||
|
||
/** | ||
* Checks if the transaction involves a specific person. | ||
* | ||
* @param person The name of the person to check. | ||
* @return True if the person is involved in the transaction, false otherwise. | ||
*/ | ||
public boolean getInvolves(String person) { | ||
return from.toString().equals(person) || to.toString().equals(person); | ||
} | ||
|
||
/** | ||
* Returns a string representation of the transaction. | ||
* | ||
* @return A string describing the transaction. | ||
*/ | ||
@Override | ||
public String toString() { | ||
return from + " owes " + to + " $" + amount; | ||
} | ||
|
||
/** | ||
* Gets the member who owes the amount. | ||
* | ||
* @return The member who owes the amount. | ||
*/ | ||
public Member getFrom() { | ||
return from; | ||
} | ||
|
||
/** | ||
* Gets the member who is owed the amount. | ||
* | ||
* @return The member who is owed the amount. | ||
*/ | ||
public Member getTo() { | ||
return to; | ||
} | ||
|
||
/** | ||
* Gets the amount of the transaction. | ||
* | ||
* @return The amount of the transaction. | ||
*/ | ||
public double getAmount() { | ||
return amount; | ||
} | ||
} |
Oops, something went wrong.