家庭收支记账程序
目标
模拟实现一个基于文本界面的《家庭记账软件》
掌握初步的编程技巧和调试技巧
灵活运用基础知识
需求
该程序能够记录家庭的收入,支出,并能够打印收支明细表
主界面:
public static void interFace() {
System.out.println("-------------家庭收支记账软件-------------");
System.out.println(" 1.收支明细");
System.out.println(" 2.登记收入");
System.out.println(" 3.登记支出");
System.out.println(" 4.退 出");
System.out.println();
System.out.println("请选择(1~4):");
}
登记收入支出
public static void record() {
for(int i = 0; i <= count; ++i) {
for(int j = 0; j < 4; ++j) {
if (i == 0) {
System.out.print(arr[i][j] + "\t\t");
} else {
System.out.print(arr[i][j] + "\t\t ");
}
}
System.out.println();
}
}
public static void expenses() {
System.out.println("支出金额:");
int money = Utility.readNumber();
System.out.println("支出原因:");
String info= Utility.readString();
storage("支出", money, info);
System.out.println("添加成功!");
}
一.源代码
package family;
import javax.sound.midi.MidiDevice;
import javax.sound.sampled.Line;
public class Test {
static String[][] arr = new String[100][4];
static int count = 0;
static int sumNum = 10000;
public static void main(String[] args) {
init();
boolean flag = true;
while(flag) {
interFace();
char c = Utility.readMenuSelection();
switch(c) {
case '1':
System.out.println("-----------------当前收支明细记录-----------------");
record();
System.out.println("------------------------------------------------");
break;
case '2':
income();
break;
case '3':
expenses();
break;
case '4':
System.out.println("确认是否退出(Y/N)?:");
char c1 = Utility.readConfirmSelection();
if (c1 == 'Y') {
flag = false;
}
}
}
}
public static void interFace() {
System.out.println("-------------家庭收支记账软件-------------");
System.out.println(" 1.收支明细");
System.out.println(" 2.登记收入");
System.out.println(" 3.登记支出");
System.out.println(" 4.退 出");
System.out.println();
System.out.println("请选择(1~4):");
}
public static void income() {
System.out.println("收入金额:");
int money = Utility.readNumber();
System.out.println("收入原因:");
String info = Utility.readString();
storage("收入", money, info);
System.out.println("添加成功!");
}
public static void expenses() {
System.out.println("支出金额:");
int money = Utility.readNumber();
System.out.println("支出原因:");
String info= Utility.readString();
storage("支出", money, info);
System.out.println("添加成功!");
}
public static void record() {
for(int i = 0; i <= count; ++i) {
for(int j = 0; j < 4; ++j) {
if (i == 0) {
System.out.print(arr[i][j] + "\t\t");
} else {
System.out.print(arr[i][j] + "\t\t ");
}
}
System.out.println();
}
}
public static void storage(String type, int money, String info) {
count++;
arr[count][0] = type;
if (type == "收入") {
arr[count][1] = sumNum + money + "";
sumNum += money;
arr[count][2] = "+" + money;
}
if (type == "支出") {
arr[count][1] = sumNum - money + "";
sumNum -= money;
arr[count][2] = "-" + money;
}
arr[count][3] = info;
}
public static void init() {
arr[count][0] = "收支";
arr[count][1] = "账户总金额";
arr[count][2] = "收支金额";
arr[count][3] = "说明";
}
}
二.工具类
package family;
import java.util.Scanner;
public class Utility {
private static Scanner scanner;
public Utility() {
}
public static char readMenuSelection() {
while(true) {
String str = readKeyBoard(1);
char c = str.charAt(0);
if (c == '1' || c == '2' || c == '3' || c == '4') {
return c;
}
System.out.print("选择错误,请重新输入:");
}
}
public static int readNumber() {
while(true) {
String str = readKeyBoard(4);
try {
int n = Integer.parseInt(str);
return n;
} catch (NumberFormatException var3) {
System.out.print("数字输入错误,请重新输入:");
}
}
}
public static String readString() {
String str = readKeyBoard(8);
return str;
}
public static char readConfirmSelection() {
while(true) {
String str = readKeyBoard(1).toUpperCase();
char c = str.charAt(0);
if (c == 'Y' || c == 'N') {
return c;
}
System.out.print("选择错误,请重新输入:");
}
}
private static String readKeyBoard(int limit) {
String line = "";
while(scanner.hasNext()) {
line = scanner.nextLine();
if (line.length() >= 1 && line.length() <= limit) {
break;
}
System.out.print("输入长度(不大于" + limit + ")错误,请重新输入:");
}
return line;
}
static {
scanner = new Scanner(System.in);
}
}
结果展示
学习总结
对前面知识体系的一个结合,灵活运用方法来减轻程序代码的复杂度,更好的区分每个模块的功能使整个代码不混乱。因为我没有及时在每个模块完成时去运行,导致最后关联太多很难找出错误所在,希望大家引以为戒。