Skip to content

Commit

Permalink
Migrate 1-application
Browse files Browse the repository at this point in the history
  • Loading branch information
hschwentner committed Jun 6, 2024
1 parent ce13c3c commit e09c966
Show file tree
Hide file tree
Showing 13 changed files with 343 additions and 44 deletions.
6 changes: 6 additions & 0 deletions 1 Hands-on Legacy Code/DDDBankingExample.sln.DotSettings.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=ad113830_002Db0ea_002D46e8_002Da5c7_002D6f1c301f86ab/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="testAMSTransferMoney" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
&lt;TestAncestor&gt;
&lt;TestId&gt;xUnit::C37DC3E6-5409-48E1-8029-A3F8200D9DC0::net8.0::Banking.Application.AccountManagementServiceTest.testAMSTransferMoney&lt;/TestId&gt;
&lt;/TestAncestor&gt;
&lt;/SessionState&gt;</s:String></wpf:ResourceDictionary>
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using Banking.Models;

namespace Banking.Application;

public class AccountManagementService
{
private IDictionary<int, Customer> customerList = new Dictionary<int, Customer>();
private int customerNumberCounter = 0;
private IDictionary<int, Account> accountList = new Dictionary<int, Account>();
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<Account> GetAccountList()
{
return new List<Account>(accountList.Values);
}

public IList<Customer> GetCustomerList()
{
return new List<Customer>(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<int> GetAccountNumberList()
{
return accountList.Keys;
}

public Account GetAccount(int accountNumber) {
return accountList[accountNumber];
}

public Customer GetCustomer(int accountNumber)
{
return accountList[accountNumber].GetAccountowner();
}

}
63 changes: 63 additions & 0 deletions 1 Hands-on Legacy Code/src/Banking/Application/CreditService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using Banking.Models;

namespace Banking.Application;

public class CreditService
{
private AccountManagementService accountManagementService = null;

Check warning on line 7 in 1 Hands-on Legacy Code/src/Banking/Application/CreditService.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

Check warning on line 7 in 1 Hands-on Legacy Code/src/Banking/Application/CreditService.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

private int creditNumberCounter = 0;
private IDictionary<int, Credit> creditList = new Dictionary<int, Credit>();

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();

Check warning on line 49 in 1 Hands-on Legacy Code/src/Banking/Application/CreditService.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.

Check warning on line 49 in 1 Hands-on Legacy Code/src/Banking/Application/CreditService.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.
float balance = creditAccount.GetBalance();

Check warning on line 50 in 1 Hands-on Legacy Code/src/Banking/Application/CreditService.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 50 in 1 Hands-on Legacy Code/src/Banking/Application/CreditService.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
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);
}

}
13 changes: 8 additions & 5 deletions 1 Hands-on Legacy Code/src/Banking/Models/Account.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


}
30 changes: 19 additions & 11 deletions 1 Hands-on Legacy Code/src/Banking/Models/Credit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,47 +10,55 @@ public class Credit

public enum Status
{
applied, refused, granted, delayed, payed
Applied, Refused, Granted, Delayed, Payed
}

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

Expand Down
4 changes: 2 additions & 2 deletions 1 Hands-on Legacy Code/src/Banking/Models/CreditAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
18 changes: 12 additions & 6 deletions 1 Hands-on Legacy Code/src/Banking/Models/Customer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,33 @@ public Customer(String firstName, String familyName, DateOnly dateOfBirth, int c
creditList = new List<Credit>();
}

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<Account> getAccountList() {
public List<Account> GetAccountList()
{
return accountList;
}

public List<Credit> getCreditList() {
public List<Credit> GetCreditList()
{
return creditList;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<int> 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());

}
}
Loading

0 comments on commit e09c966

Please sign in to comment.