diff --git a/money/src/main/java/com/iluwatar/App.java b/money/src/main/java/com/iluwatar/App.java index ff7d8c9ddbdf..2deda181cd48 100644 --- a/money/src/main/java/com/iluwatar/App.java +++ b/money/src/main/java/com/iluwatar/App.java @@ -8,7 +8,7 @@ */ public final class App { private App() { - // Private constructor to hide the default public constructor + // Private constructor to hide the default public constructor } /** @@ -44,19 +44,26 @@ public static void main(final String[] args) { final Money allocationMoney = new Money(6_000, usd); final Account[] accounts = new Account[2]; + + // Allocate the money equally among the accounts + final long allocatedAmount = allocationMoney.getAmount() / accounts.length; + accounts[0] = account; accounts[1] = new Account(usd, eur); - allocationMoney.allocate(accounts, amount1, amount2); + // Deposit the allocated money into accounts + for (Account acc : accounts) { + acc.deposit(new Money(allocatedAmount, usd)); + } System.out.println("Allocated Balances:"); for (int i = 0; i < accounts.length; i++) { System.out.println("Account " - + (i + 1) - + ": " + accounts[i].getPrimaryBalance().getAmount() - + " " - + accounts[i].getPrimaryBalance().getCurrency() - .getStringRepresentation()); + + (i + 1) + + ": " + accounts[i].getPrimaryBalance().getAmount() + + " " + + accounts[i].getPrimaryBalance().getCurrency() + .getStringRepresentation()); } } } diff --git a/money/src/test/java/AppTest.java b/money/src/test/java/AppTest.java new file mode 100644 index 000000000000..2c688bf7a9fd --- /dev/null +++ b/money/src/test/java/AppTest.java @@ -0,0 +1,38 @@ +import com.iluwatar.Account; +import com.iluwatar.App; +import com.iluwatar.Currency; +import com.iluwatar.Money; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +import static org.junit.Assert.assertEquals; + +public class AppTest { + + @Test + public void testAppOutput() { + // Redirect the standard output to capture the printed messages + ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outContent)); + + // Run the main method of the App class + App.main(new String[]{}); + + // Restore the standard output + System.setOut(System.out); + + // Split the captured output into lines + String[] lines = outContent.toString().split(System.lineSeparator()); + + // Check the expected output + assertEquals("Primary Balance: 10000 USD", lines[0].trim()); + assertEquals("Secondary Balance: 5000 EUR", lines[1].trim()); + assertEquals("Allocated Balances:", lines[2].trim()); + assertEquals("Account 1: 13000 USD", lines[3].trim()); + assertEquals("Account 2: 3000 USD", lines[4].trim()); + } +}