package co.hp.lx.gtIo;
import co.hp.lx.gtIo.GoodsLx;
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
public class CounterLx {
int num;
private GoodsLx[] goods = new GoodsLx[10];
//展示柜台所有的商品(不能输出null)
private void show() throws IOException {
System.out.println("-->商品货架信息");
//创建字符流过滤输入流读入
FileReader fr = new FileReader("E:\\goods.txt");
BufferedReader br = new BufferedReader(fr);//封装为过滤流
//开始读取
int i = 0;
String line = null;
//readLine()一次读取一行的字符粗
while ((line = br.readLine()) != null){
String[] data = line.split(",");
//System.out.println(data);
//System.out.println(data[0]+","+data[1]+","+data[2]+","+data[3]);
//System.out.println(data[0]);
//System.out.println(Integer.getInteger(data[0])+data[1]+Integer.getInteger(data[2])+data[3]);
this.goods[i] = new GoodsLx(data[0],data[1],data[2],data[3]);
i++;
num ++;
}
//创建字符流过滤输出流读取
FileWriter fw = new FileWriter("E:\\goods.txt");
BufferedWriter bw = new BufferedWriter(fw);
// //开始写入
for (int j = 0; j < this.goods.length; j++) {
if (this.goods[j]!=null){
bw.write(this.goods[j].getId()+","+this.goods[j].getGoodsName()+","+this.goods[j].getPrice()+","+this.goods[j].getDesc());
bw.newLine();//newLine() 写行分隔符。
System.out.println(goods[j]);
}
}
br.close();
fr.close();
bw.close();
fw.close();
System.out.println("货架目前商品数量"+num);
return;
}
//修改商品价格
// private void updata() {
// //System.out.println("updata");
// show();
// System.out.println("请输入要修改的商品编号:");
// int id = 0;
// try {
// id = new Scanner(System.in).nextInt();
// } catch (Exception e) {
// //e.printStackTrace();
// System.out.println("请输入正确的格式如:1001...");
// updata();
// return;
// }
// for (int i = 0; i < this.lxgoods.length; i++) {
// if(this.lxgoods[i] != null && this.lxgoods[i].getId() == id) {
// System.out.println("商品的原价为" + this.lxgoods[i].getPrice());
// System.out.println("请输入要修改的商品价格:");
// double price = 0;
// try {
// price = new Scanner(System.in).nextDouble();
// } catch (Exception e) {
// //e.printStackTrace();
// System.out.println("请输入正确的格式如:10.1...");
// price = new Scanner(System.in).nextDouble();
// }
// //把改的价格初始化到原来的价格中
// GoodsLx lxgoods = new GoodsLx(this.lxgoods[i].getId(), this.lxgoods[i].getGoodsName(), price, this.lxgoods[i].getDesc());
// //最后再把新的商品信息放回到原来的位置
// this.lxgoods[i] = lxgoods;
// show();
// return;
// }
// }
// }
// //上架商品
// private void add() {
// // System.out.println("add");
// //判断货架是否还有空位,有空位才可以添加
// if (this.lxgoods.length == num) {
// System.out.println("糟糕!商品没位置放了");
// return;
// }
// System.out.println("请输入要添加的商品编号:");
// int id = 0;
// try {
// id = new Scanner(System.in).nextInt();
// } catch (Exception e) {
// //e.printStackTrace();
// System.out.println("请输入正确的格式如:1001...");
// add();
// return;
// }
// for (int i = 0; i < this.lxgoods.length; i++) {
// //判断商品编号是否存在
// if (this.lxgoods[i] != null && this.lxgoods[i].getId() == id) {
// System.out.println("商品编号已存在");
// add();
// }
// }
// System.out.println("请输入要添加的商品名称:");
// String name = new Scanner(System.in).next();
// System.out.println("请输入要添加的商品价格:");
// double price = 0;
// try {
// price = new Scanner(System.in).nextDouble();
// } catch (Exception e) {
// //e.printStackTrace();
// System.out.println("请输入正确的格式如:10.1...");
// price = new Scanner(System.in).nextDouble();
// }
// System.out.println("请输入要添加的商品描述:");
// String desc = new Scanner(System.in).next();
// //调用构造器,初始化商品信息
// GoodsLx lxgoods = new GoodsLx(id, name, price, desc);
// for (int i = 0; i < this.lxgoods.length; i++) {
// //把初始化的商品放入null的位置
// if (this.lxgoods[i] == null) {
// this.lxgoods[i] = lxgoods;
// System.out.println("商品添加成功");
// num++;
// show();
// return;
// }
// }
// }
// //下架商品
// private void delete() {
// System.out.println("请输入要删除的商品编号:");
// int id = 0;
// try {
// id = new Scanner(System.in).nextInt();
// } catch (Exception e) {
// //e.printStackTrace();
// System.out.println("请输入正确的格式如:1001...");
// }
// for (int i = 0; i < this.lxgoods.length; i++) {
// if(this.lxgoods[i] != null && this.lxgoods[i].getId() == id) {
// this.lxgoods[i] = null;
// num--;
// //System.out.println("货架目前商品数量" + num);
// show();
// return;
// }
// }
// System.out.println("商品不存在");
// delete();
// }
public void main() throws IOException {
while (true){
System.out.println("-->欢迎使用商品管理系统");
System.out.println("-->输入1显示所有商品信息");
System.out.println("-->输入2下架商品");
System.out.println("-->输入3上架商品");
System.out.println("-->输入4修改商品价格");
System.out.println("-->输入0退出商品货架");
System.out.println("-->请输入热键:");
int i = new Scanner(System.in).nextInt();
switch (i){
case 1:show();
break;
// case 2:delete();
// break;
// case 3:add();
// break;
// case 4:updata();
// break;
case 0:System.exit(0);//退出jvm
}
}
}
}
-->欢迎使用商品管理系统
-->输入1显示所有商品信息
-->输入2下架商品
-->输入3上架商品
-->输入4修改商品价格
-->输入0退出商品货架
-->请输入热键:
1
-->商品货架信息
GoodsLx{id=1001, goodsName='肠粉', price=6, desc='真好吃啊'}
GoodsLx{id=1002, goodsName='辣条', price=2, desc='真好吃啊'}
GoodsLx{id=1003, goodsName='巧克力', price=36, desc='真好吃啊'}
货架目前商品数量3