Skip to content

Instantly share code, notes, and snippets.

@Jakemangan
Created July 7, 2018 18:14
Show Gist options
  • Save Jakemangan/3e4928ee82625eff3f3190218b63bce7 to your computer and use it in GitHub Desktop.
Save Jakemangan/3e4928ee82625eff3f3190218b63bce7 to your computer and use it in GitHub Desktop.
package core;
import java.util.ArrayList;
/*
* OrderDB class stores all of the orders created by OrderSystem
* within an ArrayList<BrickOrder> variable. The stored ArrayList
* can be returned to OrderSystem at any time for updating and
* information extraction.
*/
public class OrderDB {
private ArrayList<BrickOrder> database;
public OrderDB()
{
database = new ArrayList<BrickOrder>();
}
/*
* Returns the order that corresponds to the reference number passed.
*
* Correct order is found by iterating through DB entries and
* comparing refNum to reference number stored in orders. Once
* match is found, the order is returned.
*/
public BrickOrder getOrder(String refNum)
{
for(BrickOrder o : database)
{
if(refNum.equals(o.getReferenceNumber()))
return o;
}
return null;
}
/*
* Adds a passed order into the db arraylist.
*/
public void addOrder(BrickOrder o)
{
database.add(o);
}
/*
* Removes an order that matches the passed reference number
* from the order db.
*/
public void removeOrder(String refNum)
{
try
{
int index = -1;
for(BrickOrder o : database) //Iterate through the whole database
{
if(refNum.equals(o.getReferenceNumber()))
index = database.indexOf(o); //Save index of matched order - Prevents concurrency exception
}
database.remove(index);
System.out.println("Order with reference number " + refNum + " removed.");
}
catch(IndexOutOfBoundsException e) //Catch exception when no matching order found
{
System.out.println("No order with reference number: " + refNum + ".");
}
}
public ArrayList<BrickOrder> getOrderList()
{
return database;
}
public void clearDb()
{
database.clear();
System.out.println("DB cleared");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment