各位小伙伴们好久不见啊哈哈,在老师的帮助下我也是摸索了好长时间,也终于是将图书管理系统这个项目搞定了,那么接下来就随我一起来看看吧!!!
构思大致框架
首先,图书管理系统,我们要有书对吧,有书就要有书架来存放这些书对吧,那么Book和Booklist这两个类就有了,有书了那是不是要有人管理和用户,那么管理员(AdminUser)和普通用户(NormalUser)这两个类就要写出来,那他们对应的功能也要写出来对吧,最后只需要对这三者进行功能连接就行了(Main类来实现)。那就一起看看具体代码吧!
Book包
Book类
package Book;
public class Book {
private String name;
private String author;
private int price;
private String type;
private boolean isBorrowed;
public Book(String name, String author, int price,String type) {
this.name = name;
this.author = author;
this.price = price;
this.type=type;
}
public String getType() {
return type;
}
public void setType(String type) {
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 boolean isBorrowed(boolean b) {
return isBorrowed;
}
public void setBorrowed(boolean borrowed) {
isBorrowed = borrowed;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
((isBorrowed==true)?"已借出":"未被借出") +
'}';
}
}
Booklist类(对书架进行初始化)
public class Booklist {
private Book[] Books=new Book[10];
public int usedSize;
public Booklist(){
this.Books[0]=new Book("三国演义","罗贯中",20,"小说");
this.Books[1]=new Book("西游记","吴承恩",21,"小说");
this.Books[2]=new Book("红楼梦","曹雪芹",22,"小说");
this.Books[3]=new Book("平凡的世界","路遥",23,"小说");
this.usedSize=4;
}
public Book getBooks(int pos){
return Books[pos];
}
public void setBooks(int pos,Book book) {
Books[pos]=book;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
}
接下来就是使用者
User包
User抽象类(为管理员和用户构造框架)
public abstract class User {
String name;
public Iopertion[] iopertions;
public User(String name){
this.name=name;
}
public abstract int menu();
}
AdminUser类
public class Adminuser extends User {
@Override
public int menu() {
System.out.println("************************");
System.out.println("hello"+name+"欢迎来到管理员界面");
System.out.println("0.退出系统");
System.out.println("1.添加书籍");
System.out.println("2.删除书籍");
System.out.println("3.查找书籍");
System.out.println("4.显示所有书籍");
Scanner scanner=new Scanner(System.in);
return scanner.nextInt();
}
public Adminuser(String name) {
super(name);
this.iopertions=new Iopertion[]{
new Exitopertion(),
new Addopertion(),
new Deleopertion(),
new findopertion(),
new showopertion()
};
}
}
NormalUser类
public class Normaluser extends User{
public Normaluser(String name) {
super(name);
this.iopertions=new Iopertion[]{
new Exitopertion(),
new findopertion(),
new borrowedopertion(),
new revertopertion()
};
}
@Override
public int menu() {
System.out.println("************************");
System.out.println("hello"+name+"欢迎来到普通用户界面");
System.out.println("0.退出系统");
System.out.println("1.查找书籍");
System.out.println("2.借用书籍");
System.out.println("3.归还书籍");
Scanner scanner=new Scanner(System.in);
return scanner.nextInt();
}
}
Operation包
Ioperation接口(减少代码的重复使用,提高效率)
public interface Iopertion {
void work(Booklist booklist);
}
Addoperation类
public class Addopertion implements Iopertion{
@Override
public void work(Booklist booklist) {
//添加书籍
Scanner scanner=new Scanner(System.in);
System.out.println("输入添加书的名称");
String name=scanner.next();
System.out.println("输入添加书的作者");
String author= scanner.next();
System.out.println("输入添加书的价格");
int price= scanner.nextInt();
System.out.println("输入添加书的类型");
String type= scanner.next();
Book book=new Book(name,author,price,type);
int size= booklist.getUsedSize();
booklist.setBooks(size,book);
booklist.setUsedSize(size+1);
System.out.println("添加成功!");
}
}
Borrowedoperation类
public class borrowedopertion implements Iopertion{
@Override
public void work(Booklist booklist) {
System.out.println("请输入你要借的书的书名");
Scanner scanner=new Scanner(System.in);
String name= scanner.nextLine();
for (int i = 0; i < booklist.getUsedSize(); i++) {
Book book=new Booklist().getBooks(i);
if(booklist.getBooks(i).getName().equals(name)){
if(booklist.getBooks(i).isBorrowed(true)){
System.out.println("该书已被借出");
}else{
System.out.println("借阅成功!");
booklist.getBooks(i).setBorrowed(true);
}
return;
}
}
System.out.println("没有你要借阅的书!");
}
}
Deleteoperation类
public class Deleopertion implements Iopertion {
@Override
public void work(Booklist booklist) {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入你要删除书的书名:");
String name= scanner.next();
int j=-1;
for (int i = 0; i < booklist.getUsedSize(); i++) {
if (booklist.getBooks(i).getName().equals(name)){
j=i;
break;
}
}
if (j==-1){
System.out.println("没有你要删除的书!!");
return;
}
for (int i = j; i < booklist.getUsedSize()-1; i++) {
booklist.setBooks(i,booklist.getBooks(i+1));
}
booklist.setUsedSize(booklist.getUsedSize()-1);
System.out.println("删除成功!!");
}
}
Exitoperation类
public class Exitopertion implements Iopertion{
@Override
public void work(Booklist booklist) {
for (int i = 0; i < booklist.getUsedSize(); i++) {
booklist.setBooks(i,null);
}
System.exit(0);
System.out.println("退出系统成功!!!");
}
}
Findoperation类
public class findopertion implements Iopertion{
@Override
public void work(Booklist booklist) {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入你要查找书籍的书名:");
String name= scanner.nextLine();
for (int i = 0; i < booklist.getUsedSize(); i++) {
if (booklist.getBooks(i).getName().equals(name)){
System.out.println(booklist.getBooks(i));
return;
}
}
System.out.println("图书馆中没有你要查找的书籍!!");
}
}
revertoperation类
public class revertopertion implements Iopertion{
@Override
public void work(Booklist booklist) {
System.out.println("请输入你要借的书的书名");
Scanner scanner=new Scanner(System.in);
String name= scanner.nextLine();
for (int i = 0; i < booklist.getUsedSize(); i++) {
Book book = new Booklist().getBooks(i);
if (booklist.getBooks(i).getName().equals(name)) {
booklist.getBooks(i).setBorrowed(false);
System.out.println("归还成功!!");
return;
}
}
}
}
Showoperation类
public class showopertion implements Iopertion{
@Override
public void work(Booklist booklist) {
//打印所有书籍
for (int i = 0; i < booklist.getUsedSize(); i++) {
System.out.println(booklist.getBooks(i));
}
}
}
那么接下来就是最关键的部分了,Main类 将上面的三者联系在一起,要是能吃透这一点的话,你对java的理解会再上一层楼。
Main类
public class Main {
public static User ret(){
System.out.println("请输入你的名字");
Scanner scanner=new Scanner(System.in);
String name=scanner.nextLine();
System.out.println("请选择你的身份:1.管理员 2.普通用户");
int choice= scanner.nextInt();
if(choice==1){
return new Adminuser(name);
}else {
return new Normaluser(name);
}
}
public static void main(String[] args) {
Booklist booklist=new Booklist();
User user=ret();
while(true){
int ret= user.menu();
user.iopertions[ret].work(booklist);
}
}
}
当你完成这些之后你就可以这样玩转你的图书管理系统了:
想知道普通用户的话就自己尝试一下吧!今天的分享就到这里再见!!