Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion app/src/main/java/com/example/app/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ protected void onCreate(Bundle savedInstanceState) {
updateSavingTitle();

// 절약 목표 클릭 시 텍스트 수정 붛가능
findViewById(R.id.end_image).setOnClickListener(view -> showConfirmationDialog());
findViewById(R.id.end_image).setOnClickListener(view -> {
saveDataToDatabase();
showConfirmationDialog(); // 기존 리셋 다이얼로그 호출
});


// 버튼 리스너 설정
Expand Down Expand Up @@ -238,5 +241,35 @@ private void resetFields() {
// 다른 리셋 로직 추가 가능
Toast.makeText(this, "리셋되었습니다.", Toast.LENGTH_SHORT).show();
}

// 데이터를 데이터베이스에 저장하는 메서드
private void saveDataToDatabase() {
SavingsDatabaseHelper dbHelper = new SavingsDatabaseHelper(this);

// 절약 목표 데이터 저장
String goalName = savingPurpose.getText().toString();
String goalDate = inputGoalDate.getText().toString();
int goalAmountInt = goalAmount.isEmpty() ? 0 : Integer.parseInt(goalAmount);

long savingId = dbHelper.addSavingsGoal(goalName, goalDate, goalAmountInt);

// 소비 내역 저장
for (Map.Entry<String, StringBuilder> entry : expenseDetails.entrySet()) {
String category = entry.getKey();
String[] expenses = entry.getValue().toString().split("\n");

for (String expense : expenses) {
if (expense.isEmpty()) continue;
String[] parts = expense.split(": ");
String subCategory = parts[0];
int price = Integer.parseInt(parts[1].replace(" 원", ""));

String currentDate = new SimpleDateFormat("yyyyMMdd").format(new Date());
dbHelper.addExpense((int) savingId, category + " - " + subCategory, price, currentDate);
}
}

Toast.makeText(this, "데이터가 저장되었습니다!", Toast.LENGTH_SHORT).show();
}
}

9 changes: 9 additions & 0 deletions app/src/main/java/com/example/app/SavingsDatabaseHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,13 @@ public int getSavingsGoalsCount() {
}
return count;
}

public void updateTodayTotalDirect(int savingId, int totalExpense) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_TODAY_TOTAL, totalExpense);
db.update(TABLE_SAVINGS, values, COLUMN_SAVING_ID + "=?", new String[]{String.valueOf(savingId)});
db.close();
}

}
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
package com.example.app.savinglist;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;

import com.example.app.R;
import com.example.app.SavingsDatabaseHelper;

import java.util.List;

public class SavingGoalAdapter extends RecyclerView.Adapter<SavingGoalAdapter.ViewHolder> {

private Context context;
private List<SavingGoal> savingGoals;
private SavingsDatabaseHelper dbHelper;

public SavingGoalAdapter(Context context, List<SavingGoal> savingGoals) {
// 생성자에서 dbHelper 주입
public SavingGoalAdapter(Context context, List<SavingGoal> savingGoals, SavingsDatabaseHelper dbHelper) {
this.context = context;
this.savingGoals = savingGoals;
this.dbHelper = dbHelper;
}



@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
Expand All @@ -33,8 +42,49 @@ public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
SavingGoal goal = savingGoals.get(position);
String goalName = goal.getGoalName();
holder.textGoalName.setText(goalName);

holder.textGoalName.setText(goal.getGoalName());

holder.itemView.setOnClickListener(view -> {
showGoalDetailsPopup(goal.getId());
});
}

// 팝업 다이얼로그를 표시하는 메서드
private void showGoalDetailsPopup(int savingId) {
// 데이터베이스에서 세부 정보를 가져옵니다.
Cursor cursor = dbHelper.getSavingsGoal(savingId);

if (cursor == null || !cursor.moveToFirst()) {
Toast.makeText(context, "데이터를 찾을 수 없습니다.", Toast.LENGTH_SHORT).show();
return;
}

// 데이터 가져오기
String title = "<< 나의 " + cursor.getInt(cursor.getColumnIndex("saving_id")) + "번째 절약 >>";
String amount = cursor.getString(cursor.getColumnIndex("goal_amount")) + " 원";
String date = cursor.getString(cursor.getColumnIndex("goal_date"));
String total = cursor.getString(cursor.getColumnIndex("today_total")) + " 원";

cursor.close();

// 팝업 생성
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(title);

// 팝업 내용을 문자열로 구성
StringBuilder message = new StringBuilder();
message.append("목표 금액: ").append(amount).append("\n");
message.append("목표 날짜: ").append(date).append("\n");
message.append("총 소비 금액: ").append(total);

builder.setMessage(message.toString());

// 확인 버튼 추가
builder.setPositiveButton("닫기", (dialog, which) -> dialog.dismiss());

// 팝업 표시
builder.create().show();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,34 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_saving_list);

// UI 초기화
recyclerView = findViewById(R.id.recyclerView_savings);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
totalSavingsCountTextView = findViewById(R.id.total_savings_count);

// Adapter 초기화
dbHelper = new SavingsDatabaseHelper(this);
adapter = new SavingGoalAdapter(this, savingGoalList, dbHelper);
recyclerView.setAdapter(adapter);


// 절약 목표 가져오기
savingGoalList = fetchSavingGoalsFromDatabase();

// 총 절약 기록 수 표시
int totalGoalsCount = dbHelper.getSavingsGoalsCount();
totalSavingsCountTextView.setText("총 절약 기록 수: " + totalGoalsCount);

adapter = new SavingGoalAdapter(this, savingGoalList);
// RecyclerView Adapter 설정
adapter = new SavingGoalAdapter(this, savingGoalList, dbHelper);
recyclerView.setAdapter(adapter);

// 상단 로고 클릭 시 메인 화면으로 이동
logImageView = findViewById(R.id.now_image);
logImageView.setOnClickListener(view -> {
Intent intent = new Intent(SavingListActivity.this, MainActivity.class);
startActivity(intent);
});

}

private List<SavingGoal> fetchSavingGoalsFromDatabase() {
Expand All @@ -61,7 +70,7 @@ private List<SavingGoal> fetchSavingGoalsFromDatabase() {
int id = cursor.getInt(cursor.getColumnIndex("saving_id"));
String goalName = cursor.getString(cursor.getColumnIndex("goal_name"));

goals.add(0, new SavingGoal(id, goalName));
goals.add(new SavingGoal(id, goalName));
}
cursor.close();
}
Expand Down