Skip to content

Commit

Permalink
Acceptance test passing, still to test DateCreator and ProConsole
Browse files Browse the repository at this point in the history
  • Loading branch information
S-DRE committed Apr 4, 2023
1 parent 7c168b8 commit 232b639
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 17 deletions.
27 changes: 24 additions & 3 deletions Bank/Account.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public class Account
private ProConsole proConsole;
private DateCreator dateCreator;
private WalletRepository wallet;
private List<Movement> movements = new();

public Account(ProConsole proConsole, DateCreator dateCreator, WalletRepository wallet)
{
Expand All @@ -15,16 +16,36 @@ public Account(ProConsole proConsole, DateCreator dateCreator, WalletRepository

public void Deposit(int amount)
{
wallet.add(1000);
wallet.add(amount);
AddMovement(amount);
}

public void Withdraw(int amount)
{
throw new NotImplementedException();
wallet.remove(amount);
AddMovement(amount*-1);
}

public void PrintStatement()
{
throw new NotImplementedException();
proConsole.printLine("Date || Amount || Balance");

for(int i=movements.Count-1; i>=0; i--)
{
proConsole.printLine(FormatLine(i));
}

}

private void AddMovement(int amount)
{
movements.Add(new Movement(dateCreator.CreateCurrentDate(), amount, wallet.getBalance()));
}

private string FormatLine(int index)
{
return movements[index].date.ToString("dd/MM/yyyy") + " || " +
movements[index].operationAmount + " || " +
movements[index].remainingBalance;
}
}
15 changes: 15 additions & 0 deletions Bank/Movement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Bank;

public class Movement
{
public Movement(DateTime date, int operationAmount, int remainingBalance)
{
this.date = date;
this.operationAmount = operationAmount;
this.remainingBalance = remainingBalance;
}

public DateTime date {get; }
public int operationAmount {get; }
public int remainingBalance {get; }
}
14 changes: 13 additions & 1 deletion Bank/Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,20 @@

public class Wallet : WalletRepository
{
private int balance = 0;

public void add(int amount)
{
throw new NotImplementedException("NOT IMPLEMENTED: Wallet repository not implemented");
balance += amount;
}

public void remove(int amount)
{
balance -= amount;
}

public int getBalance()
{
return balance;
}
}
2 changes: 2 additions & 0 deletions Bank/WalletRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
public interface WalletRepository
{
public void add(int amount);
void remove(int amount);
public int getBalance();
}
20 changes: 13 additions & 7 deletions BankTests/AccountShould.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
using Bank;
using Moq;

namespace AccountShould;
namespace BankUnitTests;

public class AccountShould
{
private Account account;

private Mock<WalletRepository> walletRepository = new();

private Mock<DateCreator> dateMocker = new();


public AccountShould()
{
account = new Account(new ProConsole(), new DateCreator(), walletRepository.Object);
account = new Account(new ProConsole(), dateMocker.Object, walletRepository.Object);
}

[Fact]
public void DepositTheGivenAmountAndSaveTheValue() {
account.Deposit(1000);
walletRepository.Verify(wallet => wallet.add(1000));
[Theory]
[InlineData(1000)]
[InlineData(2000)]
public void DepositTheGivenAmountAndSaveTheValue(int depositAmount) {
dateMocker.Setup(x => x.CreateCurrentDate())
.Returns(DateTime.Parse("01/01/2000"));

account.Deposit(depositAmount);
walletRepository.Verify(wallet => wallet.add(depositAmount));
}
}
12 changes: 6 additions & 6 deletions BankTests/BankAcceptanceShould.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Bank;
using Moq;

namespace BankTests;
namespace BankAcceptanceTests;

public class BankAcceptanceShould
{
Expand All @@ -26,9 +26,9 @@ public void PerformPrintAListOfPreviousDepositsAndWithdrawals()
// given
var account = new Account(proConsoleMock, dateCreatorMock, walletInMemoryRepo);
dateMocker.SetupSequence(x => x.CreateCurrentDate())
.Returns(DateTime.Parse("14/01/2012"))
.Returns(DateTime.Parse("10/01/2012"))
.Returns(DateTime.Parse("13/01/2012"))
.Returns(DateTime.Parse("10/01/2012"));
.Returns(DateTime.Parse("14/01/2012"));

// when
account.Deposit(1000);
Expand All @@ -40,9 +40,9 @@ public void PerformPrintAListOfPreviousDepositsAndWithdrawals()

// It could also check a bunch of lines if the console has a method for stack of transactions
consoleMocker.Verify(console => console.printLine("Date || Amount || Balance"));
consoleMocker.Verify(console => console.printLine("14/01/2012 || -500 || 2500"));
consoleMocker.Verify(console => console.printLine("13/01/2012 || 2000 || 3000"));
consoleMocker.Verify(console => console.printLine("10/01/2012 || 1000 || 1000"));
consoleMocker.Verify(console => console.printLine("14/01/2012 || -500 || 2500"));
consoleMocker.Verify(console => console.printLine("13/01/2012 || 2000 || 3000"));
consoleMocker.Verify(console => console.printLine("10/01/2012 || 1000 || 1000"));

}
}
Expand Down
40 changes: 40 additions & 0 deletions BankTests/WalletShould.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Bank;

namespace BankUnitTests;

public class WalletShould
{
private Wallet wallet;

public WalletShould()
{
wallet = new();
}

[Fact]
public void beInitializedWithABalanceOf0()
{
Assert.Equal(0, wallet.getBalance());
}

[Theory]
[InlineData(1000)]
[InlineData(2000)]
public void AddGivenAmountToTheBalanceWhenADepositIsMade(int depositAmount)
{
wallet.add(depositAmount);

Assert.Equal(depositAmount, wallet.getBalance());
}

[Theory]
[InlineData(10000, 1000, 9000)]
[InlineData(10000, 2000, 8000)]
public void RemoveGivenAmountToTheBalanceWhenAWithdrawIsMade(int startingAmount, int withdrawAmount, int expectedAmount)
{
wallet.add(startingAmount);
wallet.remove(withdrawAmount);

Assert.Equal(expectedAmount, wallet.getBalance());
}
}

0 comments on commit 232b639

Please sign in to comment.