阶段三的家庭收支表的总结

家庭收支系统

系统介绍
记录家庭的收入、支出,并能够打印收支明细表。
涉及到的知识点
1.变量的定义
2.基本数据类型的使用
3.循环语句
4.分支语句
5.方法声明、调用和返回值的接受
6.简单的屏幕输出格式控制
7.导入包并调用相关方法
(其实 这阶段主要考验我们对工具包的使用)
程序代码示例
package comhomecustom;

import java.util.Scanner;

public class HomeCommas {
//定义三个成员变量
private static String[] incomeList; //收入
private static String[] outcomeList; //支出
private static int money;

public static void main(String[] args) {
    money = 10000;
    char x = menu();
    login(x);
}
public static char menu() {
    System.out.println("----------家庭收支记账软件----------");
    System.out.println("\n");
    System.out.println("----------1.收支明细----------");
    System.out.println("----------2.登记收入----------");
    System.out.println("----------3.登记支出----------");
    System.out.println("----------4.退   出----------");
    System.out.println("          请选择(1-4)");
    char x = Utility.readMenuSelection();
    return x;

}
public static void login(char x) {
    while (true) {
        switch (x) {
            case '1':
                in_and_ex_detail();
                //返回收支明细的方法
                break;
            case '2':
                income();
                break;
            case '3':
                outcome();
                break;
            case '4':
                System.out.println("确认请输入Y,取消请输入N");
                char c = Utility.readConfirmSelection();
                if (c == 'Y') {
                    return;
                } else {
                    break;
                }
            default:
                System.out.println("输入的数字有误");
        }
        x = menu();
    }
}

public static String[] in_and_ex_sout(int change[],String reason[]){
    String[] arr1 = new String[6];
    System.out.println("收支      账户总金额       收支金额        说明");
    for (int i = 0; i < change.length; i++) {
        money += change[i];

        if (change[i] > 0) {
            System.out.println("收入          " + money + "       " + "+" + change[i] + "           " + reason[i]);
            arr1[i] = "收入      " + money + "          " + "+" + change[i] + "       " + reason[i];
        } else if (change[i] < 0) {
            arr1[i] = "支出      " + money + "         " + change[i] + "            " + reason[i];
            System.out.println("支出          " + money + "       " + change[i] + "           " + reason[i]);
        }

    }
    return arr1;

}

public static void income(){
    Scanner sc=new Scanner(System.in);
    System.out.println("请输入收入的金额,要求为整数,当金额为0时停止,最多输入六条");
    int[] arr = new int[6];
    String[] brr = new String[6];
    for (int i = 0; i < 6; i++) {
        System.out.println("请输入收入的金额");
        //int income = sc.nextInt();
        int income = Utility.readNumber();
        if (income == 0) break;
        System.out.println("请输入收入的原因");

// String reason = sc.next();
String reason = Utility.readString();
arr[i] = income;
brr[i] = reason;
}
incomeList = in_and_ex_sout(arr, brr);
return;

}

public static void outcome(){
    Scanner sc = new Scanner(System.in);
    System.out.println("请输入支出的金额,要求为整数,当金额为0时停止,最多输入六条");
    int[] arr = new int[6];
    String[] brr = new String[6];
    for (int i = 0; i < 6; i++) {
        System.out.println("请输入支出的金额");

// int cost = sc.nextInt();
int cost = Utility.readNumber();
if (cost == 0) break;
System.out.println(“请输入支出的原因”);
String reason = Utility.readString();

        arr[i] = -cost;
        brr[i] = reason;
    }
    outcomeList = in_and_ex_sout(arr, brr);
    return;
}

public static void in_and_ex_detail() {
    //定义一个数组,没有给定长度的时候,
    // 直接使用 数组名.Length 方法时候,就会报空指针异常
    System.out.println("收支      账户总金额       收支金额        说明");
    if (incomeList!=null)
    {
    for (int i = 0; i < incomeList.length; i++) {
        if (incomeList[i] != null)
            System.out.println(incomeList[i]);
        else break;
    }}else
    {
        System.out.println("请录入收入信息");
    }
    if (outcomeList!=null){
    for (int i = 0; i < outcomeList.length; i++) {
        if (outcomeList[i] != null)
            System.out.println(outcomeList[i]);
        else break;
    } }
    else
        {
            System.out.println("请录入支出信息");
        }


}

}
**

工具包演示

package comhomecustom;

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;
}

}

** 结果演示

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值