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

change and implementation of new features #339

Open
wants to merge 1 commit 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
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>16</maven.compiler.source>
<maven.compiler.target>16</maven.compiler.target>
</properties>

<dependencies>
Expand All @@ -20,5 +22,15 @@
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.20.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.20.0</version>
</dependency>
</dependencies>
</project>
73 changes: 0 additions & 73 deletions src/main/java/com/abc/Account.java

This file was deleted.

50 changes: 23 additions & 27 deletions src/main/java/com/abc/Bank.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,39 @@
import java.util.List;

public class Bank {
private List<Customer> customers;

public Bank() {
customers = new ArrayList<Customer>();
}
private final List<Customer> customers = new ArrayList<>();

/**
* Adds a new Customer to Bank
*/
public void addCustomer(Customer customer) {
customers.add(customer);
}

public String customerSummary() {
String summary = "Customer Summary";
/**
* prints a report showing the list of customers and how many accounts they have
*/
public String printCustomerSummary() {
StringBuilder summary = new StringBuilder();
summary.append("Customer Summary");
for (Customer c : customers)
summary += "\n - " + c.getName() + " (" + format(c.getNumberOfAccounts(), "account") + ")";
return summary;
}

//Make sure correct plural of word is created based on the number passed in:
//If number passed in is 1 just return the word otherwise add an 's' at the end
private String format(int number, String word) {
return number + " " + (number == 1 ? word : word + "s");
summary.append(System.lineSeparator())
.append(" - ")
.append(c.getName()).append(" (")
.append(c.getAccounts().size())
.append(c.getAccounts().size()==1?" account":" accounts")
.append(")");
return summary.toString();
}

public double totalInterestPaid() {
/**
* prints a report showing the total interest paid by the bank on all accounts
*/
public double calculateTotalInterestsPaid() {
double total = 0;
for(Customer c: customers)
for(Customer c: customers) {
total += c.totalInterestEarned();
return total;
}

public String getFirstCustomer() {
try {
customers = null;
return customers.get(0).getName();
} catch (Exception e){
e.printStackTrace();
return "Error";
}
return total;
}
}
75 changes: 39 additions & 36 deletions src/main/java/com/abc/Customer.java
Original file line number Diff line number Diff line change
@@ -1,78 +1,81 @@
package com.abc;

import com.abc.account.Account;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

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

import static java.lang.Math.abs;

public class Customer {
private String name;
private List<Account> accounts;

private static final Logger LOG = LogManager.getLogger(Customer.class);
private final String name;
private final List<Account> accounts = new ArrayList<>();

public Customer(String name) {
this.name = name;
this.accounts = new ArrayList<Account>();
}

public String getName() {
return name;
}

public Customer openAccount(Account account) {
public List<Account> getAccounts() {
return accounts;
}

public void openAccount(Account account) {
accounts.add(account);
return this;
LOG.debug("New {} account created for customer {}", account.getType(), getName());
}

public int getNumberOfAccounts() {
return accounts.size();
public void transfer(Account from, Account to, double amount) {
LOG.debug("Transfer of {} triggered", amount);
from.withdraw(amount);
to.deposit(amount);
}

public double totalInterestEarned() {
double total = 0;
for (Account a : accounts)
total += a.interestEarned();
total += a.getInterestsEarned();
return total;
}

public String getStatement() {
String statement = null;
statement = "Statement for " + name + "\n";
public String printStatement() {
StringBuilder statement = new StringBuilder();
statement.append("Statement for ").append(name).append(System.lineSeparator());
double total = 0.0;
for (Account a : accounts) {
statement += "\n" + statementForAccount(a) + "\n";
total += a.sumTransactions();
statement.append(System.lineSeparator()).append(getStatementForAccount(a)).append(System.lineSeparator());
total += a.calculateAccountBalance();
}
statement += "\nTotal In All Accounts " + toDollars(total);
return statement;
statement.append(System.lineSeparator()).append("Total In All Accounts ").append(toDollars(total));
return statement.toString();
}

private String statementForAccount(Account a) {
String s = "";

//Translate to pretty account type
switch(a.getAccountType()){
case Account.CHECKING:
s += "Checking Account\n";
break;
case Account.SAVINGS:
s += "Savings Account\n";
break;
case Account.MAXI_SAVINGS:
s += "Maxi Savings Account\n";
break;
}
private String getStatementForAccount(Account a) {
StringBuilder str = new StringBuilder();
str.append(a.getType()).append(" Account").append(System.lineSeparator());

//Now total up all the transactions
double total = 0.0;
for (Transaction t : a.transactions) {
s += " " + (t.amount < 0 ? "withdrawal" : "deposit") + " " + toDollars(t.amount) + "\n";
total += t.amount;
for (Transaction t : a.getTransactions()) {
str.append(" ")
.append(t.getTransactionType().getType())
.append(" ")
.append(toDollars(t.getAmount()))
.append(System.lineSeparator());
total += t.getAmount();
}
s += "Total " + toDollars(total);
return s;
str.append("Total ").append(toDollars(total));
return str.toString();
}

private String toDollars(double d){
private String toDollars(double d) {
return String.format("$%,.2f", abs(d));
}
}
18 changes: 0 additions & 18 deletions src/main/java/com/abc/DateProvider.java

This file was deleted.

33 changes: 27 additions & 6 deletions src/main/java/com/abc/Transaction.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,37 @@
package com.abc;

import java.util.Calendar;
import java.util.Date;
import java.time.LocalDateTime;

public class Transaction {
public final double amount;
private final double amount;

private Date transactionDate;
private TransactionType transactionType;

public Transaction(double amount) {
private LocalDateTime transactionDate;

public Transaction(double amount, TransactionType transactionType, LocalDateTime transactionDate) {
this.amount = amount;
this.transactionDate = DateProvider.getInstance().now();
this.transactionType = transactionType;
this.transactionDate = transactionDate;
}

public double getAmount() {
return amount;
}

public TransactionType getTransactionType() {
return transactionType;
}

@SuppressWarnings("unused")
public void setTransactionType(TransactionType transactionType) {
this.transactionType = transactionType;
}
public LocalDateTime getTransactionDate() {
return transactionDate;
}

public void setTransactionDate(LocalDateTime transactionDate) {
this.transactionDate = transactionDate;
}
}
21 changes: 21 additions & 0 deletions src/main/java/com/abc/TransactionType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.abc;

/**
* Enum class that holds types of transactions
*/
public enum TransactionType {

WITHDRAWAL("withdrawal"),
DEPOSIT("deposit");

private final String type;

TransactionType(String type) {
this.type = type;
}

public String getType() {
return type;
}

}
Loading