家庭收支记账程序

家庭收支记账程序

目标

模拟实现一个基于文本界面的《家庭记账软件》
掌握初步的编程技巧和调试技巧
灵活运用基础知识

需求

该程序能够记录家庭的收入,支出,并能够打印收支明细表

主界面:
在这里插入图片描述

 public static void interFace() {
        System.out.println("-------------家庭收支记账软件-------------");
        System.out.println("               1.收支明细");
        System.out.println("               2.登记收入");
        System.out.println("               3.登记支出");
        System.out.println("               4.退    出");
        System.out.println();
        System.out.println("请选择(1~4):");
    }

登记收入支出
在这里插入图片描述

public static void record() {
        for(int i = 0; i <= count; ++i) {
            for(int j = 0; j < 4; ++j) {
                if (i == 0) {
                    System.out.print(arr[i][j] + "\t\t");
                } else {
                    System.out.print(arr[i][j] + "\t\t  ");
                }
            }

            System.out.println();
        }

    }

public static void expenses() {
        System.out.println("支出金额:");
        int money = Utility.readNumber();
        System.out.println("支出原因:");
        String info= Utility.readString();
        storage("支出", money, info);
        System.out.println("添加成功!");
    }

一.源代码

package family;

import javax.sound.midi.MidiDevice;
import javax.sound.sampled.Line;

public class Test {
    static String[][] arr = new String[100][4];
    static int count = 0;
    static int sumNum = 10000;

    public static void main(String[] args) {
        init();
        boolean flag = true;

        while(flag) {
            interFace();
            char c = Utility.readMenuSelection();
            switch(c) {
                case '1':
                    System.out.println("-----------------当前收支明细记录-----------------");
                    record();
                    System.out.println("------------------------------------------------");
                    break;
                case '2':
                    income();
                    break;
                case '3':
                    expenses();
                    break;
                case '4':
                    System.out.println("确认是否退出(Y/N)?:");
                    char c1 = Utility.readConfirmSelection();
                    if (c1 == 'Y') {
                        flag = false;
                    }
            }
        }

    }

    public static void interFace() {
        System.out.println("-------------家庭收支记账软件-------------");
        System.out.println("               1.收支明细");
        System.out.println("               2.登记收入");
        System.out.println("               3.登记支出");
        System.out.println("               4.退    出");
        System.out.println();
        System.out.println("请选择(1~4):");
    }

    public static void income() {
        System.out.println("收入金额:");
        int money = Utility.readNumber();
        System.out.println("收入原因:");
        String info = Utility.readString();
        storage("收入", money, info);
        System.out.println("添加成功!");
    }

    public static void expenses() {
        System.out.println("支出金额:");
        int money = Utility.readNumber();
        System.out.println("支出原因:");
        String info= Utility.readString();
        storage("支出", money, info);
        System.out.println("添加成功!");
    }

    public static void record() {
        for(int i = 0; i <= count; ++i) {
            for(int j = 0; j < 4; ++j) {
                if (i == 0) {
                    System.out.print(arr[i][j] + "\t\t");
                } else {
                    System.out.print(arr[i][j] + "\t\t  ");
                }
            }

            System.out.println();
        }

    }

    public static void storage(String type, int money, String info) {
        count++;
        arr[count][0] = type;
        if (type == "收入") {
            arr[count][1] = sumNum + money + "";
            sumNum += money;
            arr[count][2] = "+" + money;
        }

        if (type == "支出") {
            arr[count][1] = sumNum - money + "";
            sumNum -= money;
            arr[count][2] = "-" + money;
        }

        arr[count][3] = info;
    }

    public static void init() {
        arr[count][0] = "收支";
        arr[count][1] = "账户总金额";
        arr[count][2] = "收支金额";
        arr[count][3] = "说明";
    }
}

二.工具类

package family;

import java.util.Scanner;

public class Utility {
    private static Scanner scanner;

    public Utility() {
    }

    public static char readMenuSelection() {
        while(true) {
            String str = readKeyBoard(1);
            char c = str.charAt(0);
            if (c == '1' || c == '2' || c == '3' || c == '4') {
                return c;
            }

            System.out.print("选择错误,请重新输入:");
        }
    }

    public static int readNumber() {
        while(true) {
            String str = readKeyBoard(4);

            try {
                int n = Integer.parseInt(str);
                return n;
            } catch (NumberFormatException var3) {
                System.out.print("数字输入错误,请重新输入:");
            }
        }
    }

    public static String readString() {
        String str = readKeyBoard(8);
        return str;
    }

    public static char readConfirmSelection() {
        while(true) {
            String str = readKeyBoard(1).toUpperCase();
            char c = str.charAt(0);
            if (c == 'Y' || c == 'N') {
                return c;
            }

            System.out.print("选择错误,请重新输入:");
        }
    }

    private static String readKeyBoard(int limit) {
        String line = "";

        while(scanner.hasNext()) {
            line = scanner.nextLine();
            if (line.length() >= 1 && line.length() <= limit) {
                break;
            }

            System.out.print("输入长度(不大于" + limit + ")错误,请重新输入:");
        }

        return line;
    }

    static {
        scanner = new Scanner(System.in);
    }
}

结果展示
在这里插入图片描述

学习总结

对前面知识体系的一个结合,灵活运用方法来减轻程序代码的复杂度,更好的区分每个模块的功能使整个代码不混乱。因为我没有及时在每个模块完成时去运行,导致最后关联太多很难找出错误所在,希望大家引以为戒。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值