Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/2 deposit withdraw #2

Merged
merged 4 commits into from Nov 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions ALM.Test/ALM.Test.csproj
Expand Up @@ -8,9 +8,14 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="Shouldly" Version="3.0.2" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="coverlet.collector" Version="1.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ALM.Web\ALM.Web.csproj" />
</ItemGroup>

</Project>
70 changes: 70 additions & 0 deletions ALM.Test/TransactionTests.cs
@@ -0,0 +1,70 @@
using ALM.Web;
using ALM.Web.Models;
using Shouldly;
using System;
using Xunit;

namespace ALM.Test
{
//public class Fixture
//{
// public BankRepository Repository { get; set; }
// public Transactioner TransactionManager { get; set; }

// public Fixture()
// {
// Repository = new BankRepository();
// Repository.Initialize();
// TransactionManager = new Transactioner(Repository);
// }
//}

public class TransactionTests
{
[Theory]
[InlineData(1337)]
[InlineData(9000)]
[InlineData(80085)]
public void DepositTest(decimal amount)
{
var account = new Account();
var transactioner = new Transactioner();
decimal originalBalance = account.Balance;

transactioner.Deposit(account, amount);

account.Balance.ShouldBe(originalBalance + amount);
}

[Theory]
[InlineData(1337, 500)]
[InlineData(9000, 100)]
[InlineData(80085, 80085)]
public void WithdrawTest(decimal initial, decimal amount)
{
var account = new Account();
account.Credit(initial);
var transactioner = new Transactioner();
decimal originalBalance = account.Balance;

transactioner.Deposit(account, amount);

account.Balance.ShouldBe(originalBalance + amount);
}

[Theory]
[InlineData(1337, 1338)]
[InlineData(1111.111, 9999.999)]
[InlineData(1, 1.000000001)]
[InlineData(0, 1)]
public void WithdrawMoreThanBalance(decimal initial, decimal amount)
{
var account = new Account();
account.Credit(initial);
var transactioner = new Transactioner();
decimal originalBalance = account.Balance;

Should.Throw<InvalidTransactionException>(() => transactioner.Withdraw(account, amount));
}
}
}
14 changes: 0 additions & 14 deletions ALM.Test/UnitTest1.cs

This file was deleted.

8 changes: 7 additions & 1 deletion ALM.Web/ALM.Web.csproj
Expand Up @@ -4,8 +4,14 @@
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>


<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.0.0" />
</ItemGroup>

</Project>
50 changes: 50 additions & 0 deletions ALM.Web/BankRepository.cs
@@ -0,0 +1,50 @@
using ALM.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ALM.Web
{
public class BankRepository
{
public List<Customer> Customers { get; set; } = new List<Customer>();
public List<Account> Accounts { get; set; } = new List<Account>();

public BankRepository()
{
this.Initialize();
}

public Customer AddCustomer(string firstName, string lastName)
{
var customer = new Customer
{

CustomerId = Customers.Any() ? Customers.Max(a => a.CustomerId) + 1 : 0,
FirstName = firstName,
LastName = lastName
};
Customers.Add(customer);
return customer;
}

public Account AddAccount(Customer owner)
{
var account = new Account
{
AccountId = Accounts.Any() ? Accounts.Max(a => a.AccountId) + 1 : 0,
Owner = owner
};
owner.Accounts.Add(account);
Accounts.Add(account);
return account;
}

public Account GetAccount(int accountId)
{
return Accounts.FirstOrDefault(a => a.AccountId == accountId);
}

}
}
12 changes: 6 additions & 6 deletions ALM.Web/Controllers/HomeController.cs
Expand Up @@ -12,20 +12,20 @@ namespace ALM.Web.Controllers
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly BankRepository _repository;

public HomeController(ILogger<HomeController> logger)
public HomeController(ILogger<HomeController> logger, BankRepository repository)
{
_logger = logger;
_repository = repository;
}

public IActionResult Index()
{
return View();
}
var model = new HomeViewModel();
model.Customers = _repository.Customers;

public IActionResult Privacy()
{
return View();
return View(model);
}

[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
Expand Down
72 changes: 72 additions & 0 deletions ALM.Web/Controllers/TransactionController.cs
@@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ALM.Web.Models;
using Microsoft.AspNetCore.Mvc;

namespace ALM.Web.Controllers
{
public class TransactionController : Controller
{
private readonly Transactioner _transactioner;
private readonly BankRepository _repository;

public TransactionController(Transactioner transactioner, BankRepository repository)
{
_transactioner = transactioner;
_repository = repository;
}

[HttpGet]
public IActionResult Index()
{
return RedirectToAction("Transact");
}

[HttpGet]
public IActionResult Transact()
{
var model = new TransactionViewModel();
return View(model);
}

[HttpPost]
public IActionResult Transact(TransactionViewModel model, TransactionType type)
{
if (!ModelState.IsValid)
{
return View(model);
}
try
{
var account = _repository.GetAccount(model.AccountId);
model.Account = account;
switch (type)
{
case TransactionType.Deposit:
_transactioner.Deposit(account, model.Amount);
break;
case TransactionType.Withdrawal:
_transactioner.Withdraw(account, model.Amount);
break;
default:
break;
}
}
catch (AccountNotFoundException ex)
{
ModelState.AddModelError("account", ex.Message);
return View(model);
}
catch (InvalidTransactionException ex)
{
ModelState.AddModelError("balance", ex.Message);
return View(model);
}

return View(model);
}

}
}
14 changes: 14 additions & 0 deletions ALM.Web/Exceptions/AccountNotFoundException.cs
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ALM.Web
{
public class AccountNotFoundException : Exception
{
public AccountNotFoundException(string message) : base(message)
{
}
}
}
14 changes: 14 additions & 0 deletions ALM.Web/Exceptions/InvalidTransactionException.cs
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ALM.Web
{
public class InvalidTransactionException:Exception
{
public InvalidTransactionException(string message) : base(message)
{
}
}
}
31 changes: 31 additions & 0 deletions ALM.Web/Initializer.cs
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ALM.Web
{
public static class Initializer
{
public static void Initialize(this BankRepository repository)
{
var customer1 = repository.AddCustomer("Albin", "Ljunghusen");
var customer2 = repository.AddCustomer("Rickard", "Uttermalm");
var customer3 = repository.AddCustomer("Fredrik", "Haglund");

var account1 = repository.AddAccount(customer1);
var account2 = repository.AddAccount(customer2);
var account3 = repository.AddAccount(customer2);
var account4 = repository.AddAccount(customer3);
var account5 = repository.AddAccount(customer3);
var account6 = repository.AddAccount(customer3);

account1.Credit(1000);
account2.Credit(4812);
account3.Credit(1782512);
account4.Credit(123);
account5.Credit(0);
account6.Credit(1337);
}
}
}
24 changes: 24 additions & 0 deletions ALM.Web/Models/Account.cs
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ALM.Web.Models
{
public class Account
{
public int AccountId { get; set; }
public Customer Owner { get; set; }
public Decimal Balance { get; private set; }

public void Credit(decimal amount)
{
Balance += amount;
}

public void Debit(decimal amount)
{
Balance -= amount;
}
}
}
15 changes: 15 additions & 0 deletions ALM.Web/Models/Customer.cs
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ALM.Web.Models
{
public class Customer
{
public int CustomerId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public List<Account> Accounts { get; set; } = new List<Account>();
}
}
12 changes: 12 additions & 0 deletions ALM.Web/Models/HomeViewModel.cs
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ALM.Web.Models
{
public class HomeViewModel
{
public List<Customer> Customers { get; set; }
}
}