diff --git a/1 Hands-on Legacy Code/DDDBankingExample.sln.DotSettings.user b/1 Hands-on Legacy Code/DDDBankingExample.sln.DotSettings.user new file mode 100644 index 0000000..405a163 --- /dev/null +++ b/1 Hands-on Legacy Code/DDDBankingExample.sln.DotSettings.user @@ -0,0 +1,6 @@ + + <SessionState ContinuousTestingMode="0" IsActive="True" Name="testAMSTransferMoney" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"> + <TestAncestor> + <TestId>xUnit::C37DC3E6-5409-48E1-8029-A3F8200D9DC0::net8.0::Banking.Application.AccountManagementServiceTest.testAMSTransferMoney</TestId> + </TestAncestor> +</SessionState> \ No newline at end of file diff --git a/1 Hands-on Legacy Code/src/Banking/Application/AccountManagementService.cs b/1 Hands-on Legacy Code/src/Banking/Application/AccountManagementService.cs new file mode 100644 index 0000000..d5f766d --- /dev/null +++ b/1 Hands-on Legacy Code/src/Banking/Application/AccountManagementService.cs @@ -0,0 +1,75 @@ +using Banking.Models; + +namespace Banking.Application; + +public class AccountManagementService +{ + private IDictionary customerList = new Dictionary(); + private int customerNumberCounter = 0; + private IDictionary accountList = new Dictionary(); + private int accountNumberCounter = 0; + + public AccountManagementService() + { + } + + public Customer NewCustomer(String firstName, String familyName, DateOnly dateOfBirth) + { + Customer customer = new Customer(firstName, familyName, dateOfBirth, customerNumberCounter++); + customerList.Add(customer.GetCustomerNumber(), customer); + return customer; + } + + public Account NewAccount(float balance, Customer customer) + { + Account account = new Account(accountNumberCounter++, customer); + account.SetBalance(balance); + accountList.Add(account.GetAccountnumber(), account); + customer.GetAccountList().Add(account); + return account; + } + + public CreditAccount NewCreditAccount(Credit credit) + { + CreditAccount account = new CreditAccount(accountNumberCounter++, credit); + accountList.Add(account.GetAccountnumber(), account); + credit.GetCustomer().GetAccountList().Add(account); + return account; + } + + public IList GetAccountList() + { + return new List(accountList.Values); + } + + public IList GetCustomerList() + { + return new List(customerList.Values); + } + + public void TransferMoney(float amount, int debitorAccountNumber, int creditorAccountNumber) + { + float balance = accountList[debitorAccountNumber].GetBalance(); + balance = balance - amount; + accountList[debitorAccountNumber].SetBalance(balance); + + balance = accountList[creditorAccountNumber].GetBalance(); + balance = balance + amount; + accountList[creditorAccountNumber].SetBalance(balance); + } + + public ICollection GetAccountNumberList() + { + return accountList.Keys; + } + + public Account GetAccount(int accountNumber) { + return accountList[accountNumber]; + } + + public Customer GetCustomer(int accountNumber) + { + return accountList[accountNumber].GetAccountowner(); + } + +} diff --git a/1 Hands-on Legacy Code/src/Banking/Application/CreditService.cs b/1 Hands-on Legacy Code/src/Banking/Application/CreditService.cs new file mode 100644 index 0000000..007926d --- /dev/null +++ b/1 Hands-on Legacy Code/src/Banking/Application/CreditService.cs @@ -0,0 +1,63 @@ +using Banking.Models; + +namespace Banking.Application; + +public class CreditService +{ + private AccountManagementService accountManagementService = null; + + private int creditNumberCounter = 0; + private IDictionary creditList = new Dictionary(); + + public CreditService(AccountManagementService accountManagementService) + { + this.accountManagementService = accountManagementService; + } + + public int ApplyForCredit(float amount, Customer customer) + { + int creditNumber = creditNumberCounter++; + Credit credit = new Credit(creditNumber, customer, amount); + creditList.Add(creditNumber, credit); + + return creditNumber; + } + + public CreditAccount GrantCredit(int creditNumber) + { + Credit credit = this.GetCredit(creditNumber); + credit.SetStatus(Credit.Status.Granted); + CreditAccount newCreditAccount = accountManagementService.NewCreditAccount(credit); + credit.SetAccount(newCreditAccount); + return newCreditAccount; + } + + public Credit GetCredit(int creditNumber) + { + return creditList[creditNumber]; + } + + public Credit GetCreditFromAccountNumber(int accountNumber) + { + CreditAccount account = (CreditAccount) accountManagementService.GetAccount(accountNumber); + return creditList[account.getCredit().GetCreditNumber()]; + } + + public void MakePaymentForCredit(int creditNumber, float amount) + { + Credit credit = creditList[creditNumber]; + CreditAccount creditAccount = credit.GetAccount(); + float balance = creditAccount.GetBalance(); + balance = balance + amount; + creditAccount.SetBalance(balance); + } + + public void MakePaymentForCreditAccount(int accountNumber, float amount) + { + Account account = accountManagementService.GetAccount(accountNumber); + float balance = account.GetBalance(); + balance = balance + amount; + account.SetBalance(balance); + } + +} diff --git a/1 Hands-on Legacy Code/src/Banking/Models/Account.cs b/1 Hands-on Legacy Code/src/Banking/Models/Account.cs index c1bbdd2..777f783 100644 --- a/1 Hands-on Legacy Code/src/Banking/Models/Account.cs +++ b/1 Hands-on Legacy Code/src/Banking/Models/Account.cs @@ -13,21 +13,24 @@ public Account(int accountNumber, Customer accountOwner) this.accountOwner = accountOwner; } - public float getBalance() { + public float GetBalance() + { return balance; } - public void setBalance(float balance) { + public void SetBalance(float balance) + { this.balance = balance; } - public int getAccountnumber() { + public int GetAccountnumber() + { return accountNumber; } - public Customer getAccountowner() { + public Customer GetAccountowner() + { return accountOwner; } - } diff --git a/1 Hands-on Legacy Code/src/Banking/Models/Credit.cs b/1 Hands-on Legacy Code/src/Banking/Models/Credit.cs index 6a39406..a6eef54 100644 --- a/1 Hands-on Legacy Code/src/Banking/Models/Credit.cs +++ b/1 Hands-on Legacy Code/src/Banking/Models/Credit.cs @@ -10,7 +10,7 @@ public class Credit public enum Status { - applied, refused, granted, delayed, payed + Applied, Refused, Granted, Delayed, Payed } public Credit(int creditNumber, Customer customer, float amountOfCredit) @@ -18,39 +18,47 @@ public Credit(int creditNumber, Customer customer, float amountOfCredit) this.amountOfCredit = amountOfCredit; this.creditNumber = creditNumber; this.customer = customer; - this.customer.getCreditList().Add(this); - this.status = Status.applied; + this.customer.GetCreditList().Add(this); + this.status = Status.Applied; } - public float getAmountOfCredit() { + public float GetAmountOfCredit() + { return amountOfCredit; } - public void setAmountOfCredit(float amountOfCredit) { + public void SetAmountOfCredit(float amountOfCredit) + { this.amountOfCredit = amountOfCredit; } - public Customer getCustomer() { + public Customer GetCustomer() + { return customer; } - public int getCreditNumber() { + public int GetCreditNumber() + { return creditNumber; } - public Status getStatus() { + public Status GetStatus() + { return status; } - public void setStatus(Status status) { + public void SetStatus(Status status) + { this.status = status; } - public CreditAccount? getAccount() { + public CreditAccount? GetAccount() + { return account; } - public void setAccount(CreditAccount account) { + public void SetAccount(CreditAccount account) + { this.account = account; } diff --git a/1 Hands-on Legacy Code/src/Banking/Models/CreditAccount.cs b/1 Hands-on Legacy Code/src/Banking/Models/CreditAccount.cs index 830bced..9b99964 100644 --- a/1 Hands-on Legacy Code/src/Banking/Models/CreditAccount.cs +++ b/1 Hands-on Legacy Code/src/Banking/Models/CreditAccount.cs @@ -5,9 +5,9 @@ public class CreditAccount : Account private Credit credit; public CreditAccount(int accountNumber, Credit credit) - : base(accountNumber, credit.getCustomer()) + : base(accountNumber, credit.GetCustomer()) { - this.setBalance(-(credit.getAmountOfCredit())); + this.SetBalance(-(credit.GetAmountOfCredit())); this.credit = credit; } diff --git a/1 Hands-on Legacy Code/src/Banking/Models/Customer.cs b/1 Hands-on Legacy Code/src/Banking/Models/Customer.cs index 2da0073..5cf372f 100644 --- a/1 Hands-on Legacy Code/src/Banking/Models/Customer.cs +++ b/1 Hands-on Legacy Code/src/Banking/Models/Customer.cs @@ -19,27 +19,33 @@ public Customer(String firstName, String familyName, DateOnly dateOfBirth, int c creditList = new List(); } - public String getFirstName() { + public String GetFirstName() + { return firstName; } - public String getFamilyName() { + public String GetFamilyName() + { return familyName; } - public DateOnly getDateOfBirth() { + public DateOnly GetDateOfBirth() + { return dateOfBirth; } - public int getCustomerNumber() { + public int GetCustomerNumber() + { return customerNumber; } - public List getAccountList() { + public List GetAccountList() + { return accountList; } - public List getCreditList() { + public List GetCreditList() + { return creditList; } diff --git a/1 Hands-on Legacy Code/tests/Banking.Test/Application/AccountManagementServiceTest.cs b/1 Hands-on Legacy Code/tests/Banking.Test/Application/AccountManagementServiceTest.cs new file mode 100644 index 0000000..bd967cf --- /dev/null +++ b/1 Hands-on Legacy Code/tests/Banking.Test/Application/AccountManagementServiceTest.cs @@ -0,0 +1,90 @@ +using Banking.Models; + +namespace Banking.Application; + +public class AccountManagementServiceTest +{ + + public static AccountManagementService PrepareTestData() { + AccountManagementService ams = new AccountManagementService(); + Customer customer = ams.NewCustomer("Carola", "Lilienthal", new DateOnly(1967, 9, 11)); + ams.NewAccount(1000, customer); + ams.NewAccount(5000, customer); + ams.NewAccount(2000, customer); + + customer = ams.NewCustomer("Hans", "Lilienthal", new DateOnly(1968, 9, 11)); + ams.NewAccount(2000, customer); + ams.NewAccount(5000, customer); + + customer = ams.NewCustomer("Dieter", "Lilienthal", new DateOnly(1969, 9, 11)); + ams.NewAccount(3000, customer); + ams.NewAccount(5000, customer); + + customer = ams.NewCustomer("Franz", "Lilienthal", new DateOnly(1964, 9, 11)); + ams.NewAccount(4000, customer); + ams.NewAccount(5000, customer); + + customer = ams.NewCustomer("Carsten", "Lilienthal", new DateOnly(1965, 9, 11)); + ams.NewAccount(5000, customer); + + return ams; + } + + [Fact] + void TestAmsCreation() { + AccountManagementService ams = AccountManagementServiceTest.PrepareTestData(); + Assert.NotNull(ams.GetAccountList()); + Assert.NotNull(ams.GetCustomerList()); + Assert.Equal(5, ams.GetCustomerList().Count); + Assert.Equal(10, ams.GetAccountList().Count); + int counter = 0; + foreach (Customer customer in ams.GetCustomerList()) { + counter = counter + customer.GetAccountList().Count; + } + Assert.Equal(10, counter); + } + + [Fact] + void TestAmsNewCustomerNewAccount() { + AccountManagementService ams = AccountManagementServiceTest.PrepareTestData(); + + Customer newCustomer = ams.NewCustomer("Tea", "Ginster", new DateOnly(1950, 12, 2)); + Assert.Contains(newCustomer, ams.GetCustomerList()); + + Account newAccount = ams.NewAccount(2000, newCustomer); + Assert.Contains(newAccount, ams.GetAccountList()); + Assert.Equal(newAccount, ams.GetAccount(newAccount.GetAccountnumber())); + Assert.Equal(newCustomer, ams.GetAccount(newAccount.GetAccountnumber()).GetAccountowner()); + Assert.Contains(newAccount, newCustomer.GetAccountList()); + Assert.Equal(11, ams.GetAccountList().Count); + + Credit credit = new Credit(1000, newCustomer, 10); + CreditAccount newCreditAccount = ams.NewCreditAccount(credit); + Assert.Contains(newCreditAccount, ams.GetAccountList()); + Assert.Equal(newCreditAccount, ams.GetAccount(newCreditAccount.GetAccountnumber())); + Assert.Equal(12, ams.GetAccountList().Count); + + Assert.Contains(newAccount.GetAccountnumber(), ams.GetAccountNumberList()); + Assert.Contains(newCreditAccount.GetAccountnumber(), ams.GetAccountNumberList()); + Assert.Contains(newCreditAccount, newCustomer.GetAccountList()); + + } + + [Fact] + void TestAmsTransferMoney() { + AccountManagementService ams = AccountManagementServiceTest.PrepareTestData(); + + ICollection accountNumbers = ams.GetAccountNumberList(); + using var enumerator = accountNumbers.GetEnumerator(); + enumerator.MoveNext(); + int debitorAccountNumber = enumerator.Current; + enumerator.MoveNext(); + int creditorAccountNumber = enumerator.Current; + float debitorSaldo = ams.GetAccount(debitorAccountNumber).GetBalance(); + float creditorSaldo = ams.GetAccount(creditorAccountNumber).GetBalance(); + ams.TransferMoney(100, debitorAccountNumber, creditorAccountNumber); + Assert.Equal(debitorSaldo - 100, ams.GetAccount(debitorAccountNumber).GetBalance()); + Assert.Equal(creditorSaldo + 100, ams.GetAccount(creditorAccountNumber).GetBalance()); + + } +} diff --git a/1 Hands-on Legacy Code/tests/Banking.Test/Application/CreditServiceTest.cs b/1 Hands-on Legacy Code/tests/Banking.Test/Application/CreditServiceTest.cs new file mode 100644 index 0000000..b28c07e --- /dev/null +++ b/1 Hands-on Legacy Code/tests/Banking.Test/Application/CreditServiceTest.cs @@ -0,0 +1,52 @@ +using Banking.Models; + +namespace Banking.Application; + +public class CreditServiceTest +{ + [Fact] + void TestCSCreation() + { + AccountManagementService ams = AccountManagementServiceTest.PrepareTestData(); + CreditService cs = new CreditService(ams); + + int creditNumber = cs.ApplyForCredit(1000, ams.GetCustomerList()[0]); + Credit credit = cs.GetCredit(creditNumber); + Assert.Equal(1000, credit.GetAmountOfCredit()); + Assert.True(credit.GetStatus() == Credit.Status.Applied); + + CreditAccount creditAccount = cs.GrantCredit(creditNumber); + Assert.Equal(credit, creditAccount.getCredit()); + Assert.True(credit.GetStatus() == Credit.Status.Granted); + Assert.True(credit.GetAccount() == creditAccount); + Assert.Contains(creditAccount, ams.GetAccountList()); + Assert.Contains(creditAccount.GetAccountowner(), ams.GetCustomerList()); + Assert.Equal(11, ams.GetAccountList().Count); + Assert.Equal(credit, creditAccount.getCredit()); + + Credit credit2 = cs.GetCreditFromAccountNumber(creditAccount.GetAccountnumber()); + Assert.Equal(credit, credit2); + } + + [Fact] + void TestCreditProcess() + { + AccountManagementService ams = AccountManagementServiceTest.PrepareTestData(); + CreditService cs = new CreditService(ams); + + int creditNumber = cs.ApplyForCredit(1000, ams.GetCustomerList()[0]); + Credit credit = cs.GetCredit(creditNumber); + + CreditAccount creditAccount = cs.GrantCredit(creditNumber); + Assert.Equal(credit, creditAccount.getCredit()); + Assert.True(credit.GetStatus() == Credit.Status.Granted); + Assert.True(credit.GetAccount() == creditAccount); + Assert.Equal(-1000, creditAccount.GetBalance()); + Assert.Equal(11, ams.GetAccountList().Count); + + cs.MakePaymentForCredit(creditNumber, 100); + Assert.Equal(-900, creditAccount.GetBalance()); + Assert.Equal(1000, credit.GetAmountOfCredit()); + } + +} diff --git a/1 Hands-on Legacy Code/tests/Banking.Test/Banking.Test.csproj b/1 Hands-on Legacy Code/tests/Banking.Test/Banking.Test.csproj index c58ca6c..2b842aa 100644 --- a/1 Hands-on Legacy Code/tests/Banking.Test/Banking.Test.csproj +++ b/1 Hands-on Legacy Code/tests/Banking.Test/Banking.Test.csproj @@ -24,8 +24,4 @@ - - - - diff --git a/1 Hands-on Legacy Code/tests/Banking.Test/Models/AccountTest.cs b/1 Hands-on Legacy Code/tests/Banking.Test/Models/AccountTest.cs index d480f95..acb71ee 100644 --- a/1 Hands-on Legacy Code/tests/Banking.Test/Models/AccountTest.cs +++ b/1 Hands-on Legacy Code/tests/Banking.Test/Models/AccountTest.cs @@ -8,9 +8,9 @@ public void TestAccountConstruction() Customer accountowner = new Customer("Carola", "Lilienthal", new DateOnly(1967, 9, 11), 11); Account account = new Account(10, accountowner); - Assert.Equal(10, account.getAccountnumber()); - Assert.Equal(0, account.getBalance()); - Assert.Equal(accountowner, account.getAccountowner()); + Assert.Equal(10, account.GetAccountnumber()); + Assert.Equal(0, account.GetBalance()); + Assert.Equal(accountowner, account.GetAccountowner()); } [Fact] @@ -18,9 +18,9 @@ void TestBalanceAccount() { Customer accountowner = new Customer("Carola", "Lilienthal", new DateOnly(1967, 9, 11), 11); Account account = new Account(10, accountowner); - Assert.Equal(0, account.getBalance()); - account.setBalance(100); - Assert.Equal(100, account.getBalance()); + Assert.Equal(0, account.GetBalance()); + account.SetBalance(100); + Assert.Equal(100, account.GetBalance()); } } diff --git a/1 Hands-on Legacy Code/tests/Banking.Test/Models/CreditTest.cs b/1 Hands-on Legacy Code/tests/Banking.Test/Models/CreditTest.cs index 4321536..a2d5614 100644 --- a/1 Hands-on Legacy Code/tests/Banking.Test/Models/CreditTest.cs +++ b/1 Hands-on Legacy Code/tests/Banking.Test/Models/CreditTest.cs @@ -7,10 +7,10 @@ void TestCreditConstruction() { Customer customer = new Customer("Carola", "Lilienthal", new DateOnly(1967, 9, 11), 11); Credit credit = new Credit(12, customer, 1000); - Assert.Equal(1000, credit.getAmountOfCredit()); - Assert.Equal(customer, credit.getCustomer()); - Assert.Equal(12, credit.getCreditNumber()); - Assert.Contains(credit, customer.getCreditList()); + Assert.Equal(1000, credit.GetAmountOfCredit()); + Assert.Equal(customer, credit.GetCustomer()); + Assert.Equal(12, credit.GetCreditNumber()); + Assert.Contains(credit, customer.GetCreditList()); } } diff --git a/1 Hands-on Legacy Code/tests/Banking.Test/Models/CustomerTest.cs b/1 Hands-on Legacy Code/tests/Banking.Test/Models/CustomerTest.cs index 3f95f9b..ec75eff 100644 --- a/1 Hands-on Legacy Code/tests/Banking.Test/Models/CustomerTest.cs +++ b/1 Hands-on Legacy Code/tests/Banking.Test/Models/CustomerTest.cs @@ -6,11 +6,11 @@ public class CustomerTest void TestCustomerConstruction() { Customer customer = new Customer("Carola", "Lilienthal", new DateOnly(1967, 9, 11), 11); - Assert.Equal("Carola", customer.getFirstName()); - Assert.Equal("Lilienthal", customer.getFamilyName()); - Assert.Equal(new DateOnly(1967, 9, 11), customer.getDateOfBirth()); - Assert.Equal(11, customer.getCustomerNumber()); - Assert.NotNull(customer.getAccountList()); - Assert.NotNull(customer.getCreditList()); + Assert.Equal("Carola", customer.GetFirstName()); + Assert.Equal("Lilienthal", customer.GetFamilyName()); + Assert.Equal(new DateOnly(1967, 9, 11), customer.GetDateOfBirth()); + Assert.Equal(11, customer.GetCustomerNumber()); + Assert.NotNull(customer.GetAccountList()); + Assert.NotNull(customer.GetCreditList()); } }