/
02 - OOP Intro - Exercise 3

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

  1. Create a new IntelliJ project and paste in the attached file Cutomer.java .

  2. 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.

  3. Create a CustomerApplication.java class with a main method. Create a couple of instances of Customer class and test how they work. Anything surprising?

  4. In the Customer class definition, declare another private instance variable to hold the customer's status as char , provide an accessor method - isHeld() - which returns true if the status is equal to 'H', otherwise false. Also provide getStatus() method that returns the char.

  5. 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’.

  6. 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

  1. Notice the method changeDetails() provided in the Customer class. In your main method add code to call the changeDetails() method to verify that the method changes the customer's name.

  2. 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 the Customer class and, where indicated by the comments, define a second changeDetails() method that takes a String argument and a char argument to change the customer's name and status, respectively.

  3. Back in CustomerApplication, add code to call the second changeDetails() method for one of your Customer variables, updating the name to some new value, and the status to 'I'.

 

Task 3 - Class variables and methods

  1. 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?

  2. 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 your main method.

  3. Define a class method setLastUsedAccountNumber() to allow the static variable to be reset to a new value.

  4. In the CustomerApplication class, call your class method to set a different last-used account number, i.e. 2000.

  5. After calling the class method, create two new customer objects - customer1 and customer2. Run the CustomerApplication file and make sure that your class method really works.