小型家庭收支系统

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();
    }
}

将收支记录保存到本地的收支系统

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值