package service; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Scanner; import dao.BookDao; import pojo.Book; public class BookService { private ArrayList bookList = new ArrayList(); private BookDao bDao = new BookDao(); public ArrayList getBookList() { return bookList; } public BookService() { bookList = bDao.file2Object(); } //添加图书 public void add(String title, String author){ bookList.add(new Book(bookList.size() + 1,title,author)); } //删除图书 public boolean remove(int num){ boolean flag = true; int size = bookList.size(); for(int i = 0; i < size; i++){ Book b = (Book)bookList.get(i); if(num == b.getNum()){ bookList.remove(i); flag = false; break; } } return flag; } //借阅 public String borrow(String title){ int size = bookList.size(); for(int i = 0; i < size; i++){ Book b = (Book)bookList.get(i); if(b.getTitle().equals(title)){ //找到要借的书 if(b.isBorrow()){//判断是否可借 b.setBorrow(false); //设置为已借出 b.setTime(b.getTime() + 1); //增加借阅次数 return "成功借出"; }else{ return "此书已借出"; } } } return "查无此书"; } //归还 public String back(String title){ int size = bookList.size(); for(int i = 0; i < size; i++){ Book b = (Book)bookList.get(i); if(b.getTitle().equals(title)){ if(!b.isBorrow()){ b.setBorrow(true); return "归还成功"; }else{ return "此书未借出"; } } } return "查无此书"; } //查询 public Book search(String title){ Book book = null; int size = bookList.size(); for(int i = 0; i < size; i++){ Book b = (Book)bookList.get(i); if(b.getTitle().indexOf(title) > -1){ book = b; break; } } return book; } //排行榜 public void sort(){ //得到一共有多少本书 Collections.sort(bookList); System.out.println("编号\t书名\t作者\t是否可借\t借阅次数"); int size = bookList.size(); for(int i = 0; i < size; i++){ Book b = (Book)bookList.get(i); System.out.println(b.getNum()+"\t"+b.getTitle()+"\t"+b.getAuthor()+"\t"+(b.isBorrow()?"可借":"已借出")+"\t"+b.getTime()); } } public void save(){ FileWriter fw = null; try { fw = new FileWriter("d:/books.txt"); //建立程序到文件的通道,文件不存在,则新建文件 int size = bookList.size(); for(int i = 0; i < size; i++){ Book b = (Book)bookList.get(i); String content = b.getNum()+","+b.getTitle()+","+b.getAuthor()+","+b.isBorrow()+","+b.getTime()+"\r\n"; fw.write(content); //想问文件里写内容 } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ try { fw.close(); //关闭通道 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public void read() { File f = new File("d:/books.txt"); //实例化books.txt这个文件的对象 if(f.exists()){ //判断文件是否存在 FileReader fr = null; BufferedReader br = null; try { fr = new FileReader("d:/books.txt"); //建立文件读取通道 br = new BufferedReader(fr); //实例化通道增强类 String content = null; // while((content = br.readLine()) != null){ //循环读取文件中每一行,直到读取内容为空 // String[] info = content.split(","); // Book b = new Book(); // b.setNum(Integer.parseInt(info[0])); // b.setTitle(info[1]); // b.setAuthor(info[2]); // b.setBorrow(Boolean.parseBoolean(info[3])); // b.setTime(Integer.parseInt(info[4])); // this.bookList.add(b); // } do{ content = br.readLine(); String[] info = content.split(","); Book b = new Book(); b.setNum(Integer.parseInt(info[0])); b.setTitle(info[1]); b.setAuthor(info[2]); b.setBorrow(Boolean.parseBoolean(info[3])); b.setTime(Integer.parseInt(info[4])); this.bookList.add(b); }while(content != null); } catch (NumberFormatException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ try { br.close(); //关闭通道 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } public void object2File() { // TODO Auto-generated method stub bDao.object2File(bookList); } }