java book store example2

package com.msb.test15;

import java.io.Serializable;

public class Book implements Serializable {
    private int bNo;
    private String bAuthor;
    private String bName;

    public void setBno(int bNo) {
        this.bNo = bNo;
    }
    public void setBAuthor(String bAuthor) {
        this.bAuthor = bAuthor;
    }
    public void setBName(String bName) {
        this.bName = bName;
    }

    public int getBno() {
        return bNo;
    }

    public String getBAuthor() {
        return bAuthor;
    }

    public String getBName() {
        return bName;
    }
}
package com.msb.test15;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //create a set to store books
        ArrayList list = new ArrayList();
        while(true) {
            System.out.println("-----welcome to book store-----");
            System.out.println("1. show books");
            System.out.println("2. new books");
            System.out.println("3. delete book");
            System.out.println("4. exit application");

            //use scanner class to get user input option
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter your choice: ");
            int choice = sc.nextInt();

            if (choice == 1) {
                System.out.println("[book store]>>>>1. show books");
                File f = new File("/Users/lengchun/Downloads/bookStoreOut.txt");
                if (f.exists()) {
                    FileInputStream fis = new FileInputStream(f);
                    ObjectInputStream ois = new ObjectInputStream(fis);
                    list = (ArrayList)(ois.readObject());
                    for (int i = 0; i < list.size(); i++) {
                        Book b = (Book) list.get(i);
                        System.out.println(b.getBno()+ "---"+b.getBName()+"----"+b.getBAuthor());
                    }
                }else{
                    System.out.println("no books in store, please new book first");
                }


            }
            if (choice == 2) {
                System.out.println("[book store]>>>>2. new books");
                System.out.println("please input book id: bNo");
                int bNo = sc.nextInt();
                System.out.println("Please input book name: bName");
                String bName = sc.next();
                System.out.println("Please input book author: bAuthor");
                String bAuthor = sc.next();
                //list= new ArrayList();

                Book b = new Book();
                b.setBno(bNo);
                b.setBName(bName);
                b.setBAuthor(bAuthor);
                list.add(b);
                File f = new File("/Users/lengchun/Downloads/bookStoreOut.txt");
                FileOutputStream fos = new FileOutputStream(f);
                ObjectOutputStream oos = new ObjectOutputStream(fos);
                oos.writeObject(list);
                oos.close();


            }
            if (choice == 3) {
                System.out.println("[book store]>>>>3. delete books");
               /* System.out.println("please input book id that you want to delete: bNo");
                int bNo = sc.nextInt();
                for (int i = 0; i < list.size(); i++) {
                    Book b = (Book) list.get(i);
                    if (b.getBno() == bNo) {
                        list.remove(b);
                        System.out.println("delete success");
                        break;
                    }
                }

                */

            }
            if (choice == 4) {
                System.out.println("[book store]>>>>4. exit application");
                break;

            }
        }


    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值