sashomaga

Polymorphism

Mar 8th, 2013
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.75 KB | None | 0 0
  1. /*A bank holds different types of accounts for its customers: deposit accounts, loan accounts and mortgage accounts. Customers could be individuals or companies.
  2.     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.
  3. 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.
  4. 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.
  5. Deposit accounts have no interest if their balance is positive and less than 1000.
  6. Mortgage accounts have ½ interest for the first 12 months for companies and no interest for the first 6 months for individuals.
  7. 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.*/
  8.  
  9.  static void Main()
  10.     {
  11.         try
  12.         {
  13.             Customer cust1 = new Individual("Pesho");
  14.             Customer cust2 = new Company("Magura ood");
  15.             Customer cust3 = new Individual("Gosho");
  16.  
  17.             Deposit dep1 = new Deposit(cust1, 1100, 10);
  18.             Loan loan1 = new Loan(cust2, 300, 20);
  19.             Mortage mort1 = new Mortage(cust3, 500, 40);
  20.  
  21.             dep1.WithdrawMoney(100);
  22.             loan1.DepositMoney(200);
  23.             mort1.DepositMoney(1000);
  24.  
  25.             Bank.Add(dep1);
  26.             Bank.Add(loan1);
  27.             Bank.Add(mort1);
  28.  
  29.             foreach (var item in Bank.AllAcounts)
  30.             {
  31.                 Console.WriteLine(item.CalculateRate(24));
  32.             }
  33.         }
  34.         catch (ArgumentException err)
  35.         {
  36.             Console.WriteLine(err.Message);
  37.         }
  38.     }
  39.  
  40. public abstract class Acount
  41. {
  42.     private Customer customer;
  43.     private decimal balance;
  44.     private decimal interestRate;
  45.  
  46.     public Customer Customer
  47.     {
  48.         get { return this.customer; }
  49.     }
  50.  
  51.     public decimal Balance
  52.     {
  53.         get { return this.balance; }
  54.         protected set
  55.         {
  56.             if (value < 0)
  57.             {
  58.                 throw new ArgumentException("Balance must be positive!");
  59.             }
  60.             this.balance = value;
  61.         }
  62.     }
  63.     public decimal InterestRate
  64.     {
  65.         get { return this.interestRate; }
  66.         protected set
  67.         {
  68.             if (value < 0)
  69.             {
  70.                 throw new ArgumentException("Interest rate must be positive!");
  71.             }
  72.             this.interestRate = value;
  73.         }
  74.     }
  75.  
  76.     public abstract decimal CalculateRate(int mounts);
  77.  
  78.     protected Acount(Customer customer, decimal balance, decimal interestRate)
  79.     {
  80.         this.customer = customer;
  81.         this.Balance = balance;
  82.         this.InterestRate = interestRate;
  83.     }
  84. }
  85.  
  86. static class Bank
  87. {
  88.     private static List<Acount> allAcounts;
  89.  
  90.     public static List<Acount> AllAcounts
  91.     {
  92.         get { return allAcounts; }
  93.     }
  94.  
  95.     static public void Add(Acount acount)
  96.     {
  97.         Bank.allAcounts.Add(acount);
  98.     }
  99.     //static constructor to initialize field
  100.     static Bank()
  101.     {
  102.         allAcounts = new List<Acount>();
  103.     }
  104. }
  105.  
  106. public class Company : Customer
  107. {
  108.     public Company(string name)
  109.         : base(name)
  110.     {
  111.  
  112.     }
  113. }
  114.  
  115. public abstract class Customer
  116. {
  117.     private string name;
  118.  
  119.     public string Name
  120.     {
  121.         get { return this.name; }
  122.         set
  123.         {
  124.             if (value.Length <= 2)
  125.             {
  126.                 throw new ArgumentException("Name must have be more than 2 symbols!");
  127.             }
  128.             this.name = value;
  129.         }
  130.     }
  131.  
  132.     protected Customer(string name)
  133.     {
  134.         this.Name = name;
  135.     }
  136. }
  137.  
  138. public class Deposit : Acount, IDeposit, IWithdraw
  139. {
  140.     public override decimal CalculateRate(int mounts)
  141.     {
  142.         if (this.Balance < 1000)
  143.         {
  144.             return 0;
  145.         }
  146.         else
  147.         {
  148.             return this.InterestRate * mounts;
  149.         }
  150.     }
  151.  
  152.     public void DepositMoney(decimal amount)
  153.     {
  154.         if (amount < 0)
  155.         {
  156.             throw new ArgumentException("Invalid deposit");
  157.         }
  158.         this.Balance += amount;
  159.     }
  160.  
  161.     public void WithdrawMoney(decimal amount)
  162.     {
  163.         if (this.Balance - amount < 0)
  164.         {
  165.             throw new ArgumentException("Invalid Withdraw");
  166.         }
  167.         this.Balance -= amount;
  168.     }
  169.  
  170.     public Deposit(Customer customer, decimal balance, decimal interestRate) : base(customer, balance, interestRate)
  171.     {
  172.  
  173.     }
  174. }
  175.  
  176. public interface IDeposit
  177. {
  178.     void DepositMoney(decimal amount);
  179. }
  180.  
  181. public class Individual : Customer
  182. {
  183.     public Individual(string name)
  184.         : base(name)
  185.     {
  186.  
  187.     }
  188. }
  189.  
  190. public interface IWithdraw
  191. {
  192.     void WithdrawMoney(decimal amount);
  193. }
  194.  
  195. public class Loan : Acount, IDeposit
  196. {
  197.     public override decimal CalculateRate(int mounts)
  198.     {
  199.         if (mounts < 0)
  200.         {
  201.             throw new ArgumentException("Invalid mounts count");
  202.         }
  203.         else if (mounts <= 2)
  204.         {
  205.             return 0;
  206.         }
  207.         else
  208.         {
  209.             return this.InterestRate * (mounts - 2);
  210.         }
  211.     }
  212.  
  213.     public void DepositMoney(decimal amount)
  214.     {
  215.         if (amount < 0)
  216.         {
  217.             throw new ArgumentException("Invalit deposit");
  218.         }
  219.         this.Balance += amount;
  220.     }
  221.  
  222.     public Loan(Customer customer, decimal balance, decimal interestRate) : base(customer, balance, interestRate)
  223.     {
  224.  
  225.     }
  226. }
  227.  
  228. public class Mortage : Acount, IDeposit
  229. {
  230.     public override decimal CalculateRate(int mounts)
  231.     {
  232.         if (this.Customer.GetType() == typeof(Individual))
  233.         {
  234.             if (mounts > 12)
  235.             {
  236.                 return (12 * this.InterestRate / 2) + (mounts - 12) * this.InterestRate;
  237.             }
  238.             else
  239.             {
  240.                 return mounts * this.InterestRate / 2;
  241.             }
  242.         }
  243.         else
  244.         {
  245.             if (mounts > 6)
  246.             {
  247.                 return (mounts - 6) * this.InterestRate;
  248.             }
  249.             else
  250.             {
  251.                 return 0;
  252.             }
  253.         }
  254.     }
  255.  
  256.     public void DepositMoney(decimal amount)
  257.     {
  258.         if (amount <= 0)
  259.         {
  260.             throw new ArgumentException("Invalid deposit value");
  261.         }
  262.         this.Balance += amount;
  263.     }
  264.  
  265.     public Mortage(Customer customer, decimal balance, decimal interestRate) : base(customer, balance, interestRate)
  266.     {
  267.  
  268.     }
  269. }
Advertisement
Add Comment
Please, Sign In to add comment