收银和库存管理员操作
需求:
登陆后,判断用户是收银员还是库存管理员,收银员权限:对客户购买商品进行扫描、计算价格、收款、找零、打印小票
库存管理员:对商品库存进行增、删、改、查操作
关联地方:收银员扫描出的商品也就是被购买的商品,库存相应减少
这里没有用socket通信,因为我对这个的使用不熟悉,只是简单的2种操作
整体代码架构如下所示
一个一个的贴吧:
1、来2个XML吧,产品和用户角色
<?xml version="1.0" encoding="utf-8"?> <products> <product id="1003"> <name>豆腐</name> <price>2.5</price> <unit>500g</unit> <num>5</num> </product> <product id="1002"> <name>胡萝卜</name> <price>3.9</price> <unit>条</unit> <num>10</num> </product> <product id="1005"> <name>胡萝卜</name> <price>3.9</price> <unit>条</unit> <num>10</num> </product> </products>
用户信息XML
<?xml version="1.0" encoding="UTF-8"?> <users> <user id="BP101"> <name>admin</name> <pwd>123</pwd> <role>manager</role> </user> <user id="BP102"> <name>main</name> <pwd>123</pwd> <role>normal</role> </user> </use
2、类文件:处理逻辑
(1)产品接口类:主要是对产品的XML进行解析,增、删、改、查、更新
package biz; import java.util.List; import entity.Products; /* * 商品的接口 */ public interface IProductBiz { public List<Products> findAllPro(); public void addPro(Products pro); public void delPro(Products pro); public void updatePro(Products pro); }
(2)用户角色接口类
package biz; import java.util.List; import entity.Products; import entity.User; /* * 用户的接口,读取用户信息,进行登陆 */ public interface IUser { //显示用户方法,初始化用户 public List<User> showuser(); //登陆方法 public void Login(); //用户角色操作、商品、购买数量、用户付款给的钱额(用来算找零的) //普通 用户购买商品,收银员结帐、找零、打印清单、管理员对商品库存进行增、删、改、查操作 // public void operation(User user); }
(3)实体类:购买商品列表(方便 打印小票)、商品类、用户类
package entity; public class BuyProduct { //商品名\t\t购买数量\t\t单价\t\t金额 private String buyname; private int buynum; private double buyprice; private double buytotolprice; public String getBuyname() { return buyname; } public void setBuyname(String buyname) { this.buyname = buyname; } public int getBuynum() { return buynum; } public void setBuynum(int buynum) { this.buynum = buynum; } public double getBuyprice() { return buyprice; } public void setBuyprice(double buyprice) { this.buyprice = buyprice; } public double getBuytotolprice() { return buytotolprice; } public void setBuytotolprice(double buytotolprice) { this.buytotolprice = buytotolprice; } public BuyProduct() { } public BuyProduct(String buyname, int buynum, double buyprice, double buytotolprice) { this.buyname = buyname; this.buynum = buynum; this.buyprice = buyprice; this.buytotolprice = buytotolprice; } }
package entity; /* * 商品实体类 */ import org.dom4j.io.SAXReader; public class Products { private int id;//id private String name;//商品名称 private double price;//价格 private String unit;//单位 private int count;//库存数量 public Products() { } public Products(int id, String name, double price, String unit, int count) { this.id = id; this.name = name; this.price = price; this.unit = unit; this.count = count; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getUnit() { return unit; } public void setUnit(String unit) { this.unit = unit; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } }
package entity; /* * 用户实体类 */ public class User { private String id; private String username;//用户名 private String password;//密码 private String role;//角色 public User() { } public User(String id,String username, String password,String role) { this.id=id; this.username = username; this.password = password; this.role=role; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getRole() { return role; } public void setRole(String role) { this.role = role; } public String getId() { return id; } public void setId(String id) { this.id = id; } }
(4)工具类:生成XML-dom树进行解析,更新XML信息
package util; import java.io.File; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.io.SAXReader; /* * 获取dom树 */ public class GetDom { private Document document; private Document userdocument; public Document getdom(File file){ SAXReader sax=new SAXReader(); try { // 生成DOM树 this.document=sax.read(file); } catch (DocumentException e) { e.printStackTrace(); } return this.document; } public Document getuserdom(File file){ SAXReader sax=new SAXReader(); try { // 生成DOM树 this.userdocument=sax.read(file); } catch (DocumentException e) { e.printStackTrace(); } return this.userdocument; } }
package util; import java.io.File; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.OutputStreamWriter; import org.dom4j.Document; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; public class SaveXML { public void saveXML(File tofile,Document document){ OutputFormat of=OutputFormat.createPrettyPrint(); XMLWriter writer=null; // 设置编码 of.setEncoding("utf-8"); // 以XML格式输出到dom4j.xml中 // 把源树DOM树输出写进dom4j.xml中 try { writer=new XMLWriter(new FileOutputStream(tofile),of); writer.write(document); writer.flush(); } catch (IOException e) { e.printStackTrace(); }finally{ try { writer.close(); } catch (IOException e) { e.printStackTrace(); } } } }
(5)实现类:实现商品新删改查的,对商品XML进行解析
package biz.impl;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import util.GetDom;
import util.SaveXML;
import entity.Products;
import biz.IProductBiz;
public class ProductImpl implements IProductBiz{
GetDom dom=new GetDom();
File file=new File("product.xml");
File tofile=new File("product.xml");
// 获取dom树
Document doc=dom.getdom(file);
Element root=doc.getRootElement();
//保存XML对象
SaveXML save=new SaveXML();
// System.out.println("获取dom树成功");
@Override
public List<Products> findAllPro() {
List<Products> prolist=new ArrayList<Products>();
List<Element> product=root.elements();
// System.out.println(product.size());
for (Iterator<Element> elementiter =product.iterator();elementiter.hasNext();) {
Products pro=new Products();
Element productele=elementiter.next();
pro.setId(Integer.parseInt(productele.attributeValue("id")));
List<Element> child=productele.elements();
// System.out.println(child.size());
for(Iterator<Element> childiter=child.iterator();childiter.hasNext();){
Element tagname=childiter.next();
if(tagname.getName().equals("name")){
pro.setName(tagname.getText());
}
if(tagname.getName().equals("price")){
pro.setPrice(Double.parseDouble(tagname.getText()));
}
if(tagname.getName().equals("unit")){
pro.setUnit(tagname.getText());
}
if(tagname.getName().equals("num")){
pro.setCount(Integer.parseInt(tagname.getText()));
}
}
prolist.add(pro);
}
//遍历集合
System.out.println("*********************商品信息如下:*******************");
for(Iterator<Products> proiter=prolist.iterator();proiter.hasNext();){
Products pro=proiter.next();
System.out.println(pro.getId()+"\t"+pro.getName()+"\t"+pro.getPrice()+"\t"+pro.getUnit()+"\t"+pro.getCount());
}
return prolist;
}
@Override
// 新增商品
public void addPro(Products pro) {
Element product=root.addElement("product");
product.addAttribute("id", pro.getId()+"");
Element name=product.addElement("name");
name.setText(pro.getName());
Element price=product.addElement("price");
price.setText(pro.getPrice()+"");
Element unit=product.addElement("unit");
unit.setText(pro.getUnit());
Element num=product.addElement("num");
num.setText(pro.getCount()+"");
System.out.println(pro.getName());
save.saveXML(tofile,doc);
}
@Override
// 删除元素
public void delPro(Products pro) {
List<Element> productlist=root.elements();
for(Iterator<Element> productiter=productlist.iterator();productiter.hasNext();){
Element product=productiter.next();
if(product.attributeValue("id").equals(pro.getId()+"")){
product.getParent().remove(product);
}
}
save.saveXML(tofile, doc);
}
@Override
// 修改商品信息
public void updatePro(Products pro) {
List<Element> productlist=root.elements();
for(Iterator<Element> productiter=productlist.iterator();productiter.hasNext();){
Element product=productiter.next();
//如果商品编号与XML中编号相同
if(product.attributeValue("id").equals(pro.getId()+"")){
// 开始便利product下面节点元素文本值
List<Element> tagnames=product.elements();
for(Iterator<Element> tagter=tagnames.iterator();tagter.hasNext();){
Element tag=tagter.next();
//如果商品各个属性不为空且XML中节点名 同时判断,因为这一级节点,一共有4个同级的节点,所以要用4个IF
if(pro.getName()!=null && tag.getName().equals("name")){
tag.setText(pro.getName());
}
if(pro.getPrice()!=0 && tag.getName().equals("price")){
tag.setText(pro.getPrice()+"");
}
if(pro.getUnit()!=null && tag.getName().equals("unit")){
tag.setText(pro.getUnit());
}
if(pro.getCount()!=0 && tag.getName().equals("num")){
tag.setText(pro.getCount()+"");
}
}
}
}
save.saveXML(tofile, doc);
}
/*public static void main(String[] args) {
ProductImpl pl=new ProductImpl();
Products pro=new Products(1005, "胡萝卜", 3.9, "条", 10);
pl.addPro(pro);
Products pro1=new Products(1001, "白菜", 3.9, "条", 10);
pl.updatePro(pro1);
// pl.findAllPro();
pl.delPro(pro1);
}*/
}
(4)实现类:实现用户登陆、收银、库存管理等的操作
package biz.impl;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import org.dom4j.Document;
import org.dom4j.Element;
import util.GetDom;
import util.SaveXML;
import entity.BuyProduct;
import entity.Products;
import entity.User;
import biz.IUser;
/*
* 用户实现接口类,登陆用户
*/
public class UserImpl implements IUser{
BuyProduct bp;
List<User> userlist=new ArrayList<User>();
String userid=null;
Scanner input=new Scanner(System.in);
@Override
// 读XML文件中的内容,对User进行初始化
public List<User> showuser() {
// 先读XML判断用户名和密码是否是一致
GetDom getdom=new GetDom();
Document document=getdom.getuserdom(new File("user.xml"));
//得到根元素users
Element root=document.getRootElement();
List<Element> userlist1=root.elements();
for(Iterator<Element> useriter=userlist1.iterator();useriter.hasNext();){
Element userele=useriter.next();
User user1=new User();
user1.setId(userele.attributeValue("id"));
List<Element>tagnames=userele.elements();
for(Iterator<Element> tagiter=tagnames.iterator();tagiter.hasNext();){
Element tags=tagiter.next();
if(tags.getName().equals("name")){
user1.setUsername(tags.getText());
}
if(tags.getName().equals("pwd")){
user1.setPassword(tags.getText());
}
if(tags.getName().equals("role")){
user1.setRole(tags.getText());
}
}
userlist.add(user1);
}
return userlist;
}
//登陆的方法
public void Login() {
showuser();
// 先读XML判断用户名和密码是否是一致
String username = null;
String pwd = null;
int flag = 0;
int count=0;
User user=null;
do {
System.out.println("1. 登陆\n2. 退出系统");
System.out.println("请选择1/2==============================================");
int num = input.nextInt();
if (num == 1) {
System.out.println("请输入用户名:");
username = input.next();
System.out.println("请输入密码:");
pwd = input.next();
for (Iterator<User> useriter = userlist.iterator(); useriter.hasNext();) {
user = useriter.next();
if (user.getUsername().equals(username) && user.getPassword().equals(pwd) ) {
count++;
flag++;
if(user.getRole().equals("normal")){
System.out.println("欢迎,收银员!");
cashier();
}else if(user.getRole().equals("manager")){
System.out.println("欢迎,库存管理员!");
manager();
}
}
}
if(count==0){
System.out.println("此用户不存在");
}
}
} while (flag == 0);
}
//收银的方法
public void cashier() {
// 用户给的付金额
double pay = 0.0;
int allcount = 0;// 总计多少件商品
int buycount = 0;// 单个商品买了多少件
// 找零
double change = 0.0;
// 某件商品的价格
double oneprice = 0.0;
// 购买商品,随便选择2样商品放入购物清单里
double totalmoney = 0.0;
int count=0;
Products product = null;
String want = null;
ProductImpl pi = new ProductImpl();
List<Products> productlist = pi.findAllPro();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat df1 = new SimpleDateFormat("HH:mm:ss");// 设置日期格式
// 定一个购买列表,把买的东西的相关信息存进去
List<BuyProduct> buylist = new ArrayList();
// 如果是收银员,则进行收费、打印工作,
do {
System.out.println("请输入购买商品编号");
int productid = input.nextInt();
for (Iterator<Products> proiter = productlist.iterator(); proiter.hasNext();) {
product = proiter.next();
if (product.getId() == productid) {
count++;
}
}
if(count>0){
// 扫描商品
System.out.println("请输入购买商品数量:");
buycount = input.nextInt();
// 计算某件商品的价格
oneprice = (product.getPrice()) * (buycount);
product.setCount(product.getCount()-buycount);
System.out.println(product.getName()+"-------"+product.getCount());
pi.updatePro(product);
break;
}else{
System.out.println("不存在此商品!");
}
// 购买商品总价格
totalmoney = totalmoney+oneprice;
// 计算总购买个数
allcount = allcount+buycount;
bp = new BuyProduct(product.getName(), buycount,
product.getPrice(), oneprice);
buylist.add(bp);
System.out.println(buylist.size());
System.out.print("是否继续购买还是结账(gou/pay):");
want = input.next();
} while (want.equals("gou"));
// 应付款
System.out.println("应付款:" + totalmoney);
// 实收款
System.out.println("实收款:");
pay = input.nextDouble();
// 找零
change = pay - totalmoney;
// 打印小票
System.out.println("\n\n华润万家便利超市");
System.out.println("收银员:" + userid + "\t日期:" + df.format(new Date()));
System.out.println("===============================================");
System.out.println("商品名\t\t购买数量\t\t单价\t\t金额");
for (Iterator<BuyProduct> buyiter = buylist.iterator(); buyiter
.hasNext();) {
BuyProduct bp1 = buyiter.next();
System.out.println(bp1.getBuyname() + "\t\t" + bp1.getBuynum()
+ "\t\t" + bp1.getBuyprice() + "\t\t"
+ bp1.getBuytotolprice());
}
System.out.println("-----------------------------------------------");
System.out.println("收款:人民币" + "\t\t" + pay);
System.out.println("收款总额:" + pay + "\t\t找回:" + change);
System.out.println("实收:" + totalmoney);
System.out.println("商品总计:" + allcount);
System.out.println("===================" + df1.format(new Date())
+ "===================");
System.out.println("欢迎再次惠顾-服务电话:020-38047535\n华润万家南区客服电话:4008222666");
}
//管理员操作
public void manager(){
ProductImpl pi=new ProductImpl();
List<Products> prolist=pi.findAllPro();
Products pro=null;
int num;
String name;
double price;
String unit;
int count;
int count1=0;
boolean flag=true;
System.out.println("你好,库存管理员!");
do{
System.out.print("请输入你要进行的操作:1、新增商品 2、修改商品 3、删除商品 4、查询商品信息 5、退出系统");
int want=input.nextInt();
switch(want){
case 1:
//新增商品
System.out.print("请输入商品的编号:");
num=input.nextInt();
for(Iterator<Products> proiter=prolist.iterator();proiter.hasNext();){
Products product=proiter.next();
if(product.getId()!=num){
count1++;
}
}
if(count1>0){
//大于0不存在
//判断商品编号是存在,不能重复,不存在 2种情况
System.out.print("请输入商品名称:");
name=input.next();
System.out.print("请输入商品价格:");
price=input.nextDouble();
System.out.print("请输入商品单位:");
unit=input.next();
System.out.print("请输入商品数量:");
count=input.nextInt();
pro=new Products(num,name,price,unit,count);
pi.addPro(pro);
prolist.add(pro);
System.out.println("新增商品成功!");
}else{
System.out.println("该编号的商品已存在");
}
break;
case 2:
//修改商品,这里为了简化,修改一个全部项都要修改
System.out.print("请输入要修改的商品的编号:");
num=input.nextInt();
//遍历集合,如果id存在,则进行其它修改,如果不存在,则提示不存在,重新输入
for(Iterator<Products> proiter=prolist.iterator();proiter.hasNext();){
pro=proiter.next();
if(pro.getId()==num){
count1++;
}
}
if(count1>0){
System.out.println(num+"的名称修改为:");
name=input.next();
System.out.println(num+"的价格修改为:");
price=input.nextDouble();
System.out.println(num+"的单位修改为:");
unit=input.next();
System.out.println(num+"的数量修改为:");
count=input.nextInt();
pro=new Products(num,name,price,unit,count);
pi.updatePro(pro);
System.out.println("商品修改成功!");
}else{
System.out.println("不存在此商品!");
}
break;
case 3:
//删除商品,找着ID直接删除整块
System.out.print("请输入要删除的商品的编号:");
num=input.nextInt();
//遍历集合,如果id存在,则进行其它修改,如果不存在,则提示不存在,重新输入
for(Iterator<Products> proiter=prolist.iterator();proiter.hasNext();){
pro=proiter.next();
if(pro.getId()==num){
count1++;
}
}
if(count1>0){
num=pro.getId();
name=pro.getName();
price=pro.getPrice();
unit=pro.getUnit();
count=pro.getCount();
pi.delPro(pro);
System.out.println("商品删除成功!");
}else{
System.out.println("不存在此商品");
}
break;
case 4:
//查询商品信息
System.out.println("*********************商品信息如下:*******************");
for(Iterator<Products> proiter=prolist.iterator();proiter.hasNext();){
pro=proiter.next();
System.out.println(pro.getId()+"\t"+pro.getName()+"\t"+pro.getPrice()+"\t"+pro.getUnit()+"\t"+pro.getCount());
}
break;
default:
//退出系统
System.out.println("退出系统!");
flag=false;
}
}while(flag);
}
public static void main(String[] args) {
UserImpl ui=new UserImpl();
ui.Login();
}
}
如果您认为阅读这篇博客让您有些收获,不妨点击一下右下角的【推荐】
本文版权归作者和博客园共有,欢迎转载