这周搞了一个不太完整图书管理系统,深刻的体会到了“用户”、“操作”、“资源”的作用,代码如下
资源(Book):包含Book,Bookshelf
package Book;
public class Book {
private int ISBN;
private String name;
private String author;
private String state;
private String returnTime;
public Book(int ISBN, String name, String author, String state) {
this.ISBN = ISBN;
this.name = name;
this.author = author;
this.state = state;
}
public String getReturnTime() {
return this.returnTime;
}
public void setReturnTime(String returnTime) {
this.returnTime = returnTime;
}
public String getState() {
return this.state;
}
public void setState(String state) {
this.state = state;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public int getISBN() {
return this.ISBN;
}
public void setISBN(int ISBN) {
this.ISBN = ISBN;
}
public String getAuthor() {
return this.author;
}
public void setAuthor(String author) {
this.author = author;
}
}
package Book;
public class Bookshelf {
private int totalCount;
private int currCount;
public Book[] books = new Book[10];
public Bookshelf() {
this.books[0] = new Book(1, "三国演义", "罗贯中", "未借出");
this.books[1] = new Book(2, "红楼梦", "曹雪芹", "未借出");
this.books[2] = new Book(3, "西游记", "吴承恩", "未借出");
this.books[3] = new Book(4, "水浒传", "施耐庵", "未借出");
this.currCount = 4;
this.totalCount = 4;
}
public int getCurrCount() {
return this.currCount;
}
public void setCurrCount(int currCount) {
this.currCount = currCount;
}
public int getTotalCount() {
return this.totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public void setBook(Book book) {
this.books[this.currCount] = book;
}
}
操作(operation):包含returnBook、exitSystem
package operation;
import Book.Bookshelf;
import java.util.Scanner;
public class returnBook {
public void work(Bookshelf bookshelf) {
System.out.println("请输入您想还的书:");
Scanner sc = new Scanner(System.in);
int flag = 0;
String name = sc.nextLine();
for(int i = 0; i < bookshelf.getCurrCount(); i++) {
if (name.equals(bookshelf.books[i].getName())) {
if(bookshelf.books[i].getState() == "未借出") {
System.out.println("该书未被借出");
}
flag = 1;
bookshelf.books[i].setState("未借出");
break;
}
}
if (flag == 0) {
System.out.println("对不起,没有这本书!");
}
}
}
package operation;
public class exitSystem {
public void exitSystem() {
System.out.println("退出成功!");
System.exit(0);
}
}
用户(user):包含User、NormalUser
package user;
import Book.Bookshelf;
import java.util.Scanner;
import operation.exitSystem;
import operation.returnBook;
public class NormalUser extends User {
public NormalUser(int num) {
super(num);
this.setNum(num);
System.out.println("*****欢迎书证号为"+num+"的读者来到图书管理系统*****");
}
public int menu() {
int num = super.getNum();
System.out.println("请输入您的操作:");
System.out.println("0.退出系统");
System.out.println("1.归还图书");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
return choice;
}
public void work(int choice,Bookshelf bookshelf) {
Scanner sc = new Scanner(System.in);
if(choice == 0) {
new exitSystem().exitSystem();
}
if (choice == 1) {
new returnBook().work(bookshelf);
}
}
}
package user;
import Book.Bookshelf;
import java.util.Scanner;
import operation.exitSystem;
import operation.returnBook;
public class NormalUser extends User {
public NormalUser(int num) {
super(num);
this.setNum(num);
System.out.println("*****欢迎书证号为"+num+"的读者来到图书管理系统*****");
}
public int menu() {
int num = super.getNum();//继承父类
System.out.println("请输入您的操作:");
System.out.println("0.退出系统");
System.out.println("1.归还图书");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
return choice;
}
public void work(int choice,Bookshelf bookshelf) {
Scanner sc = new Scanner(System.in);
if(choice == 0) {
new exitSystem().exitSystem();
}
if (choice == 1) {
new returnBook().work(bookshelf);
}
}
}
最后还有一个主代码(Main)
import Book.Bookshelf;
import java.util.Scanner;
import user.NormalUser;
import user.User;
public class Main {
public static User login() {
int[] users = new int[2000];
for (int i = 1; i < users.length; i++) {
users[i] = i;
}
System.out.println("*******欢迎来到图书管理系统*******");
System.out.println("请选择您的身份:");
System.out.println("2->用户");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
if(choice == 2) {
System.out.println("请输入您的书证号:");
int num = sc.nextInt();
for (int i = 1; i < users.length; i++) {
if (num == i) {
System.out.println("登入成功!");
return new NormalUser(num);
}
}
System.out.println("没有该用户!");
return null;
}
return null;
}
public static void main(String[] args) {
Bookshelf bookshelf = new Bookshelf();
User user = login();
while(true) {
int choice = user.menu();
user.work(choice,bookshelf);
}
}
}
代码的执行不是那么完整,实力不太够,哈哈哈哈