Lab: Defining Classes
Problems for exercises and homework for the "C# OOP Basics" course @ SoftUni.
You can check your solutions here: [Link]
Part I: Defining Classes
1. Define Bank Account Class
Create a class named BankAccount.
The class should have private fields for:
id: int
balance: double
The class should also have public properties for:
ID: int
Balance: double
You should be able to use the class like this:
Solution
Create a new class and ensure proper naming
2. Methods
Create a class BankAccount (you can use class from previous task)
The class should have private fields for:
id: int
balance: double
The class should also have properties for:
© Software University Foundation ([Link]). This work is licensed under the CC-BY-NC-SA license.
Follow us: Page 1 of 5
ID: int
Balance: double
Deposit(Double amount): void
Withdraw(Double amount): void
Override the method ToString().
You should be able to use the class like this:
Solution
Create a method Deposit(double amount)
Create a method Withdraw(double amount)
Override the method toString()
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
© Software University Foundation ([Link]). This work is licensed under the CC-BY-NC-SA license.
Follow us: Page 2 of 5
If you try to create an account with existing Id, print "Account already exists".
If you try to perform an operation on non-existing account with existing Id, 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.
Examples
Input Output
Create 1 Account already exists
Create 2 Insufficient balance
Deposit 1 20 Account ID1, balance 10.00
Withdraw 1 30
Withdraw 1 10
Print 1
End
Create 1 Account does not exist
Deposit 2 20 Account does not exist
Withdraw 2 30 Account does not exist
Print 2
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
© Software University Foundation ([Link]). This work is licensed under the CC-BY-NC-SA license.
Follow us: Page 3 of 5
Implement the rest of the commands following the same logic
4. Define 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(): double
Solution
Create the class as usual
Create a constructor with two parameters
© Software University Foundation ([Link]). This work is licensed under the CC-BY-NC-SA license.
Follow us: Page 4 of 5
Create a constructor with three parameters
Create method GetBalance()
Optional: You can take advantage of constructor chaining
© Software University Foundation ([Link]). This work is licensed under the CC-BY-NC-SA license.
Follow us: Page 5 of 5