用2维数组制作一个简易的可以实现增删改查借还功能的cd管理系统
我们的目标是实现以下功能的cd管理系统
以上就是我们所要完成的系统所要实现的所有的功能,接下来我们就要去一一实现这些功能
对于功能实现的思路分析
利用一个二维的字符串数组去存储cd的信息,每行存入某张光盘的所有信息(其中包括状态,cd的名字,借出的时间,借出的次数),通过对这个二维数组中的值的改变来实现所要的功能.有了大致思路接下来具体的实现功能.
程序的主页面
程序的主页面是需要重重使用的而且输入不同的数字是会实现不同的功能的,所以我们在一个循环里用一个switch语句
boolean cao = true;
do {
System.out.println("input以下按键以实现相应的功能");
System.out.println("================================================");
System.out.println("1.新增专辑 2.查看当前专辑 3.删除专辑 ");
System.out.println("4.借出专辑 5.归还专辑 6.退出系统 ");
System.out.println("================================================");
int q = input.nextInt();
switch (q) {
case 1:
Node.insert();
break;
case 2:
Node.select();
break;
case 3:
Node.delete();
break;
case 4:
Node.borrow();
break;
case 5:
Node.repay();
break;
case 6:
cao = false;
break;
}
} while (cao);
具体的方法实现
import java.util.Scanner;
public class Node {
static String[][] cd = new String[10][4];
static Scanner sc = new Scanner(System.in);
public static void insert() {
System.out.println("请输入要增加的cd名:");
String name = sc.nextLine();
for (int i = 0; i < cd.length; i++) {
if (cd[i][0] == null || cd[i][0].isEmpty()) {
cd[i][0] = "在架";
cd[i][1] = name;
cd[i][2] = "null";
cd[i][3] = "0";
break;
}
}
}
public static void select() {
System.out.println("序号" +
" 状态" +
" cd名" +
" 借出时间" +
" 借出次数");
for (int i = 0; i < cd.length; i++) {
if (cd[i][0] == null || cd[i][0].isEmpty()) {
break;
}
System.out.print(i + 1 + " ");
for (int j = 0; j < cd[i].length; j++) {
System.out.print(cd[i][j] + " ");
}
System.out.println();
}
}
public static void delete() {
System.out.println("请输入要删除的cd");
String qq = sc.nextLine();
for (int i = 0; i < cd.length; i++) {
if (cd[i][1].equals(qq)) {
cd[i][0] = null;
cd[i][1] = null;
cd[i][2] = null;
cd[i][3] = null;
for (int j = i; j < cd.length - 1; j++) {
cd[j][0] = cd[j + 1][0];
cd[j][1] = cd[j + 1][1];
cd[j][2] = cd[j + 1][2];
cd[j][3] = cd[j + 1][3];
}
break;
}
}
}
public static void borrow() {
System.out.println("请输入要借出的cd名");
String cdname = sc.nextLine();
for (int i = 0; i < cd.length; i++) {
if (cd[i][1].equals(cdname)) {
cd[i][0] = "借出";
System.out.println("请输入借出时间");
String cddata = sc.nextLine();
cd[i][2] = cddata;
cd[i][3] = String.valueOf(Integer.valueOf(cd[i][3]) + 1);
break;
}
}
}
public static void repay() {
System.out.println("请输入要归还的cd名");
String cdname = sc.nextLine();
for (int i = 0; i < cd.length; i++) {
if (cd[i][1].equals(cdname)) {
cd[i][0] = "在架";
System.out.println("请输入归还时间");
String cddata = sc.nextLine();
int mon = (Integer.valueOf(cddata) - Integer.valueOf(cd[i][2])) * 5;
System.out.println("借出时间 " + cd[i][2]);
System.out.println("归还时间 " + cddata);
System.out.println("共计本次的租金为 " + mon);
cd[i][2] = null;
break;
}
}
}
}