采用MVC风格,将数据写入文件,模拟公司置办货物系统。
A类表示普通员工,B类表示部门精力,C类表示采购部,D类表示资源管理部。
订单状态 1.表示申请状态 2.表示通过审批 3.表示未通过审批 4.表示订单完成,购物完成
第一步:首先创建Employeer类,代表员工类.
package com.jereh14;
public class Employeer implements java.io.Serializable{
private String name;
private int empNo;
private String pwd;
private String lev;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getEmpNo() {
return empNo;
}
public void setEmpNo(int empNo) {
this.empNo = empNo;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getLev() {
return lev;
}
public void setLev(String lev) {
this.lev = lev;
}
}
定义EmployeerBiz类,实现员工增删改查功能.
package com.jereh14;
import java.io.*;
import java.util.*;
public class EmployeerBiz {
private List<Employeer> list = new ArrayList<Employeer>();
public void addEmp(Employeer emp){
readDb();
list.add(emp);
writeDb();
}
public void updateEmp(Employeer emp){
readDb();
Iterator<Employeer> i = list.iterator();
while(i.hasNext()){
Employeer e = i.next();
if(e.getEmpNo()==emp.getEmpNo()){
list.remove(e);
list.add(emp);
}
}
writeDb();
}
public void deleteEmp(int empNo){
readDb();
Iterator<Employeer> i = list.iterator();
while(i.hasNext()){
Employeer e = i.next();
if(e.getEmpNo() == empNo){
list.remove(e);
break;
}
}
writeDb();
}
public String searchPor(int userId,String pwd){
readDb();
String type = "";
Iterator<Employeer> i = list.iterator();
while(i.hasNext()){
Employeer e = i.next();
if(e.getEmpNo()==userId&&e.getPwd().equals(pwd)){
type = e.getLev();
break;
}else{
type = null;
}
}
writeDb();
return type;
}
public void showEmp(){
readDb();
Iterator<Employeer> i = list.iterator();
while(i.hasNext()){
Employeer e = i.next();
System.out.println("\t"+e.getEmpNo()+"\t"+e.getName()+"\t"+e.getPwd()+"\t"+e.getLev());
}
writeDb();
}
public boolean check(int upEmpNo){
Iterator<Employeer> i = list.iterator();
boolean bool = false;
while(i.hasNext()){
Employeer e = i.next();
if(e.getEmpNo() == upEmpNo){
bool = true;
break;
}else{
bool = false;
}
}
return bool;
}
public void readDb(){
FileInputStream fis = null;
ObjectInputStream is = null;
InputStreamReader isr = null;
BufferedReader bis = null;
try {
fis = new FileInputStream("F:\\test\\Employeer.txt");
is = new ObjectInputStream(fis);
list = (List<Employeer>) is.readObject();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void writeDb(){
FileOutputStream fos = null;
ObjectOutputStream os = null;
try {
fos = new FileOutputStream("F:\\test\\Employeer.txt");
os = new ObjectOutputStream(fos);
os.writeObject(list);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
定义EmployeerView,用于与用户交互。
package com.jereh14;
import java.util.*;
public class EmployeerView {
public void empView(){
Scanner scn = new Scanner(System.in);
Employeer emp = null;
EmployeerBiz elb = new EmployeerBiz();
System.out.println("===============欢迎来到雇员管理===============");
while(true){
System.out.println("\t\t1、添加雇员\n\t\t2、修改雇员\n\t\t3、删除雇员\n\t\t4、查看雇员\n\t\t5、退出系统");
System.out.println("********************************************");
System.out.print("请选择您的操作:");
int chooseNum = scn.nextInt();
switch(chooseNum){
case 1:
System.out.println("===================添加雇员==================");
System.out.print("请输入雇员编号:");
int empNo = scn.nextInt();
System.out.print("请输入雇员姓名:");
String name = scn.next();
System.out.print("请输入登录密码:");
String pwd = scn.next();
System.out.print("请输入雇员类型:");
String lev = scn.next();
emp = new Employeer();
emp.setEmpNo(empNo);
emp.setName(name);
emp.setPwd(pwd);
emp.setLev(lev);
elb.addEmp(emp);
if(elb.check(empNo)){
System.out.println("--添加成功!--");
}else{
System.out.println("--添加失败!--");
}
System.out.println("********************************************");
break;
case 2:
System.out.println("===================修改雇员==================");
System.out.print("请输入雇员编号:");
int upEmpNo = scn.nextInt();
if(elb.check(upEmpNo)){
System.out.print("请输入要修改的姓名:");
String upName = scn.next();
System.out.print("请输入要修改的密码:");
String upPwd = scn.next();
System.out.print("请输入要修改的类型:");
String upLev = scn.next();
emp = new Employeer();
emp.setEmpNo(upEmpNo);
emp.setName(upName);
emp.setPwd(upPwd);
emp.setLev(upLev);
elb.updateEmp(emp);
System.out.println("--修改成功!--");
}else{
System.out.println("没有该雇员!");
}
System.out.println("********************************************");
break;
case 3:
System.out.println("===================删除雇员==================");
System.out.print("请输入雇员编号:");
int delEmpNo = scn.nextInt();
if(elb.check(delEmpNo)){
elb.deleteEmp(delEmpNo);
System.out.println("--删除成功!--");
}else{
System.out.println("没有该雇员!");
}
System.out.println("********************************************");
break;
case 4:
System.out.println("===================查看雇员==================");
System.out.println("\t编号\t姓名\t密码\t类型");
elb.showEmp();
System.out.println("********************************************");
break;
case 5:
System.out.println("谢谢您的使用!");
System.exit(0);
break;
}
}
}
}
运行效果图:
第二步:创建Por类代表订单类。
package com.jereh14;
public class Por implements java.io.Serializable{
public int getPor_no() {
return por_no;
}
public void setPor_no(int por_no) {
this.por_no = por_no;
}
public String getPart_desc() {
return part_desc;
}
public void setPart_desc(String part_desc) {
this.part_desc = part_desc;
}
public String getPart_type() {
return part_type;
}
public void setPart_type(String part_type) {
this.part_type = part_type;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public String getPart_use() {
return part_use;
}
public void setPart_use(String part_use) {
this.part_use = part_use;
}
public String getPnd() {
return pnd;
}
public void setPnd(String pnd) {
this.pnd = pnd;
}
public String getRgdt() {
return rgdt;
}
public void setRgdt(String rgdt) {
this.rgdt = rgdt;
}
public int getStus() {
return stus;
}
public void setStus(int stus) {
this.stus = stus;
}
public String getRgdt_emp() {
return rgdt_emp;
}
public void setRgdt_emp(String rgdt_emp) {
this.rgdt_emp = rgdt_emp;
}
public String getCf_date() {
return cf_date;
}
public void setCf_date(String cf_date) {
this.cf_date = cf_date;
}
public String getCf_emp() {
return cf_emp;
}
public void setCf_emp(String cf_emp) {
this.cf_emp = cf_emp;
}
private int por_no;
private String part_desc;
private String part_type;
private int count;
private String part_use;
private String pnd;
private String rgdt;
private int stus;
private String rgdt_emp;
private String cf_date;
private String cf_emp;
}
定义PorBiz类,实现订单的增删改查。
package com.jereh14;
import java.io.*;
import java.util.*;
public class PorBiz {
private List<Por> list = new ArrayList<Por>();
public void addPor(Por por){
readDb();
list.add(por);
writeDb();
}
public void updatePor(Por por){
readDb();
Iterator<Por> i = list.iterator();
while(i.hasNext()){
Por p = i.next();
if(p.getPor_no()==por.getPor_no()){
list.remove(p);
list.add(por);
}
}
writeDb();
}
public boolean changeStuts(int porId,int chooseNum){
readDb();
boolean bool = false;
Iterator<Por> i = list.iterator();
while(i.hasNext()){
Por p = i.next();
if(p.getPor_no() == porId){
if(chooseNum == 1){
p.setStus(2);
}else if(chooseNum == 2){
p.setStus(3);
}else {
p.setStus(4);
}
bool = true;
break;
}else{
bool = false;
}
}
writeDb();
return bool;
}
public void deletePor(int del_por_no){
readDb();
Iterator<Por> i = list.iterator();
while(i.hasNext()){
Por p = i.next();
if(p.getPor_no() == del_por_no){
list.remove(p);
break;
}
}
writeDb();
}
//传入type,决定输入的订单类型,1表示全部输出,2表示只输出状态为1的订单.
public void showPor(int type){
readDb();
Iterator<Por> i = list.iterator();
if(type == 1){
while(i.hasNext()){
Por p = i.next();
System.out.println(p.getPor_no()+"\t"+p.getPart_desc()+"\t"+p.getPart_type()+"\t"+p.getCount()+"\t"+
p.getPart_use()+"\t"+p.getPnd()+"\t"+p.getRgdt()+"\t"+p.getStus()+"\t"+p.getRgdt_emp()+"\t"+
p.getCf_date()+"\t"+p.getCf_emp());
}
}else if(type == 2){
while(i.hasNext()){
Por p = i.next();
if(p.getStus()==1){
System.out.println(p.getPor_no()+"\t"+p.getPart_desc()+"\t"+p.getPart_type()+"\t"+p.getCount()+"\t"+
p.getPart_use()+"\t"+p.getPnd()+"\t"+p.getRgdt()+"\t"+p.getStus()+"\t"+p.getRgdt_emp()+"\t"+
p.getCf_date()+"\t"+p.getCf_emp());
}
}
}else{
while(i.hasNext()){
Por p = i.next();
if(p.getStus()==2){
System.out.println(p.getPor_no()+"\t"+p.getPart_desc()+"\t"+p.getPart_type()+"\t"+p.getCount()+"\t"+
p.getPart_use()+"\t"+p.getPnd()+"\t"+p.getRgdt()+"\t"+p.getStus()+"\t"+p.getRgdt_emp()+"\t"+
p.getCf_date()+"\t"+p.getCf_emp());
}
}
}
writeDb();
}
public boolean check(int por_no){
Iterator<Por> i = list.iterator();
boolean bool = false;
while(i.hasNext()){
Por p = i.next();
if(p.getPor_no() == por_no){
bool = true;
break;
}else{
bool = false;
}
}
return bool;
}
public Por getPor(int porId){
Iterator<Por> i = list.iterator();
while(i.hasNext()){
Por p = i.next();
if(p.getPor_no() == porId){
return p;
}else{
p = null;
}
}
return null;
}
public void readDb(){
FileInputStream fis = null;
ObjectInputStream is = null;
try {
fis = new FileInputStream("F:\\test\\Por.txt");
is = new ObjectInputStream(fis);
list = (List<Por>) is.readObject();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void writeDb(){
FileOutputStream fos = null;
ObjectOutputStream os = null;
try {
fos = new FileOutputStream("F:\\test\\Por.txt");
os = new ObjectOutputStream(fos);
os.writeObject(list);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
定义PorView类,用于与用户交互。
package com.jereh14;
import java.util.Scanner;
public class PorView {
public void porView(){
Scanner scn = new Scanner(System.in);
Por por = null;
PorBiz pb = new PorBiz();
System.out.println("===============欢迎来到采购申请===============");
while(true){
System.out.println("\t\t1、添加申请\n\t\t2、修改申请\n\t\t3、删除申请\n\t\t4、查看申请\n\t\t5、退出系统");
System.out.println("********************************************");
System.out.print("请选择您的操作:");
int chooseNum = scn.nextInt();
switch(chooseNum){
case 1:
System.out.println("===================添加申请==================");
System.out.print("请输入订单编号:");
int por_no = scn.nextInt();
System.out.print("请输入订单物品:");
String part_desc = scn.next();
System.out.print("请输入物品型号:");
String part_type = scn.next();
System.out.print("请输入物品数量:");
int count = scn.nextInt();
System.out.print("请输入物品用途:");
String part_use = scn.next();
System.out.print("请输入需要日期:");
String pnd = scn.next();
System.out.print("请输入申请日期:");
String rgdt = scn.next();
System.out.print("请输入订单状态:");
int stus = scn.nextInt();
System.out.print("请输入申请者:");
String rgdt_emp = scn.next();
System.out.print("请输入批准日期:");
String cf_date = scn.next();
System.out.print("请输入审核者:");
String cf_emp = scn.next();
por = new Por();
por.setPor_no(por_no);
por.setPart_desc(part_desc);
por.setPart_type(part_type);
por.setCount(count);
por.setPart_use(part_use);
por.setPnd(pnd);
por.setRgdt(rgdt);
por.setStus(stus);
por.setRgdt_emp(rgdt_emp);
por.setCf_date(cf_date);
por.setCf_emp(cf_emp);
pb.addPor(por);
if(pb.check(por_no)){
System.out.println("--添加成功!--");
}else{
System.out.println("--添加失败!--");
}
System.out.println("********************************************");
break;
case 2:
System.out.println("===================修改申请==================");
System.out.print("请输入申请编号:");
int up_por_no = scn.nextInt();
if(pb.check(up_por_no)){
System.out.print("请输入订单物品:");
String up_part_desc = scn.next();
System.out.print("请输入物品型号:");
String up_part_type = scn.next();
System.out.print("请输入物品数量:");
int up_count = scn.nextInt();
System.out.print("请输入物品用途:");
String up_part_use = scn.next();
System.out.print("请输入需要日期:");
String up_pnd = scn.next();
System.out.print("请输入申请日期:");
String up_rgdt = scn.next();
System.out.print("请输入订单状态:");
int up_stus = scn.nextInt();
System.out.print("请输入申请者:");
String up_rgdt_emp = scn.next();
System.out.print("请输入批准日期:");
String up_cf_date = scn.next();
System.out.print("请输入审核者:");
String up_cf_emp = scn.next();
por = new Por();
por.setPor_no(up_por_no);
por.setPart_desc(up_part_desc);
por.setPart_type(up_part_type);
por.setCount(up_count);
por.setPart_use(up_part_use);
por.setPnd(up_pnd);
por.setRgdt(up_rgdt_emp);
por.setStus(up_stus);
por.setRgdt_emp(up_rgdt_emp);
por.setCf_date(up_cf_date);
por.setCf_emp(up_cf_emp);
pb.updatePor(por);
System.out.println("--修改成功!--");
}else{
System.out.println("--没有该申请!--");
}
System.out.println("********************************************");
break;
case 3:
System.out.println("===================删除申请==================");
System.out.print("请输入申请编号:");
int del_por_no = scn.nextInt();
if(pb.check(del_por_no)){
pb.deletePor(del_por_no);
System.out.println("--删除成功!--");
}else{
System.out.println("--没有该申请!--");
}
System.out.println("********************************************");
break;
case 4:
System.out.println("===================查看申请==========================================================\n");
System.out.println("编号\t名称\t型号\t数量\t用途\t需要日期\t申请日期\t状态\t申请者\t批准日期\t审核者\n");
pb.showPor(1);
System.out.println("\n************************************************************************************");
break;
case 5:
System.out.println("谢谢您的使用!");
System.exit(0);
break;
default:
System.out.println("--输入有误!--");
break;
}
}
}
}
运行效果图:
定义PorCfView类,用于审批订单。
package com.jereh14;
import java.util.Scanner;
public class PorCfView {
public void showInfoView(){
Scanner scn = new Scanner(System.in);
PorBiz pb = new PorBiz();
while(true){
System.out.println("=======================================订单审批=======================================\n");
System.out.println("编号\t名称\t型号\t数量\t用途\t需要日期\t申请日期\t状态\t申请者\t批准日期\t审核者\n");
pb.showPor(2);
System.out.println("\n******************************1、批准\t2、驳回\t3、退出*******************************");
System.out.print("请选择您的操作:");
int chooseNum = scn.nextInt();
int porId = -1;
switch(chooseNum){
case 1:
System.out.print("请输入您要操作的单号:");
porId = scn.nextInt();
if(pb.changeStuts(porId,1)){
System.out.println("--订单审批完成!--\n");
}else{
System.out.println("--该订单不存在!--\n");
}
break;
case 2:
System.out.print("请输入您要操作的单号:");
porId = scn.nextInt();
if(pb.changeStuts(porId,2)){
System.out.println("--该订单被拒绝!--\n");
}else{
System.out.println("--该订单不存在!--\n");
}
break;
case 3:
System.out.println("谢谢您的使用!");
System.exit(0);
break;
default:
System.out.println("--输入有误!--");
break;
}
}
}
}
运行效果图:
下篇继续。