如:goods.txt,参考数据
1001,肠粉,6,真好吃啊,那些吃不到的人真可怜..
1002,辣条,2,真好吃啊,那些吃不到的人真可怜..
1003,巧克力,36,真好吃啊,那些吃不到的人真可怜..
package cn.hp.demo02;
import java.io.*;
import java.util.Scanner;
public class Counter {
//为什么定义数组来作为柜台的存储?
private Goods[]goodses = new Goods[10];//商品柜台,固定10个
private int num;//柜台商品数量
//无参构造器初始化2个商品
public Counter(){
//从文件初始化数据
//创建字符流过滤流输入流
FileReader in = null;//字符流节点流
try {
in = new FileReader("data.txt");
BufferedReader br = new BufferedReader(in);//字符流过滤流(对上面的字符流节点流进行了封装)
//使用字符流过滤流读取字符
int i = 0;//作为下标
String s = null;
while ((s = br.readLine()) != null) {//一次读取一行,读取不到返回null
//System.out.println(s);
//把字符串数据获取,转存为Goods对象
//数据:1001,肠粉,6,真好吃啊,那些吃不到的人真可怜..
String[] datas = s.split(",");
this.goodses[i] = new Goods(Integer.parseInt(datas[0]), datas[1], Double.parseDouble(datas[2]), datas[3]);
i++;
this.num++;//数量+1
}
//关闭流释放资源
br.close();
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//柜台的功能菜单
public void main() throws Exception {
//在这里启动子线程,没过10秒备份
UpdateDataThread t = new UpdateDataThread();
t.setCounter(this);//当前柜台对象,传到子线程中,在子线程中就可以调用Counter的备份方法
t.start();//启动后台子线程,每过10秒的备份
while(true){
System.out.println("--------------------------------");
System.out.println("-- 1.商品列表 2.上架商品---");
System.out.println("-- 3.下架商品 4.调整价格---");
System.out.println("-- 0.退出 ---");
//选择执行功能
System.out.println("-->请输入0-4的数字选择功能执行:");
int key = InputUtils.getNum();
switch (key){
case 1:this.show();break;
case 2:this.add();break;
case 3:this.delete();break;
case 4:this.update();break;
case 0:
//退出的时候,把数组的数组写入到文件中
updateData();
System.exit(0);//退出Java虚拟机(JVM)
}
}
}
public void updateData() {
try {
//创建BufferedWriter写入数据
BufferedWriter bw = new BufferedWriter(new FileWriter("data.txt"));
//循环数组中的数据
for (int i = 0; i < this.goodses.length; i++) {
if(this.goodses[i]!=null){
//数据:1001,肠粉,6,真好吃啊,那些吃不到的人真可怜..
bw.write(this.goodses[i].getId()+","+this.goodses[i].getGoodsName()+","+this.goodses[i].getPrice()+","+this.goodses[i].getDesc());
bw.newLine();//换行,一行字符串就是一个商品信息
}
}
bw.close();
System.out.println("-->数据已更新到data.txt文件中...");
} catch (IOException e) {
e.printStackTrace();
}
}
private void show() throws Exception {
System.out.println("------------- 柜台商品信息 ---------------");
if(this.num==0){
System.out.println("-->警告:柜台没有商品!!");
return;
}
for (int i = 0; i < this.goodses.length; i++) {
if(this.goodses[i]!=null){
System.out.println(this.goodses[i]);
}
}
System.out.println("------------------------------------------");
}
private void update() {
//当前柜台是否为空
if(this.num==0){
System.out.println("-->警告:柜台已空!!没得修改!!");
return;
}
System.out.println("-->请输入商品id:");
int goodsId = InputUtils.getGoodsId();
//查找
for (int i = 0; i < this.goodses.length; i++) {
//找为null的位置
if(this.goodses[i]!=null && this.goodses[i].getId()==goodsId){
System.out.println(this.goodses[i]);//重现商品信息
System.out.println("-->请输入商品价格:");
double price = new Scanner(System.in).nextDouble();
this.goodses[i].setPrice(price);//底层实际改价格的代码
System.out.println("-->价格调整成功!!!");
return;//方法结束
}
}
System.out.println("-->警告:商品找不到!!");
}
private void delete() {
//当前柜台是否为空
if(this.num==0){
System.out.println("-->警告:柜台已空!!无法删除!!");
return;
}
System.out.println("-->请输入要删除的商品的id:");
int goodsId = InputUtils.getGoodsId();
//从柜台列表(属性:数组)
for (int i = 0; i < this.goodses.length; i++) {
//查看是否有匹配的id
if(this.goodses[i]!=null && this.goodses[i].getId()==goodsId){
System.out.println("-->提示:商品已找到,正在删除...");
this.goodses[i] = null;//实际删除代码
this.num--;//柜台商品的数量
System.out.println("-->删除成功!!!");
return;//方法结束
}
}
//如果程序执行到这里,说明什么?说明没有找到对应id的商品
System.out.println("-->警告:商品不存在,删除失败!!");
}
private void add() {
//当前柜台是否已满
if(this.num==10){
System.out.println("-->警告:柜台已满!!不能在上架了!!");
return;
}
System.out.println("-->请输入商品id:");
int goodsId = new Scanner(System.in).nextInt();
System.out.println("-->请输入商品名称:");
String name = InputUtils.getGoodsName(this.goodses);
System.out.println("-->请输入商品价格:");
double price = new Scanner(System.in).nextDouble();
System.out.println("-->请输入商品描述:");
String desc = new Scanner(System.in).next();
//创建商品实体类对象,作为数据的载体
Goods goods = new Goods(goodsId, name, price, desc);
//添加到柜台没有放商品的位置 元素为null的位置
for (int i = 0; i < this.goodses.length; i++) {
//找为null的位置
if(this.goodses[i]==null){
this.goodses[i] = goods;
this.num++;//柜台商品的数量
System.out.println("-->上架成功!!!");
return;//方法结束
}
}
}
}
package cn.hp.demo02;
import java.io.Serializable;
public class Goods {
private int id;// 商品编号
private String goodsName;// 商品名称
private double price;// 商品价格
private String desc;// 商品描述
//无参构造器和有参构造器
public Goods() {
}
public Goods(int id, String goodsName, double price, String desc) {
this.id = id;
this.goodsName = goodsName;
this.price = price;
this.desc = desc;
}
//重写toString()方法
@Override
public String toString() {
return "Goods{" +
"id=" + id +
", goodsName='" + goodsName + ''' +
", price=" + price +
", desc='" + desc + ''' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getGoodsName() {
return goodsName;
}
public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
package cn.hp.demo02;
import java.util.Scanner;
public class InputUtils {
public static int getNum(){
int n = 0;
try {//try语句块放置的是可能会发生异常的代码
n = new Scanner(System.in).nextInt();
} catch (Exception e) {//一旦发生异常,catch语句块会捕获异常,并处理
//e.printStackTrace();
System.out.println("-->警告:输入非法数字!!请重新输入");
n = getNum();//递归,方法自己调用自己
}
//检验是否是0-4的功能号
if(n<0 || n>4){
System.out.println("-->警告:非法命令!,请重新输入");
n = getNum();//递归
}
return n;
}
public static int getGoodsId(){
int n = 0;
try {//try语句块放置的是可能会发生异常的代码
n = new Scanner(System.in).nextInt();
} catch (Exception e) {//一旦发生异常,catch语句块会捕获异常,并处理
//e.printStackTrace();
System.out.println("-->警告:输入非法数字!!请重新输入");
n = getGoodsId();//递归,方法自己调用自己
}
return n;
}
public static String getGoodsName(Goods[]goodses){
String name = new Scanner(System.in).next();
//增加:商品名称不能重复
for (int i = 0; i < goodses.length; i++) {
//查找柜台中是否已经存在同名的商品,如果存在就不能添加
if(goodses[i]!=null && goodses[i].getGoodsName().equals(name)) {
System.out.println("-->警告:商品名称已存在,请重新输入");
name = getGoodsName(goodses);
break;
}
}
return name;
}
}
package cn.hp.demo02;
import java.text.SimpleDateFormat;
import java.util.Date;
public class UpdateDataThread extends Thread{
private Counter counter;//柜台对象
@Override
public void run() {
while(true){
counter.updateData();
String s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
System.out.println(s+" 备份一次");
try {
Thread.sleep(10000);//每次备份完,休眠10秒
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public Counter getCounter() {
return counter;
}
public void setCounter(Counter counter) {
this.counter = counter;
}
}
package cn.hp.demo02;
public class CounterTest {
public static void main(String[] args) throws Exception {
//创建柜台对象,
Counter counter = new Counter();
//柜台菜单方法
counter.main();
}
}


![将“柜台商品管理”数据保存到本地文件中,原本的Goods[]数组作为柜台删除此行,现改为本地文件保存。 将“柜台商品管理”数据保存到本地文件中,原本的Goods[]数组作为柜台删除此行,现改为本地文件保存。](http://www.mshxw.com/aiimages/31/1018149.png)
