java保存書店每日交易記錄


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 = "\r\n";
    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;
    }    
}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM