Professional Documents
Culture Documents
LAB 1
Introduction to C#, .NET, console applications & File Handling
Objectives
•Learn to use OOP in C#
•Introduction to C# syntax
•Intoduction to FileHandling in C#
Tools Used
• Studio 2019 Community Version
BSCS 5-A Burhan Ahmed Satti 01-134172-065
Solution
Contents of Program.cs File
using System;
namespace VP_Lab_Task_0101
{
class Program
{
static void Main(string[] args)
{
Manager manager = new Manager();
manager.ReadCurrentAccount(manager.CreateCurrentAccount());
manager.WriteAccountsToFile();
manager.PrintAllAccounts();
}
}
}
Contents of Manager.cs File
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace VP_Lab_Task_0101
{
class Manager
{
ArrayList accountList = new ArrayList();
public ArrayList ListOfAccounts
{
get
{
return accountList;
}
}
public CurrentAccount CreateCurrentAccount()
{
InputOutputHandling InputOutputHandler = new InputOutputHandling();
CurrentAccount newCurrentAccount = InputOutputHandler.getCurrentAccountInformation();
accountList.Add(newCurrentAccount);
return newCurrentAccount;
}
public void ReadCurrentAccount(CurrentAccount newCurrentAccount)
{
InputOutputHandling InputOutputHandler = new InputOutputHandling();
InputOutputHandler.printAccountInformation(newCurrentAccount);
}
public void WriteAccountsToFile()
{
StreamWriter AccountsFileWriter = new StreamWriter("CurrentAccounts.txt", true);
AccountsFileWriter.WriteLine(currentObject.AccountNumber);
AccountsFileWriter.WriteLine(currentObject.AccountTitle);
AccountsFileWriter.WriteLine(currentObject.CNIC);
AccountsFileWriter.WriteLine(currentObject.Balance);
AccountsFileWriter.Flush();
}
AccountsFileWriter.Close();
}
public void ReadAccountsFromFile()
{
StreamReader AccountsFileReader = new StreamReader("CurrentAccounts.txt");
while(!AccountsFileReader.EndOfStream)
{
string AccountNumber = AccountsFileReader.ReadLine().ToString();
string AccountTitle = AccountsFileReader.ReadLine().ToString();
string CNIC = AccountsFileReader.ReadLine().ToString();
double Balance = double.Parse(AccountsFileReader.ReadLine());
namespace VP_Lab_Task_0101
{
class InputOutputHandling
{
public CurrentAccount getCurrentAccountInformation()
{
Console.WriteLine("----------------- You are creating a new Current Account ------------------");
}
}
}
Contents of Accounts.cs File
using System;
using System.Collections.Generic;
using System.Text;
namespace VP_Lab_Task_0101
{
abstract class Account
{
protected string accountNo;
protected string accountTitle;
protected string cnic;
protected double balance;
public Account(string AccountNo = "", string AccountTitle = "", string cnic = "", double Balance = 0)
{
this.accountNo = AccountNo;
this.accountTitle = AccountTitle;
this.cnic = cnic;
this.balance = Balance;
}
public string AccountNumber
{
set
{
accountNo = value;
}
get
{
return accountNo;
}
}
public string AccountTitle
{
set
{
accountTitle = value;
}
get
{
return accountTitle;
}
}
public string CNIC
{
set
{
cnic = value;
}
get
{
return cnic;
}
}
public double Balance
{
set
{
balance = value;
}
get
{
return balance;
}
}
virtual public void withdraw(double amount)
{
this.balance -= amount;
return;
}
virtual public void deposit(double amount)
{
this.balance += amount;
return;
}
virtual public string showData()
{
string accountData = "Account Title : " + AccountTitle.ToString() + "\n";
accountData += "Account Number : " + AccountNumber.ToString() + "\n";
accountData += "CNIC : " + CNIC.ToString() + "\n";
accountData += "Balance : " + Balance.ToString() + "\n";
return accountData;
}
}
}
Contents of CurrentAccount.cs File
using System;
using System.Collections.Generic;
using System.Text;
namespace VP_Lab_Task_0101
{
class CurrentAccount:Account
{
double withDrawalLimit;
public CurrentAccount(string AccountNo = "", string AccountTitle = "", string cnic = "", double Balance = 0, double withdrawal =
0):base(AccountNo, AccountTitle, cnic, Balance)
{
this.withDrawalLimit = withdrawal;
}
public override void deposit(double amount)
{
withDrawalLimit += balance * 0.05;
base.deposit(amount);
}
public override void withdraw(double amount)
{
withDrawalLimit -= balance * 0.05;
base.withdraw(amount);
}
public override string showData()
{
string accountInformation = base.showData();
accountInformation += "Withdrawal Limit: " + withDrawalLimit;
accountInformation = "------------------ Current Account Information ------------------\n" + accountInformation;
return accountInformation;
}
}
}
Contents of SavingAccount.cs File
using System;
using System.Collections.Generic;
using System.Text;
namespace VP_Lab_Task_0101
{
class SavingAccount:Account
{
double profitPercentage;
public SavingAccount(string AccountNo = "", string AccountTitle = "", string cnic = "", double Balance = 0, double ProfitPercentage= 0)
Result
Conclusion
Task is done.