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

打印 上一主题 下一主题

主题 506|帖子 506|积分 1518

/*
*@author: Noiimplant
*@version: 1.0
*/
1. 利用java实现简易记账软件

根据尚硅谷java教程进行练习
2. 实现功能


  • 记录家庭支出、收入,打印收支明细表
  • 使用分级菜单的方式
3. 代码实现

3.1 GuliAccount.java
  1. package GuliAccount;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Date;
  4. /**
  5. @author Noiimplant
  6. @data 2023-4-21
  7. @name: 阶段一 项目:谷粒记
  8. 账软件实现
  9. */
  10. public class GuliAccount {
  11.     public static void main(String args[]){
  12.         boolean flag = true;
  13.         SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss"); //设计日期格式
  14.         System.out.println(df.format(new Date())); //new Data()获取当前时间
  15.         String currentTime = "";
  16.         //初始金额
  17.         int init_Money = 0;
  18.         String info = ""; //用来记录收支的记录
  19.         while(flag){
  20.             System.out.println("------------谷粒记账软件--------------\n");
  21.             System.out.println("            1.收支明细\n");
  22.             System.out.println("            2.登记收入\n");
  23.             System.out.println("            3.登记支出\n");
  24.             System.out.println("            4.退   出\n");
  25.             System.out.println("            请选择(1-4)\n");
  26.             char selction = Unility.readMenuSelection();
  27.             switch(selction){
  28.                 case '1':
  29.                     System.out.println("--------------收入明细--------------");
  30.                     System.out.println("收支\t账户金额\t收支金额\t说  明\t日  期\n");
  31.                     System.out.println(info);
  32.                     System.out.println("-----------------------------------");
  33.                     break;
  34.                 case '2':
  35.                     System.out.println("本次收入金额:");
  36.                     int money1 = Unility.readNumber();
  37.                     if(money1 > 0){
  38.                         init_Money += money1; //
  39.                     }
  40.                     System.out.println("本次收入金额说明:");
  41.                     String addExample = Unility.readString();
  42.                     Date Now1 = new Date();
  43.                     currentTime = df.format(Now1);
  44.                     info += "收入\t\t" + init_Money + "\t\t" + money1 + "\t\t" + addExample + "\t\t" + currentTime + "\n";
  45.                     System.out.println("--------------登记完成--------------");
  46.                     break;
  47.                 case '3':
  48.                     System.out.println("登记支出");
  49.                     System.out.println("本次收入金额:");
  50.                     int money2 = Unility.readNumber();
  51.                     if(money2 > 0 && init_Money > money2){
  52.                         init_Money -= money2; //
  53.                     }
  54.                     System.out.println("本次支出金额说明:");
  55.                     String addExample2 = Unility.readString();
  56.                     Date Now2 = new Date();
  57.                     currentTime = df.format(Now2);
  58.                     info += "收入\t\t" + init_Money + "\t\t" + money2 + "\t" + currentTime + addExample2 + "\n";
  59.                     System.out.println("--------------登记完成--------------");
  60.                     break;
  61.                 case '4':
  62.                     System.out.println("请确认是否退出(Y/N)");
  63.                     char isExit = Unility.readConfirmSelect();
  64.                     if(isExit == 'Y'){
  65.                         flag = false;
  66.                     }
  67.                     break;
  68.             }
  69.         }
  70.     }
  71. }
复制代码
3.2 Unility
  1. package GuliAccount;
  2. import java.util.*;
  3. public class Unility {
  4.     private static Scanner sc = new Scanner(System.in);
  5.     /*
  6.     * 判断selection
  7.     * */
  8.     public static char readMenuSelection(){
  9.         char c;
  10.         for(;;){
  11.             String str = readKeyBoard(1);
  12.             c = str.charAt(0);
  13.             if(c != '1' && c != '2' && c != '3' && c != '4'){
  14.                 System.out.println("输入的数据有误,请重新输入:");
  15.             }
  16.             else{
  17.                 break;
  18.             }
  19.         }
  20.         return c;
  21.     }
  22.     /*
  23.      * 用于收入和支出说明的输入。
  24.      * 该方法从键盘读取一个不超过8位长度的字符串,并将其作为方法的返回值。
  25.      */
  26.     public static String readString(){
  27.         String str = readKeyBoard(8);
  28.         return str;
  29.     }
  30.     /*
  31.      * 用于收入和支出金额的存储
  32.      */
  33.     public static int readNumber(){
  34.       int num;
  35.       for(;;){
  36.           String str = readKeyBoard(4);
  37.           try{
  38.               num = Integer.parseInt(str);
  39.               break;
  40.           }
  41.           catch(NumberFormatException e){
  42.               System.out.println("输入数据有误,请重新输入");
  43.           }
  44.       }
  45.       return num;
  46.     };
  47.     /*
  48.      * 用于确认选择的输入。
  49.      * 该方法从键盘读取‘Y’或‘N’,并将其作为方法的返回值。
  50.      */
  51.     public static char readConfirmSelect() {
  52.         char c;
  53.         for(;;){
  54.             String str = readKeyBoard(1).toUpperCase();
  55.             c = str.charAt(0);
  56.             if(c == 'Y' || c == 'N'){
  57.                 break;
  58.             }
  59.             else{
  60.                 System.out.println("输入有误,请重新输入");
  61.             }
  62.         }
  63.         return c;
  64.     }
  65.     /*
  66.     * 从键盘键入的字符判断
  67.     */
  68.     public static String readKeyBoard(int limit){
  69.         String line = "";
  70.         while(sc.hasNext()){
  71.             line = sc.nextLine();
  72.             if(line.length() < 1 || line.length() > limit){
  73.                 System.out.println("输入的长度不大于" + limit + ",请重新输入");
  74.                 continue;
  75.             }
  76.             else break;
  77.         }
  78.         return line;
  79.     }
  80. }
复制代码
原文链接:
尚硅谷java教程链接

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

万万哇

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表