目录
1.图书管理系统展示
这是最终效果展示,可以根据输入身份的不同,从而进入不同的身份内容去,不同的身份内容有不同的功能

2.图书管理系统实现
2.1 book包
2.1.1 Book类
书类中封装度很高,一般不露出任何一点成员变量
package book;
public class Book {
private String name;
private String author;
private double price;
private String type;
private boolean isBorrowed;
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
","+((isBorrowed==true)?"已借出":"未借出") +
'}';
}
public Book(String name, String author, double 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 double getPrice() {
return price;
}
public void setPrice(double 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;
}
}
2.1.1 BookList
BookList是一个书架,存储书,我们默认是10本书,并且给定三本默认的图书
package book;
public class BookList {
Book[] books = new Book[10];
private int usedSize;
public BookList() {
books[0] = new Book("三国演义","罗贯中",19,"小说");
books[1] = new Book("西游记","吴承恩",29,"小说");
books[2] = new Book("红楼梦","曹雪芹",9,"小说");
usedSize = 3;
}
public void setBooks(int pos,Book book) {
books[pos] = book;
}
public Book getBooks(int pos){
return books[pos];
}
public void setUsedSize(int size){
usedSize=size;
}
public int getUsedSize(){
return usedSize;
}
}
2.2 Operation包
2.2.1 IOperation接口
实现这个接口去让下方的类去实现
package operation;
import book.BookList;
public interface IOperation {
void work(BookList bookList);
}
2.2.2 AddOperation
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class AddOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("新增图书!");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入图书的名字:");
String name = scanner.nextLine();
System.out.println("请输入图书的作者:");
String author = scanner.nextLine();
System.out.println("请输入图书的价格");
int price = scanner.nextInt();
System.out.println("请输入图书的类型:");
String type = scanner.nextLine();
Book book = new Book(name,author,price,type);
int curSize = bookList.getUsedSize();
bookList.setBooks(curSize,book);
bookList.setUsedSize(curSize+1);
System.out.println("新增图书成功!");
}
}
2.2.3 BorrowOperation
package operation;
import book.BookList;
import java.util.Scanner;
public class BorrowOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("借阅图书!");
System.out.println("请输入要借阅图书的名字:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
for (int i = 0; i < bookList.getUsedSize(); i++) {
if(name.equals(bookList.getBooks(i).getName())){
bookList.getBooks(i).setBorrowed(true);
System.out.println("借阅成功!");
return ;
}
}
}
}
2.2.4 DelOperation
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class DelOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("删除图书!");
System.out.println("输入要删除的图书名称:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int curSize = bookList.getUsedSize();
for (int i = 0; i < bookList.getUsedSize(); i++) {
if(name.equals(bookList.getBooks(i).getName())){
for (int j = bookList.getUsedSize()-1; j > i ; j--) {
Book book = bookList.getBooks(j-1);
book=bookList.getBooks(j);
}
bookList.setUsedSize(curSize-1);
return ;
}
}
}
}
2.2.5 DisplayOperation
package operation;
import book.BookList;
public class DisplayOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("显示图书!");
for (int i = 0; i < bookList.getUsedSize(); i++) {
System.out.println(bookList.getBooks(i));
}
}
}
2.2.6 ExitOperation
package operation;
import book.BookList;
public class ExitOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("退出系统!");
System.exit(0);
}
}
2.2.7 FindOperation
package operation;
import book.BookList;
import java.util.Scanner;
public class FindOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("查找图书!");
System.out.println("请输入要查找图书的名字:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
for (int i = 0; i < bookList.getUsedSize(); i++) {
if(name.equals(bookList.getBooks(i).getName())){
System.out.println("找到了这本书!");
System.out.println(bookList.getBooks(i));
return ;
}
}
}
}
2.2.8 ReturnOperation
package operation;
import book.BookList;
import java.util.Scanner;
public class ReturnOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("归还图书!");
System.out.println("请输入要借阅图书的名字:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
for (int i = 0; i < bookList.getUsedSize(); i++) {
if(name.equals(bookList.getBooks(i).getName())){
bookList.getBooks(i).setBorrowed(false);
System.out.println("归还成功!");
return ;
}
}
}
}
2.3 user类
2.3.1 User抽象类
package user;
import book.BookList;
import operation.IOperation;
public abstract class User {
protected String name;
protected IOperation[] ioperations;
public User(String name) {
this.name = name;
}
public abstract int menu();
public void doOperation(int choice, BookList bookList){
this.ioperations[choice].work(bookList);
}
}
2.3.2 AdminUser
package user;
import operation.*;
import java.util.Scanner;
public class AdminUser extends User{
public AdminUser(String name) {
super(name);
super.ioperations = new IOperation[]{
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new DisplayOperation()
};
}
@Override
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.退出系统!");
System.out.println("请输入你的操作:");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
}
2.3.3 NormalUser
package user;
import operation.*;
import java.util.Scanner;
public class NomalUser extends User {
public NomalUser(String name) {
super(name);
super.ioperations = new IOperation[]{
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation()
};
}
@Override
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 scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
}
3. Main
串接上面所有内容,做到具体的实现
import java.util.Scanner;
public class Main {
public static User login(){
System.out.println("请输入你的姓名");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
System.out.println("请输入你的身份:1:->管理员,0->普通用户");
int choice = scanner.nextInt();
if(choice == 1){
return new AdminUser(name);
}else{
return new NomalUser(name);
}
}
public static void main(String[] args) {
BookList bookList = new BookList();
User user = login();
while(true){
int choice = user.menu();
user.doOperation(choice,bookList);
}
}
}
版权声明:本文为weixin_55547460原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。