diff --git a/src/main/java/longah/exception/ExceptionMessage.java b/src/main/java/longah/exception/ExceptionMessage.java index 4762c0cc65..0a2b90cf08 100644 --- a/src/main/java/longah/exception/ExceptionMessage.java +++ b/src/main/java/longah/exception/ExceptionMessage.java @@ -6,9 +6,10 @@ public enum ExceptionMessage { INVALID_INDEX ("Invalid index."), // Member Exceptions DUPLICATE_MEMBER ("Duplicate member."), + INVALID_MEMBER_NAME ("Invalid member name."), + MEMBER_NOT_FOUND ("Member not found."), // Transaction Exceptions INVALID_TRANSACTION_FORMAT ("Invalid transaction format."), - INVALID_MEMBER_NAME ("Invalid member name."), INVALID_TRANSACTION_VALUE ("Invalid transaction value."), INVALID_VALUE_FORMAT ("Invalid value format."), // Data Storage Exceptions diff --git a/src/main/java/longah/node/Member.java b/src/main/java/longah/node/Member.java index dbd5fc5c02..4745fca570 100644 --- a/src/main/java/longah/node/Member.java +++ b/src/main/java/longah/node/Member.java @@ -1,5 +1,10 @@ package longah.node; +import java.util.regex.Pattern; + +import longah.exception.LongAhException; +import longah.exception.ExceptionMessage; + /** * Represents a member in the LongAh application. */ @@ -11,8 +16,14 @@ public class Member { * Constructs a new Member instance with the given name and zero balance. * * @param name The name of the member. + * @throws LongAhException If the name is invalid. */ - public Member(String name) { + public Member(String name) throws LongAhException { + // Check if name is fully alphanumeric + if (!Pattern.matches("[A-Za-z0-9]+", name)) { + throw new LongAhException(ExceptionMessage.INVALID_MEMBER_NAME); + } + this.name = name; this.balance = 0.0; } @@ -22,7 +33,10 @@ public Member(String name) { * * @param amount The amount to add to the balance. */ - public void addToBalance(double amount) { + public void addToBalance(double amount) throws LongAhException { + if (amount <= 0) { + throw new LongAhException(ExceptionMessage.INVALID_TRANSACTION_VALUE); + } this.balance += amount; } @@ -31,7 +45,10 @@ public void addToBalance(double amount) { * * @param amount The amount to subtract from the balance. */ - public void subtractFromBalance(double amount) { + public void subtractFromBalance(double amount) throws LongAhException { + if (amount <= 0) { + throw new LongAhException(ExceptionMessage.INVALID_TRANSACTION_VALUE); + } this.balance -= amount; } diff --git a/src/main/java/longah/node/Transaction.java b/src/main/java/longah/node/Transaction.java index def28ebb26..98f438ba7d 100644 --- a/src/main/java/longah/node/Transaction.java +++ b/src/main/java/longah/node/Transaction.java @@ -80,7 +80,7 @@ public Double addPayee(String expression, MemberList memberList) throws LongAhEx /** * Updates the balances of the members involved in the transaction. */ - public void updateBalances() { + public void updateBalances() throws LongAhException { for (HashMap.Entry entry : this.subtransactions.entrySet()) { Member member = entry.getKey(); double amount = entry.getValue(); diff --git a/src/main/java/longah/util/MemberList.java b/src/main/java/longah/util/MemberList.java index 8e891391eb..83a55da3a9 100644 --- a/src/main/java/longah/util/MemberList.java +++ b/src/main/java/longah/util/MemberList.java @@ -68,7 +68,7 @@ public Member getMember(String name) throws LongAhException { return member; } } - throw new LongAhException(ExceptionMessage.INVALID_MEMBER_NAME); + throw new LongAhException(ExceptionMessage.MEMBER_NOT_FOUND); } /** diff --git a/src/test/java/longah/node/MemberTest.java b/src/test/java/longah/node/MemberTest.java new file mode 100644 index 0000000000..8c90fefa49 --- /dev/null +++ b/src/test/java/longah/node/MemberTest.java @@ -0,0 +1,32 @@ +package longah.node; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; + +import longah.exception.ExceptionMessage; + +public class MemberTest { + @Test + public void memberConstructor_validName_success() { + try { + Member member = new Member("Alice"); + assertEquals("Alice", member.getName()); + assertEquals(0.0, member.getBalance()); + } catch (Exception e) { + fail(); + } + } + + @Test + public void memberConstructor_invalidName_exceptionThrown() { + try { + new Member("Alice123-"); + fail(); + } catch (Exception e) { + String expectedString = ExceptionMessage.INVALID_MEMBER_NAME.getMessage(); + assertEquals(expectedString, e.getMessage()); + } + } +}