一、项目需求及流程说明
本项目学习目标:
1.模拟实现一个基于文本界面(程序控制台)的《家庭收支记录程序》
2.掌握初步的编程技巧和调试技巧
3.灵活运用基础知识
1.需求说明
模拟实现一个基于文本界面(程序控制台)的《家庭收支记录程序》,该程序能够记录家庭的收入、支出,并能够打印收支明细表。
项目采用分级菜单方式。
主菜单如下:
具体需求说明:
1、假设家庭起始的生活基本金为10000元;
2、每次登记收入(菜单2)后,收入的金额应累加到基本金上,并记录本次收入明细,以便后续的查询;
3、每次登记支出(菜单3)后,支出的金额应从基本金中扣除,并记录本次支出明细,以便后续的查询;
4、查询收支明细(菜单1)时,将显示所有的收入、支出名细列表。
2.流程说明
项目阶段1
首先,实现界面展示功能,并且能进入每个功能里面!
主界面展示功能代码:
public static void menu() {
System.out.println("----------家庭收支记账软件----------");
System.out.println();
System.out.println("\t\t\t1.收支明细");
System.out.println("\t\t\t2.登记收入");
System.out.println("\t\t\t3.登记支出");
System.out.println("\t\t\t4.退出");
System.out.println();
System.out.print("请选择(1-4):");
}
进入每个功能的代码(main方法):
public static void main(String[] args) {
init();
boolean loopFlag = true;
while (loopFlag) {
menu();
char n = Utility.readMenuSelection();
switch (n) {
case '1':
list();
System.out.println();
break;
case '2':
income();
break;
case '3':
expend();
break;
case '4':
System.out.print("是否确认退出(Y/N):");
char select = Utility.readConfirmSelection();
if (select == 'Y')
loopFlag = false;
break;
}
}
}
项目阶段2
在阶段1的过程中完成主要的“收入”,“支出”功能!
收入功能:
1)从键盘读取收入的金额(int)
2)从键盘读取收入的原因(String)
3)按照要求存储数据(数组)
4)添加成功
收入功能代码:
public static void income() {
System.out.print("收入金额:");
int amount = Utility.readNumber();
System.out.print("收入原因:");
String reason = Utility.readString();
storage("收入", amount, reason);
System.out.println("添加成功!");
}
支出功能:
1)从键盘读取支出的金额(int)
2)从键盘读取支出的原因(String)
3)按照要求存储数据(数组)
4)添加成功
支出功能代码:
public static void expend() {
System.out.print("支出金额:");
int amount = Utility.readNumber();
System.out.print("支出原因:");
String reason = Utility.readString();
storage("支出", amount, reason);
System.out.println("添加成功!");
}
项目阶段3
在显示账单明细完成遍历数组即可。
public static void list() {
for (int i = 0; i <= r; 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();
}
}
二、完整源代码及工具类
1.源代码
package Stage3;
import java.util.Scanner;
public class FamilyBookkeeping {
static int r = 0, count = 10000;
static String[][] arr = new String[100][4];//不灵活
public static void main(String[] args) {
init();
boolean loopFlag = true;
while (loopFlag) {
menu();
char n = Utility.readMenuSelection();
switch (n) {
case '1':
list();
System.out.println();
break;
case '2':
income();
break;
case '3':
expend();
break;
case '4':
System.out.print("是否确认退出(Y/N):");
char select = Utility.readConfirmSelection();
if (select == 'Y')
loopFlag = false;
break;
}
}
}
public static void menu() {
System.out.println("----------家庭收支记账软件----------");
System.out.println();
System.out.println("\t\t\t1.收支明细");
System.out.println("\t\t\t2.登记收入");
System.out.println("\t\t\t3.登记支出");
System.out.println("\t\t\t4.退出");
System.out.println();
System.out.print("请选择(1-4):");
}
public static void income() {
System.out.print("收入金额:");
int amount = Utility.readNumber();
System.out.print("收入原因:");
String reason = Utility.readString();
storage("收入", amount, reason);
System.out.println("添加成功!");
}
public static void expend() {
System.out.print("支出金额:");
int amount = Utility.readNumber();
System.out.print("支出原因:");
String reason = Utility.readString();
storage("支出", amount, reason);
System.out.println("添加成功!");
}
public static void list() {
for (int i = 0; i <= r; 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 amount, String reason) {
r++;
arr[r][0] = type;
if (type == "收入") {
arr[r][1] = (count + amount) + "";
count += amount;
arr[r][2] = "+" + amount;
}
if (type == "支出") {
arr[r][1] = (count - amount) + "";
count -= amount;
arr[r][2] = "-" + amount;
}
arr[r][3] = reason;
}
public static void init() {
arr[r][0] = "收支";
arr[r][1] = "账户总金额";
arr[r][2] = "收支金额";
arr[r][3] = "说明";
}
}
2.工具类
引入工具类Utility,实现一些错误输入判断,使程序更完善。(注意此工具类要和上面的源代码在同一个包下)
工具类Utility代码如下:
package Stage3;
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;
}
}
三、程序部分运行结果展示
总结
本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注编程网的更多内容!