Advertisement
Batisk_AFF

C# - lr3

Oct 5th, 2022
821
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.47 KB | Source Code | 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 lr3
  8. {
  9.     public class Account
  10.     {
  11.         public string Number { get; private set; }
  12.         public double Balance { get; private set; }
  13.         public List<string> History { get; private set; }
  14.  
  15.         public Account(string num) { this.Number = num; this.Balance = 0; this.History = new List<string>(); }
  16.  
  17.         public void Add(double money)
  18.         {
  19.             this.Balance += money;
  20.             this.History.Add("+ " + money.ToString());
  21.         }
  22.  
  23.         public void Withdraw(double money)
  24.         {
  25.             Balance -= money;
  26.             History.Add("- " + money.ToString());
  27.         }
  28.  
  29.         public void ShowBalance()
  30.         {
  31.             Console.WriteLine(Balance);
  32.         }
  33.  
  34.         public void ShowHistory()
  35.         {
  36.             Console.WriteLine($"Transaction history of account {Number}:");
  37.             foreach (string transaction in History)
  38.             {
  39.                 Console.WriteLine(transaction);
  40.             }
  41.         }
  42.     }
  43.  
  44.     public class Client
  45.     {
  46.         public string FullName { get; private set; }
  47.         public int Age { get; private set; }
  48.         public string Job { get; private set; }
  49.         private List<Account> Accounts { get; set; }
  50.  
  51.         public Client(string name, int age, string job)
  52.         {
  53.             this.FullName = name;
  54.             this.Age = age;
  55.             this.Job = job;
  56.             this.Accounts = new List<Account>();
  57.         }
  58.  
  59.         public void OpenAccount(string number)
  60.         {
  61.             Accounts.Add(new Account(number));
  62.         }
  63.  
  64.         public void CloseAccount(string number)
  65.         {
  66.             for (int i = 0; i < Accounts.Count; ++i)
  67.             {
  68.                 if (Accounts[i].Number == number)
  69.                 {
  70.                     Accounts.RemoveAt(i);
  71.                 }
  72.             }
  73.         }
  74.  
  75.         public void Transaction(string account, char mode, double money)
  76.         {
  77.             for (int i = 0; i < Accounts.Count; ++i)
  78.             {
  79.                 if (Accounts[i].Number == account)
  80.                 {
  81.                     if (mode == '+') Accounts[i].Add(money);
  82.                     else if (mode == '-') Accounts[i].Withdraw(money);
  83.                     break;
  84.                 }
  85.             }
  86.         }
  87.  
  88.         public void ShowAccountHistory(string number)
  89.         {
  90.             for (int i = 0; i < Accounts.Count; ++i)
  91.             {
  92.                 if (Accounts[i].Number == number)
  93.                 {
  94.                     Console.WriteLine($"Account {number} history:");
  95.                     Accounts[i].ShowHistory();
  96.                 }
  97.             }
  98.         }
  99.  
  100.         public void ShowAllAccounts()
  101.         {
  102.             Console.WriteLine($"All bank accounts of {this.FullName}: ");
  103.             foreach (Account acc in Accounts)
  104.             {
  105.                 Console.WriteLine($"Account {acc.Number} balance: {acc.Balance}");
  106.             }
  107.         }
  108.  
  109.         public void ShowClientInfo()
  110.         {
  111.             Console.WriteLine($"Public user info:\nFull name: {this.FullName}\nAge: {this.Age}\nJob: {this.Job}");
  112.         }
  113.     }
  114.  
  115.     class Program
  116.     {
  117.         static void Main(string[] args)
  118.         {
  119.             Console.WriteLine("Creating new user...");
  120.             Client testClient = new Client("Saveliy Savvovich Tvorozhochkin", 228, "Dungeon");
  121.             testClient.ShowClientInfo();
  122.  
  123.             Console.WriteLine($"\nOpening some accounts for {testClient.FullName}...");
  124.             testClient.OpenAccount("22813371488");
  125.             testClient.OpenAccount("00000000000");
  126.             testClient.OpenAccount("12345678910");
  127.             testClient.ShowAllAccounts();
  128.  
  129.             Console.WriteLine("\nSome test transactions coming...\n");
  130.             testClient.Transaction("22813371488", '+', 42424242);
  131.             testClient.Transaction("22813371488", '-', 42);
  132.             testClient.Transaction("00000000000", '+', 2281488);
  133.             testClient.Transaction("12345678910", '+', 228);
  134.             testClient.ShowAllAccounts();
  135.  
  136.             Console.WriteLine($"\nChecking history of an account 22813371488...");
  137.             testClient.ShowAccountHistory("22813371488");
  138.            
  139.             Console.WriteLine("\nClosing an account 22813371488...\n");
  140.             testClient.CloseAccount("22813371488");
  141.             testClient.ShowAllAccounts();
  142.         }
  143.     }
  144. }
  145.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement