课后作业-代码优化

public class Main {
public static void main(String[] args) {
WarehouseManagement wm=new WarehouseManagement();
wm.set();
while(true){
wm.mune();
}
}
}
public class WarehouseInformation {
private String itemno="";
private String itemname;
private String suppliername;
private String warehousingtime;
private String shipmenttime;
private String warehousenumber;
private String warehouseplace;
private int itemnumber;
private int outnumber;

public WarehouseInformation(String itemno, String itemname, String suppliername, String warehousingtime, String warehousenumber, String warehouseplace, int itemnumber) {
this.itemno = itemno;
this.itemname = itemname;
this.suppliername = suppliername;
this.warehousingtime = warehousingtime;
this.warehousenumber = warehousenumber;
this.warehouseplace = warehouseplace;
this.itemnumber = itemnumber;
}

public WarehouseInformation(String itemno, String itemname, String suppliername, String warehousingtime, String shipmenttime, String warehousenumber, String warehouseplace, int itemnumber, int outnumber) {
this.itemno = itemno;
this.itemname = itemname;
this.suppliername = suppliername;
this.warehousingtime = warehousingtime;
this.shipmenttime = shipmenttime;
this.warehousenumber = warehousenumber;
this.warehouseplace = warehouseplace;
this.itemnumber = itemnumber;
this.outnumber = outnumber;
}

public WarehouseInformation() {
}

public String getItemno() {
return itemno;
}

public void setItemno(String itemno) {
this.itemno = itemno;
}

public String getItemname() {
return itemname;
}

public void setItemname(String itemname) {
this.itemname = itemname;
}

public String getSuppliername() {
return suppliername;
}

public void setSuppliername(String suppliername) {
this.suppliername = suppliername;
}

public String getWarehousingtime() {
return warehousingtime;
}

public void setWarehousingtime(String warehousingtime) {
this.warehousingtime = warehousingtime;
}

public String getShipmenttime() {
return shipmenttime;
}

public void setShipmenttime(String shipmenttime) {
this.shipmenttime = shipmenttime;
}

public String getWarehousenumber() {
return warehousenumber;
}

public void setWarehousenumber(String warehousenumber) {
this.warehousenumber = warehousenumber;
}

public String getWarehouseplace() {
return warehouseplace;
}

public void setWarehouseplace(String warehouseplace) {
this.warehouseplace = warehouseplace;
}

public int getItemnumber() {
return itemnumber;
}

public void setItemnumber(int itemnumber) {
this.itemnumber = itemnumber;
}

public int getOutnumber() {
return outnumber;
}

public void setOutnumber(int outnumber) {
this.outnumber = outnumber;
}
public void show(){
System.out.println(itemno+" "+itemname+" "+suppliername+" "+warehousingtime+" "+shipmenttime+" "+warehousenumber+" "+warehouseplace+" "+itemnumber+" "+outnumber);
}
}
import java.util.Scanner;

public class WarehouseManagement {
static int n=0;

static WarehouseInformation[] w=new WarehouseInformation[20];
public static void set() {
for (int i = 0; i < 20; i++) {
w[i] = new WarehouseInformation();
}
}
public static void mune(){
Scanner sc=new Scanner(System.in);
System.out.println("***********************************************************");
System.out.println(" 石家庄铁道大学前进22软件开发有限公司");
System.out.println(" 仓库管理系统2022版");
System.out.println("***********************************************************");
System.out.println("\t\t1、商品入库管理");
System.out.println("\t\t2、商品信息修改");
System.out.println("\t\t3、商品出库管理");
System.out.println("\t\t4、仓库盘点管理");
System.out.println("**********************************************************");
int type;
type=sc.nextInt();
if(type<1||type>4){
return;
}
switch (type){
case 1:addgood();
break;
case 2:changegood();
break;
case 3:outgood();
break;
case 4:allsee();
break;
default:
System.out.println("输入数据非数字,程序结束");
System.exit(0);
}
}
//货物入库
public static void addgood(){
System.out.println("***********************************************************");
System.out.println("\t\t石家庄铁道大学前进22软件开发有限公司");
System.out.println("\t\t\t仓库管理系统2022版");
System.out.println("***********************************************************");
Scanner sc=new Scanner(System.in);
System.out.println("请输入商品编码:");
String no= sc.next();
System.out.println("请输入商品名称");
String name=sc.next();
System.out.println("请输入供货商信息");
String sname= sc.next();
System.out.println("请输入入库时间");
String htime= sc.next();
System.out.println("请输入存放仓库号");
String hnum= sc.next();
System.out.println("请输入存放位置信息");
String hplace= sc.next();
System.out.println("请输入入库数量");
int number= sc.nextInt();
int flag=0;
WarehouseInformation war=new WarehouseInformation(no,name,sname,htime,hnum,hplace,number);
if(no.length()!=8||htime.length()!=8||hplace.length()!=8||hnum.length()!=3){
System.out.println("录入错误");
System.out.println("请重新输入");
addgood();
return;
}
for(int i=0;i<8;i++){
if(no.charAt(i)-'0'<0||no.charAt(i)-'0'>9){
flag=1;
}
if(htime.charAt(i)-'0'<0||htime.charAt(i)-'0'>9){
flag=1;
}
if(hplace.charAt(i)-'0'<0||hplace.charAt(i)-'0'>9){
flag=1;
}
}
for(int i=0;i<3;i++){
if(hnum.charAt(i)-'0'<0||hnum.charAt(i)-'0'>9){
flag=1;
}
}
if(flag==1){
System.out.println("录入错误");
System.out.println("请重新输入");
addgood();
return;
}
System.out.println("***********************************************************");
System.out.println("\t\t石家庄铁道大学前进22软件开发有限公司");
System.out.println("\t\t\t仓库管理系统2022版");
System.out.println("***********************************************************");
System.out.println("\t\t\t商品编号:"+war.getItemno());
System.out.println("\t\t\t商品名称:"+war.getItemname());
System.out.println("\t\t供货商信息:"+war.getSuppliername());
System.out.println("\t\t\t入库时间:"+war.getWarehousingtime());
System.out.println("\t\t\t存放仓库号:"+war.getWarehousenumber());
System.out.println("\t\t\t存放位置信息:"+war.getWarehouseplace());
System.out.println("\t\t\t入库商品数量:"+war.getItemnumber());
System.out.println("该商品入库操作已完成,是否提交(Y/N)");
String s= sc.next();
if(s.equals("N")||s.equals("n")){
addgood();
return;
}else if(s.equals("Y")||s.equals("y")){
w[n]=war;
n++;
}

}
//信息修改
public static void changegood(){
Scanner sc=new Scanner(System.in);
int flag=0;
System.out.println("***********************************************************");
System.out.println("\t石家庄铁道大学前进22软件开发有限公司");
System.out.println("\t\t仓库管理系统2022版");
System.out.println("***********************************************************");
System.out.println("请输入商品编号:");
System.out.println("**********************************************************");
String s= sc.next();
int index=0;
for(int i=0;i<20;i++){
if(s.equals(w[i].getItemno())){
index=i;
flag=1;
}
}
if(flag==0){
System.out.println("库中没有该商品");
changegood();
return;
}else{
System.out.println("***********************************************************");
System.out.println("\t石家庄铁道大学前进22软件开发有限公司");
System.out.println("\t\t仓库管理系统2022版");
System.out.println("***********************************************************");
System.out.println("\t\t1、商品编号:"+w[index].getItemno());
System.out.println("\t\t2、商品名称:"+w[index].getItemname());
System.out.println("\t\t3、供货商信息:"+w[index].getSuppliername());
System.out.println("\t\t4、入库时间:"+w[index].getWarehousingtime());
System.out.println("\t\t5、存放仓库号:"+w[index].getWarehousenumber());
System.out.println("\t\t6、存放位置信息:"+w[index].getWarehouseplace());
System.out.println("\t\t7、入库商品数量:"+w[index].getItemnumber());
System.out.println("请选择需要修改的信息编号(1-7):");
System.out.println("**********************************************************");
int type= sc.nextInt();
String chan;
switch(type){
case 1:chan="商品编号";
break;
case 2:chan="商品名称";
break;
case 3:chan="供货商信息";
break;
case 4:chan="入库时间";
break;
case 5:chan="存放仓库号";
break;
case 6:chan="存放位置信息";
break;
case 7:chan="入库商品数量";
break;
default:
System.out.println("该选项不存在");
return;
}
WarehouseInformation wi=w[index];
System.out.println("***********************************************************");
System.out.println("\t石家庄铁道大学前进22软件开发有限公司");
System.out.println("\t\t仓库管理系统2022版");
System.out.println("***********************************************************");
System.out.println("\t\t1、商品编号:"+w[index].getItemno());
System.out.println("\t\t2、商品名称:"+w[index].getItemname());
System.out.println("\t\t3、供货商信息:"+w[index].getSuppliername());
System.out.println("\t\t4、入库时间:"+w[index].getWarehousingtime());
System.out.println("\t\t5、存放仓库号:"+w[index].getWarehousenumber());
System.out.println("\t\t6、存放位置信息:"+w[index].getWarehouseplace());
System.out.println("\t\t7、入库商品数量:"+w[index].getItemnumber());
System.out.println("请选择需要修改的信息编号(1-7):"+type);
System.out.println("请输入修改后的"+chan+":XXXXXXXX");
System.out.println("**********************************************************");
switch(type){
case 1:String no=sc.next();
wi.setItemno(no);
break;
case 2:String name=sc.next();
wi.setItemname(name);
break;
case 3:String si=sc.next();
wi.setSuppliername(si);
break;
case 4:String it=sc.next();
wi.setWarehousingtime(it);
break;
case 5:String wnum=sc.next();
wi.setWarehousenumber(wnum);
break;
case 6:String wp=sc.next();
wi.setWarehouseplace(wp);
break;
case 7:int number=sc.nextInt();
wi.setItemnumber(number);
break;
}
System.out.println("***********************************************************");
System.out.println("\t石家庄铁道大学前进22软件开发有限公司");
System.out.println("\t\t仓库管理系统2022版");
System.out.println("***********************************************************");
System.out.println("\t\t1、商品编号:"+wi.getItemno());
System.out.println("\t\t2、商品名称:"+wi.getItemname());
System.out.println("\t\t3、供货商信息:"+wi.getSuppliername());
System.out.println("\t\t4、入库时间:"+wi.getWarehousingtime());
System.out.println("\t\t5、存放仓库号:"+wi.getWarehousenumber());
System.out.println("\t\t6、存放位置信息:"+wi.getWarehouseplace());
System.out.println("\t\t7、入库商品数量:"+wi.getItemnumber());
System.out.println("该商品修改操作已完成,是否保存(Y/N)");
System.out.println("**********************************************************");
String t=sc.next();
if(t.equals("Y")||t.equals("y")){
w[index]=wi;
return;
}else if(t.equals("N")||t.equals("n")){
System.out.println("修改未保存,返回入库界面");
addgood();
return;
}
}
}
public static void outgood() {
Scanner sc = new Scanner(System.in);
int flag = 0;
int falg=0;
System.out.println("***********************************************************");
System.out.println("\t石家庄铁道大学前进22软件开发有限公司");
System.out.println("\t\t仓库管理系统2022版");
System.out.println("***********************************************************");
System.out.println("请输入商品编号:");
System.out.println("**********************************************************");
String s = sc.next();
int index = 0;
for (int i = 0; i < 20; i++) {
if (s.equals(w[i].getItemno())) {
index = i;
flag = 1;
}
}
if (flag == 0) {
System.out.println("库中没有该商品");
changegood();
return;
} else {
System.out.println("***********************************************************");
System.out.println("\t石家庄铁道大学前进22软件开发有限公司");
System.out.println("\t\t仓库管理系统2022版");
System.out.println("***********************************************************");
System.out.println("\t\t1、商品编号:"+w[index].getItemno());
System.out.println("\t\t2、商品名称:"+w[index].getItemname());
System.out.println("\t\t3、供货商信息:"+w[index].getSuppliername());
System.out.println("\t\t4、入库时间:"+w[index].getWarehousingtime());
System.out.println("\t\t5、存放仓库号:"+w[index].getWarehousenumber());
System.out.println("\t\t6、存放位置信息:"+w[index].getWarehouseplace());
System.out.println("\t\t7、入库商品数量:"+w[index].getItemnumber());
System.out.println("出库时间:XXXXXXXX(请填写)");
System.out.println("出库数量:xxx(请填写)");
System.out.println("**********************************************************");
String otime= sc.next();
int onum= sc.nextInt();
if(otime.length()!=8){
System.out.println("出库时间错误");
return;
}
for(int i=0;i<8;i++){
if(otime.charAt(i)-'0'<0||otime.charAt(i)-'0'>9){
falg=1;
}
}
int k=10000000;
int ip=0,op=0;
for(int i=0;i<8;i++){
ip+=(w[index].getWarehousingtime().charAt(i)-'0')*k;
op+=(otime.charAt(i)-'0')*k;
k/=10;
}
if(ip>=op){
System.out.println("入库时间大于出库时间");
}
if(onum>w[index].getItemnumber()){
System.out.println("error!出库数量大于已入库数量");
return;
}else if(falg==1){
System.out.println("出库时间错误");
return;
} else{
System.out.println("***********************************************************");
System.out.println("\t石家庄铁道大学前进22软件开发有限公司");
System.out.println("\t\t仓库管理系统2022版");
System.out.println("***********************************************************");
System.out.println("\t\t1、商品编号:"+w[index].getItemno());
System.out.println("\t\t2、商品名称:"+w[index].getItemname());
System.out.println("\t\t3、供货商信息:"+w[index].getSuppliername());
System.out.println("\t\t4、入库时间:"+w[index].getWarehousingtime());
System.out.println("\t\t5、存放仓库号:"+w[index].getWarehousenumber());
System.out.println("\t\t6、存放位置信息:"+w[index].getWarehouseplace());
System.out.println("\t\t7、入库商品数量:"+w[index].getItemnumber());
System.out.println("出库时间:"+otime);
System.out.println("出库数量:"+onum);
System.out.println("该商品出库操作已完成,是否保存(Y/N)");
System.out.println("**********************************************************");
String type=sc.next();
if(type.equals("Y")||type.equals("y")){
w[index].setShipmenttime(otime);
w[index].setOutnumber(onum);
w[index].setItemnumber(w[index].getItemnumber()-onum);
System.out.println("出库操作完成,数据以保存");
}else if(type.equals("N")||type.equals("n")){
System.out.println("数据未保存");
outgood();
return;
}
}
}
}
public static void allsee(){
System.out.println("***********************************************************");
System.out.println("\t石家庄铁道大学前进22软件开发有限公司");
System.out.println("\t\t仓库管理系统2022版");
System.out.println("***********************************************************");
for(int i=0;i<20;i++){
if(w[i].getItemnumber()!=0){
System.out.println(i+1+"、 商品编号:"+w[i].getItemno()+"、商品名称:"+w[i].getItemname()+"、库存数量:"+w[i].getItemnumber());
}
}
System.out.println("**********************************************************");
}
}
按照建民老师的要求修改了程序框架
posted on 2023-09-16 10:46  带带带集美  阅读(9)  评论(0编辑  收藏  举报