/*
*@author: Noiimplant
*@version: 1.0
*/
1. 利用java实现简易记账软件
根据尚硅谷java教程进行练习
2. 实现功能
- 记录家庭支出、收入,打印收支明细表
- 使用分级菜单的方式
3. 代码实现
3.1 GuliAccount.java
- package GuliAccount;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- /**
- @author Noiimplant
- @data 2023-4-21
- @name: 阶段一 项目:谷粒记
- 账软件实现
- */
- public class GuliAccount {
- public static void main(String args[]){
- boolean flag = true;
- SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss"); //设计日期格式
- System.out.println(df.format(new Date())); //new Data()获取当前时间
- String currentTime = "";
- //初始金额
- int init_Money = 0;
- String info = ""; //用来记录收支的记录
- while(flag){
- System.out.println("------------谷粒记账软件--------------\n");
- System.out.println(" 1.收支明细\n");
- System.out.println(" 2.登记收入\n");
- System.out.println(" 3.登记支出\n");
- System.out.println(" 4.退 出\n");
- System.out.println(" 请选择(1-4)\n");
- char selction = Unility.readMenuSelection();
- switch(selction){
- case '1':
- System.out.println("--------------收入明细--------------");
- System.out.println("收支\t账户金额\t收支金额\t说 明\t日 期\n");
- System.out.println(info);
- System.out.println("-----------------------------------");
- break;
- case '2':
- System.out.println("本次收入金额:");
- int money1 = Unility.readNumber();
- if(money1 > 0){
- init_Money += money1; //
- }
- System.out.println("本次收入金额说明:");
- String addExample = Unility.readString();
- Date Now1 = new Date();
- currentTime = df.format(Now1);
- info += "收入\t\t" + init_Money + "\t\t" + money1 + "\t\t" + addExample + "\t\t" + currentTime + "\n";
- System.out.println("--------------登记完成--------------");
- break;
- case '3':
- System.out.println("登记支出");
- System.out.println("本次收入金额:");
- int money2 = Unility.readNumber();
- if(money2 > 0 && init_Money > money2){
- init_Money -= money2; //
- }
- System.out.println("本次支出金额说明:");
- String addExample2 = Unility.readString();
- Date Now2 = new Date();
- currentTime = df.format(Now2);
- info += "收入\t\t" + init_Money + "\t\t" + money2 + "\t" + currentTime + addExample2 + "\n";
- System.out.println("--------------登记完成--------------");
- break;
- case '4':
- System.out.println("请确认是否退出(Y/N)");
- char isExit = Unility.readConfirmSelect();
- if(isExit == 'Y'){
- flag = false;
- }
- break;
- }
- }
- }
- }
复制代码 3.2 Unility
- package GuliAccount;
- import java.util.*;
- public class Unility {
- private static Scanner sc = new Scanner(System.in);
- /*
- * 判断selection
- * */
- 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.println("输入的数据有误,请重新输入:");
- }
- else{
- break;
- }
- }
- return c;
- }
- /*
- * 用于收入和支出说明的输入。
- * 该方法从键盘读取一个不超过8位长度的字符串,并将其作为方法的返回值。
- */
- public static String readString(){
- String str = readKeyBoard(8);
- return str;
- }
- /*
- * 用于收入和支出金额的存储
- */
- public static int readNumber(){
- int num;
- for(;;){
- String str = readKeyBoard(4);
- try{
- num = Integer.parseInt(str);
- break;
- }
- catch(NumberFormatException e){
- System.out.println("输入数据有误,请重新输入");
- }
- }
- return num;
- };
- /*
- * 用于确认选择的输入。
- * 该方法从键盘读取‘Y’或‘N’,并将其作为方法的返回值。
- */
- public static char readConfirmSelect() {
- char c;
- for(;;){
- String str = readKeyBoard(1).toUpperCase();
- c = str.charAt(0);
- if(c == 'Y' || c == 'N'){
- break;
- }
- else{
- System.out.println("输入有误,请重新输入");
- }
- }
- return c;
- }
- /*
- * 从键盘键入的字符判断
- */
- public static String readKeyBoard(int limit){
- String line = "";
- while(sc.hasNext()){
- line = sc.nextLine();
- if(line.length() < 1 || line.length() > limit){
- System.out.println("输入的长度不大于" + limit + ",请重新输入");
- continue;
- }
- else break;
- }
- return line;
- }
- }
复制代码 原文链接:
尚硅谷java教程链接
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |