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;
}
}
}
}