Docsity

Defining a BankAccount Class and Testing it with a Client, Lab Reports of Data Structures and Algorithms

Toggle sidebar
bg1
Page 1 of 5
Lab: Defining Classes
Problem 1. Bank Account
Create a class named BankAccount.
The class should have private fields for:
id: int
balance: decimal
The class should also have public properties for:
Id: int
Balance: decimal
You should be able to use the class like this:
Solution
Create a new class and ensure proper naming
Problem 2. Bank Account Methods
Create a class BankAccount (you can use class from previous task)
The class should have private fields for:
id: int
balance: decimal
The class should also have properties for:
Id: int
Balance: decimal
Deposit(decimal amount): void
Withdraw(decimal amount): void
Override the method ToString().
You should be able to use the class like this:
pf3
pf4
pf5

Partial preview of the text

Download Defining a BankAccount Class and Testing it with a Client and more Lab Reports Data Structures and Algorithms in PDF only on Docsity!

Lab: Defining Classes

Problem 1. Bank Account

Create a class named BankAccount. The class should have private fields for:  id: intbalance: decimal The class should also have public properties for:  Id: intBalance: decimal You should be able to use the class like this:

Solution

Create a new class and ensure proper naming

Problem 2. Bank Account Methods

Create a class BankAccount (you can use class from previous task) The class should have private fields for:  id: intbalance: decimal The class should also have properties for:  Id: intBalance: decimalDeposit(decimal amount): voidWithdraw(decimal amount): void Override the method ToString(). You should be able to use the class like this:

Solution

Create a method Deposit(decimal amount) Create a method Withdraw(decimal amount) Override the method ToString() Problem 3. Test Client Create a test client that tests your BankAccount class. Support the following commands :  Create {Id}Deposit {Id} {amount}Withdraw {Id} {amount}Print {Id}End If you try to create an account with an existing Id, print "Account already exists". If you try to perform an operation on a non-existing account , print "Account does not exist". If you try to withdraw an amount larger than the balance, print "Insufficient balance". The Print command should print "Account ID{id}, balance {balance}". Round the balance to the second digit after the decimal separator.

Implement the rest of the commands following the same logic. Problem 4. Person Class Create a Person class. The class should have private fields for:  name: stringage: intaccounts: List The class should have constructors :  Person(string name, int age)Person(string name, int age, List accounts) The class should also have public methods for:  GetBalance(): decimal

Solution

Create the class as usual: Create a constructor with two parameters: Create a constructor with three parameters:

Create method GetBalance() : Optional: You can take advantage of constructor chaining :