Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For the attached java code... Create a client class to test OrderProcessor. You will need to create a datagenerator for testing purpose. It is not

For the attached java code... Create a client class to test OrderProcessor. You will need to create a datagenerator for testing purpose. It is not mandatory but you may use a variation of Data Generator in TIJ pages 637 to 638.

****///////////////////////////////////////////////////JAVA CODE//////////////////////////////////////////////////////*****

abstract class Product { protected float price; protected String ID;

// return the price of a particular product abstract float price(); // return the order ID of a particular product abstract String ID(); }

//------------------------------------------------------------

class ComputerPart extends Product { public ComputerPart(String orderID, float p) { price = p; ID = orderID; }

public float price() { return price; } public String ID() { return ID; } }

class Motherboard extends ComputerPart { protected String manufacturer; public Motherboard(String ID, String mfg, float p) { super(ID, p); manufacturer = mfg; } public String getManufacturer() { return manufacturer; } }

class RAM extends ComputerPart { protected int size; protected String manufacturer; public RAM(String ID, String mfg, int size, float p) { super(ID, p); this.manufacturer = mfg; this.size = size; } public String getManufacturer() { return manufacturer; } }

class Drive extends ComputerPart { protected String type; protected int speed; public Drive(String ID, String type, int speed, float p) { super(ID, p); this.type = type; this.speed = speed; } public String getType() { return type; } public int getSpeed() { return speed; } }

class Peripheral extends Product { public Peripheral(String orderID, float p) { ID = orderID; price = p; } public float price() { return price; } public String ID() { return ID; } }

class Printer extends Peripheral { protected String model; public Printer(String ID, String model, float p) { super(ID, p); this.model = model; } public String getModel() { return model; } }

class Monitor extends Peripheral { protected String model; public Monitor(String ID, String model, float p) { super(ID, p); this.model = model; } public String getModel() { return model; } }

class Service extends Product { public Service(String orderID, float p) { price = p; ID = orderID; } public float price() { return price; } public String ID() { return ID; } }

class AssemblyService extends Service { String provider; public AssemblyService(String ID, String pv, float p) { super(ID, p); provider = pv; } public String getProvider() { return provider; } }

class DeliveryService extends Service { String courier; public DeliveryService(String ID, String c, float p) { super(ID, p); courier = c; } public String getCourier() { return courier; } }

//------------------------------------------------------- class Cheese extends Product { public Cheese(String orderID, float p) { price = p; ID = orderID; } public float price() { return price; } public String ID() { return ID; } }

class Cheddar extends Cheese { public Cheddar(String ID, float p) { super(ID, p); } } class Mozzarella extends Cheese { public Mozzarella(String ID, float p) { super(ID, p); } }

class Fruit extends Product { public Fruit(String orderID, float p) { price = p; ID = orderID; } public float price() { return price; } public String ID() { return ID; } } class Apple extends Fruit { public Apple(String ID, float p) { super(ID, p); } } class Orange extends Fruit { public Orange(String ID, float p) { super(ID, p); } }

//------------------------------------------------------- /** * Products class, used to hold the main method which calls the client class * to test OrderProcessor functionality */ public class Products { public static void main(String[] args) { ClientTest.testOrder(); ClientTest2.testOrder(); } }

//------------------------------------------------------- //GenericOrder acts as a collection of an arbitrary nnumber of Product types: class GenericOrder { public ArrayList order; //field to hold the ArrayList of Products (the order) public String ID; // field to hold the order ID

//GenericOrder constructor: public GenericOrder(String ID) { ArrayList order = new ArrayList(0); //creates an initial ArrayList of Products with a size of 0 (to be added to) this.order = order; //sets the order field to be the created ArrayList, upon the creation of a GenericOrder this.ID = ID; //sets the ID field to be the sent in ID, upon the creation of a GenericOrder }

//Generic class since several subclasses of Product can be added to the order: public void add(T item) { order.add(item); //adds item to order ArrayList }

public Product get(int element) { return order.get(element); //gets the element of the order as sent in numerically }

public String getID() { return ID; //returns the order ID }

public int size() { return order.size(); //returns the size of the order } }

//-----------------------------------------------------------------------------------------------------------

//ComputerPartyOrder extends GenericOrder, and can accept all types of Products(from each category): class ComputerPartyOrder extends GenericOrder { //ComputerPartyOrder constructor: public ComputerPartyOrder(String ID) { super(ID); //calls the superclass } }

//-----------------------------------------------------------------------------------------------------------

//ComputerOrder extends GenericOrder, however it can only accept specific types of Products: class ComputerOrder extends GenericOrder { //ComputerOrder constructor: public ComputerOrder(String ID) { super(ID); //calls the superclass }

public void add(Product item) { //overrides the add method in the superclass to only add the correct types of Product to the ComputerOrder: if(item.getClass().getSuperclass().equals(ComputerPart.class) || item.getClass().getSuperclass().equals(Peripheral.class) || item.getClass().getSuperclass().equals(Service.class)) { order.add(item); } else { //if an incorrect item is attempted to be added, this error message is printed and the item is not added: System.out.println("The item you are trying to purchase does not fit within the order type and cannot be added."); } } }

/-----------------------------------------------------------------------------------------------------------

//PartyTrayOrder extends GenericOrder, however it can only accept specific types of Products: class PartyTrayOrder extends GenericOrder { //PartyTrayOrder constructor: public PartyTrayOrder(String ID) { super(ID); //calls the superclass }

public void add(Product item) { //overrides the add method in the superclass to only add the correct types of Product to the PartyTrayOrder: if(item.getClass().getSuperclass().equals(Cheese.class) || item.getClass().getSuperclass().equals(Fruit.class) || item.getClass().getSuperclass().equals(Service.class)) { order.add(item); } else { //if an incorrect item is attempted to be added, this error message is printed and the item is not added: System.out.println("The item you are trying to purchase does not fit within the order type and cannot be added."); } } }

//-----------------------------------------------------------------------------------------------------------

//OrderProcessor uses a variety of organization methods to process and dispatch multiple orders class OrderProcessor { public ArrayList orderList; //field to hold the ArrayList of orders that are accepted

//OrderProcessor constructor: public OrderProcessor() { ArrayList orderList = new ArrayList(0); //creates an initial ArrayList of GenericOrder type with a size of 0 (to be added to) this.orderList = orderList; //sets the orderList field to be the created ArrayList, upon the creation of an OrderProcessor }

public void accept(T order) { orderList.add(order); //adds item to order ArrayList }

public ArrayList get() { return orderList; //returns the orderList ArrayList to be sent for further processing }

//the process method taked in all accepted orders and sorts them into more specific collections: public static void process(ArrayList orderList) { //ArrayLists to hold the sorted collections: ArrayList computerList = new ArrayList(); ArrayList peripheralList = new ArrayList(); ArrayList cheeseList = new ArrayList(); ArrayList fruitList = new ArrayList(); ArrayList serviceList = new ArrayList();

//for each order in orderList, each Product in the order is type checked and sorted accordingly: for(int i=0; i

//dispatching the orders to the appropriate dispatch methods: dispatchComputerParts(computerList); dispatchPeripherals(peripheralList); dispatchCheeses(cheeseList); dispatchFruits(fruitList); dispatchServices(serviceList); }

//Each dispatchXXX method produces output based on the type of items within the collection to be dispatched after type checking through instanceof:

public static void dispatchComputerParts(ArrayList order) { for(int i=0;i

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning Microsoft SQL Server 2012 Programming

Authors: Paul Atkinson, Robert Vieira

1st Edition

1118102282, 9781118102282

More Books

Students also viewed these Databases questions

Question

Define Decision making

Answered: 1 week ago

Question

What are the major social responsibilities of business managers ?

Answered: 1 week ago

Question

What are the skills of management ?

Answered: 1 week ago