package come.u1.limingxing.shopping.imp;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import come.u1.limingxing.shopping.bean.Customers;
import come.u1.limingxing.shopping.bean.Goods;
import come.u1.limingxing.shopping.bean.ImportantCustomers;
import come.u1.limingxing.shopping.bean.User;
import come.u1.limingxing.shopping.factory.CustomersFactory;
import come.u1.limingxing.shopping.factory.NewgoodswriteXml;
import come.u1.limingxing.shopping.io.IAdmin;
import come.u1.limingxing.shopping.io.Userio;
import come.u1.limingxing.shopping.readxml.ReadXml;
import come.u1.limingxing.shopping.writexml.Writexml;
public class Userimp implements Userio {
// 总金额
private float impnum = 0;
public float getImpnum() {
return impnum;
}
public void setImpnum(float impnum) {
this.impnum = impnum;
}
@Override
public void scan(int id) throws Exception {
// TODO Auto-generated method stub
IAdmin iadmin = new IAdminimp();
List<Goods> goods = iadmin.show();
Goods good = null;
Boolean flag = false;
int yuancount = 0;
for (int i = 0; i < goods.size(); i++) {
// 判断商品中是否由此商品
if (id == goods.get(i).getId()) {
good = goods.get(i);
yuancount = good.getCount();
Iterator<Goods> it = Userio.GOODS.values().iterator();
flag = true;
if (Userio.GOODS.isEmpty()) {
// 订单中无商品
// 如果订单中没有此商品则添加此商品
good.setCount(1);
Userio.GOODS.put(good.getCount() * good.getPrice(), good);
System.out.println(good + "金额:" + good.getCount()
* good.getPrice());
// 修改原商品数量
NewgoodswriteXml.newgoodswritexml(id, yuancount);
break;
} else {
while (it.hasNext()) {
Goods g = it.next();
if (g.getId() == id) {
// 如果订单中有商品
// 判断订单中的商品数量是否大于商品中的数量
if (g.getCount() < yuancount) {
g.setCount(g.getCount() + 1);
System.out.println(g + "金额:" + g.getCount()
* g.getPrice());
// 修改原商品
NewgoodswriteXml
.newgoodswritexml(id, yuancount);
} else {
System.out.println("库存中已无,无法扫描");
}
break;
} else {
// 如果订单中没有此商品则添加此商品
good.setCount(1);
Userio.GOODS.put(good.getCount() * good.getPrice(),
good);
System.out.println(g + "金额:" + g.getCount()
* g.getPrice());
// 修改原商品
NewgoodswriteXml.newgoodswritexml(id, yuancount);
break;
}
}
break;
}
} else {
flag = false;
}
}
if (flag == false)
System.out.println("对不起没有此商品,扫描失败");
else {
System.out.println("扫描成功");
System.out.println("原商品中的数量已修改");
}
}
@Override
public void modify(int id) throws Exception {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
Goods good = null;
IAdmin iadmin = new IAdminimp();
// 原商品集合
List<Goods> oldgoods = iadmin.show();
int yuancount = 0;
for (int i = 0; i < oldgoods.size(); i++) {
if (id == oldgoods.get(i).getId()) {
yuancount = oldgoods.get(i).getCount();
break;
}
}
// 订单中商品迭代
Iterator<Goods> it = Userio.GOODS.values().iterator();
boolean flag = false;
while (it.hasNext()) {
good = it.next();
if (id == good.getId()) {
flag = true;
System.out.println("请输入修改数量");
int count = in.nextInt();
if (count <= yuancount) {
good.setCount(count);
System.out.println(good + "金额" + good.getCount()
* good.getPrice());
// 修改原商品
List<Goods> newgoods04 = new ArrayList<Goods>();
for (int j = 0; j < oldgoods.size(); j++) {
if (id == oldgoods.get(j).getId()) {
good = oldgoods.get(j);
good.setCount(yuancount - count);
newgoods04.add(good);
continue;
}
newgoods04.add(oldgoods.get(j));
}
// 写入xml
Writexml.writegoods(newgoods04);
System.out.println("原商品中的数量已修改");
} else {
System.out.println("库存不足无法修改数量");
System.out.println("请重新输入要扫描的商品编号");
int idd = in.nextInt();
this.modify(idd);
}
break;
} else {
flag = false;
}
}
if (flag == false) {
System.out.println("没有扫描此商品");
Scanner inn = new Scanner(System.in);
System.out.println("请输入要扫描的商品编号");
int idd = inn.nextInt();
this.scan(idd);
}
}
@Override
public void calc() throws Exception {
// TODO Auto-generated method stub
this.print();
if (this.impnum == 0) {
System.out.println("您还没有扫描商品");
} else {
System.out.println("1、普通账户 2、会员账户");
Scanner in = new Scanner(System.in);
int a = in.nextInt();
String classname = "come.u1.limingxing.shopping.bean.";
switch (a) {
case 1:
classname += "Customers";
break;
case 2:
classname += "ImportantCustomers";
break;
}
Customers c = CustomersFactory.getcustomers(classname);
c.setNum(this.impnum);
if (c instanceof ImportantCustomers) {
System.out.println("请输入卡号");
String imcustomersname = in.next();
ImportantCustomers i = ReadXml
.readimpcustomers(imcustomersname);
if (i == null) {
System.out.println("卡号不正确");
} else {
String impname = i.getImpcustomersname();
float balance = 0;
System.out.println("请输入金额");
float money = in.nextFloat();
if (money >= c.getNum()) {
// 找零
balance = money - c.getNum();
// 修改会员积分
int jifen = 10;
// 得到之前的积分
int lastjifen = i.getIntegral();
// 修改积分
((ImportantCustomers) c).setIntegral(lastjifen + jifen);
// 打印
System.out.println("票以打印");
FileWriter fw = new FileWriter("会员票"
+ new Date().getTime() + ".txt");
fw.write(" 会员超市 ");
fw.write("\r\n");
fw.write("收银员号:1001");
fw.write("\r\n");
fw.write("小票号:34646747963");
fw.write("\r\n");
Iterator<Goods> i1 = Userio.GOODS.values().iterator();
Goods good = null;
int discount = 0;
while (i1.hasNext()) {
good = i1.next();
fw.write(good.toString() + "金额:" + good.getCount()
* good.getPrice());
discount += good.getCount();
}
fw.write("\r\n");
fw.write("总数量:" + discount);
fw.write("\r\t\t");
fw.write("应收:" + c.getNum());
fw.write("\r\n");
fw.write("实收:" + money);
fw.write("\r\t\t");
fw.write("找零:" + balance);
fw.write("\r\n");
fw.write("本次累计成功 卡号:" + imcustomersname);
fw.write("\r\n");
String st = String.format(
"消费前积分:%d+本次消费积分:%d=消费后积分:%d", lastjifen,
jifen, ((ImportantCustomers) c).getIntegral());
fw.write(st);
fw.write("\r\n");
SimpleDateFormat str = new SimpleDateFormat(
"yyyy年MM月dd日 HH:mm:ss");
String date = str.format(new Date());
fw.write(date);
fw.write("\r\n");
fw.write("次小票是您退换及核对积分的凭证,请妥善保管");
fw.write("\r\n");
fw.write("==============================");
fw.close();
// 重写xml
System.out.println("卡号的积分已修改");
Writexml.writeimpcustomers(((ImportantCustomers) c)
.getIntegral());
// 清空
Userio.GOODS.clear();
} else {
- 1
- 2
前往页