02 - OOP Intro - Exercise 3
Overview
In this exercise we will define overloaded constructors, instance methods, we will use class variables and class methods. We will be working with an existing class - Customer.java.
Task 1
Create a new IntelliJ project and paste in the attached file
Cutomer.java
.Notice that the existing constructor takes a single argument to set the customer’s name, and does not update the account number. Every customer will have their account set to zero.
Create a
CustomerApplication.java
class with a main method. Create a couple of instances of Customer class and test how they work. Anything surprising?In the
Customer
class definition, declare another private instance variable to hold the customer's status aschar
, provide an accessor method -isHeld()
- which returnstrue
if the status is equal to 'H', otherwisefalse
. Also providegetStatus()
method that returns thechar
.Define a second constructor that has two arguments to initialise the customer’s status as well as their name. If the value received is not ‘A’ or ‘I’ or ‘H’ (for active, inactive or on-hold), then set it to ‘H’. Remember that you can use
this()
to avoid duplicating code in multiple constructors. Modify the first constructor (which does not receive a char argument) to set the status to ‘A’.Save the project and fix any compiler errors before proceeding. Write up some code in your
main
method to prove that your code is working correctly.
Task 2
Notice the method
changeDetails()
provided in theCustomer
class. In yourmain
method add code to call thechangeDetails()
method to verify that the method changes the customer's name.Now you need to add another instance method to the
Customer
class to change both the name and the status. As you have already learnt, there's no need to specify different names for methods as long as they have different signatures (i.e. different numbers and/or types of parameters). This is know as method overloading. Examine theCustomer
class and, where indicated by the comments, define a secondchangeDetails()
method that takes aString
argument and achar
argument to change the customer's name and status, respectively.Back in
CustomerApplication
, add code to call the secondchangeDetails()
method for one of yourCustomer
variables, updating the name to some new value, and the status to 'I'.
Task 3 - Class variables and methods
Add a class variable to the
Customer
class to hold the last used account number. This needs to be a class variable because there is only one last-used number, and it's the same for all customers. You could initialise this variable to some reasonable value, i.e. 1000, in the variable declaration. What would be suitable type for this variable?Modify the constructors so that the last-used account number is incremented by 1, and assigned to the instance variable
accountNumber
. Bearing in mind that one constructor may be called from the other, you can implement this requirement by changing the code in just one of the constructors. Test your work in yourmain
method.Define a class method
setLastUsedAccountNumber()
to allow the static variable to be reset to a new value.In the
CustomerApplication
class, call your class method to set a different last-used account number, i.e. 2000.After calling the class method, create two new customer objects -
customer1
andcustomer2
. Run theCustomerApplication
file and make sure that your class method really works.