import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class Account { LocalDateTime lastTransactionDate = LocalDateTime.of(1900,1,1,1,1); int lastTransactionAmount = 0; int lastTransactionBalance = 0; private String statement = "Date Amount Balance" + "\n"; private static final String DATE_FORMATTER= "dd-MM-yyyy"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_FORMATTER); public String printStatement() { if(this.lastTransactionBalance < 0){ return this.statement + "\n Failed: Insufficient Funds"; } else if(this.lastTransactionAmount <= 0){ return this.statement + "Failed: Negative Transaction"; } return this.statement + "\n" + lastTransactionDate.format(formatter) + " " + lastTransactionAmount + " " + lastTransactionBalance; } public void deposit(int amount){ this.lastTransactionBalance += amount; this.lastTransactionAmount = amount; this.lastTransactionDate = LocalDateTime.now();; } public void withdraw(int amount){ this.lastTransactionBalance -= amount; this.lastTransactionAmount = amount; this.lastTransactionDate = LocalDateTime.now(); } }
- import java.time.LocalDateTime;
- import java.time.format.DateTimeFormatter;
- public class Account {
- LocalDateTime lastTransactionDate = LocalDateTime.of(1900,1,1,1,1);
- int lastTransactionAmount = 0;
- int lastTransactionBalance = 0;
- private String statement = "Date Amount Balance" + "\n";
- private static final String DATE_FORMATTER= "dd-MM-yyyy";
- DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_FORMATTER);
- public String printStatement() {
- if(this.lastTransactionBalance < 0){
- return this.statement + "\n Failed: Insufficient Funds";
- } else if(this.lastTransactionAmount <= 0){
- return this.statement + "Failed: Negative Transaction";
- }
- return this.statement + "\n" + lastTransactionDate.format(formatter) + " " + lastTransactionAmount + " " + lastTransactionBalance;
- }
- public void deposit(int amount){
- this.lastTransactionBalance += amount;
- this.lastTransactionAmount = amount;
- this.lastTransactionDate = LocalDateTime.now();;
- }
public String printStatement() {return "Date Amount Balance" + "\n" + "31.10.2022 " + "400 " + "400";- public void withdraw(int amount){
- this.lastTransactionBalance -= amount;
- this.lastTransactionAmount = amount;
- this.lastTransactionDate = LocalDateTime.now();
- }
public void deposit(int amount){};- }
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; // TODO: Replace examples and use TDD by writing your own tests class SolutionTest { @Test void printStatement_displays_column_headings() { Account account = new Account(); String statement = account.printStatement(); String[] rows = statement.split("\\n"); String[] columns= rows[0].split("\\s"); String[] expectedColumns = "Date Amount Balance".split(" "); assertArrayEquals(expectedColumns, columns); } @Test void depositAmountIsDisplayedOnStatement(){ Account account = new Account(); account.deposit(500); String testStatement = account.printStatement(); String[] lines = testStatement.split("\n"); StatementLine line = parseStatementLine(lines[lines.length - 1]); assertEquals(500, line.amount()); } @Test void depositAmountIsAddedToAccountBalance(){ Account account = new Account(); account.deposit(500); String testStatement = account.printStatement(); String[] lines = testStatement.split("\n"); StatementLine line = parseStatementLine(lines[lines.length - 1]); assertEquals(500, line.balance()); } @Test void depositDateIsDisplayedOnStatment(){ String DATE_FORMATTER= "dd-MM-yyyy"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_FORMATTER); Account account = new Account(); account.deposit(500); String testStatement = account.printStatement(); String[] lines = testStatement.split("\n"); StatementLine line = parseStatementLine(lines[lines.length - 1]); assertEquals(LocalDateTime.now().format(formatter), line.date()); } @Test void withdrawalAmountIsDisplayedOnStatement(){ Account account = new Account(); account.deposit(500); account.withdraw(100); String testStatement = account.printStatement(); String[] lines = testStatement.split("\n"); StatementLine line = parseStatementLine(lines[lines.length - 1]); assertEquals(100, line.amount()); } @Test void withdrawalAmountIsSubtractedFromAccountBalance(){ Account account = new Account(); account.deposit(500); account.withdraw(100); String testStatement = account.printStatement(); String[] lines = testStatement.split("\n"); StatementLine line = parseStatementLine(lines[lines.length - 1]); assertEquals(400, line.balance()); } @Test void withdrawalDateIsDisplayedOnStatment(){ String DATE_FORMATTER= "dd-MM-yyyy"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_FORMATTER); Account account = new Account(); account.lastTransactionBalance = 100; account.withdraw(100); String testStatement = account.printStatement(); String[] lines = testStatement.split("\n"); StatementLine line = parseStatementLine(lines[lines.length - 1]); assertEquals(LocalDateTime.now().format(formatter), line.date()); } @Test public void cannotOverdrawAccount(){ } //test helper method private StatementLine parseStatementLine(String statementLine) { String[] lineContents = statementLine.trim().split("\\s"); String date = lineContents[0]; int amount = Integer.parseInt(lineContents[1]); int balance = Integer.parseInt(lineContents[2]); return new StatementLine(date, amount, balance); } private record StatementLine(String date, int amount, int balance){} }
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- import static org.junit.jupiter.api.Assertions.assertTrue;
- import static org.junit.jupiter.api.Assertions.assertArrayEquals;
- import java.time.LocalDateTime;
- import java.time.format.DateTimeFormatter;
- // TODO: Replace examples and use TDD by writing your own tests
- class SolutionTest {
- @Test
- void printStatement_displays_column_headings() {
- Account account = new Account();
- String statement = account.printStatement();
- String[] rows = statement.split("\\n");
- String[] columns= rows[0].split("\\s");
- String[] expectedColumns = "Date Amount Balance".split(" ");
- assertArrayEquals(expectedColumns, columns);
- }
- @Test
- void depositAmountIsDisplayedOnStatement(){
- Account account = new Account();
account.deposit(400);- account.deposit(500);
- String testStatement = account.printStatement();
- String[] lines = testStatement.split("\n");
StatementLine line = parseStatementLine(lines[1]);assertEquals(400, line.amount());- StatementLine line = parseStatementLine(lines[lines.length - 1]);
- assertEquals(500, line.amount());
- }
- @Test
- void depositAmountIsAddedToAccountBalance(){
- Account account = new Account();
- account.deposit(500);
- String testStatement = account.printStatement();
- String[] lines = testStatement.split("\n");
- StatementLine line = parseStatementLine(lines[lines.length - 1]);
- assertEquals(500, line.balance());
- }
- @Test
- void depositDateIsDisplayedOnStatment(){
- String DATE_FORMATTER= "dd-MM-yyyy";
- DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_FORMATTER);
- Account account = new Account();
- account.deposit(500);
- String testStatement = account.printStatement();
- String[] lines = testStatement.split("\n");
- StatementLine line = parseStatementLine(lines[lines.length - 1]);
- assertEquals(LocalDateTime.now().format(formatter), line.date());
- }
- @Test
- void withdrawalAmountIsDisplayedOnStatement(){
- Account account = new Account();
- account.deposit(500);
- account.withdraw(100);
- String testStatement = account.printStatement();
- String[] lines = testStatement.split("\n");
- StatementLine line = parseStatementLine(lines[lines.length - 1]);
- assertEquals(100, line.amount());
- }
- @Test
- void withdrawalAmountIsSubtractedFromAccountBalance(){
- Account account = new Account();
- account.deposit(500);
- account.withdraw(100);
- String testStatement = account.printStatement();
- String[] lines = testStatement.split("\n");
- StatementLine line = parseStatementLine(lines[lines.length - 1]);
- assertEquals(400, line.balance());
- }
- @Test
- void withdrawalDateIsDisplayedOnStatment(){
- String DATE_FORMATTER= "dd-MM-yyyy";
- DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_FORMATTER);
- Account account = new Account();
- account.lastTransactionBalance = 100;
- account.withdraw(100);
- String testStatement = account.printStatement();
- String[] lines = testStatement.split("\n");
- StatementLine line = parseStatementLine(lines[lines.length - 1]);
- assertEquals(LocalDateTime.now().format(formatter), line.date());
- }
- @Test
- public void cannotOverdrawAccount(){
- }
- //test helper method
- private StatementLine parseStatementLine(String statementLine) {
- String[] lineContents = statementLine.trim().split("\\s");
- String date = lineContents[0];
- int amount = Integer.parseInt(lineContents[1]);
- int balance = Integer.parseInt(lineContents[2]);
- return new StatementLine(date, amount, balance);
- }
- private record StatementLine(String date, int amount, int balance){}
- }
public class Account { public String printStatement() { return "Date Amount Balance" + "\n" + "31.10.2022 " + "400 " + "400"; } public void deposit(int amount){}; }
- public class Account {
- public String printStatement() {
return "Date Amount Balance";- return "Date Amount Balance" + "\n" + "31.10.2022 " + "400 " + "400";
- }
- public void deposit(int amount){};
- }
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertArrayEquals; // TODO: Replace examples and use TDD by writing your own tests class SolutionTest { @Test void printStatement_displays_column_headings() { Account account = new Account(); String statement = account.printStatement(); String[] rows = statement.split("\\n"); String[] columns= rows[0].split("\\s"); String[] expectedColumns = "Date Amount Balance".split(" "); assertArrayEquals(expectedColumns, columns); } @Test void depositAmountIsDisplayedOnStatement(){ Account account = new Account(); account.deposit(400); String testStatement = account.printStatement(); assertEquals("Date Amount Balance\n31.10.2022 400 400", testStatement); } }
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- import static org.junit.jupiter.api.Assertions.assertTrue;
- import static org.junit.jupiter.api.Assertions.assertArrayEquals;
- // TODO: Replace examples and use TDD by writing your own tests
- class SolutionTest {
- @Test
- void printStatement_displays_column_headings() {
- Account account = new Account();
- String statement = account.printStatement();
String[] columns= statement.split("(\\s)+");- String[] rows = statement.split("\\n");
- String[] columns= rows[0].split("\\s");
- String[] expectedColumns = "Date Amount Balance".split(" ");
- assertArrayEquals(expectedColumns, columns);
- }
- @Test
- void depositAmountIsDisplayedOnStatement(){
- Account account = new Account();
- account.deposit(400);
- String testStatement = account.printStatement();
- assertEquals("Date Amount Balance\n31.10.2022 400 400", testStatement);
- }
- }