在图书管理系统中,我们有三个事务,一个是管理员事务,一个是购物者事务,另一个是存储图书事务,这三个事务可以写成三个class类。
导入我们需要的模块:
import java.util.*; import java.util.Scanner;
首先是我们的存储类。存储类需要四个变量:增加变量、减少变量、总变量和初始变量:
class kucun{ //增变量 int bok,shu,qbii; //总变量 int bkzon,shuzon,qbzon; //减变量 int bokj,shuj,qbj; //名称 char bk = '书'; char sb = '包'; char qb = '铅'; ///初始变量 int book = 10; int shubao = 10; int qbi = 10; }
二是我们的管理员(需要继承我们的变量):
class guanli extends kucun { public void yu() { String tui; Scanner sc=new Scanner(System.in); System.out.println("成功进入管理员"); while (true){ System.out.println("继续请按5,退出请按4"); ///键盘接收数据 tui =sc.nextLine(); if (tui.equals("4")) { //如果是,提示退出输入 System.out.println("退出系统!"); break; }else { System.out.println("请输入您需要选择的内容 1.增加库存 2.减少库存 3.查询库存 4.退出系统"); Scanner oneo = new Scanner(System.in); int on1 = oneo.nextInt(); switch (on1) { case 1: System.out.println("请输入您需要添加的库存1." bk " 2." sb " 3." qb); Scanner nq = new Scanner(System.in); int nq1 = nq.nextInt(); switch (nq1) { case 1: System.out.println("输入您需要添加的库存:"); Scanner oneon = new Scanner(System.in); bok = oneon.nextInt(); bkzon = book bok; System.out.println("添加了书" bok "本书的总库存为:" bkzon); break; case 2: System.out.println("输入您需要添加的库存:"); Scanner one = new Scanner(System.in); shu = one.nextInt(); shuzon = shubao shu; System.out.println("添加了包" shu "本包总库存为:" shuzon); break; case 3: System.out.println("输入您需要添加的库存:"); Scanner on = new Scanner(System.in); qbii = on.nextInt(); qbzon = qbi qbii; System.out.println("添加了书" qbii "本书的总库存为:" qbzon); break; } break; case 2: System.out.println("请输入您需要减少的库存1." bk " 2." sb " 3." qb); Scanner n = new Scanner(System.in); int n1 = n.nextInt(); switch (n1) { case 1: System.out.println("输入您需要减少的库存:"); Scanner oneon = new Scanner(System.in); bokj = oneon.nextInt(); book = bkzon - bokj; System.out.println("减少了书" bok "本书的总库存为:" book); break; case 2: System.out.println("输入您需要减少的库存:"); Scanner one = new Scanner(System.in); shuj = one.nextInt(); shubao = shuzon - shuj; System.out.println("减少了包" shuj "本包总库存为:" shubao); break; case 3: System.out.println("输入您需要减少的库存:"); Scanner on = new Scanner(System.in); qbj = on.nextInt(); qbi = qbzon - qbj; System.out.println("减少了书" qbii "本书的总库存为:" qbi); break; } break; case 3: int chabk = book bok - bokj; int chasz = shubao shu - shuj; int chaqb = qbi qbii - qbj; System.out.println("查询后的库存1." bk "剩余:" chabk "2." sb "剩余:" chasz "3." qb "剩余:" chaqb); break; } } } } }
然后是我们的购物者:
class ygon extends kucun{
public void yue(){
String tui;
Scanner sc=new Scanner(System.in);
while (true){
System.out.println("成功进入购物者系统");
System.out.println("继续请按5,退出请按3");
//键盘接收数据
tui =sc.nextLine();
if (tui.equals("3")) {
//如果是,提示退出输入
System.out.println("退出系统!");
break;
}else {
System.out.println("请输入你需要选择的内容 1.购买商品 2.查询库存 3.退出系统");
Scanner oneo = new Scanner(System.in);
int on1 = oneo.nextInt();
switch (on1) {
case 1:
System.out.println("请输入你需要购买什么的库存1." + bk + " 2." + sb + " 3." + qb);
Scanner n = new Scanner(System.in);
int n1 = n.nextInt();
switch (n1) {
case 1:
System.out.println("请输入你需要购买的数量:");
Scanner oneon = new Scanner(System.in);
bokj = oneon.nextInt();
bkzon = book - bokj;
System.out.println("减少了书" + bokj + "本书的总库存为:" + bkzon);
break;
case 2:
System.out.println("请输入你需要购买的数量:");
Scanner one = new Scanner(System.in);
shuj = one.nextInt();
shuzon = shubao - shuj;
System.out.println("减少了包" + shuj + "本包的总库存为:" + shuzon);
break;
case 3:
System.out.println("请输入你需要购买的数量:");
Scanner on = new Scanner(System.in);
qbj = on.nextInt();
qbzon = qbi - qbj;
System.out.println("减少了书" + qbj + "本书的总库存为:" + qbzon);
break;
}
break;
case 2:
int chabk = book - bokj;
int chasz = shubao - shuj;
int chaqb = qbi - qbj;
System.out.println("查询后的库存1." + bk + "剩余:" + chabk + "2." + sb + "剩余:" + chasz + "3." + qb + "剩余:" + chaqb);
break;
}
}
}
}
}
这些类写完后,我们再加入我们的导语类:
class daoru{
public void daoru(){
System.out.println("欢迎进入管理系统");
System.out.println("管理员进入请按1,购物者进入请按2");
}
}
然后开始逐个调用我们的类:
public static void main(String[] arge){
daoru dr = new daoru();
dr.daoru();
guanli gl = new guanli();
ygon yg = new ygon();
Scanner oneone=new Scanner(System.in);
int one = oneone.nextInt();
switch (one){
case 1: gl.yu();
break;
case 2: yg.yue();
break;
}
}
大功告成运行截图:
后续会更新对接数据库来进行数据存储,实现用户与管理员的交互,目前的此系统没有管理员和购物者的交互,只有个初始值交互,这个后续也会更新补上