Question: Use Java to solve the problem. There is a delivery service for Amazon, and each delivery person is represented by a DeliveryPersonnel object. Each delivery
Use Java to solve the problem.
There is a delivery service for Amazon, and each delivery person is represented by a DeliveryPersonnel object. Each delivery person is in charge of a certain list of deliveries. Each delivery is represented as a Delivery object. The DeliveryPersonnel object should be able to add Delivery objects as new delivery requests come in, and it should also be able to remove Delivery objects as it completes them. We have defined the following Delivery class:
class Delivery { // class variables private static int count; // total number of deliveries (static) private final int id; // ID of this delivery private final String name; // name of this delivery // constructor public Delivery(String n) { this.name = n; this.id = Delivery.count++; } // getter methods public int getId() { return this.id; } public String getName() { return this.name; } // equals method public boolean equals(Object o) { return o instanceof Delivery && this.id == ((Delivery)o).id; } } TASK: Create a class called DeliveryPersonnel that has the following properties:
- It should have an instance variable of type ArrayList
called deliveries that will contain the deliveries that need to be completed by this DeliveryPersonnel object - It must have a no-parameter constructor that should initialize the deliveries instance variable to an empty ArrayList
- It must have a no-parameter instance method called getNumDeliveries that returns the number of deliveries needed to be completed by this DeliveryPersonnel object (i.e., the number of elements in the deliveries instance variable)
- It must have an instance method called addDelivery that has a single parameter of type Delivery, and it should add the method argument to the deliveries instance variable. It should not return anything
- It must have an instance method called addDeliveries that has a single parameter of type Iterable
, and it should add all the Delivery objects contained in the argument to the deliveries instance variable. It should not return anything - It must have an instance method called removeDelivery that has a single parameter of type int, and it should remove the Delivery object that has the argument as its id from the deliveries instance variable (if such an object exists). If it successfully removes a Delivery, it should return the object that was removed; otherwise, it should return null
- For the entirety of this challenge, you can assume that you will never encounter duplicate IDs nor duplicate Delivery objects
HINT: You can use a for-each loop to iterate over the elements of any class that implements the Iterable interface
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
