Java实验1-文件IO
目标:掌握Java类的创建,Java I/O操作,Java集合类的使用等
内容:
(1) 把每本书的信息存到文件“D:/BookList.txt”中,格式如下:
Java 2大全,Herbert Schildt,电子工业出版社, 2003/01,86
面向对象软件工程,Stephen R.Schach,机械工业出版社,2002/08,56
软件工程,Roger S. Pressman,清华大学出版社,2001/01,42
Design Patterns,Erich Gamma,机械工业出版社,2002/09,98
软件工程,张海藩,人民邮电出版社,2002/07,37
现代软件工程,孙涌,北京希望电子出版社,2002/08,42
面向对象技术UML教程,王少锋,清华大学出版社,2004/02,54
(2) 设计“书”类,包括属性:书名、作者、出版社版号、出版日期、价格;和设置、得到这些属性值的方法,并用Java实现;
(3) 设计“书包”类,包括一个TreeSet属性,代表所有书的集合,和向书包中加书的方法,从书包中拿出书的方法,以及得到所有书价格的方法,并用Java实现;
(4) 设计“图书管理员”类,包括一个“书包”类属性,代表对所有书的管理,和从数据文件“D:/BookList.txt”中读取每本书的信息加入书包中的方法,和在屏幕上打印所有书的信息(格式如下)的main方法,并用Java实现
书名 作者 出版社版号 出版日期 价格
-------------------------------------------------------------
Java 2大全 Herbert Schildt 电子工业出版社 2003/01 86
实现代码清单:Book.java(Book的类模型)SchoolBag.java(书包的类模型)BookManager.java(图书管理员类模型)
代码一:Book.java
public class Book implements Comparable{ private String bookname; private String author; private String press; private String date; private int price; public Book (){ } public Book (String bookname,String author,String press,String date,int price) { this.bookname = bookname; this.author = author; this.press = press; this.date = date; this.price = price; } public void setBookname (String bookname) { this.bookname = bookname; } public void setAuthor (String author) { this.author = author; } public void setPress (String press) { this.press = press; } public void setDate(String date) { this.date = date; } public void setPrice(int price) { this.price = price; } public String getBookname() { return bookname; } public String getAuthor() { return author; } public String getpress() { return press; } public int getPrice() { return price; } public String toString(){ return bookname+" "+author+" "+press+" "+date+" "+price; } public int compareTo(Object o){ Book b = (Book)o; if((this.bookname.compareTo(b.bookname))>0){ return 1; } else if((this.bookname.compareTo(b.bookname))==0){ return 0; } else return -1; } public static void main(String[] args){ Book myBook= new Book("ff","yy","love you","haha",712); System.out.println(myBook); } }
代码2:BookManager.java
import java.util.*; public class SchoolBag { private TreeSet<Book> booklist = new TreeSet<Book>(); public SchoolBag() { } public SchoolBag (Book book) { booklist.add(book); } public ArrayList<Integer> getAllPrice(){ ArrayList<Integer> bookprices = new ArrayList<Integer> (); for(Book element: booklist) { bookprices.add(element.getPrice()); } return bookprices; } public boolean add(Book book) { return booklist.add(book); } public <E extends TreeSet<Book>> void setBooklist(E treeBook){ this.booklist = treeBook; } //泛型方法,将泛型放在函数放回类型之前 public TreeSet<Book> getBooklist(){ return booklist; } public boolean remove(Book book) { if(booklist.isEmpty()) { System.out.println("Warning ,SchoolBag is Empty!"); return false; } else{ booklist.remove(book); return true; } } public static void main(String[] args){ Book mybook = new Book("xx","ff","geg","fege",12); Book herbook = new Book("ss","ll","hh","rr",712); //测试构造方法 SchoolBag sb = new SchoolBag(mybook); //测试add sb.add(herbook); System.out.println(sb.booklist); TreeSet<Book> bl = sb.getBooklist(); System.out.println(bl); //测试remove sb.remove(mybook); System.out.println(bl); //测试setBooklist Book hisbook = new Book("hh","ss","nn","gg",464); Book itsbook = new Book("ff","hh","KK","tt",375); TreeSet<Book> b2 = new TreeSet<Book>(); b2.add(hisbook); b2.add(itsbook); sb.setBooklist(b2); System.out.println("-----------------------"); System.out.println(sb.booklist); //测试printAllPrice ArrayList al = sb.getAllPrice(); System.out.println(al); } }
代码3:BookManager.java
import java.util.*; import java.io.*; public class BookManager { private SchoolBag schoolBag = new SchoolBag(); public BookManager(){ } public BookManager(SchoolBag s){ } public void readData() { try { File data = new File("BookList.txt"); FileReader fr = new FileReader(data); BufferedReader br = new BufferedReader(fr); String[] content = new String[5]; Book book; String str; while((str = br.readLine())!=null){ content = str.trim().split(",|,"); book = new Book(); book.setBookname(content[0].trim()); book.setAuthor(content[1].trim()); book.setPress(content[2].trim()); book.setDate(content[3].trim()); book.setPrice(Integer.parseInt(content[4].trim())); schoolBag.add(book); } br.close(); }catch(IOException e) { e.printStackTrace(); } } public void print() { System.out.println("------------------------------------------------------------------------"); System.out.println("书名 作者 出版号 出版日期 价格 "); System.out.println("------------------------------------------------------------------------"); TreeSet<Book> treeBook = schoolBag.getBooklist(); for(Book book:treeBook){ System.out.println(book.toString()); } } public static void main(String[] args) { BookManager bm = new BookManager(); bm.readData(); bm.print(); } }
程序运行后的结果(使用JDK在DOS下运行的控制台界面)