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

Nikolett Liesinger Solution for Programming Test #320

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
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
19 changes: 19 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
<groupId>com.abc</groupId>
<artifactId>bank</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>
<packaging>jar</packaging>

<name>bank</name>
Expand All @@ -20,5 +32,12 @@
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.7.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
73 changes: 0 additions & 73 deletions src/main/java/com/abc/Account.java

This file was deleted.

46 changes: 0 additions & 46 deletions src/main/java/com/abc/Bank.java

This file was deleted.

78 changes: 0 additions & 78 deletions src/main/java/com/abc/Customer.java

This file was deleted.

16 changes: 0 additions & 16 deletions src/main/java/com/abc/Transaction.java

This file was deleted.

85 changes: 85 additions & 0 deletions src/main/java/com/abc/account/Account.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.abc.account;

import com.abc.customer.Customer;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public abstract class Account {

private final Customer customer;
private final AccountType accountType;
private final List<Transaction> transactions;
private double balance;

protected Account(Customer customer, AccountType accountType) {
this.customer = customer;
this.accountType = accountType;
this.transactions = new ArrayList<>();
}

public final void deposit(double amount) {
if (amount <= 0) {
throw new IllegalArgumentException("Amount must be greater than zero.");
}
transactions.add(new Transaction(Transaction.TransactionType.DEPOSIT, amount));
balance += amount;
}

public final void withdraw(double amount) {
if (amount <= 0) {
throw new IllegalArgumentException("Amount must be greater than zero.");
}
if (balance < amount) {
throw new IllegalArgumentException("Insufficient founds.");
}
transactions.add(new Transaction(Transaction.TransactionType.WITHDREW, amount));
balance -= amount;
}

public void transfer(double amount, Account targetAccount) {
if (targetAccount == this) {
throw new IllegalArgumentException("Target account must be a different account.");
}
if (targetAccount == null) {
throw new IllegalArgumentException("Target account cannot be null.");
}
withdraw(amount);
targetAccount.deposit(amount);
}

// Currently it's the same as the account balance, but I choose not to remove it.
// Later it could be easily extended with a parameter (eg. time interval)
public double sumTransactions() {
double amount = 0.0;
for (Transaction t : transactions)
amount += t.getAmount();
return amount;
}

public Transaction getLastTransaction() {
if (transactions.isEmpty()) {
return null;
}
return transactions.get(transactions.size() - 1);
}

public abstract double calcInterestEarned();

public double getBalance() {
return balance;
}

public Customer getCustomer() {
return customer;
}

public AccountType getAccountType() {
return accountType;
}

public List<Transaction> getTransactions() {
return Collections.unmodifiableList(transactions);
}
}
21 changes: 21 additions & 0 deletions src/main/java/com/abc/account/AccountFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.abc.account;

import com.abc.customer.Customer;

public class AccountFactory {

private AccountFactory() {
}

public static Account create(Customer customer, AccountType accountType) {
switch (accountType) {
case CHECKING:
return new CheckingAccount(customer, accountType);
case SAVINGS:
return new SavingsAccount(customer, accountType);
case MAXI_SAVINGS:
return new MaxiSavingsAccount(customer, accountType);
}
throw new IllegalArgumentException("Invalid account type.");
}
}
17 changes: 17 additions & 0 deletions src/main/java/com/abc/account/AccountType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.abc.account;

public enum AccountType {
CHECKING("Checking Account"),
SAVINGS("Savings Account"),
MAXI_SAVINGS("Maxi Savings Account");

private final String prettyName;

AccountType(String prettyName) {
this.prettyName = prettyName;
}

public String getPrettyName() {
return prettyName;
}
}
16 changes: 16 additions & 0 deletions src/main/java/com/abc/account/CheckingAccount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.abc.account;

import com.abc.customer.Customer;

public class CheckingAccount extends Account {

CheckingAccount(Customer customer, AccountType accountType) {
super(customer, accountType);
}

@Override
public double calcInterestEarned() {
double amount = getBalance();
return amount * 0.001;
}
}
Loading