Abdul Hayee
Member
- Joined
- Mar 31, 2020
- Messages
- 24
- Programming Experience
- Beginner
I am learning c# and following an example from the docs.microsoft.com, link is below
Classes and objects - Introduction to C# tutorial
code is below
The example code is working fine but i have a query. In the example List<Transaction> allTransaction = new List<Transaction>() has been created.
I want to know that all the transactions made by different clients will be add to one "allTransaction" List or every client have its own "allTransaction" list? e.g Client1.allTransaction, Client2.allTransaction.....
for example i have made two clients in this example Client1 and Client2. Where are all of their transactions storing?
in the end of Program there is a GetAccountHistory() function which is showing history of Clients. How it is recognizing which transaction are of which Client.
Kindly guide me
Thanks
Classes and objects - Introduction to C# tutorial
code is below
Main Program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace classes
{
class Program
{
static void Main(string[] args)
{
var Client1 = new BankAccounts("ABC", 1000);
var Client2 = new BankAccounts("XYZ", 500);
Console.WriteLine($"Account {Client1.Number} was created for {Client1.Owner} with {Client1.Balance} initial balance.");
Console.WriteLine($"Account {Client2.Number} was created for {Client2.Owner} with {Client2.Balance} initial balance.");
Console.WriteLine();
Client1.MakeWithdrawal(500, DateTime.Now, "Rent Payment");
Console.WriteLine(Client1.Balance);
Client1.MakeDeposit(100, DateTime.Now, "Friend paid me back");
Console.WriteLine(Client1.Balance);
Console.WriteLine();
Client2.MakeWithdrawal(500, DateTime.Now, "Fuel Charges");
Console.WriteLine(Client2.Balance);
Client2.MakeDeposit(100, DateTime.Now, "Bonus");
Console.WriteLine(Client2.Balance);
Console.WriteLine(Client1.GetAccountHistory());
Console.WriteLine();
Console.WriteLine(Client2.GetAccountHistory());
Console.ReadLine();
}
}
}
BankAccount Class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace classes
{
class BankAccounts
{
private static int accountNumberSeed = 1234567890;
public string Number { get; }
public string Owner { get; set; }
public decimal Balance
{
get
{
decimal balance = 0;
foreach(var item in allTransactions)
{
balance += item.Amount;
}
return balance;
}
}
public List<Transaction> allTransactions = new List<Transaction>();
public BankAccounts(string name, decimal initialBalance)
{
Number = accountNumberSeed.ToString();
accountNumberSeed++;
Owner = name;
MakeDeposit(initialBalance, DateTime.Now, "Initial Balance");
}
public void MakeDeposit(decimal amount, DateTime date, string note)
{
if(amount < 0)
{
throw new ArgumentOutOfRangeException(nameof(amount), "Amount of deposit must be positive");
}
var deposit = new Transaction(amount, date, note);
allTransactions.Add(deposit);
}
public void MakeWithdrawal(decimal amount, DateTime date, string note)
{
if(amount <=0)
{
throw new ArgumentOutOfRangeException(nameof(amount), "Amount of withdrawal must be positive");
}
if(Balance - amount < 0)
{
throw new InvalidOperationException("Not sufficient funds for this withdrawal");
}
var withdrawal = new Transaction(-amount, date, note);
allTransactions.Add(withdrawal);
}
public string GetAccountHistory()
{
var report = new System.Text.StringBuilder();
decimal balance = 0;
report.AppendLine("Date\t\tBalance\tNote");
foreach(var item in allTransactions)
{
balance += item.Amount;
report.AppendLine($"{item.Date.ToShortDateString()}\t{balance}\t{item.Notes}");
}
return report.ToString();
}
}
}
Transaction Class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace classes
{
public class Transaction
{
public decimal Amount { get; }
public DateTime Date { get; }
public string Notes { get; }
public Transaction(decimal amount, DateTime date, string note)
{
Amount = amount;
Date = date;
Notes = note;
}
}
}
The example code is working fine but i have a query. In the example List<Transaction> allTransaction = new List<Transaction>() has been created.
I want to know that all the transactions made by different clients will be add to one "allTransaction" List or every client have its own "allTransaction" list? e.g Client1.allTransaction, Client2.allTransaction.....
for example i have made two clients in this example Client1 and Client2. Where are all of their transactions storing?
in the end of Program there is a GetAccountHistory() function which is showing history of Clients. How it is recognizing which transaction are of which Client.
Kindly guide me
Thanks