QuestionQuestion

Instructions:

Step 1. Copy and Compile
Download Lab.java and import it into your preferred IDE. The program should compile without errors.

Step 2. Implement the Iterator Interface
Modify CustomerContainer to implement the Iterator interface,

First, add an implements clause to the class heading. Since Iterator is a "typed" (generic) interface, the implements clause can specify the type of the object (Customer) that will be returned by the iterator. The type goes in angle brackets following the interface name, as in InterfaceName<Type>

Second, add skeletons for the necessary interface methods. A list of Iterator methods can be found in the Java core API documentation.
Leave the method definitions empty for this step of the exercise, with null or dummy return values if necessary.

Note: the Iterator interface is part of the java.util package, so an import will be necessary.

Be sure that the program compiles correctly before proceeding.

Step 2. Implement Iterator Methods
Complete each of the Iterator methods except remove( ), which should remain empty for now. You will need to add an index variable to support the iteration.

Also add to CustomerContainer the method public Iterator<Customer> iterator() to initialize the iteration. This method should set the index to zero and then return a self-reference.

Add statements to the main method to instantiate a Customer Iterator and use it to list all Customers.

Step 3. Implement the Comparable Interface
Modify Customer to implement the Comparable interface, based upon the customer number.

Use this interface to compare customer numbers in the main method so that the iteration will not list any customer with a number less than the one that preceded it. Hint: you will need to add variables for lastCustomer and nextCustomer; lastCustomer will be null initially.

Step 4. Implement an Enumeration
Add to the Customer class an enumerator for customer types. Place it in the "Static" area at the end.

Change the code in the Customer class, and in the main method of Lab to use the enumerator values, rather than integer constants.

Step 5. Complete the Remove Method (optional, if you have time)
Implement the remove() function of the Iterator interface by collapsing the array around the current entry (the one that was most recently returned by next() ). If the current entry is invalid, however, remove() should do nothing.

Add code to the main method to test your remove method.

Lab.java:

public class Lab {
    public static void main(String args[]) {
       CustomerContainer cc = new CustomerContainer(10);

       cc.addCustomer(new Customer(101, "Smith", 1));
       cc.addCustomer(new Customer(102, "Jones", 2));
       cc.addCustomer(new Customer(104, "Oblitey", 1));
       cc.addCustomer(new Customer(105, "McKombie", 2));
       cc.addCustomer(new Customer(106, "Baldwin", 2));
       cc.addCustomer(new Customer(107, "Burgher", 2));
       cc.addCustomer(new Customer(103, "Baker", 3));
       cc.addCustomer(new Customer(108, "Simmons", 3));
       cc.addCustomer(new Customer(109, "VanHorn", 2));
       cc.addCustomer(new Customer(140, "Zeeker", 2));
      
       System.out.println(cc.getCustomer(0).toString());
    }
}

class CustomerContainer {
    private Customer[] customers;
    private int count = 0;

    public CustomerContainer(int size) {
       customers = new Customer[size];
    }

    public void addCustomer(Customer c) {
       if (count < customers.length) {
            customers[count++] = c;
       }
       else {
            System.out.println("Error: array overflow");
            System.exit(0);
       }
    }
   
    public Customer getCustomer(int position) {
       return customers[position];
    }
}

class Customer {
    private int number;
    private String name;
    private int status; // 1=REGULAR, 2=DELINQUENT, 3=SPECIAL
   
    public Customer(int number, String name, int status) {
       this.number = number;
       this.name = name;
       this.status = status;
    }
   
    public String toString() {
       return ("Customer: " + number + " " + name + " " + statusName(status));
    }
   
    /* STATIC AREA */
    public static String statusName(int status) {
       switch(status) {
            case 1:
                return "Regular";
            case 2:
                return "Delinquent";
            case 3:
                return "Special";
            default:
                return "UNKNOWN";
       }
    }
}

Solution PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

class CustomerContainer implements Iterator<Customer>, Enumeration<Customer>{
    private Customer[] customers;
    private int count = 0;
    private int position = 0;
    private int idx = 0;
   
    public CustomerContainer(int size) {
       customers = new Customer[size];
    }

    public void addCustomer(Customer c) {
       if (count < customers.length) {
            customers[count++] = c;
       }
       else {
            System.out.println("Error: array overflow");
            System.exit(0);
       }
    }
   
    public Customer getCustomer(int position) {
       return customers[position];
    }

    @Override
    public boolean hasNext() {
       return position < count;
    }...
$20.00 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats