package teammemes.tritonbudget;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by andrewli on 2/26/17.
 */

public class TransactionDataSource {
    // Database fields
    private SQLiteDatabase database;
    private Database dbHelper;

    public TransactionDataSource(Context context) {
        dbHelper = new Database(context);
    }
    public Transaction createTransaction(Transaction trans) {
        ContentValues values = new ContentValues();
        values.put(TransactionDB.MENUID, trans.getMenuId());
        values.put(TransactionDB.QUANTITY, trans.getQuantity());
        SimpleDateFormat dformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        values.put(TransactionDB.TDATE, dformat.format(trans.gettDate()));
        values.put(MenuDB.COSTCOL, trans.getCost());
        long insertId = database.insert(MenuDB.Table_Menu, null,
                values);
        Cursor cursor = database.query(MenuDB.Table_Menu,
                TransactionDB.allColumns, MenuDB.IDCOL + " = " + insertId, null,
                null, null, null);
        cursor.moveToFirst();
        Transaction newComment = cursorToTransaction(cursor);
        cursor.close();
        return newComment;
    }

    public List<Transaction> getAllTransaction() {
        List<Transaction> transactions = new ArrayList<Transaction>();
        Cursor cursor = database.query(TransactionDB.Table_Transaction, TransactionDB.allColumns, null, null, null, null, null);
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            Transaction transaction = cursorToTransaction(cursor);
            transactions.add(transaction);
            cursor.moveToNext();
        }
        // make sure to close the cursor
        cursor.close();
        return transactions;
    }

    private Transaction cursorToTransaction(Cursor cursor) {
        Transaction transaction = new Transaction();
        try {
            transaction.setId(cursor.getInt(cursor.getColumnIndex(TransactionDB.ID)));
            transaction.setMenuId(cursor.getInt(cursor.getColumnIndex(TransactionDB.MENUID)));
            transaction.setQuantity(cursor.getInt(cursor.getColumnIndex(TransactionDB.QUANTITY)));
            SimpleDateFormat dformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            transaction.settDate(dformat.parse(cursor.getString(cursor.getColumnIndex(TransactionDB.TDATE))));
            transaction.setCost(cursor.getDouble(cursor.getColumnIndex(MenuDB.COSTCOL)));
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return transaction;
    }
}

@@ -1969,24 +1969,1757 @@
 ons;%0A    %7D%0A%0A
+    public double%5B%5D getTransactionByWeek() %7B%0A        double%5B%5D cost = new double%5B7%5D;%0A        List%3CTranHistory%3E histories= this.getAllTransaction();%0A        int currDay = Calendar.getInstance().get(Calendar.DAY_OF_YEAR);%0A        for(int i=0;i%3Chistories.size();i++)%0A        %7B%0A            Date day = histories.get(i).getTdate();%0A            Calendar cal = Calendar.getInstance();%0A            cal.setTime(day);%0A            int dayOfYear = cal.get(Calendar.DAY_OF_YEAR);%0A            if(0%3CcurrDay-dayOfYear && currDay-dayOfYear%3C8)%0A            %7B%0A                cost%5BcurrDay-dayOfYear-1%5D+=histories.get(i).getCost();%0A            %7D%0A        %7D%0A        return cost;%0A    %7D%0A    public double%5B%5D getTransactionByMonth() %7B%0A        double%5B%5D cost = new double%5B4%5D;%0A        List%3CTranHistory%3E histories= this.getAllTransaction();%0A        int currDay = Calendar.getInstance().get(Calendar.DAY_OF_YEAR);%0A        for(int i=0;i%3Chistories.size();i++)%0A        %7B%0A            Date day = histories.get(i).getTdate();%0A            Calendar cal = Calendar.getInstance();%0A            cal.setTime(day);%0A            int dayOfYear = cal.get(Calendar.DAY_OF_YEAR);%0A            if(0%3CcurrDay-dayOfYear && currDay-dayOfYear%3C8)%0A            %7B%0A                cost%5B0%5D+=histories.get(i).getCost();%0A            %7D%0A            if(7%3CcurrDay-dayOfYear && currDay-dayOfYear%3C15)%0A            %7B%0A                cost%5B1%5D+=histories.get(i).getCost();%0A            %7D%0A            if(14%3CcurrDay-dayOfYear && currDay-dayOfYear%3C22)%0A            %7B%0A                cost%5B2%5D+=histories.get(i).getCost();%0A            %7D%0A            if(21%3CcurrDay-dayOfYear && currDay-dayOfYear%3C29)%0A            %7B%0A                cost%5B3%5D+=histories.get(i).getCost();%0A            %7D%0A        %7D%0A        return cost;%0A    %7D%0A    %0A
     private 

Update TransactionDataSource.java