目录
首先定义一个存储商店商品编号/商品名称/商品价格的类:
并重写toString方法为了方便查看。
Goods类:
public class Goods {
private Integer id;//商品编号
private String name;//商品名称
private Integer price;//商品价格
public Goods(){}
public Goods(Integer id, String name, Integer price) {
this.id = id;
this.name = name;
this.price = price;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
@Override
public String toString() {
return "Goods{" +
"id=" + id +
", name='" + name + '\'' +
", price=" + price +
'}';
}
}
写增删改查的方法:
ShopTwo类:
首先建一个缓冲流输入输出流对象和一个集合,集合是为了方便实现删除等操作,起到一个中间文件的作用,因为IO流没有删除更改等操作,所以需要把文本文件转换成一个集合方便操作:
public class ShopTwo {
private static BufferedReader bufferedReader =null;
private static BufferedWriter bufferedWriter =null;
private static List<Goods> list;
}
增加商品:
public static void insert(Scanner sc){
System.out.println("请输入商品编号:");
Integer id=sc.nextInt();
System.out.println("请输入商品名称");
String name = sc.next();
System.out.println("请输入商品价格");
Integer price= sc.nextInt();
try {
bufferedWriter = new BufferedWriter(new FileWriter("c:/summerjava/d.txt",true));
String read=id+" "+name+" "+price;
bufferedWriter.write("\n"+read);//换行添加
} catch (Exception e) {
throw new RuntimeException(e);
}finally {
IOUtil.closeIO(null,bufferedWriter);
}
//直接追加:
}
我先封装了一个方法addList(),因为他们都需要先把文件的内容添加到集合里:
public static void addList(){
try {
list = new ArrayList<>();
bufferedReader = new BufferedReader(new FileReader("c:/summerjava/d.txt"));
String read;
while((read=bufferedReader.readLine())!=null){
String[] s =read.split(" ");//把文本文档的内容以空格为界分为数组存储到s里
Goods goods = new Goods(Integer.valueOf(s[0]),s[1],Integer.valueOf(s[2]));
//利用构造器存到对象中,然后添加到集合中
list.add(goods);
}
}catch (FileNotFoundException e){
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally {
IOUtil.closeIO(bufferedReader,null);
}
}
删除商品:
public static void delete(Scanner sc){
System.out.println("请输入商品编号:");
//全部拿出来,删除后覆盖
int id = sc.nextInt();
try {
addList();
bufferedWriter = new BufferedWriter(new FileWriter("c:/summerjava/d.txt"));
Iterator<Goods> iterator = list.iterator();
while(iterator.hasNext()){//利用迭代器循环判断删除传入id的商品
Goods goods=iterator.next();
if(id==goods.getId()){
iterator.remove();
}
}
for (Goods goods: list) {//把删除后的集合写入
bufferedWriter.write(goods.getId()+" "+goods.getName()+" "+ goods.getPrice());
bufferedWriter.newLine();
}
} catch (Exception e) {
throw new RuntimeException(e);
}finally {
IOUtil.closeIO(null,bufferedWriter);
}
}
查找一个:
public static void findOne(Scanner sc){
System.out.println("请输入商品编号:");
int id = sc.nextInt();
ShopTwo.addList();//把文件读取到集合里;
for (Goods goods: list) {
if(id==goods.getId()){
System.out.println(goods);//查找到并输出
}
}
}
修改:
public static void update(Scanner sc){
//全部拿出来,放集合里,然后改。更新后覆盖
System.out.println("请输入商品编号:");
//全部拿出来,删除后覆盖
int id = sc.nextInt();
try {
addList();
bufferedWriter = new BufferedWriter(new FileWriter("c:/summerjava/d.txt"));
Iterator<Goods> iterator = list.iterator();
while(iterator.hasNext()){
Goods goods=iterator.next();
if(id==goods.getId()){
System.out.println("请输入您要修改后的商品名称");
goods.setName(sc.next());
System.out.println("请输入您要修改后的商品价格");
goods.setPrice(sc.nextInt());
}
}
for (Goods goods: list) {
bufferedWriter.write(goods.getId()+" "+goods.getName()+" "+ goods.getPrice());
bufferedWriter.newLine();
}
} catch (Exception e) {
throw new RuntimeException(e);
}finally {
IOUtil.closeIO(null,bufferedWriter);
}
}
操作类
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(true) {
System.out.println("请选择功能:1.插入新商品、2.删除商品、3.修改商品、4.查找一个商品、5.退出");
String flag = sc.next();
switch (flag) {
case "1":
ShopTwo.insert(sc);
break;
case "2":
ShopTwo.delete(sc);
break;
case "3":
ShopTwo.update(sc);
break;
case "4":
ShopTwo.findOne(sc);
break;
case "5":
System.out.println("退出成功");
System.exit(-1);
break;
default:
break;
}
}
}
}
运行效果:
增加:
查看:
删除:
修改: