Demo entry 4029732

stats (java)

   

Submitted by anonymous on Mar 14, 2016 at 02:20
Language: Java. Code size: 2.1 kB.

package com.cmps101w2016.ucsc.climbfinder;



import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import java.util.Calendar;
import java.util.Date;


public class Stats extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stats);
    }

    public void onStart() {
        SavedData data = SavedData.getSavedData(this);
        int highest_grade = getHighestGrade(data);
        int average_grade = getAverageGrade(data);
        long days_since_last = daysSinceLast(data);
        TextView a = (TextView) findViewById(R.id.highest_value);
        a.setText(highest_grade);
        TextView b = (TextView) findViewById(R.id.avg_value);
        b.setText(average_grade);
        TextView c = (TextView) findViewById(R.id.days_value);
        c.setText(Long.toString(days_since_last));
        super.onStart();

    }
    public void Back(View v){
        Intent intent = new Intent(this, Training.class);
        startActivity(intent);
    }

    private int getHighestGrade(SavedData data) {
        data.sortClimbsByGrade();
        int highest = 0;
        highest = data.getClimbs().get(0).getRawGrade();
        return highest;
    }

    private int getAverageGrade(SavedData data) {
        int average = 0;
        int sum = 0;
        int n = 0;
        for (; n < data.getClimbs().size(); n++) {
            sum += data.getClimbs().get(n).getRawGrade();
        }
        average = sum/n;
        return average;
    }

    private long daysSinceLast(SavedData data) {
        Calendar c = Calendar.getInstance();
        Date today = c.getTime();
        data.sortClimbsByDate();
        Day d = data.getClimbs().get(0).getDay();
        c.set(d.year, d.month, d.day);
        Date last = c.getTime();
        long diff = (today.getTime() - last.getTime()) / 1000 / 60 / 60 / 24;
        return diff;
    }
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).