家庭收支记账软件

需求

目标

模拟实现一个基于文本界面的《家庭记账软件》

需求

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

实现

第一次实现版

package com.oceanstar;

import java.util.Scanner;

public class FamilyAccount {
    private static int baseMoney = 10000;
    private static String datails = "";
    private static Scanner scanner = new Scanner(System.in);

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


    public static  void income(){
        System.out.print("本次收入金额:");
        int t = scanner.nextInt();
        if (t < 0){
            System.out.println("请输入一个正整数");
            return;
        }

        baseMoney = baseMoney + t;
        System.out.print("本次收入说明:");
        String s = scanner.next();
        datails = datails + "收入\t" + baseMoney + "\t" + t + "\t" + s + "\n";
    }
    public static  void pay(){
        System.out.print("本次支出金额:");
        int t = scanner.nextInt();
        if (t < 0){
            System.out.println("请输入一个正整数");
            return;
        }

        baseMoney = baseMoney - t;
        System.out.print("本次支出说明:");
        String s = scanner.next();
        datails = datails + "支出\t" + baseMoney + "\t" + t + "\t" + s + "\n";
    }

    public static void showdetails(){
        System.out.println("-----------------当前收支明细记录-----------------");
        System.out.println("收支\t账户金额\t收支金额\t说明");
        System.out.println(datails);
    }



    public static void main(String[] args) {
        while (true){
            mainMenu();
            Scanner scanner = new Scanner(System.in);
            int type = scanner.nextInt();
            if (type == 4){
                break;
            }else if (type == 1){
                showdetails();
            }else if (type == 2){
                income();
            }else if (type == 3){
                pay();
            }
        }
    }
}

评价:好混乱,而且没有错误处理机制:比如nextInt()只能输入int类型的数据,如果不小心输入字符串就会出现异常

改进:以后写程序时注意尽量不要把输入提示和操作混合再一起

第二版

package com.oceanstar;

import java.util.Scanner;

public class FamilyAccount {

    private static Scanner scanner = new Scanner(System.in);

    // 一定能得到n位数
    public static String readKeyBoard(int limit) {
        String line = "";

        while (scanner.hasNext()) {
            line = scanner.nextLine();
            if (line.length() < 1 || line.length() > limit) {
                System.out.print("输入长度(不大于" + limit + ")错误,请重新输入:");
                continue;
            }
            break;
        }

        return line;
    }

    public static String readString(){
        // 因为这个函数得到的已经是小于等于8长度的字符串了,所以不需要处理,直接返回即可
        return readKeyBoard(8);  // 各个函数一定是相对封闭的,写函数的时候要确保这个函数独立正确
    }
    // 直到输入正确才跳出循环: 这里它确保了输入正确
    private static char readMenuSelection(){
        char c;
        for (; ; ) {
            String str = readKeyBoard(1);
            c = str.charAt(0);
            if (c != '1' && c != '2' && c != '3' && c != '4') {
                System.out.print("选择错误,请重新输入:");
            } else
                break;
        }
        return c;
    }

    // 确保能够得到'Y', 'y', 'N', 'n'
    private static char readConfirmSelection(){
        char c;
        for (; ; ) {
            String str = readKeyBoard(1);
            c = str.charAt(0);
            if (c != 'Y' && c != 'y' && c != 'N' && c != 'n') {
                System.out.print("选择错误,请重新输入:");
            } else
                break;
        }
        return c;
    }

    // 直到输入正确才能跳出循环
    private static int readNumber(){
        int i = 0;
        for (; ; ) {
            String str = readKeyBoard(4);

            try {
                i = Integer.parseInt(str);
                if (i < 0){
                    throw new Exception("必须输入一个正整数");
                }
                break;
            }catch (Exception e){
                System.out.print("必须输入一个正整数:");
            }
        }
        return i;
    }


    public static void main(String[] args) {
        int balance = 10000;
        String details = "";
        while (true){
            System.out.print("-----------------家庭收支记账软件-----------------\n" +
                    "1 收支明细\n" +
                    "2 登记收入\n" +
                    "3 登记支出\n" +
                    "4 退 出\n" +
                    "请选择(1-4):");
            char c = readMenuSelection(); // 工具一定是正确的输出
            switch (c){
                case '1':
                    System.out.println("-----------------当前收支明细记录-----------------");
                    System.out.println("收支\t账户金额\t收支金额\t说明");
                    System.out.println(details);
                    System.out.println("---------------------------------------------");
                    break;
                case '2':
                    System.out.println("----------------------本次收入-----------------------");
                    System.out.print("本次收入金额:");
                    int t = readNumber();// 工具得到的数一定是正整数
                    balance = balance + t;
                    System.out.print("本次收入说明:");
                    String s = readString();
                    details = details + "收入\t" + balance + "\t" + t + "\t" + s + "\n";
                    System.out.println("---------------------登记完成-------------------");
                    break;
                case '3':
                    System.out.println("----------------------本次支出-----------------------");
                    System.out.print("本次支出金额:");
                    int t1 = readNumber();// 工具得到的数一定是正整数
                    if (t1 > balance){
                        System.out.println("余额不足");
                        break;
                    }
                    balance = balance - t1;
                    System.out.print("本次支出说明:");
                    String s1 = readString();
                    details = details + "支出\t" + balance + "\t" + t1 + "\t" + s1 + "\n";
                    System.out.println("---------------------登记完成-------------------");
                    break;
                case '4':
                    System.out.print("确认是否退出(Y/N):");
                    char d = Character.toLowerCase(readConfirmSelection());
                    if (d == 'y'){
                        return;
                    }
                    break;
            }
        }
    }
}

最大感触:
*写程序时注意尽量不要把输入提示和操作混合再一起

  • 尽量拦住不正确的操作
  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值