import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.*; import java.io.*; import java.util.Date; public class Bookstore { static final String SEPARATE_FIELD = ","; static final String SEPARATE_LINE = " "; static HashMap<Integer, Book> map = new HashMap<Integer, Book>(); static ArrayList<Integer> noid = new ArrayList<Integer>(); public static void main(String[] args) { Scanner sc = new Scanner(System.in); createbooks();//将书的信息存储; for(int key: noid) { System.out.println(map.get(key)); } while(true) { System.out.println("请输入您想要买的书籍编号"); int n; n = sc.nextInt(); if(map.containsKey(n)) { Book buybook = map.get(n); if(buybook.number == 0) { System.out.println("不好意思,您要的书本已经买完"); } else { int booknum; System.out.println("请输入您要买的书本的数量"); booknum = sc.nextInt(); if(booknum <= buybook.number) { Book books = new Book(buybook.id,buybook.name,buybook.price,booknum,buybook.price * booknum,buybook.publish); saveBooks(books); buybook.setNumber(buybook.number - booknum); buybook.setMoney((buybook.number - booknum) * buybook.price); System.out.println("购买成功"); } else { System.out.println("不好意思,您要买的书库存不足且只有" + buybook.number + "本"); System.out.println("请问您是否将库存全部买走请回答yes或no"); String s; s = sc.next(); if(s == "yes") { Book books = new Book(buybook.id,buybook.name,buybook.price,booknum,buybook.price * buybook.number,buybook.publish); saveBooks(books); buybook.setNumber(0); buybook.setMoney(0.00); System.out.println("购买成功"); } else { System.out.println("好的,欢迎下次光临"); } } } } else { System.out.println("你输入的编号错误"); } } } public static void createbooks() { Book good1 = new Book(101,"java基础入门",44.50,100,4450.00,"清华大学出版社"); Book good2 = new Book(102,"java编程竞赛",108.00,50,5400.00,"ak算法出版社"); Book good3 = new Book(103,"java遍历算法",99.00,100,9900.00,"ac自动机出版社"); Book good4 = new Book(104,"java编程思想",44.50,100,4450.00,"dfs建树出版社"); map.put(101, good1); map.put(102, good2); map.put(103, good3); map.put(104, good4); noid.add(101); noid.add(102); noid.add(103); noid.add(104); } public static void saveBooks(Book sellBook){ Date date = new Date(); //parse()返回的是一个Date类型数据,format()返回的是一个StringBuffer类型的数据 DateFormat format = new SimpleDateFormat("yyyyMMdd"); String name = "sell_log" + format.format(date) + ".csv"; InputStream in = null; //判断本地是否有此文件 try{ in = new FileInputStream(name); if(in != null){ //存在文件,采取修改文件的方式 in.close(); createFile(name, true, sellBook); } } catch (FileNotFoundException e){ //不存在该文件,应创建文件 createFile(name, false, sellBook); } catch (IOException e){ e.printStackTrace(); } } public static void createFile(String name, boolean label, Book sellBook){ BufferedOutputStream bos = null; StringBuffer sbf = new StringBuffer(); try{ if(label){ bos = new BufferedOutputStream(new FileOutputStream(name, true)); }else{ bos = new BufferedOutputStream(new FileOutputStream(name)); String[] str = new String[] {"图书编号", "图书名称", "购买数量", "单价", "总价", "出版社"}; for(String s : str){ sbf.append(s + SEPARATE_FIELD); } } sbf.append(SEPARATE_LINE); sbf.append(sellBook.id).append(SEPARATE_FIELD); sbf.append(sellBook.name).append(SEPARATE_FIELD); sbf.append(sellBook.number).append(SEPARATE_FIELD); sbf.append(sellBook.price).append(SEPARATE_FIELD); sbf.append(sellBook.money).append(SEPARATE_FIELD); sbf.append(sellBook.publish).append(SEPARATE_FIELD); String str = sbf.toString(); byte[] b = str.getBytes(); for (int i=0; i<b.length; i++){ bos.write(b[i]); } } catch(Exception e){ e.printStackTrace(); } finally{ try { if(bos!=null) bos.close(); }catch(Exception e2){ e2.printStackTrace(); } } } } class Book { int id; String name; double price; int number; double money; String publish; public Book() { super(); } public Book(int id, String name, double price, int number, double money, String publish) { super(); this.id = id; this.name = name; this.price = price; this.number = number; this.money = money; this.publish = publish; } @Override public String toString() { return "book [图书编号:" + id + " 图书名称:" + name + " 单价:" + price + " 库存数量:" + number + " 出版社:" + publish + "]"; } public void setPrice(double price) { this.price = price; } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public void setMoney(double money) { this.money = money; } }