javase -- 小项目:家庭收支记账系统


基于文本界面的家庭收支记账系统.

//工具类 : 不需要自己写
import java.util.Scanner;
/**
Utility工具类:
将不同的功能封装为方法,就是可以直接通过调用方法使用它的功能,而无需考虑具体的功能实现细节。
*/
public class 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()) {
            line = scanner.nextLine();
            if (line.length() < 1 || line.length() > limit) {
                System.out.print("输入长度(不大于" + limit + ")错误,请重新输入:");
                continue;
            }
            break;
        }

        return line;
    }
}
//FamilyAccount.java
//使用字符串及整形变量 暂时存储数据
class FamilyAccount{
  public static void main(String[] args){
      boolean flag = true;
      int balance = 100;//余额
      String details = "";//数据内容
      int money = 0;
      String info = "";
      while(flag){
          System.out.println("-----------------家庭收支记账软件-----------------\n");
          System.out.println("        1 收支明细");
          System.out.println("        2 登记收入");
          System.out.println("        3 登记支出");
          System.out.println("        4 退   出\n");
          System.out.print("        请选择(1-4):");
          char selection = Utility.readMenuSelection();
          switch(selection){
              case '1':
                  System.out.println("\n-----------------当前收支明细记录-----------------");
                   System.out.println("收  支    账户金额        收支金额        说  明");
                  System.out.println(details);//数据输出
                  System.out.println("--------------------------------------------------");
                  break;
              case '2':
                  System.out.print("本次收入金额:");
                  money = Utility.readNumber();
                  System.out.print(" ");
                  System.out.print("本次收入说明:");
                  info = Utility.readString();
                  //拼接数据
                  balance += money;
                  details += "收入\t" + balance + "\t\t" + money + "\t\t" + info + "\n";
                  System.out.println("---------------------登记完成---------------------");
                  break;
              case '3':
                  System.out.print("本次支出金额:");
                  money = Utility.readNumber();
                  System.out.print("");
                  System.out.print("本次支出说明:");
                  info = Utility.readString();
                  //拼接数据
                  if(balance >= money){
                      balance -= money;
                      details += "支出\t" + balance + "\t\t" + money + "\t\t" + info + "\n";
                      System.out.println("---------------------登记完成---------------------");
                  }else{
                      System.out.println("余额不足,余额:" + balance);
                  }                  
                  break;
              case '4':
                  char isExit = Utility.readConfirmSelection();
                  if(isExit == 'Y'){
                        flag = false;
                  }
          }
      }
  }
}

文章作者: liuminkai
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 liuminkai !
评论
 上一篇
javase -- 数组 javase -- 数组
数组(Array). 多个相同类型数据按一定顺序排序的集合,并使用一个名字命名,并通过编号的形式,对这些数据统一管理 数组要素 数组名 数组元素 数组下标(索引) 数组长度 数组特点:有序排列 数组是引用数据类型 元素可以是任意
2020-07-20
下一篇 
javase -- breakGET javase -- breakGET
break小知识点.众所周知,break用于跳出switch-case和终止循环,但是他只能终止本层循环,那么有没有方法使其终止外层,甚至其他的循环代码块呢?结果,当然是有的 label:for(int i = 0; i < 10;
2020-07-20
  目录