学习了类和对象,封装,继承,多态,抽象类和接口后,来做一个小练习。
系统功能
- 用户登录
- 用户分为管理员和普通用户
- 管理员和普通用户有不同的功能
基础框架
book包
Book类
这个类放图书的属性,还有属性的get和set方法,以及toString方法输出图书的属性
package book;
public class Book {
private String name;//书名
private String author;//作者
private String type;//类型
private double price;//价格
private boolean isBorrowed;//是否被借出
//不用给isBorrowed赋初值,默认为false
public Book(String name, String author, String type, double price) {
this.name = name;
this.author = author;
this.type = type;
this.price = price;
}
//重写toString
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", type='" + type + '\'' +
", price=" + price +
", isBorrowed=" + isBorrowed +
'}';
}
//get和set
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 String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public boolean isBorrowed() {
return isBorrowed;
}
public void setBorrowed(boolean borrowed) {
isBorrowed = borrowed;
}
}
BookList类
这个类放Book类数组
数组默认大小是10
package book;
public class BookList {
private Book[] books = new Book[10];
private int usedSize;//记录当前books数组当中 有多少本书
//这里默认放了3本书
public BookList() {
books[0] = new Book("三国演义","罗贯中","小说",69);
books[1] = new Book("西游记","吴承恩","小说",79);
books[2] = new Book("红楼梦","曹雪芹","小说",59);
this.usedSize = 3;
}
public Book getBook(int pos) {
return this.books[pos];
}
//这里两个setBook方法构成重载
//增加图书功能要用到
public void setBook(Book book) {
this.books[usedSize] = book;
}
//删除图书时,将后面的图书往前移会用到
public void setBook(int pos,Book book) {
this.books[pos] = book;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
}
operate包
这个包