1.Utility模块
import java. util. Scanner ;
public class Utility { private static Scanner scanner = new Scanner ( System . in) ; 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; } 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; } public static String readString ( ) { String str = readKeyBoard ( 8 ) ; return str; } 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; }
}
2. 账户模块
public class GuLiAccount { public static void main ( String [ ] args) { boolean isFlag = true ; int balance = 10000 ; String info = "" ; while ( isFlag) { System . out. println ( "-------斐秋记账软件---------\n" ) ; System . out. println ( "-------1、收支明细---------" ) ; System . out. println ( "-------2、登记收入---------" ) ; System . out. println ( "-------3、登记支出---------" ) ; System . out. println ( "-------4、退 出---------\n" ) ; System . out. println ( "------- 请选择(1-4):" ) ; char selection = Utility . readMenuSelection ( ) ; switch ( selection) { case '1' : System . out. println ( "------------------当前收支明细记录-------------------" ) ; System . out. println ( "收支\t账户金额\t收支金额\t说 明" ) ; System . out. println ( info) ; System . out. println ( "--------------------------------------------------" ) ; break ; case '2' : System . out. println ( "本次收入金额:" ) ; int money1 = Utility . readNumber ( ) ; if ( money1 > 0 ) { balance += money1; } System . out. print ( "本次收入说明:" ) ; String addDesc = Utility . readString ( ) ; info += "收入\t" + balance + "\t\t" + money1 + "\t" + addDesc + "\n" ; System . out. println ( "------------------------登记完成-------------------" ) ; break ; case '3' : System . out. println ( "本次支出金额:" ) ; int money2 = Utility . readNumber ( ) ; if ( money2 > 0 && balance >= money2) { balance -= money2; } System . out. println ( "本次支出说明:" ) ; String minusDesc = Utility . readString ( ) ; info += "支出\t" + balance + "\t\t" + money2 + "\t\t" + minusDesc + "\n" ; System . out. println ( "------------------------登记完成--------------------------" ) ; break ; case '4' : System . out. print ( "\n确认是否退出(Y/N):" ) ; char isExit = Utility . readConfirmSelection ( ) ; if ( isExit == 'Y' ) { isFlag = false ; } else { } break ; } } }
}