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

CSharp OOP Basics Defining Classes Lab

The document provides instructions and examples for four programming exercises to define classes for a bank account, test client, and person as part of an object-oriented programming course. The exercises involve: 1. Defining a BankAccount class with ID and balance fields and properties. 2. Adding deposit and withdraw methods to the BankAccount class and overriding the ToString method. 3. Creating a test client that can create and perform operations on bank accounts, handling exceptions. 4. Defining a Person class with name, age, and list of bank accounts, including getter and constructors.

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

CSharp OOP Basics Defining Classes Lab

The document provides instructions and examples for four programming exercises to define classes for a bank account, test client, and person as part of an object-oriented programming course. The exercises involve: 1. Defining a BankAccount class with ID and balance fields and properties. 2. Adding deposit and withdraw methods to the BankAccount class and overriding the ToString method. 3. Creating a test client that can create and perform operations on bank accounts, handling exceptions. 4. Defining a Person class with name, age, and list of bank accounts, including getter and constructors.

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

Problems for exercises and homework for the "C# OOP Basics" course @ SoftUni.
You can check your solutions here: https://judge.softuni.bg/Contests/674/Defining-Classes-Lab

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 (softuni.org). 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 (softuni.org). 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 (softuni.org). 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 (softuni.org). 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 (softuni.org). This work is licensed under the CC-BY-NC-SA license.

Follow us: Page 5 of 5

You might also like