Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package rasmus_kurstein.budgetapp;
- import android.support.v7.app.ActionBarActivity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.widget.Spinner;
- import android.widget.TextView;
- import java.util.ArrayList;
- import java.util.Calendar;
- public class Tendencies extends ActionBarActivity {
- ApplicationObject app;
- Spinner spinner;
- TextView monthlyAvg;
- TextView weeklyAvg;
- TextView dailyAvg;
- long avgDay;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_tendencies);
- app = (ApplicationObject) getApplicationContext();
- monthlyAvg = (TextView) findViewById(R.id.monthlyAvg);
- weeklyAvg = (TextView) findViewById(R.id.weeklyAvg);
- dailyAvg = (TextView) findViewById(R.id.dailyAvg);
- ArrayList<String> categories = new ArrayList();
- categories.add("Total");
- categories.add("Category 1");
- categories.add("Category 2");
- categories.add("Category 3");
- categories.add("Category 4");
- categories.add("Category 5");
- categories.add("Category 6");
- //drop-down menu from http://developer.android.com/guide/topics/ui/controls/spinner.html
- spinner = (Spinner) findViewById(R.id.spinner);
- // Create an ArrayAdapter using the string array and a default spinner layout
- ArrayAdapter<String> adapter = new ArrayAdapter(this,
- android.R.layout.simple_spinner_item, categories);
- // Specify the layout to use when the list of choices appears
- adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
- // Apply the adapter to the spinner
- spinner.setAdapter(adapter);
- /*
- if(daysPast()>0){
- avgDay=(long)totalSpent("Total")/daysPast();
- } else{
- avgDay=(long)totalSpent("Total");
- }
- long avgWeek = avgDay*7;
- long avgMonth = avgDay*365/12;
- dailyAvg.setText(""+avgDay);
- weeklyAvg.setText(""+avgWeek);
- monthlyAvg.setText(""+avgMonth);
- */
- // spinner action from http://stackoverflow.com/questions/1337424/android-spinner-get-the-selected-item-change-event
- spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
- @Override
- public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
- if(daysPast()>0){
- avgDay=(long)totalSpent(spinner.getSelectedItem().toString())/daysPast();
- } else{
- avgDay=(long)totalSpent(spinner.getSelectedItem().toString());
- }
- long avgWeek = avgDay*7;
- long avgMonth = avgDay*365/12;
- dailyAvg.setText(""+avgDay);
- weeklyAvg.setText(""+avgWeek);
- monthlyAvg.setText(""+avgMonth);
- }
- @Override
- public void onNothingSelected(AdapterView<?> parentView) {
- if(daysPast()>0){
- avgDay=(long)totalSpent("Total")/daysPast();
- } else{
- avgDay=(long)totalSpent("Total");
- }
- long avgWeek = avgDay*7;
- long avgMonth = avgDay*365/12;
- dailyAvg.setText(""+avgDay);
- weeklyAvg.setText(""+avgWeek);
- monthlyAvg.setText(""+avgMonth);
- }
- });
- }
- public long daysPast(){
- Transaction firstTrans = app.getTransactions().get(app.getTransactions().size()-1);
- Calendar calendar1 = Calendar.getInstance();
- Calendar calendar2 = Calendar.getInstance();
- calendar1.set(firstTrans.getYear(), (firstTrans.getMonth()-1), firstTrans.getDay());
- long milsecs1= calendar1.getTimeInMillis();
- long milsecs2 = calendar2.getTimeInMillis();
- long diff = milsecs2 - milsecs1;
- long ddays = diff / (24 * 60 * 60 * 1000);
- return ddays;
- }
- public int totalSpent(String category){
- int total = 0;
- ArrayList<Transaction> transactions = new ArrayList<Transaction>();
- int size = app.getTransactions().size();
- if (size>0) {
- for (int i = 0; i < size; i++) {
- Transaction trans = app.getTransactions().get(i);
- switch(category){
- case "Total":
- transactions.add(trans);
- break;
- case "Category 1":
- if (category==trans.getCategory()){
- transactions.add(trans);
- }
- case "Category 2":
- if (category==trans.getCategory()){
- transactions.add(trans);
- }
- case "Category 3":
- if (category==trans.getCategory()){
- transactions.add(trans);
- }
- case "Category 4":
- if (category==trans.getCategory()){
- transactions.add(trans);
- }
- case "Category 5":
- if (category==trans.getCategory()){
- transactions.add(trans);
- }
- case "Category 6":
- if (category==trans.getCategory()){
- transactions.add(trans);
- }
- }
- }
- }
- int size2 = transactions.size();
- if (size2>0) {
- for (int i = 0; i < size2; i++) {
- total = total+transactions.get(i).getAmount();
- }
- }
- return total;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement