package zuoye; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; public class productView { private productlist productlist = new productlist(200); public void printmenu() { System.out.println("------品牌鞋信息管理系统------"); System.out.println("info:----品牌鞋基本信息管理----"); System.out.println("quit:----退出系统"); System.out.println("---------------------------"); while (true) { Scanner sc = new Scanner(System.in); System.out.print("main>"); String choice = sc.next(); switch (choice) { case "info": System.out.println("---品牌鞋基本信息管理---"); System.out.println("show:----显示品牌信息"); System.out.println("add:-----添加品牌信息"); System.out.println("delete:--删除品牌信息"); System.out.println("modify:--修改品牌信息"); System.out.println("save:--导出文件"); System.out.println("load:---导入文件"); System.out.println("quit:---退出-----"); System.out.println("------------------"); while (true) { sc = new Scanner(System.in); System.out.print("info>"); String choice1 = sc.next(); switch (choice1) { case "show": System.out.println("---展示产品信息---"); show(); break; case "add": System.out.println("---添加产品信息---"); add(); break; case "delete": System.out.println("---删除品牌信息---"); delete(); break; case "modify": System.out.println("---修改品牌信息---"); modify(); System.out.println("---修改品牌信息---"); modify(); break; case "save": System.out.println("---导出文件---"); exportFile(); break; case "load": System.out.println("---导入文件---"); try { load(); } catch (IOException e) { throw new RuntimeException(e); } break; case "return": return; } } case "quit": System.exit(0); } } } public void process() { printmenu(); while (true) { Scanner sc = new Scanner(System.in); System.out.print("info>"); String choice = sc.next(); switch (choice) { case "add": add(); break; case "delete": delete(); break; case "modify": modify(); break; case "load": try { load(); } catch (IOException e) { throw new RuntimeException(e); } break; case "return": return; } } } public static void main(String args[]) { productView st = new productView(); st.process(); } public int find(String id) { product[] pros = productlist.getallproduct(); int k = -1; for (int i = 0; i < pros.length; i++) { if (id.equals(pros[i].getId())) { k = i; break; } } return k; } public void add() { Scanner sc = new Scanner(System.in); System.out.println("序号"); String id = sc.next(); System.out.println("品牌名称"); String name = sc.next(); System.out.println("价钱"); String price = sc.next(); System.out.println("颜色"); String colour = sc.next(); System.out.println("产地"); String producer = sc.next(); product pro = new product(id, name, price, colour, producer); boolean flag = productlist.addproduct(pro); if (flag) { System.out.println("---添加成功---"); } else { System.out.println("---添加失败---"); } } public void delete() { System.out.println("请输入要删除的序号:"); Scanner sc = new Scanner(System.in); String id = sc.next(); int index = find(id); if (index == -1) { System.out.println("---该品牌鞋不存在---"); return; } boolean flag = productlist.delproduct(index); if (flag) { System.out.println("---删除成功---"); } else { System.out.println("---删除失败---"); } } public void modify() { System.out.print("请输入要修改的序号:"); Scanner sc = new Scanner(System.in); String id = sc.next(); int index = find(id); if (index == -1) { System.out.println("---该品牌不存在---"); return; } System.out.println("品牌名称"); String name = sc.next(); System.out.println("价钱"); String price = sc.next(); System.out.println("颜色"); String colour = sc.next(); System.out.println("产地"); String producer = sc.next(); product pro = new product(index); boolean flag = productlist.modifyproduct(index, pro); if (flag) { System.out.println("---修改成功---"); } else { System.out.println("---修改失败---"); } } public void show() { System.out.println("----品牌信息----"); // product[] pros; product[] pros = productlist.getallproduct(); if (product.length == 0) { System.out.println("尚没有该品牌记录"); } else { System.out.println("序号\t名称\t价钱\t颜色\t产地"); for (int i = 0; i <= product.length - 1; i++) { System.out.println(pros[i].getId() + "\t" + pros[i].getName() + "\t" + pros[i].getPrice() + "\t" +pros[i].getColour() + "\t" + pros[i].getProducer() + "\t"); } } System.out.println("------------------------"); } public void exportFile() { String filePath = "D:\\product.txt"; product[] pros = productlist.getallproduct(); try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) { writer.write("序号\t\t品牌名称\t\t价格\t\t颜色\t\t产地\n"); for (int i = 0; i < pros.length; i++) { writer.write(pros[i].getId() + "\t\t\t" + pros[i].getName() + "\t\t" + pros[i].getPrice() + "\t\t" + pros[i].getColour() + "\t\t" + pros[i].getProducer() + "\n"); } System.out.println("品牌信息已成功写入文件!"); } catch (IOException e) { System.err.println("写入文件时发生错误:" + e.getMessage()); } } public void load() throws IOException { Scanner sc = new Scanner(System.in); System.out.print("请输入要导入的文件名:"); String fn = sc.next(); File file = new File(fn); if (file.exists()) { BufferedReader br = new BufferedReader(new FileReader(fn)); String str; while ((str = br.readLine()) != null) ; {String id = str.split(",")[0]; String name = str.split(",")[1]; String price = str.split(",")[2]; String colour = str.split(",")[3]; String producer = str.split(",")[4]; product s = new product(id, name,price, colour,producer); if (idExists(id)) { System.out.println("---该顾客已存在---"); } else { productlist.addproduct(s); } } br.close(); System.out.println("--导入成功--"); } else { System.out.println("---要导入的文件不存在--"); } } private boolean idExists(String id) { return false; } public class product { public static int length; private String id; private String name; private String price; private String colour; private String producer; public product(int i) { this.name = this.id; this.id = this.name; this.price= this.price; this.colour= this.colour; this.producer= this.producer; } public product(String id, String name, String price, String colour, String producer) { this.id = id; this.name = name; this.price= price; this.colour= colour; this.producer= producer; } public static product[] addproduct() { return new product[0]; } product get(int i) { return new product(i); } public String getName(){ return name; } public void setName(String name){ this.name = name; } public String getPrice() { return price; } public void setPrice(String price){ this.price= price; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getColour() { return colour; } public void setColour(String colour) { this.colour = colour; } public String getProducer() { return producer; } public void setProducer(String producer) { this.producer = producer; } @Override public String toString() { return "Product{" + "id='" + id + '\'' + ", name=" + name + ", price=" + price+ ", colour='" + colour + '\'' + ",producer=" + producer + '}'; } } public class productlist { public static Object remove; private product[] products; private static int total = 0; public productlist(int maxlength) { products = new product[maxlength]; } public boolean modifyproduct(int index, product pro) { // TODO Auto-generated method stub return false; } public product[] getallproduct() { // TODO Auto-generated method stub return null; } public static void remove(int i) { } public boolean addproduct(product pro) { if (total >= products.length) { return false; } products[total++] = pro; return true; } public boolean delproduct(int index) { if (total < 0 || index >= total) { return false; } for (int i = index; i < total - 1; i++) { products[i] = products[i++]; } products[--total] = null; return true; } public class productlist { public static Object remove; private product[] products; private static int total = 0; public productlist(int maxlength) { products = new product[maxlength]; } public static void remove(int i) { } public boolean addproduct(product pro) { if (total >= products.length) { return false; } products[total++] = pro; return true; } public boolean delproduct(int index) { if (total < 0 || index >= total) { return false; } for (int i = index; i < total - 1; i++) { products[i] = products[i++]; } products[--total] = null; return true; } public boolean modifyproduct(int index, product pro) { if (index < 0 || index >= total) { return false; } productts[index] = pro; return true; } public product[] getallproducts() { product[] pros = new product[total]; for (int i = 0; i < total; i++) { pros[i] = products[i]; } return pros; } }
【无标题】err
最新推荐文章于 2024-10-01 22:12:42 发布