14.3k views
5 votes
Create an BankAccount Class that has an account id number (of type integer), balance (of type double), and rate (of type double) as private data. Define three overloaded constructors, one full constructor which brings in parameters to set all attrributes, one partial constructor which brings in one p[arameter to set account id and uses 0 for the balance and rate, and a non-arg constructor which uses default value for each attribute. Make sure to include error handlings, either selection or try/catch, to make sure the balance and rate are not negative.Add get and set methods for these three variables to work with these attributes. Create a deposit method and a withdraw method for user to deposit or withdraw user specified amount to or from the BankAccount.

In main method defined in another class file BankAccountTest, create 2-3 objects of BankAccount and work with their values and output them to the user to test your new class.

1 Answer

2 votes

Final answer:

To create a BankAccount class with the specified requirements, you need to define private variables for the account id, balance, and rate. You should also create three overloaded constructors, handle errors using try/catch blocks, and create get and set methods. Finally, you need to create deposit and withdraw methods to allow the user to interact with the BankAccount objects.

Step-by-step explanation:

To create a BankAccount class with the specified requirements, you can define private variables for the account id number, balance, and rate.

You need to create three overloaded constructors - one full constructor that takes in parameters to set all the attributes, one partial constructor that sets the account id and uses 0 for the balance and rate, and a non-arg constructor that uses default values for each attribute.

To handle errors, you can use try/catch blocks to ensure that the balance and rate are not negative. In the constructors, you can throw an exception if the values are negative.

For the get and set methods, create public methods that allow access to the private variables so that they can be used to work with the attributes.

To allow the user to deposit or withdraw a specified amount, create methods like 'deposit' and 'withdraw' that take in the amount as a parameter and update the balance accordingly.

In the main method of the BankAccountTest class, you can create objects of BankAccount and test their values by depositing or withdrawing amounts and printing the updated balance to the user.

User Sergey Zyuzin
by
8.4k points