import java.util.Comparator;

import database.Database;
import database.table.Table;
import database.table.column.Column;
import database.table.relation.Relation;
import database.table.row.Row;

public class ExpensesDatabase {
	Database db;
	Table<String> users;
	Table<Integer> invoices;
	Table<String > items;

	private void getDB() throws Exception {
		try {
			// create the database
			db = new Database("Expenses");
			// create the tables
			users = db.createTable("Users", String.class);
			invoices = db.createTable("Invoices", Integer.class);
			items = db.createTable("Items", String.class);

			// add columns to the tables
			users.addColumn("Address", String.class);
			Column invoiceCol = users.addColumn("Invoices", Integer[].class);
			invoices.addColumn("Total", Integer.class);
			Column itemCol = invoices.addColumn("Items", String[].class);
			items.addColumn("Expense", Integer.class);

			// create relations between the tables
			invoiceCol.createRelation(invoices, Relation.Type.oneToMany);
			itemCol.createRelation(items, Relation.Type.oneToMany);
		} catch (Exception e) {
			// maybe the database already exists, try to load it.
			db = Database.load("Expenses");
			users = db.getTable("Users", String.class);
			invoices = db.getTable("Invoices", Integer.class);
			items = db.getTable("Items", String.class);
		}
	}

	public void addItem(String itemName, Integer itemExpense) throws Exception {
		items.addRow(itemName).set(items.getColumn("Expense"), itemExpense);
	}

	// items must be in Items table
	public void addInvoice(String userName, Integer invoiceUID, String[] itemsNames) throws Exception {
		Row userRow = users.getRow(userName);
		if (userRow == null) {
			throw new RuntimeException("No such user: " + userName);
		}
		int total = 0;
		for (String item : itemsNames) {
			Integer itemExpense = items.getRow(item).get(items.getColumn("Expense"));
			total += itemExpense;
		}
		invoices.addRow(invoiceUID).set(items.getColumn("Total"), total)
			.set(items.getColumn("Items"), itemsNames);

		Integer[] currentInvoices = userRow.get(users.getColumn("Invoices"));
		Integer[] newInvoices = new Integer[currentInvoices.length + 1];
		System.arraycopy(currentInvoices, 0, newInvoices, 0, currentInvoices.length);
		newInvoices[currentInvoices.length] = invoiceUID;
		userRow.set(users.getColumn("Invoices"), newInvoices);
	}

	public void addUser(String name, String address, Integer[] invoices) throws Exception {
		users.addRow(name).set(users.getColumn("Address"), address).set(users.getColumn("Invoices"), invoices);
	}

	public void delteUser(String userName, boolean removeInvoices) throws Exception {
		if (removeInvoices) {
			Integer[] uids = users.getRow(userName).get(users.getColumn("Invoices"));
			for (Integer invoiceId : uids) {
				this.invoices.removeRow(invoiceId);
			}
		}
		users.removeRow(userName);
	}

	public void deleteInvoice(Integer invoiceUID) throws Exception {
		invoices.removeRow(invoiceUID);
	}

	public void deleteItem(String itemName) throws Exception {
		items.removeRow(itemName);
	}
}

@@ -195,16 +195,22 @@
 %0Apublic 
+final 
 class Ex
@@ -227,16 +227,23 @@
 base %7B%0A%09
+static 
 Database
@@ -248,16 +248,23 @@
 se db;%0A%09
+static 
 Table%3CSt
@@ -273,24 +273,31 @@
 ng%3E users;%0A%09
+static 
 Table%3CIntege
@@ -310,16 +310,23 @@
 oices;%0A%09
+static 
 Table%3CSt
@@ -345,45 +345,199 @@
 ;%0A%0A%09
-private void getDB() throws Exception
+// prevent class from being instantiable using reflection.%0A%09private ExpensesDatabase() %7B%0A%09%09throw new RuntimeException(%22ExpensesDatabase not instantiable%22);%0A%09%7D%0A%0A%09// initialize database%0A%09static
  %7B%0A%09
@@ -1282,16 +1282,26 @@
 on e) %7B%0A
+%09%09%09try %7B%0A%09
 %09%09%09// ma
@@ -1349,16 +1349,17 @@
 oad it.%0A
+%09
 %09%09%09db = 
@@ -1384,24 +1384,25 @@
 enses%22);%0A%09%09%09
+%09
 users = db.g
@@ -1425,32 +1425,33 @@
  String.class);%0A
+%09
 %09%09%09invoices = db
@@ -1480,32 +1480,33 @@
 Integer.class);%0A
+%09
 %09%09%09items = db.ge
@@ -1536,16 +1536,96 @@
 class);%0A
+%09%09%09%7D catch (Exception e2) %7B%0A%09%09%09%09e2.printStackTrace();%0A%09%09%09%09System.exit(-1);%0A%09%09%09%7D%0A
 %09%09%7D%0A%09%7D%0A%0A
@@ -1624,32 +1624,39 @@
 %09%09%7D%0A%09%7D%0A%0A%09public 
+static 
 void addItem(Str
@@ -1817,32 +1817,39 @@
 s table%0A%09public 
+static 
 void addInvoice(
@@ -2663,24 +2663,31 @@
 %09%7D%0A%0A%09public 
+static 
 void addUser
@@ -2865,32 +2865,39 @@
 s);%0A%09%7D%0A%0A%09public 
+static 
 void delteUser(S
@@ -3100,13 +3100,8 @@
 %09%09%09%09
-this.
 invo
@@ -3169,32 +3169,39 @@
 e);%0A%09%7D%0A%0A%09public 
+static 
 void deleteInvoi
@@ -3288,16 +3288,23 @@
 %09public 
+static 
 void del

Make ExpensesDatabase final and "static".