Main Class
package main;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;
import dao.AccountDao;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
LocalDateTime now = LocalDateTime.now();
String dateTime = now.format(DateTimeFormatter.ofPattern("yyyy년 MM월 dd일 HH시 mm분 ss초"));
System.out.println(dateTime);
AccountDao dao = new AccountDao();
while(true) {
System.out.println("*****************************");
System.out.println("*********가계부 ver1.0*********");
System.out.println("**** 1. 수입/지출 항목 입력 *****");
System.out.println("**** 2. 전체 데이터 열람 ********");
System.out.println("**** 3. 연/월별 데이터 검색 *****");
System.out.println("**** 4. 데이터 수정 ***********");
System.out.println("**** 5. 데이터 삭제 ***********");
System.out.println("**** 6. 메모 내용으로 검색 *****");
System.out.println("**** 7. 내역을 파일로 저장 *****");
System.out.println("**** 8. 파일을 콘솔에 출력 *****");
System.out.println("**** 9. 프로그램 종료 *********");
System.out.println("*****************************");
System.out.print("사용할 메뉴 번호 입력 >> ");
int select = sc.nextInt();
sc.nextLine();
switch (select) {
case 1:
dao.categorization();
break;
case 2:
dao.printAllData();
break;
case 3:
dao.search();
break;
case 4:
dao.editData();
break;
case 5:
dao.deleteDate();
break;
case 6:
dao.searchWithMemoVariable();
break;
case 7:
dao.saveAsTextFile();
break;
case 8:
try {
dao.readFileAsList();
} catch (IOException e) {
e.printStackTrace();
}
break;
case 9:
System.exit(0);
break;
default:
System.out.println("없는 메뉴입니다. 다시 시도하세요.");
break;
}
}
}
}
DTO
package dto;
import java.time.LocalDateTime;
public class AccountDto {
private LocalDateTime dateTime;
private String use;
private String classify;
private int money;
private String memo;
public AccountDto(String classify) {
this.classify = classify;
}
public AccountDto(LocalDateTime dateTime, String classify, String use, int money, String memo) {
this.dateTime = dateTime;
this.classify = classify;
this.use = use;
this.money = money;
this.memo = memo;
}
public LocalDateTime getDateTime() {
return dateTime;
}
public String getUse() {
return use;
}
public void setUse(String use) {
this.use = use;
}
public String getClassify() {
return classify;
}
public void setClassify(String classify) {
this.classify = classify;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public String getMemo() {
return memo;
}
public void setMemo(String memo) {
this.memo = memo;
}
@Override
public String toString() {
return dateTime + " 작성 " + classify + " " + use + " " + money + " " + memo;
}
}
DAO
package dao;
import dto.AccountDto;
import java.io.*;
import java.time.LocalDateTime;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class AccountDao {
Scanner sc = new Scanner(System.in);
private List<AccountDto> list = new LinkedList<AccountDto>() {{
add(0, new AccountDto(LocalDateTime.now(), "수입", "용돈 수령", 500000, "용돈 수령"));
add(0, new AccountDto(LocalDateTime.of(2021, 12, 18, 10, 15, 20), "수입", "빈병 팔기", 3800, "빈병팔기"));
add(1, new AccountDto(LocalDateTime.of(2021, 12, 19, 10, 10, 10), "지출", "용돈 삭제", 8000, "용돈 삭제"));
add(2, new AccountDto(LocalDateTime.of(2021, 11, 10, 20,25,48), "수입", "폐지줍기", 700, "폐지 많이 주웠다!!"));
add(2, new AccountDto(LocalDateTime.of(2021, 11, 17, 10,25,48), "지출", "떡볶이", 3000, "떡볶이"));
add(3, new AccountDto(LocalDateTime.of(2021, 11, 21, 22, 15,14), "지출", "야쿠르트", 400, "야쿠르트"));
}};
public void create(String classify) {
LocalDateTime now = LocalDateTime.now();
System.out.print("키워드를 입력하세요. >>> ");
String use = sc.next();
System.out.print("금액(숫자만)을 입력하세요. >> ");
int money = sc.nextInt();
sc.nextLine();
System.out.print("메모할 내용을 입력하세요. >> ");
String memo = sc.next();
list.add(new AccountDto(now, classify, use, money, memo));
}
public void categorization() {
System.out.print("수입 또는 지출여부를 입력하세요. >> ");
String classify = sc.next();
while (true) {
if (classify.equals("수입")) {
create(classify);
break;
} else if (classify.equals("지출")) {
create(classify);
break;
} else {
System.out.println("잘못된 입력입니다.");
create(classify);
break;
}
}
}
public void printAllData() {
for (int i = 0; i < list.size(); i++) {
System.out.println((i+1) + ". " + list.get(i).toString());
}
}
public void search() {
System.out.print("검색하려는 연도를 입력하세요.(숫자만) >> ");
int wantSearchYearly = sc.nextInt();
sc.nextLine();
System.out.print("검색하려는 월을 입력하세요.(숫자만) >> ");
int wantSearchMonthly = sc.nextInt();
sc.nextLine();
System.out.println("****************************");
List<AccountDto> income = new LinkedList<>();
List<AccountDto> expense = new LinkedList<>();
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getClassify().equals("수입")) {
income.add(list.get(i));
} else if (list.get(i).getClassify().equals("지출")) {
expense.add(list.get(i));
}
}
List<AccountDto> sortedIncomeList = new LinkedList<>();
for (int i = 0; i < income.size(); i++) {
int checkYear = income.get(i).getDateTime().getYear();
int checkMonth = income.get(i).getDateTime().getMonthValue();
if(checkYear == wantSearchYearly && checkMonth == wantSearchMonthly) {
sortedIncomeList.add(income.get(i));
}
}
for (AccountDto dto : sortedIncomeList) {
System.out.println(dto);
}
System.out.println("****************************");
List<AccountDto> sortedExceptionList = new LinkedList<>();
for (int i = 0; i < expense.size(); i++) {
int checkYear = expense.get(i).getDateTime().getYear();
int checkMonth = expense.get(i).getDateTime().getMonthValue();
if(checkYear == wantSearchYearly && checkMonth == wantSearchMonthly) {
sortedExceptionList.add(expense.get(i));
}
}
for (AccountDto dto : sortedExceptionList) {
System.out.println(dto);
}
int total = 0;
for (int i = 0; i < sortedIncomeList.size(); i++) {
total += sortedIncomeList.get(i).getMoney();
}
System.out.println("총 수입은 " + total + "원 입니다.");
total = 0;
for (int i = 0; i < sortedExceptionList.size(); i++) {
total += sortedExceptionList.get(i).getMoney();
}
System.out.println("총 지출은 " + total + "원 입니다.");
}
public void editData() {
System.out.print("수정하고자 하는 데이터의 번호를 입력하세요. >> ");
int wantEditNum = sc.nextInt() - 1;
sc.nextLine();
System.out.println("수정할 항목이 없으면 Enter키를 누르세요.");
AccountDto findAccountDto = list.get(wantEditNum);
System.out.print("수입 또는 지출여부 >> ");
String inputClassify = sc.nextLine();
System.out.print("카테고리 입력 >> ");
String inputUse = sc.nextLine();
System.out.print("금액 >> ");
String inputMoney = sc.nextLine();
System.out.print("메모 >> ");
String inputMemo = sc.nextLine();
AccountDto accountDto = new AccountDto(findAccountDto.getDateTime(),
inputClassify.isEmpty() ? findAccountDto.getClassify() : inputClassify,
inputUse.isEmpty() ? findAccountDto.getUse() : inputUse,
inputMoney.isEmpty() ? findAccountDto.getMoney() :Integer.parseInt(inputMoney),
inputMemo.isEmpty() ? findAccountDto.getMemo() : inputMemo);
list.set(wantEditNum, accountDto);
}
public void deleteDate() {
System.out.print("삭제하고자 하는 데이터의 번호를 입력하세요 >> ");
int wantDelNum = sc.nextInt() - 1;
sc.nextLine();
list.remove(list.get(wantDelNum));
}
public void searchWithMemoVariable() {
System.out.print("검색할 메모 내용을 입력하세요 >> ");
String searchMemo = sc.next();
for (AccountDto accountDto : list) {
if (accountDto.getMemo().contains(searchMemo)) {
System.out.println(accountDto);
}
}
}
public void saveAsTextFile() {
File newFile = new File("/Users/hwangduil/Desktop/Account.text");
try {
newFile.createNewFile();
} catch (Exception e) {
System.out.println("파일 생성에 실패하였습니다.");
}
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(newFile));
PrintWriter pw = new PrintWriter(bw);
StringBuilder result = new StringBuilder();
for(AccountDto accountDto: list) {
result.append(accountDto.getDateTime()).append(", ");
result.append(accountDto.getClassify()).append(", ");
result.append(accountDto.getMoney()).append(", ");
result.append(accountDto.getMemo()).append(", ");
result.append(accountDto.getUse()).append(" ");
result.append("\n");
}
pw.print(result);
pw.close();
bw.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
FileWriter fw = new FileWriter(newFile, true);
StringBuilder result = new StringBuilder();
for (int i = 0; i < list.size(); i++) {
for (AccountDto accountDto : list) {
result.append(accountDto.toString());
}
}
fw.write(result.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
public void readFileAsList() throws IOException {
File origin = new File("/Users/hwangduil/Desktop/Account.text");
BufferedReader br = new BufferedReader(new FileReader(origin));
String line;
while((line = br.readLine()) != null) {
System.out.println(line);
String[] s = line.split(", ");
AccountDto accountDto = new AccountDto(
LocalDateTime.parse(s[0]), s[1], s[3], Integer.parseInt(s[2]), s[4]
);
list.add(accountDto);
}
}
}