package teammemes.tritonbudget.db;

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

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


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

public class HistoryDataSource extends BaseDataSource{

    public HistoryDataSource(Context context) {
        super(context);
    }

    public TranHistory createTransaction(TranHistory trans) {
        ContentValues values = new ContentValues();
        values.put(HistoryDB.MENUID, trans.getMenuId());
        values.put(HistoryDB.QUANTITY, trans.getQuantity());
        SimpleDateFormat dformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        values.put(HistoryDB.TDATE, dformat.format(trans.getTdate()));
        values.put(MenuDB.COSTCOL, trans.getCost());
        long insertId = database.insert(HistoryDB.Table_Transaction, null,
                values);
        Cursor cursor = database.query(HistoryDB.Table_Transaction,
                HistoryDB.allColumns, MenuDB.IDCOL + " = " + insertId, null,
                null, null, null);
        cursor.moveToFirst();
        TranHistory newTran = cursorToTransaction(cursor);
        cursor.close();
        return newTran;
    }

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

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

@@ -1821,24 +1821,1722 @@
 ions;%0A    %7D%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    private

Update HistoryDataSource.java