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

my-bank changes - sandor branya #200

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
47 changes: 28 additions & 19 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.abc</groupId>
<artifactId>bank</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<groupId>com.abc</groupId>
<artifactId>bank</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>bank</name>
<name>bank</name>

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

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.1.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.

62 changes: 24 additions & 38 deletions src/main/java/com/abc/Bank.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,31 @@

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class Bank {
private List<Customer> customers;
private List<Customer> customers;

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

public void addCustomer(Customer customer) {
customers.add(customer);
}

public String customerSummary() {
StringBuilder sb = new StringBuilder("Customer Summary");
customers.forEach(c -> {
sb.append("\n - ");
sb.append(c.getSummary());
});
return sb.toString();
}

public double totalInterestPaid() {
return customers.stream().map(Customer::totalInterestEarned)
.collect(Collectors.summingDouble(Double::doubleValue));
}

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

public void addCustomer(Customer customer) {
customers.add(customer);
}

public String customerSummary() {
String summary = "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");
}

public double totalInterestPaid() {
double total = 0;
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";
}
}
}
133 changes: 63 additions & 70 deletions src/main/java/com/abc/Customer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,77 +2,70 @@

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static java.lang.Math.abs;
import com.abc.account.Account;
import com.abc.account.CurrencyUtil;

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

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

public String getName() {
return name;
}

public Customer openAccount(Account account) {
accounts.add(account);
return this;
}

public int getNumberOfAccounts() {
return accounts.size();
}

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

public String getStatement() {
String statement = null;
statement = "Statement for " + name + "\n";
double total = 0.0;
for (Account a : accounts) {
statement += "\n" + statementForAccount(a) + "\n";
total += a.sumTransactions();
}
statement += "\nTotal In All Accounts " + toDollars(total);
return statement;
}

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;
}

//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;
}
s += "Total " + toDollars(total);
return s;
}

private String toDollars(double d){
return String.format("$%,.2f", abs(d));
}
private String name;
private List<Account> accounts;

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

public String getName() {
return name;
}

public Customer openAccount(Account account) {
accounts.add(account);
return this;
}

public int getNumberOfAccounts() {
return accounts.size();
}

public double totalInterestEarned() {
return accounts.stream().map(Account::interestEarned).collect(Collectors.summingDouble(Double::doubleValue));
}

public String getStatement() {
StringBuilder sb = new StringBuilder(String.format("Statement for %s\n", name));
accounts.forEach(a -> sb.append(String.format("\n%s\n", a.getStatement())));
sb.append(String.format("\nTotal In All Accounts %s", CurrencyUtil.toDollars(sumAllTransactions())));
return sb.toString();
}

public Double sumAllTransactions() {
return accounts.stream().map(Account::sumTransactions).collect(Collectors.summingDouble(Double::doubleValue));
}

public String getSummary() {
return String.format("%s (%s)", getName(), StringUtil.pularize(getNumberOfAccounts(), "account"));
}

synchronized public void selfTransfer(Account src, Account dest, Double amount) {
if (!accounts.contains(src)) {
throw new IllegalArgumentException("Source account must belong to the customer!");
}

if (!accounts.contains(dest)) {
throw new IllegalArgumentException("Destination account must belong to the customer!");
}

if (amount <= 0) {
throw new IllegalArgumentException("Amount must be greater than zero!");
}

if (src.sumTransactions() < amount) {
throw new IllegalArgumentException("Insufficient money on the source account to perform that transaction!");
}

src.withdraw(amount);
dest.deposit(amount);
}
}
17 changes: 4 additions & 13 deletions src/main/java/com/abc/DateProvider.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
package com.abc;

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

public class DateProvider {
private static DateProvider instance = null;

public static DateProvider getInstance() {
if (instance == null)
instance = new DateProvider();
return instance;
}

public Date now() {
return Calendar.getInstance().getTime();
}
public LocalDate now() {
return LocalDate.now();
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/abc/StringUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.abc;

public final class StringUtil {
// 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
public static String pularize(int number, String word) {
return number + " " + (number == 1 ? word : word + "s");
}
}
Loading