2024/04/02(2024春季)
学习时长:1小时
代码行数:177行
博客总数:1篇
今天主要完成了个人作业的最后一部分,能力达标分析部分。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".AnalyseActivity"> <TextView android:text="@string/analyse" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textSize="40sp" /> <Space android:layout_width="wrap_content" android:layout_height="30dp"/> <TextView android:id="@+id/count_mon" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textSize="25sp" /> <Space android:layout_width="wrap_content" android:layout_height="20dp"/> <TextView android:id="@+id/count_mes" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textSize="25sp" /> <Space android:layout_width="wrap_content" android:layout_height="20dp"/> <TextView android:id="@+id/ave_reach" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textSize="25sp" /> </LinearLayout>
package com.example.myapplication; import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; import android.util.Log; import android.widget.TextView; import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import com.example.myapplication.dphelper.GoalsDbHelper; import com.example.myapplication.dphelper.RecordDbHelper; import com.example.myapplication.utils.JDBCUtils; import com.example.myapplication.utils.WorkingDaysCalculator; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.time.DayOfWeek; import java.time.Duration; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.temporal.TemporalAdjusters; public class AnalyseActivity extends AppCompatActivity { private RecordDbHelper recordDbHelper; private GoalsDbHelper goalsDbHelper; private SharedPreferences preferences; private String username; private TextView count_mon; private TextView count_mes; private TextView reach; private Integer days; private Float hours; private Integer count ; private long time;//打卡总时长 private long minute; private Float hour; // @Override // protected void onStart() { // super.onStart(); // recordDbHelper = RecordDbHelper.getInstance(this); // goalsDbHelper = GoalsDbHelper.getInstance(this); // recordDbHelper.openReadLink(); // recordDbHelper.openWriteLink(); // goalsDbHelper.openReadLink(); // goalsDbHelper.openWriteLink(); // Integer sum = recordDbHelper.selectmonth(username, LocalDate.now()); // System.out.println(sum); // count_mes.setText("坚持发表每日学习总结的天数:" + sum + "天"); // // // Integer sum_mon = goalsDbHelper.select(username, LocalDate.now()); // count_mon.setText("本月设置的目标总数为:" + sum_mon + "个"); // Float sum1=Float.valueOf(String.valueOf(sum_mon)); // // String anl = recordDbHelper.anly(username, LocalDate.now()); // String[] split = anl.split(":"); // Float num_fin= Float.valueOf(split[0]); // Float fin= (float) 0; // if(sum_mon==0) // { // if(split.length==1) // { // fin=0.0F; // } // else { // fin= 100.0F; // } // } // else { // System.out.println(num_fin+" "+sum1); // fin= (float) ((num_fin/sum1)*100.0); // } // String mes="你的达成度为:"+fin+"%"; // System.out.println(split[0]); // if(split.length==1) // { // mes+="\n你未完成任何任务,继续加油吧!"; // } // else { // mes+=("\n"+split[1]); // } // reach.setText(mes); // } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.activity_analyse); preferences = getSharedPreferences("user2", Context.MODE_PRIVATE); username = preferences.getString("username", null); init(); new Thread(new Runnable() { @Override public void run() { Connection connection=JDBCUtils.getConn(); LocalDate today=LocalDate.now(); LocalDate start_Date = today.with(TemporalAdjusters.firstDayOfMonth()); LocalDate end_Date = today.with(TemporalAdjusters.lastDayOfMonth()).plusDays(1); String sql = "select * from goals where username=? and start_date between ? and ?"; try { PreparedStatement ps=connection.prepareStatement(sql); ps.setString(1,username); ps.setString(2, String.valueOf(start_Date)); ps.setString(3,String.valueOf(end_Date)); ResultSet resultSet=ps.executeQuery(); while (resultSet.next()) { hours+=(Float.valueOf(resultSet.getString(5)))*5; } AnalyseActivity.this.runOnUiThread(new Runnable() { @Override public void run() { if(hours!=0) { count_mon.setText("学习目标:共打卡"+hours+"小时"); } else{ count_mon.setText("您未在本周设置学习计划"); } } }); } catch (SQLException ex) { throw new RuntimeException(ex); } } }).start(); new Thread(new Runnable() { @Override public void run() { Connection connection= JDBCUtils.getConn(); LocalDate today=LocalDate.now(); LocalDate start_Date = today.with(TemporalAdjusters.firstDayOfMonth()); LocalDate end_Date = today.with(TemporalAdjusters.lastDayOfMonth()).plusDays(1); String sql="select count(*) from record where username=? and start_time between ? and ?"; String sql2="select * from record where username=? and start_time between ? and ?"; try { PreparedStatement preparedStatement=connection.prepareStatement(sql); preparedStatement.setString(1,username); preparedStatement.setString(2, String.valueOf(start_Date)); preparedStatement.setString(3, String.valueOf(end_Date)); ResultSet resultSet=preparedStatement.executeQuery(); if(resultSet.next()) { days=resultSet.getInt(1); } String sql3 = "select * from goals where username=? and start_date =?"; LocalDate monday = today.with(DayOfWeek.MONDAY); preparedStatement=connection.prepareStatement(sql3); preparedStatement.setString(1,username); preparedStatement.setString(2, String.valueOf(monday)); resultSet=preparedStatement.executeQuery(); while (resultSet.next()) { hour=Float.valueOf(resultSet.getString(5)); } preparedStatement=connection.prepareStatement(sql2); preparedStatement.setString(1,username); preparedStatement.setString(2, String.valueOf(start_Date)); preparedStatement.setString(3, String.valueOf(end_Date)); resultSet=preparedStatement.executeQuery(); while (resultSet.next()) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); LocalDateTime start= LocalDateTime.parse(resultSet.getString(3).substring(0, 19), formatter); LocalDateTime end= LocalDateTime.parse(resultSet.getString(4).substring(0, 19), formatter); Duration dur= Duration.between(start, end ); minute = dur.toMinutes(); time+=minute; System.out.println(minute); if(minute>=hour*60) { count++; } } AnalyseActivity.this.runOnUiThread(new Runnable() { @Override public void run() { count_mes.setText("坚持发表每日学习总结的天数:" + days +"天\n"+ "满足每日打卡目标的天数为:"+count+"天\n"+"本月打卡总时长为:"+time+"分钟"); int day= WorkingDaysCalculator.getWorkingDaysOfMonth(); float week_check= ((float)count/(float)5)*100; Log.e("time", String.valueOf(time)); float week_sumtime=((float) time/(float)(hours*60))*100; reach.setText("本周打卡完成度为:"+week_check+"%\n"+"本月学习总时长完成度为:"+week_sumtime+"%"); } }); } catch (SQLException e) { throw new RuntimeException(e); } } }).start(); } private void init() { reach = findViewById(R.id.ave_reach); count_mon = findViewById(R.id.count_mon); count_mes = findViewById(R.id.count_mes); count=0; hours=0f; time=0; minute=0; hour=0f; } }