Advertisement
amarek

OOP LV7 - Zadatak1

Nov 18th, 2019
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.12 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace LV7___Zadatak_1
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             List<IPayable> list = new List<IPayable>();
  14.             BankAccount B1 = new BankAccount("Enio", 1000, 0.0);
  15.             BankAccount B2 = new BankAccount("Petar", 2000, 500);
  16.             MobileAccount P1 = new MobileAccount("0992051444", 2.5, 50);
  17.             MobileAccount P2 = new MobileAccount("0976432454", 1.4, 100);
  18.  
  19.             list.Add(B1);
  20.             list.Add(B2);
  21.             list.Add(P1);
  22.             list.Add(P2);
  23.  
  24.             Random rnd = new Random();
  25.             foreach (IPayable account in list)
  26.             {
  27.                 account.SendPayment(rnd.Next(10, 100));
  28.             }
  29.             foreach (IPayable account in list)
  30.             {
  31.                 account.RequestPayment(rnd.Next(1, 100));
  32.             }
  33.             foreach (IPayable account in list)
  34.             {
  35.                 Console.WriteLine("Balance: " + account.GetBalance() + "$");
  36.             }
  37.         }
  38.     }
  39.  
  40.     interface IPayable
  41.     {
  42.         double GetBalance();
  43.         void SendPayment(double payment);
  44.         void RequestPayment(double payment);
  45.     }
  46.  
  47.     public class BankAccount : IPayable
  48.     {
  49.         private string mName;
  50.         private double mLimit;
  51.         private double mBalance;
  52.  
  53.         public BankAccount(string name, double limit, double balance)
  54.         {
  55.             this.mName = name;
  56.             this.mLimit = limit;
  57.             this.mBalance = balance;
  58.         }
  59.  
  60.         public double GetBalance()
  61.         {
  62.             return this.mBalance;
  63.         }
  64.  
  65.         public void SendPayment(double payment)
  66.         {
  67.             this.mBalance += payment;
  68.         }
  69.  
  70.         public void RequestPayment(double payment)
  71.         {
  72.             double balance = this.mBalance;
  73.  
  74.             balance -= payment;
  75.  
  76.             if (balance > mLimit)
  77.             {
  78.                 this.mBalance += payment;
  79.             }
  80.             else
  81.             {
  82.                 Console.WriteLine("Error!");
  83.             }
  84.         }
  85.     }
  86.  
  87.     public class MobileAccount : IPayable
  88.     {
  89.         private string mNumber;
  90.         private double mPrice;
  91.         private double mBalance;
  92.  
  93.         public MobileAccount(string number, double price, double balance)
  94.         {
  95.             this.mNumber = number;
  96.             this.mPrice = price;
  97.             this.mBalance = balance;
  98.         }
  99.  
  100.         public double GetBalance()
  101.         {
  102.             return this.mBalance;
  103.         }
  104.  
  105.         public void SendPayment(double payment)
  106.         {
  107.             this.mBalance += payment;
  108.         }
  109.  
  110.         public void RequestPayment(double payment)
  111.         {
  112.             double balance = this.mBalance;
  113.  
  114.             balance -= payment;
  115.  
  116.             if (balance > 0)
  117.             {
  118.                 this.mBalance += payment;
  119.             }
  120.             else
  121.             {
  122.                 Console.WriteLine("Error!");
  123.             }
  124.         }
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement