Created
July 7, 2018 18:13
-
-
Save Jakemangan/19a56426f12fed77fa2fbc9e5888b935 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package core; | |
import java.util.ArrayList; | |
import java.util.Scanner; | |
/* | |
* Design principles ideas to implement | |
* - Reduce responsibility of this class, move excess functionality into different classes | |
* -> Order builder class, instead of building orders within this class | |
* -> Printer class? | |
* -> Reference number generator class? | |
*/ | |
public class OrderSystem { | |
/* | |
* The amount of orders that have been created by the system in this session. | |
*/ | |
private int ordersCreated = 0; | |
/* | |
* The order db that stores the information of all created orders. | |
*/ | |
private static OrderDB db; | |
public OrderSystem() | |
{ | |
db = new OrderDB(); | |
} | |
/* | |
* Utilises OrderBuilder class to construct a BrickOrder object | |
* using the number of bricks on the order, as a parameter. | |
* | |
* Reference number is generated and is unique to the order that | |
* has been created. Reference number is returned by the method | |
* after the BrickOrder object has been created and added into | |
* the database of orders. | |
*/ | |
public String createOrder(int numBricks) | |
{ | |
OrderBuilder builder = new OrderBuilder(); | |
BrickOrder o = builder.constructOrder(ordersCreated, numBricks); | |
incOrdersCreated(); //To keep refNum unique to each order. | |
db.addOrder(o); | |
System.out.println("Order ref " + o.getReferenceNumber() + " for " + o.getNumberOfBricks() + " bricks, created and added to DB."); | |
return o.getReferenceNumber(); | |
} | |
public String updateOrder(String refNum, int newNumBricks) | |
{ | |
ArrayList<BrickOrder> orderList = db.getOrderList(); | |
for(BrickOrder o : orderList) | |
{ | |
if(refNum.equals(o.getReferenceNumber())) | |
{ | |
o.setNumberOfBricks(newNumBricks); | |
System.out.println("Order " + refNum + " details updated to " + newNumBricks + " bricks."); | |
return o.getReferenceNumber(); | |
} | |
} | |
return "No order with reference number " + refNum + " in database."; | |
} | |
/* | |
* Return a specific order from the database using a reference number | |
*/ | |
public BrickOrder getOrder(String refNum) | |
{ | |
return db.getOrder(refNum); | |
} | |
/* | |
* Return all of the orders in the database in the form of an ArrayList | |
*/ | |
public ArrayList<BrickOrder> getOrders() | |
{ | |
return db.getOrderList(); | |
} | |
/* | |
* Print the details of all orders present in the database to the console. | |
*/ | |
public void printAllOrders() | |
{ | |
ArrayList<BrickOrder> orderList = db.getOrderList(); | |
Printer pr = new Printer(); | |
pr.printOrderList(orderList); | |
} | |
/* | |
* Remove a specific order from the database using a reference number. | |
*/ | |
public void removeOrder(String refNum) | |
{ | |
db.removeOrder(refNum); | |
} | |
/* | |
* Return the number of orders created in this session. | |
*/ | |
public int getOrdersCreated() | |
{ | |
return ordersCreated; | |
} | |
/* | |
* Increments the value of ordersCreated | |
*/ | |
private void incOrdersCreated() | |
{ | |
ordersCreated++; | |
} | |
/* | |
* Returns a reference to the db. | |
*/ | |
public OrderDB getDb() | |
{ | |
return db; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment