Skip to content

Commit

Permalink
Merge pull request #35 from 1simjustin/test/junit
Browse files Browse the repository at this point in the history
JUnit Test for Member Class Constructor
  • Loading branch information
djleong01 authored Mar 13, 2024
2 parents 1025ffb + 0305ea6 commit edc58a7
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/main/java/longah/exception/ExceptionMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 20 additions & 3 deletions src/main/java/longah/node/Member.java
Original file line number Diff line number Diff line change
@@ -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.
*/
Expand All @@ -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;
}
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/longah/node/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Member, Double> entry : this.subtransactions.entrySet()) {
Member member = entry.getKey();
double amount = entry.getValue();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/longah/util/MemberList.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
32 changes: 32 additions & 0 deletions src/test/java/longah/node/MemberTest.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
}

0 comments on commit edc58a7

Please sign in to comment.