You are on page 1of 8

VISUAL PROGRAMMING LAB

LAB 1
Introduction to C#, .NET, console applications & File Handling

Name : Burhan Ahmed


Enrollment No. : 01-134172-065
Class : BSCS 5-A
Subject : Visual Programming Lab
Lab Assistant : Sir Ali Mirza

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

Task No. 1.1


Simulate a Virtual Bank just like traditional Banking using OOP concepts and C# Console
Application. Implementation details are as follows;
a) Write an abstract class Account with attributes {accountNO, accountTitle, CNIC,
balance}
b) Inherit two classes from Account Class
1. CurrentAccount with an attribute {withdrawalLimit},
2. SavingAccount with an attribute {profitPercentage};
c) Program should allow user to open a new account with all information of an account holder
along with account type. (CRUD Operations)
d) There must be Manager class which will be performing CRUD Operations.
e) Enable an account holder to perform transactions such as withdrawal and deposit on his
account.
f) Deposit on CurrentAccount will increase withdrawalLimit with 5% of total deposit.
g) Deposit on SavingAccount will increase 2% profitPercentage.
h) Withdraw will do the reverse.
i) Validate your all inputs; specially for withdrawal and deposit.
j) Make sure you do not mix your user interface code (in this example Printing Information and
Input from console) with your other classes code.
k,l & g are optional and contains extra marks.
k) Store account information in text files
l) Restrict an account holder to withdraw up to Rs. 50,000 PKR worth of cash transaction per
day with total transactions limited to 5.
m) Design reports for transcations.

Visual Programming Lab Page 2 of 8


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);

foreach (Object i in ListOfAccounts)


{
CurrentAccount currentObject = i as CurrentAccount;

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());

Visual Programming Lab Page 3 of 8


BSCS 5-A Burhan Ahmed Satti 01-134172-065

CurrentAccount newCurrentAccount = new CurrentAccount(AccountNumber, AccountTitle, CNIC, Balance);


accountList.Add(newCurrentAccount);
}
AccountsFileReader.Close();
}

public void PrintAllAccounts()


{
this.ReadAccountsFromFile();
InputOutputHandling IO = new InputOutputHandling();
foreach(Object i in ListOfAccounts)
{
CurrentAccount account = i as CurrentAccount;
IO.printAccountInformation(account);
}
}
}
}
Contents of InputOutputHandling.cs File
using System;
using System.Collections.Generic;
using System.Text;

namespace VP_Lab_Task_0101
{
class InputOutputHandling
{
public CurrentAccount getCurrentAccountInformation()
{
Console.WriteLine("----------------- You are creating a new Current Account ------------------");

Console.Write("Input Account Number : ");


string AccountNumber = Console.ReadLine().ToString();

Console.Write("Input Title : ");


string title = Console.ReadLine().ToString();

Console.Write("Input CNIC : ");


string cnic= Console.ReadLine().ToString();

Console.Write("Input Balance : ");


double balance = double.Parse(Console.ReadLine());

Console.Write("Input Withdraw Limit : ");


double withdraw= double.Parse(Console.ReadLine());

return new CurrentAccount(AccountNumber, title, cnic, balance, withdraw);


}
public void printAccountInformation(CurrentAccount account)
{
Console.WriteLine(account.showData());
}
public SavingAccount getSavingAccountInformation()
{
Console.WriteLine("-------- You are creating a new Saving Account --------------");

Console.Write("Input Account Number : ");


string AccountNumber = Console.ReadLine().ToString();

Console.Write("Input Title : ");


string title = Console.ReadLine().ToString();

Console.Write("Input CNIC : ");


string cnic = Console.ReadLine().ToString();

Console.Write("Input Balance : ");


double balance = double.Parse(Console.ReadLine());

Console.Write("Input Profit Percentage : ");


double ProfitPercentage = double.Parse(Console.ReadLine());

return new SavingAccount(AccountNumber, title, cnic, balance, ProfitPercentage);


}
public void printAccountInformation(SavingAccount account)
{
Console.WriteLine(account.showData());

Visual Programming Lab Page 4 of 8


BSCS 5-A Burhan Ahmed Satti 01-134172-065

}
}
}
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;

Visual Programming Lab Page 5 of 8


BSCS 5-A Burhan Ahmed Satti 01-134172-065

}
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)

Visual Programming Lab Page 6 of 8


BSCS 5-A Burhan Ahmed Satti 01-134172-065

: base(AccountNo, AccountTitle, cnic, Balance)


{
this.profitPercentage = ProfitPercentage;
}
public override void deposit(double amount)
{
profitPercentage += profitPercentage * 0.02;
base.deposit(amount);
}
public override void withdraw(double amount)
{
profitPercentage -= profitPercentage * 0.02;
base.withdraw(amount);
}
public override string showData()
{
string accountInformation = base.showData();
accountInformation += "Profit Percentage : " + profitPercentage;
accountInformation = "------------------ Saving Account Information ------------------\n" + accountInformation;
return accountInformation;
}
}
}

Visual Programming Lab Page 7 of 8


BSCS 5-A Burhan Ahmed Satti 01-134172-065

Result

Conclusion
Task is done.

Visual Programming Lab Page 8 of 8

You might also like