Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*A bank holds different types of accounts for its customers: deposit accounts, loan accounts and mortgage accounts. Customers could be individuals or companies.
- All accounts have customer, balance and interest rate (monthly based). Deposit accounts are allowed to deposit and with draw money. Loan and mortgage accounts can only deposit money.
- All accounts can calculate their interest amount for a given period (in months). In the common case its is calculated as follows: number_of_months * interest_rate.
- Loan accounts have no interest for the first 3 months if are held by individuals and for the first 2 months if are held by a company.
- Deposit accounts have no interest if their balance is positive and less than 1000.
- Mortgage accounts have ½ interest for the first 12 months for companies and no interest for the first 6 months for individuals.
- Your task is to write a program to model the bank system by classes and interfaces. You should identify the classes, interfaces, base classes and abstract actions and implement the calculation of the interest functionality through overridden methods.*/
- static void Main()
- {
- try
- {
- Customer cust1 = new Individual("Pesho");
- Customer cust2 = new Company("Magura ood");
- Customer cust3 = new Individual("Gosho");
- Deposit dep1 = new Deposit(cust1, 1100, 10);
- Loan loan1 = new Loan(cust2, 300, 20);
- Mortage mort1 = new Mortage(cust3, 500, 40);
- dep1.WithdrawMoney(100);
- loan1.DepositMoney(200);
- mort1.DepositMoney(1000);
- Bank.Add(dep1);
- Bank.Add(loan1);
- Bank.Add(mort1);
- foreach (var item in Bank.AllAcounts)
- {
- Console.WriteLine(item.CalculateRate(24));
- }
- }
- catch (ArgumentException err)
- {
- Console.WriteLine(err.Message);
- }
- }
- public abstract class Acount
- {
- private Customer customer;
- private decimal balance;
- private decimal interestRate;
- public Customer Customer
- {
- get { return this.customer; }
- }
- public decimal Balance
- {
- get { return this.balance; }
- protected set
- {
- if (value < 0)
- {
- throw new ArgumentException("Balance must be positive!");
- }
- this.balance = value;
- }
- }
- public decimal InterestRate
- {
- get { return this.interestRate; }
- protected set
- {
- if (value < 0)
- {
- throw new ArgumentException("Interest rate must be positive!");
- }
- this.interestRate = value;
- }
- }
- public abstract decimal CalculateRate(int mounts);
- protected Acount(Customer customer, decimal balance, decimal interestRate)
- {
- this.customer = customer;
- this.Balance = balance;
- this.InterestRate = interestRate;
- }
- }
- static class Bank
- {
- private static List<Acount> allAcounts;
- public static List<Acount> AllAcounts
- {
- get { return allAcounts; }
- }
- static public void Add(Acount acount)
- {
- Bank.allAcounts.Add(acount);
- }
- //static constructor to initialize field
- static Bank()
- {
- allAcounts = new List<Acount>();
- }
- }
- public class Company : Customer
- {
- public Company(string name)
- : base(name)
- {
- }
- }
- public abstract class Customer
- {
- private string name;
- public string Name
- {
- get { return this.name; }
- set
- {
- if (value.Length <= 2)
- {
- throw new ArgumentException("Name must have be more than 2 symbols!");
- }
- this.name = value;
- }
- }
- protected Customer(string name)
- {
- this.Name = name;
- }
- }
- public class Deposit : Acount, IDeposit, IWithdraw
- {
- public override decimal CalculateRate(int mounts)
- {
- if (this.Balance < 1000)
- {
- return 0;
- }
- else
- {
- return this.InterestRate * mounts;
- }
- }
- public void DepositMoney(decimal amount)
- {
- if (amount < 0)
- {
- throw new ArgumentException("Invalid deposit");
- }
- this.Balance += amount;
- }
- public void WithdrawMoney(decimal amount)
- {
- if (this.Balance - amount < 0)
- {
- throw new ArgumentException("Invalid Withdraw");
- }
- this.Balance -= amount;
- }
- public Deposit(Customer customer, decimal balance, decimal interestRate) : base(customer, balance, interestRate)
- {
- }
- }
- public interface IDeposit
- {
- void DepositMoney(decimal amount);
- }
- public class Individual : Customer
- {
- public Individual(string name)
- : base(name)
- {
- }
- }
- public interface IWithdraw
- {
- void WithdrawMoney(decimal amount);
- }
- public class Loan : Acount, IDeposit
- {
- public override decimal CalculateRate(int mounts)
- {
- if (mounts < 0)
- {
- throw new ArgumentException("Invalid mounts count");
- }
- else if (mounts <= 2)
- {
- return 0;
- }
- else
- {
- return this.InterestRate * (mounts - 2);
- }
- }
- public void DepositMoney(decimal amount)
- {
- if (amount < 0)
- {
- throw new ArgumentException("Invalit deposit");
- }
- this.Balance += amount;
- }
- public Loan(Customer customer, decimal balance, decimal interestRate) : base(customer, balance, interestRate)
- {
- }
- }
- public class Mortage : Acount, IDeposit
- {
- public override decimal CalculateRate(int mounts)
- {
- if (this.Customer.GetType() == typeof(Individual))
- {
- if (mounts > 12)
- {
- return (12 * this.InterestRate / 2) + (mounts - 12) * this.InterestRate;
- }
- else
- {
- return mounts * this.InterestRate / 2;
- }
- }
- else
- {
- if (mounts > 6)
- {
- return (mounts - 6) * this.InterestRate;
- }
- else
- {
- return 0;
- }
- }
- }
- public void DepositMoney(decimal amount)
- {
- if (amount <= 0)
- {
- throw new ArgumentException("Invalid deposit value");
- }
- this.Balance += amount;
- }
- public Mortage(Customer customer, decimal balance, decimal interestRate) : base(customer, balance, interestRate)
- {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment