import java.io.*;
import java.util.Scanner;
public class familycount {
public static void readTxt(){
try {
String pathname = "E:/家庭收支.txt";
File filename = new File(pathname);
InputStreamReader reader = new InputStreamReader(new FileInputStream(filename));
BufferedReader br = new BufferedReader(reader);
String[] str = new String[7];
int j = 0;
int i = j;
String line = "";
while ((line = br.readLine())!= null) {
str[i] = line;
i++;
if (i==7) {
i=0;
}
}
if (i!=0) {
for (int m = i+1; m < 7; m++) {
System.out.println(str[m]);
}
for (int l=0;l<i;l++) {
System.out.println(str[l]);
}
} else {
for (int n = 0;n<7;n++) {
System.out.println(str[n]);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static String read() {
String str = null;
try {
String pathname = "E:/家庭收支.txt";
File filename = new File(pathname);
InputStreamReader reader = new InputStreamReader(new FileInputStream(filename));
BufferedReader br = new BufferedReader(reader);
String line = "";
while ((line = br.readLine())!= null) {
str = line;
}
str = str.substring(3);
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
public static void writein(String str1,int i) {
try {
File writename = new File("E:/家庭收支.txt");
BufferedWriter out = new BufferedWriter(new FileWriter(writename,true));
out.write(str1+i+"\r\n");
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void back() {
System.out.println("输入“#”返回");
Scanner sc = new Scanner(System.in);
String back = sc.nextLine();
String str = new String("#");
if (back.equals(str)==true) {
choose();
}
}
public static int register() {
System.out.println("请输入日期");
Scanner scdata = new Scanner(System.in);
String Scdata = scdata.nextLine();
System.out.println("请输入收入/支出原因");
Scanner screas = new Scanner(System.in);
String Screas = screas.nextLine();
System.out.println("请输入金额");
Scanner scnum = new Scanner(System.in);
String Scnum = scnum.nextLine();
String reg = Scdata+Screas+Scnum;
int a = Integer.valueOf(Scnum);
try {
File writename = new File("E:/家庭收支.txt");
BufferedWriter out = new BufferedWriter(new FileWriter(writename,true));
out.write(reg+"\r\n");
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
return a;
}
public static void choose() {
System.out.println("请选择功能");
System.out.println("1.收支明细");
System.out.println("2.登记收入");
System.out.println("3.登记支出");
System.out.println("4.退出");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
switch (num) {
case 1:
readTxt();
back();
break;
case 2:
int total1 =Integer.valueOf(read());
int a = register();
total1 = total1 + a;
System.out.println("现有资金"+total1);
writein("总资金",total1);
back();
break;
case 3:
int total2 = Integer.valueOf(read());
int b = register();
total2 = total2 - b;
System.out.println("现有资金"+total2);
writein("总资金",total2);
back();
break;
case 4:
break;
}
}
public static void main(String[] args){
choose();
}
}
将收支记录保存到本地的收支系统