程序的具体需求为:
每个商城都需要进货,而这些进货记录整理起来很不方便,本案例要求编写一个商城进货记录交易的程序,使用字节流将商场的进货信息记录在本地的cvs文件中。
具体程序要求如下:
当用户输入商品编号时,后台会根据商品编号查询到相应商品信息,并打印商品信息。接着让用户输入需要进货的商品数量,程序将原有的库存数量与输入的数量相加作为商品最新的库存信息,并将商品进货的记录保存至本地的cvs文件中。在cvs文件中,每条记录包含商品编号、商品名称、购买数量、单价、总价、联系人等数据,每条记录的数据之间直接用英文逗号或空格分隔,每条记录之间由换行符分隔。文件命名格式为“进货记录”加上当天日期加上“.cvs”后缀,如进货记录20211019.cvs。保存文件时需要判断本地是否存在当天的数据,如果存在则追加,不存在则新建。
package javaIO; public class Goods { private String id; //商品编号 private double unitPrice; //单价 private String goodsName; //商品名称 private int stock; //库存 private String custumerName;//联系人 public Goods() { super(); } public Goods(String id, double unitPrice, String goodsName, int stock, String custumerName) { super(); this.id = id; this.unitPrice = unitPrice; this.goodsName = goodsName; this.stock = stock; this.custumerName = custumerName; } //返回总价(总价 = 库存 * 单价) public double getTotalPrice() { return stock * unitPrice; } @Override public String toString() { //返回商品的相关信息 return "进货记录编号:"+id+"n商品名称:"+goodsName+"n联系人:"+ custumerName+"n单价:"+unitPrice+"n库存数量:"+stock+"n"; } public String currentRecord() { //返回商品当前的相关信息 return "当前商品库存信息"+toString(); } public String getId() { return id; } public void setId(String id) { this.id = id; } public double getUnitPrice() { return unitPrice; } public void setUnitPrice(double unitPrice) { this.unitPrice = unitPrice; } public String getGoodsName() { return goodsName; } public void setGoodsName(String goodsName) { this.goodsName = goodsName; } public int getStock() { return stock; } public void setStock(int stock) { this.stock = stock; } public String getCustumerName() { return custumerName; } public void setCustumerName(String custumerName) { this.custumerName = custumerName; } }
package javaIO; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; public class GoodsRecorder { public static final String TOKEN = " "; //空格符 public static final String LINE_SEPARATOR = "n"; //换行符 public static void save(Goods good) throws IOException { DateFormat date = new SimpleDateFormat("yyyyMMdd"); //创建日期格式对象 String fileName = "进货记录"+date.format(new Date())+".cvs"; //构造文件名 File file = new File("E:\java商品进货记录",fileName); //创建文件对象 if(file.exists()) { //如果文件存在,记录追加到文件 save(good, file, true); }else { file.createNewFile(); //如果文件不存在,创建新文件并输入记录 save(good, file, false); } } private static void save(Goods good, File file, boolean flag) throws IOException { if(flag) { byte[] b = ToStringBuffer(good).getBytes(); //返回字符串的字节数组格式并赋给数组 BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file,true)); bufferedOutputStream.write(b, 0, b.length); //借用输出流对象输出b数组长度的字节数据 bufferedOutputStream.close(); //关闭输出流对象,防止数据输出失败 }else { StringBuffer sb = new StringBuffer(); //创建字符串缓冲器对象 String string = "商品编号 "+"商品名称 "+"购买数量 "+"单价 "+"总价 "+"联系人 n"+ToStringBuffer(good); sb.append(string); //缓冲器添加字符串string byte[] b = sb.toString().getBytes(); //返回缓冲器的字符串的数组格式 BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file)); bufferedOutputStream.write(b, 0, b.length); bufferedOutputStream.close(); } } private static String ToStringBuffer(Goods good) { StringBuffer sb = new StringBuffer(); sb.append(good.getId()).append(TOKEN); //缓冲器添加商品编号 sb.append(good.getGoodsName()).append(TOKEN); //缓冲器添加商品名称 sb.append(good.getStock()).append(TOKEN); //缓冲器添加商品库存 sb.append(good.getUnitPrice()).append(TOKEN); //缓冲器添加商品单价 sb.append(good.getTotalPrice()).append(TOKEN); //缓冲器添加商品总价 sb.append(good.getCustumerName()).append(LINE_SEPARATOR);//缓冲器添加商品联系人名称 return sb.toString(); } public static void saveRealRecord(ArrayListlist) throws IOException { DateFormat date = new SimpleDateFormat("yyyyMMdd"); String fileName = "商品进货实时记录"+date.format(new Date())+".cvs"; File file = new File("E:\java商品进货记录",fileName); if(file.exists()) { String string = "商品编号 "+"商品名称 "+"购买数量 "+"单价 "+"总价 "+"联系人n"; for (Goods goods : list) { //字符串string依次加入各商品的相关信息 string += ToStringBuffer(goods); } byte[] b = string.getBytes(); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file)); bufferedOutputStream.write(b, 0, b.length); bufferedOutputStream.close(); }else { //如果文件不存在,创造文件并再次调用本方法 file.createNewFile(); saveRealRecord(list); } } }
package javaIO; import java.io.IOException; import java.util.ArrayList; import java.util.Scanner; public class TestRecord { private static ArrayListgoodsList = new ArrayList<>(); public static void main(String[] args) throws IOException { Scanner input = new Scanner(System.in); int choice; Init(); do { menu(); choice = input.nextInt(); switch (choice) { case 1: query();break; case 2: addStock();break; case 3: addGoods();break; case 4: printAllGoods();break; case 0: break; default: System.out.println("选择有误!请再次选择。"); } }while(choice != 0); System.out.println("本次 *** 作结束."); input.close(); } //显示所有商品的相关信息 public static void printAllGoods() { for (Goods goods : goodsList) { System.out.println(goods); } } //查询商品信息 public static void query() { Scanner input = new Scanner(System.in); System.out.println("请输入商品编号"); String id = input.next(); try { System.out.println(((Goods)queryID(id)).currentRecord()); }catch(NullPointerException ex) { System.out.println("无此编号"); System.exit(1); } } //判断商品编号是否存在,存在时返回相应对象 public static Object queryID(String id) { for (Goods goods : goodsList) { if(goods.getId().equals(id)) return goods; } return null; } //增加库存 public static void addStock() throws IOException { Scanner input = new Scanner(System.in); System.out.print("请输入进货数量:"); int number = input.nextInt(); System.out.println("请输入商品编号"); String id = input.next(); try { Goods good = (Goods)queryID(id); good.setStock(number + good.getStock()); //商品设置新库存(新库存 = 原库存 + 进货数量) GoodsRecorder.save(good); //将记录保存到进货记录 GoodsRecorder.saveRealRecord(goodsList); //将商品的实时信息保存到进货实时记录 System.out.println(good.currentRecord()); //输出商品的当前记录 }catch(NullPointerException ex) { //当(Goods)queryID(id)查询不到相应编号时,发生异常 System.out.println("无此编号"); System.exit(2); //退出程序 } } //初始化 *** 作 public static void Init() throws IOException { goodsList.add(new Goods("1001",4.5,"百事可乐",100,"张三")); //将原始数据添加进数组列表 goodsList.add(new Goods("1002",4.0,"可口可乐",100,"李四")); goodsList.add(new Goods("1003",3.8,"百事雪碧",100,"张三")); for (Goods goods : goodsList) { //将商品信息添加到进货记录 GoodsRecorder.save(goods); } GoodsRecorder.saveRealRecord(goodsList); //将商品信息添加到进货实时记录 } //增加新商品 public static void addGoods() throws IOException { Scanner input = new Scanner(System.in); System.out.print("请输入商品编号:"); String id = input.next(); if(((Goods)queryID(id)) != null) { System.out.println("已有此编号,不可重复!"); System.exit(3); } System.out.print("请输入商品名称:"); String goodsname = input.next(); System.out.print("请输入购买数量:"); int amount = input.nextInt(); System.out.print("请输入单价:"); double unitPrice = input.nextDouble(); System.out.print("请输入联系人:"); String custumer = input.next(); goodsList.add(new Goods(id, unitPrice, goodsname, amount, custumer)); GoodsRecorder.save(goodsList.get(goodsList.size()-1)); GoodsRecorder.saveRealRecord(goodsList); System.out.println("商品进货成功.n"); } //显示菜单 public static void menu() { System.out.println(" 商品进货信息及 *** 作"); 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("==================================="); System.out.print("请输入选择:"); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)