目录
Java实现一个完整的对象:首先我们要找对象
其次是创建对象和使用对象
在图书管理系统中,首先要想到的对象:Book 、Book LIst 、User等等
还有各个operation类,我将把他们放进同一个包里
Book类
public class Book {
private String name;//书名
private String author;//作者
private int price;//价格
private String type;//类型
private boolean isBorrowed;//是否被借出
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;
}
public boolean isBorrowed() {
return isBorrowed;
}
public void setBorrowed(boolean borrowed) {
isBorrowed = borrowed;
}
public Book(String name, String author, int price, String type) {
this.name = name;
this.author = author;
this.price = price;
this.type = type;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
((isBorrowed==true)?"已借出":"未借出")+
//", isBorrowed="+isBorrowed+
'}';
}
}
BookList类
public class BookList {
private Book[] books=new Book[10];
private int usedSize;
public BookList(){
books[0]=new Book("三国演义","罗贯中",90,"小说");
books[1]=new Book("西游记","吴承恩",70,"小说");
books[2]=new Book("红楼梦","曹雪芹",89,"小说");
this.usedSize=3;
}
//获取当前数组元素的个数
public int getUsedSize(){
return usedSize;
}
//修改当前数组中元素的个数
public void setUsedSize(int useSize){
this.usedSize=usedSize;
}
//获取下表为pos的书
public Book getPos(int pos){
return books[pos];
}
//给数组的pos位置放一本书
public void setBooks(int pos,Book book){
books[pos]=book;
}
}
AddOperation类
public class AddOperation implements IOPeration {
@Override
public void work(BookList bookList) {
System.out.println("新增图书!");
System.out.println("请输入图书的名字:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
System.out.println("请输入图书的作者:");
String author = scanner.nextLine();
System.out.println("请输入图书的类型:");
String type = scanner.nextLine();
System.out.println("请输入图书的价格:");
int price = scanner.nextInt();
Book book = new Book(name,author,price,type);
int currentSize = bookList.getUsedSize();
bookList.setBooks(currentSize,book);
bookList.setUsedSize(currentSize+1);
System.out.println("新增成功!");
}
}
BorrowOperation类
public class BorrowOperation implements IOPeration {
@Override
public void work(BookList bookList){
System.out.println("借阅图书!");
System.out.println("请输入你借阅图书的名字");
Scanner scan=new Scanner(System.in);
String name=scan.nextLine();
int currentSize=bookList.getUsedSize();
int i = 0;
for (; i < currentSize; i++) {
Book book=bookList.getPos(i);
if(book.getName().equals(name)){
book.setBorrowed(true);
System.out.println("借阅成功!!");
return;
}
}
System.out.println("没有你要借的书!");
}
}
DelOperation类
public class DelOperation implements IOPeration{
@Override
public void work(BookList bookList){
System.out.println("删除图书!");
System.out.println("请输入图书的名字");
Scanner scan=new Scanner(System.in);
String name=scan.nextLine();
int i = 0;int index=0;
int currentSize=bookList.getUsedSize();
for (; i < currentSize; i++) {
Book book=bookList.getPos(i);
if(book.getName().equals(name)){
index=i;
break;
}
}
if(i==currentSize){
System.out.println("没有你要删除的图书");
return;
}
for (int j = index; j < currentSize; j++) {
Book book=bookList.getPos(j+1);
bookList.setBooks(j,book);
}
bookList.setBooks(currentSize-1,null);
bookList.setUsedSize(currentSize-1);
System.out.println("删除结束");
}
}
DisplayOperation类
public class DisplayOperation implements IOPeration{
@Override
public void work(BookList bookList){
System.out.println("展示图书!");
int currentSize=bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book=bookList.getPos(i);
System.out.println(book);
}
}
}
ExitOperation类
public class ExitOperation implements IOPeration{
public void work(BookList bookList){
System.out.println("退出!");
int currentSize=bookList.getUsedSize();
for(int i=0;i<currentSize;i++){
bookList.setBooks(i,null);
}
System.exit(0);
}
}
FindOperation类
public class FindOperation implements IOPeration{
@Override
public void work(BookList bookList){
System.out.println("查找图书!");
System.out.println("请输入你要查找的书的名字");
Scanner scan=new Scanner(System.in);
String name=scan.nextLine();
int currentSize=bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book=bookList.getPos(i);
if(book.getName().equals(name)){
System.out.println("找到这本书了,信息如下:");
System.out.println(book);
return;
}
}
System.out.println("没有这本书");
}
}
IOPeration接口
public interface IOPeration {
void work(BookList bookList);
}
ReturnOperation类
public class ReturnOperation implements IOPeration{
@Override
public void work(BookList bookList){
System.out.println("归还图书!");
System.out.println("请输入你归还图书的名字");
Scanner scan=new Scanner(System.in);
String name=scan.nextLine();
int currentSize=bookList.getUsedSize();
int i = 0;
for (; i < currentSize; i++) {
Book book=bookList.getPos(i);
if(book.getName().equals(name)){
book.setBorrowed(true);
System.out.println("归还成功!!");
return;
}
}
System.out.println("没有你要归还的书!");
}
}
AdminUser类
public class AdminUser extends User {
public AdminUser(String name){
super(name);
//对应相应的方法
this.ioperations=new IOPeration[]{
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new DisplayOperation(),
};
}
public int menu(){
System.out.println("hello "+this.name);
System.out.println("1.查找图书");
System.out.println("2.新增图书");
System.out.println("3.删除图书");
System.out.println("4.显示系统");
System.out.println("0.退出系统");
Scanner scan=new Scanner(System.in);
int choice=scan.nextInt();
return choice;
}
}
NormalUser类
public class NormalUser extends User {
public NormalUser(String name){
super(name);
this.ioperations=new IOPeration[]{
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation()
};
}
public int menu(){
System.out.println("hello "+this.name);
System.out.println("1.查找图书");
System.out.println("2.借阅图书");
System.out.println("3.归还图书");
System.out.println("0.退出系统");
System.out.println("请输入您的操作:");
Scanner scan=new Scanner(System.in);
int choice=scan.nextInt();
return choice;
}
}
User类
public abstract class User {
protected String name;
public IOPeration[] ioperations;
public User(String name) {
this.name = name;
}
public abstract int menu();
public void doOperation(int choice, BookList bookList){
ioperations[choice].work(bookList);
}
}