创建图书管理系统大致分为四个部分,图书、使用者、操作系统,运行窗口等四个方面,我们今天先做图书这一部分。
对于图书,我们首先要创建它的属性,即书名、价格、作者、图书类型等等,
public String name; public String author; public String price; public String type; public boolean condition;
此外,我们要得到这些成员属性,会用到get和set方法,以及本身的构造方法。(get~与set~是为了将这些独立的信息串起来)
public Book(String price, String name, String author, String type) { this.price = price; this.name = name; this.author = author; this.type = type; }public String getName() { return name; } public void setName(String name) { this.name = name; } ......
写出这些基本的变量后,我们就要存储具体的图书,我们可以建立一个Booklist书架来存储我们的图书,这样一本一本的图书的储存肯定要用的数组,所以,我们需要建立一个数组并将它初始化,当然也需要将独立的书本通过get获得。
this.books[0] = new Book("三国演义","罗贯中",20,"小说"); this.books[1] = new Book("西游记","吴承恩",21,"小说"); this.books[2] = new Book("红楼梦","曹雪芹",22,"小说");
book代码:
package book;
public class Book {
public String name;
public String author;
public int price;
public String type;
public boolean condition;
public Book(String name, String author, int price, String type) {
this.name = name;
this.author = author;
this.price = price;
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price='" + price + '\'' +
", type='" + type + '\'' +
((condition == true) ? "借出" : "未借出") +
'}';
}
}
Bookshelf代码:
public class Bookshelf {
public Book[] books = new Book[10] ;
public Bookshelf(){
this.books[0] = new Book("三国演义","罗贯中",20,"小说");
this.books[1] = new Book("西游记","吴承恩",21,"小说");
this.books[2] = new Book("红楼梦","曹雪芹",22,"小说");
}
public Book getBooks(int pos) {
return books[pos];
}
public void setBooks(int pos,Book book) {
this.books[pos] = book;
}
public Book[] getBooks() {
return books;
}
public void setBooks(Book[] books) {
this.books = books;
}
};