From 1bdd54ad84b15f2b8b4bb005212412c8c2f47686 Mon Sep 17 00:00:00 2001 From: Yusupova Ramilya Date: Thu, 23 May 2024 11:42:17 +0300 Subject: [PATCH 1/2] wip --- .../BankAccountTests.cs | 94 +++++++++++++++++++ TDDLesson.BankAccount/BankAccount.cs | 20 +++- 2 files changed, 110 insertions(+), 4 deletions(-) diff --git a/TDDLesson.BankAccount.Tests/BankAccountTests.cs b/TDDLesson.BankAccount.Tests/BankAccountTests.cs index 99508d3..6ed5791 100644 --- a/TDDLesson.BankAccount.Tests/BankAccountTests.cs +++ b/TDDLesson.BankAccount.Tests/BankAccountTests.cs @@ -2,7 +2,101 @@ using FluentAssertions; +/* + * - Добавление на счет денеждых средств + * - Снятие средств со счета + */ + + [TestClass] public sealed class BankAccountTests { + [TestMethod] + public void ShouldBe20OnAccount_When20Added() + { + // Arrange + var balance = 20; + var account = new BankAccount(); + + // Act + var newAccount = account.Add(balance); + + // Assert + newAccount.Balance.Should().Be(balance); + } + + [TestMethod] + public void ShouldBe50OnAccount_When20AddedAnd30Was() + { + // Arrange + var balanceBefore = 30; + var balanceAdded = 20; + var account = new BankAccount(balanceBefore); + + // Act + var newAccount = account.Add(balanceAdded); + + // Assert + newAccount.Balance.Should().Be(50); + } + + [TestMethod] + public void ShouldThrow_WhenNegativeValueAdded() + { + // Arrange + var balanceBefore = 30; + var balanceAdded = -20; + var account = new BankAccount(balanceBefore); + + // Act + var newAccount = () => account.Add(balanceAdded); + + // Assert + newAccount.Should().ThrowExactly(); + } + + [TestMethod] + public void ShouldBe20OnAccount_When10Withdrawn() + { + // Arrange + var balanceBefore = 30; + var balance = 10; + var account = new BankAccount(balanceBefore); + + // Act + var newAccount = account.Withdraw(balance); + + // Assert + newAccount.Balance.Should().Be(20); + } + + [TestMethod] + public void ShouldThrow_WhenNegativeValueWithdrawn() + { + // Arrange + var balanceBefore = 30; + var balanceWithdrawn = -20; + var account = new BankAccount(balanceBefore); + + // Act + var newAccount = () => account.Withdraw(balanceWithdrawn); + + // Assert + newAccount.Should().ThrowExactly(); + } + + [TestMethod] + public void ShouldThrow_WhenWithdrawnMoreThanOnAccount() + { + // Arrange + var balanceBefore = 30; + var balanceWithdrawn = 50; + var account = new BankAccount(balanceBefore); + + // Act + var newAccount = () => account.Withdraw(balanceWithdrawn); + + // Assert + newAccount.Should().ThrowExactly(); + } } \ No newline at end of file diff --git a/TDDLesson.BankAccount/BankAccount.cs b/TDDLesson.BankAccount/BankAccount.cs index cdc2e4e..6328c06 100644 --- a/TDDLesson.BankAccount/BankAccount.cs +++ b/TDDLesson.BankAccount/BankAccount.cs @@ -1,13 +1,25 @@ namespace TDDLesson.BankAccount; -public sealed class BankAccount +public record BankAccount { + public BankAccount(int balance = 0) + { + Balance = balance; + } + public int Balance { get; private set; } - public void Add(int money) + public BankAccount Add(int money) + { + if (money < 0) throw new ArgumentException(); + return new BankAccount(Balance + money); + } + + public BankAccount Withdraw(int money) { - if (money < 0) throw new InvalidOperationException(); + if (money < 0) throw new ArgumentException(); + if (money > Balance) throw new InvalidOperationException(); - Balance = money; + return new BankAccount(Balance - money); } } \ No newline at end of file From e034ff7bf1872c0fdbc68a1bd0368a045f3d3b84 Mon Sep 17 00:00:00 2001 From: Akim Adzhi-Asan Date: Thu, 23 May 2024 12:09:11 +0300 Subject: [PATCH 2/2] add overdraft --- .../BankAccountTests.cs | 26 +++++++++++++++---- TDDLesson.BankAccount/BankAccount.cs | 14 +++------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/TDDLesson.BankAccount.Tests/BankAccountTests.cs b/TDDLesson.BankAccount.Tests/BankAccountTests.cs index 6ed5791..ef29aa3 100644 --- a/TDDLesson.BankAccount.Tests/BankAccountTests.cs +++ b/TDDLesson.BankAccount.Tests/BankAccountTests.cs @@ -16,7 +16,7 @@ public void ShouldBe20OnAccount_When20Added() { // Arrange var balance = 20; - var account = new BankAccount(); + var account = new BankAccount(0); // Act var newAccount = account.Add(balance); @@ -61,7 +61,7 @@ public void ShouldBe20OnAccount_When10Withdrawn() // Arrange var balanceBefore = 30; var balance = 10; - var account = new BankAccount(balanceBefore); + var account = new BankAccount(0, balanceBefore); // Act var newAccount = account.Withdraw(balance); @@ -76,7 +76,7 @@ public void ShouldThrow_WhenNegativeValueWithdrawn() // Arrange var balanceBefore = 30; var balanceWithdrawn = -20; - var account = new BankAccount(balanceBefore); + var account = new BankAccount(0, balanceBefore); // Act var newAccount = () => account.Withdraw(balanceWithdrawn); @@ -86,12 +86,13 @@ public void ShouldThrow_WhenNegativeValueWithdrawn() } [TestMethod] - public void ShouldThrow_WhenWithdrawnMoreThanOnAccount() + public void ShouldThrow_WhenWithdrawnMoreThanOnAccountIncludingOverdraft() { // Arrange + var balanceOverdraft = 10; var balanceBefore = 30; var balanceWithdrawn = 50; - var account = new BankAccount(balanceBefore); + var account = new BankAccount(0, balanceBefore); // Act var newAccount = () => account.Withdraw(balanceWithdrawn); @@ -99,4 +100,19 @@ public void ShouldThrow_WhenWithdrawnMoreThanOnAccount() // Assert newAccount.Should().ThrowExactly(); } + + [TestMethod] + public void ShouldBeMinus50OnAccount_WhenWithdrawn50AndZeroOnAccount() + { + // Arrange + var balanceBefore = 10; + var balanceWithdrawn = 50; + var account = new BankAccount(0, balanceBefore); + + // Act + var newAccount = account.Withdraw(balanceWithdrawn); + + // Assert + newAccount.Balance.Should().Be(-40); + } } \ No newline at end of file diff --git a/TDDLesson.BankAccount/BankAccount.cs b/TDDLesson.BankAccount/BankAccount.cs index 6328c06..a8d565e 100644 --- a/TDDLesson.BankAccount/BankAccount.cs +++ b/TDDLesson.BankAccount/BankAccount.cs @@ -1,13 +1,8 @@ namespace TDDLesson.BankAccount; -public record BankAccount +public record BankAccount(int Overdraft, int Balance = 0) { - public BankAccount(int balance = 0) - { - Balance = balance; - } - - public int Balance { get; private set; } + public int Balance { get; private set; } = Balance; public BankAccount Add(int money) { @@ -18,8 +13,7 @@ public BankAccount Add(int money) public BankAccount Withdraw(int money) { if (money < 0) throw new ArgumentException(); - if (money > Balance) throw new InvalidOperationException(); - - return new BankAccount(Balance - money); + if (Balance + Overdraft - money < 0) throw new InvalidOperationException(); + return new BankAccount(Balance + Overdraft - money, Overdraft); } } \ No newline at end of file