项目一 Java基础 家庭记账系统

  1. 该项目来自bilibili尚硅谷,Java培训课程的一个Java基础项目。里面包含了所有的java基础的知识点。

  1. 以下是尚硅谷原创项目。共有两个类,一个主类FamilyAccountTm,一个工具类UtiltyTm,将数据都封装进了UtiltyTm中。

package com.company.project1;

public class FamilyAccountTm {
        public static void main(String[] args) {
            String details = "收支\t账户金额\t收支金额\t说    明\n";
            int balance = 10000;

            boolean loopFlag = true;
            do {
                System.out.println("\n-----------------家庭收支记账软件-----------------\n");
                System.out.println("                   1 收支明细");
                System.out.println("                   2 登记收入");
                System.out.println("                   3 登记支出");
                System.out.println("                   4 退    出\n");
                System.out.print("                   请选择(1-4):");

                char key = UtilityTm.readMenuSelection();
                System.out.println();
                switch (key) {
                    case '1':
                        System.out.println("-----------------当前收支明细记录-----------------");
                        System.out.println(details);
                        System.out.println("--------------------------------------------------");
                        break;
                    case '2':
                        System.out.print("本次收入金额:");
                        int amount1 = UtilityTm.readNumber();
                        System.out.print("本次收入说明:");
                        String desc1 = UtilityTm.readString();

                        balance += amount1;
                        details += "收入\t" + balance + "\t\t" +
                                amount1 + "\t\t" + desc1 + "\n";
                        System.out.println("---------------------登记完成---------------------");
                        break;
                    case '3':
                        System.out.print("本次支出金额:");
                        int amount2 = UtilityTm.readNumber();
                        System.out.print("本次支出说明:");
                        String desc2 = UtilityTm.readString();

                        balance -= amount2;
                        details += "支出\t" + balance + "\t\t" +
                                amount2 + "\t\t" + desc2 + "\n";
                        System.out.println("---------------------登记完成---------------------");
                        break;
                    case '4':
                        System.out.print("确认是否退出(Y/N):");
                        char yn = UtilityTm.readConfirmSelection();
                        if (yn == 'Y') loopFlag = false;
                        break;
                }
            } while (loopFlag);
        }
    }

package com.company.project1;
import java.util.Scanner;

public class UtilityTm {
    /**
     Utility工具类:
     将不同的功能封装为方法,就是可以直接通过调用方法使用它的功能,而无需考虑具体的功能实现细节。
     */
        private static Scanner scanner = new Scanner(System.in);
        /**
         用于界面菜单的选择。该方法读取键盘,如果用户键入’1’-’4’中的任意字符,则方法返回。返回值为用户键入字符。
         */
        public 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;
        }
        /**
         用于收入和支出金额的输入。该方法从键盘读取一个不超过4位长度的整数,并将其作为方法的返回值。
         */
        public static int readNumber() {
            int n;
            for (; ; ) {
                String str = readKeyBoard(4);
                try {
                    n = Integer.parseInt(str);
                    break;
                } catch (NumberFormatException e) {
                    System.out.print("数字输入错误,请重新输入:");
                }
            }
            return n;
        }
        /**
         用于收入和支出说明的输入。该方法从键盘读取一个不超过8位长度的字符串,并将其作为方法的返回值。
         */
        public static String readString() {
            String str = readKeyBoard(8);
            return str;
        }

        /**
         用于确认选择的输入。该方法从键盘读取‘Y’或’N’,并将其作为方法的返回值。
         */
        public static char readConfirmSelection() {
            char c;
            for (; ; ) {
                String str = readKeyBoard(1).toUpperCase();
                c = str.charAt(0);
                if (c == 'Y' || c == 'N') {
                    break;
                } else {
                    System.out.print("选择错误,请重新输入:");
                }
            }
            return c;
        }


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

            while (scanner.hasNext()) {//返回值是bool值,作用是当在缓冲区内扫描到字符时,会返回true, 否则会发生阻塞,等待数据输入。注意 hasNext是不会返回false的
                line = scanner.nextLine();
                if (line.length() < 1 || line.length() > limit) {
                    System.out.print("输入长度(不大于" + limit + ")错误,请重新输入:");
                    continue;
                }
                break;
            }

            return line;
        }
    }

  1. 我自己写了一个项目大概跟原创项目一样,但是逻辑不是很严谨,只是为了记录一下,实现效果大概差不多,有bug的产生。

package com.company.project1;

import java.util.Scanner;

public class FamilyAccount {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String account = "";
        int income; //收支
        String description;//说明
        int count = 10000;
        boolean isFlag = true;
        do {
            System.out.println("--------------------家庭收支记账软件-------------------");
            System.out.println("                     1  收支明细");
            System.out.println("                     2  登记收入");
            System.out.println("                     3  登记支出");
            System.out.println("                     4  退出");
            System.out.println();
            System.out.print("                     请选择<1-4>: ");
            int input = scanner.nextInt();
            switch (input) {
                case 1:
                    System.out.println("--------------------当前收支明细记录-------------------");
                    System.out.println("收支        账户金额         收支金额             说   明");
                    System.out.print(account);
                    System.out.println("----------------------------------------------------");
                    break;
                case 2:
                    System.out.print("本次收入金额:");
                    income = scanner.nextInt();
                    System.out.print("本次收入说明:");
                    description = scanner.next();
                    count += income;
                    account += "收入\t     " + count + "\t         " + income + "\t         " + description + "\n";
                    System.out.println("-----------------------登记完成-----------------------");
                    break;
                case 3:
                    System.out.print("本次支出金额:");
                    income = scanner.nextInt();
                    System.out.print("本次支出说明:");
                    description = scanner.next();
                    if(count>=income){
                        count -= income;
                        account += "支出\t     " + count + "\t         " + income + "\t           " + description + "\n";
                    }else {
                        System.out.println("支出超出账户额度,支付失败");
                    }
                    System.out.println("-----------------------登记完成-----------------------");
                    break;
                case 4:
                    System.out.print("确认是否退出<Y/N>:");       
                    for (; ; ) {
                        String as = scanner.next();
                        if (as.equalsIgnoreCase("Y")) {
                            isFlag = false;
                            break;
                        } else if (as.equalsIgnoreCase("N")) {
                            break;
                        } else
                            System.out.print("选择错误,请重新输入:");
                    }
                    break;

            }
        } while (isFlag); //开始使用while,实现不了 重复提示“选择错误,请重新输入”的效果
        //注意:使用do--while()可以使用case 4 的重复提示:选择错误,请重新输入: 的提醒
    }
}

实现效果:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

奋斗的小白【demo】

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值