0% found this document useful (0 votes)
41 views

Defining Classes Lab

The document describes problems to create BankAccount and Person classes with fields, properties, methods and constructors. It includes creating a test client to test the BankAccount class with commands like Create, Deposit, Withdraw and Print. The solutions involve creating the classes with the specified fields and members, adding validation checks to the test client and using collections like List and Dictionary to link accounts to persons and store existing accounts.

Uploaded by

Tấn Phong
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Defining Classes Lab

The document describes problems to create BankAccount and Person classes with fields, properties, methods and constructors. It includes creating a test client to test the BankAccount class with commands like Create, Deposit, Withdraw and Print. The solutions involve creating the classes with the specified fields and members, adding validation checks to the test client and using collections like List and Dictionary to link accounts to persons and store existing accounts.

Uploaded by

Tấn Phong
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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:

Page 1 of 5
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.

Page 2 of 5
Examples
Input Output
Create 1 Account already exists
Create 1 Insufficient balance
Deposit 1 20 Account ID1, balance 10.00
Withdraw 1 30
Withdraw 1 10
Print 1
End
Deposit 2 20 Account does not exist
Withdraw 2 30 Account does not exist
Print 2 Account does not exist
End

Solution
Create a Dictionary<int, BankAccount> to store existing accounts
Create the input loop:

Check the type of command and execute accordingly (optional: you can create a separate method for each
command)
Implement the Create command:

Page 3 of 5
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: string
 age: int
 accounts: List<BankAccount>
The class should have constructors:
 Person(string name, int age)
 Person(string name, int age, List<BankAccount> 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:

Page 4 of 5
Create method GetBalance():

Optional: You can take advantage of constructor chaining:

Page 5 of 5

You might also like