今天来实现了一下Java的小项目:图书管理系统
文件:
代码部分:
Book:
package bookManagement.Book;
public class Book {
private String name;
private String author;
private double price;
private String type;
private boolean isBorrowed;
public Book(String name, String author, double price, String type) {
this.name = name;
this.author = author;
this.price = price;
this.type = type;
this.isBorrowed = false;
}
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;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
", isBorrowed=" + isBorrowed +
'}';
}
}
bookList:
package bookManagement.Book;
import java.util.Iterator;
import java.util.LinkedList;
public class bookList {
public static final LinkedList<Book> booksList = new LinkedList<>();
public static final LinkedList<String> booksName = new LinkedList<>();
static {
booksList.add(new Book("水浒传", "施耐庵", 85.5, "小说"));
booksList.add(new Book("三国演义", "罗贯中", 120.6, "小说"));
booksList.add(new Book("红楼梦", "曹雪芹", 99.9, "小说"));
booksList.add(new Book("西游记", "吴承恩", 66.6, "小说"));
booksName.add("水浒传");
booksName.add("三国演义");
booksName.add("红楼梦");
booksName.add("西游记");
}
public void addBook(Book book) {
if (booksName.contains(book.getName())) {
System.out.println("该书目已存在!");
return;
}
booksList.add(book);
booksName.add(book.getName());
}
public Book findBook(String bookName) {
if (booksName.contains(bookName)) {
Iterator<Book> iterator = booksList.iterator();
while (iterator.hasNext()) {
Book book = iterator.next();
if (book.getName().equals(bookName)) {
return book;
}
}
} else {
System.out.println("书架上没有该书目!");
}
return null;
}
public void deleteBook(String bookName) {
if (booksName.contains(bookName)) {
Iterator<Book> iterator = booksList.iterator();
while (iterator.hasNext()) {
Book book = iterator.next();
if (book.getName().equals(bookName)) {
booksList.remove(book);
booksName.remove(bookName);
System.out.println("删除成功!");
return;
}
}
} else {
System.out.println("删除失败!书架上没有该书目!");
}
}
public void showBooks() {
Iterator<Book> iterator = booksList.iterator();
while (iterator.hasNext()) {
Book book = iterator.next();
System.out.println(book);
}
}
public void borrowBook(String bookName) {
Book book = findBook(bookName);
if (booksName.contains(bookName)) {
if (book.isBorrowed()) {
System.out.println("该书已被借出!");
} else {
System.out.println("借书成功!!!");
book.setBorrowed(true);
}
} else {
System.out.println("借阅失败!书架上没有该书目!");
}
}
public void returnBook(String bookName) {
Book book = findBook(bookName);
if (book == null) {
System.out.println("书架上没有该书");
} else if (booksName.contains(bookName) && book.isBorrowed()) {
System.out.println("还书成功!!!");
book.setBorrowed(false);
}
}
public void updateBook(String bookName, double price, String type) {
Book book = findBook(bookName);
if (book == null) {
System.out.println("书架上没有该书!");
} else {
book.setPrice(price);
book.setType(type);
System.out.println("更新成功!");
}
}
}
Operation:
package bookManagement.Operation;
import bookManagement.Book.bookList;
public interface Operation {
void operation(bookList booklist);
}
add:
package bookManagement.Operation;
import bookManagement.Book.Book;
import bookManagement.Book.bookList;
import java.util.Scanner;
public class addOperation implements Operation {
@Override
public void operation(bookList booklist) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你需要添加的书的名称:");
String name = sc.nextLine();
System.out.println("请输入该书的作者:");
String author = sc.nextLine();
System.out.println("请输入该书的价格:");
double price = sc.nextDouble();
System.out.println("请输入该书的类型:");
String type = sc.nextLine();
Book book = new Book(name, author, price, type);
booklist.addBook(book);
}
}
borrow:
package bookManagement.Operation;
import bookManagement.Book.bookList;
import java.util.Scanner;
public class borrowOperation implements Operation {
@Override
public void operation(bookList booklist) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要借阅的书名:");
String name = sc.nextLine();
booklist.borrowBook(name);
}
}
delete:
package bookManagement.Operation;
import bookManagement.Book.bookList;
import java.util.Scanner;
public class deleteOperation implements Operation {
@Override
public void operation(bookList booklist) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要删除的书名:");
String name = sc.nextLine();
booklist.deleteBook(name);
}
}
find:
package bookManagement.Operation;
import bookManagement.Book.Book;
import bookManagement.Book.bookList;
import java.util.Scanner;
public class findOperation implements Operation {
@Override
public void operation(bookList booklist) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要查找的书名:");
String name = sc.nextLine();
Book book = booklist.findBook(name);
System.out.println(book);
}
}
return:
package bookManagement.Operation;
import bookManagement.Book.bookList;
import java.util.Scanner;
public class returnOperation implements Operation {
@Override
public void operation(bookList booklist) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要归还的书名:");
String name = sc.nextLine();
booklist.returnBook(name);
}
}
show:
package bookManagement.Operation;
import bookManagement.Book.bookList;
public class showOperation implements Operation {
@Override
public void operation(bookList booklist) {
booklist.showBooks();
}
}
update:
package bookManagement.Operation;
import bookManagement.Book.bookList;
import java.util.Scanner;
public class updateOperation implements Operation {
@Override
public void operation(bookList booklist) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你需要更新的书的名称:");
String name = sc.nextLine();
System.out.println("请输入更新后的的价格:");
double price = sc.nextDouble();
System.out.println("请输入更新后的的类型:");
String type = sc.nextLine();
booklist.updateBook(name, price, type);
}
}
User:
package bookManagement.User;
import bookManagement.Book.bookList;
import bookManagement.Operation.Operation;
public abstract class User {
protected String name;
protected Operation[] operations;
// 根据用户输入执行相应操作
public abstract int menu();
public void doOperation(int choice, bookList bookList) {
operations[choice - 1].operation(bookList);
}
}
AdminUser:
package bookManagement.User;
import bookManagement.Operation.*;
import java.util.Scanner;
public class AdminUser extends User {
private Scanner sc = new Scanner(System.in);
public AdminUser(String name) {
this.name = name;
operations = new Operation[]{
new addOperation(),
new deleteOperation(),
new updateOperation(),
new findOperation(),
new showOperation(),
};
}
@Override
public int menu() {
Scanner sc = new Scanner(System.in);
System.out.println("====================================");
System.out.println("欢迎" + this.name + "登录图书管理系统");
System.out.println("1.添加书籍");
System.out.println("2.删除书籍");
System.out.println("3.更新书籍");
System.out.println("4.查找书籍");
System.out.println("5.显示当前书籍列表");
System.out.println("-1.退出");
System.out.println("请输入您的选择");
System.out.println("====================================");
int select = sc.nextInt();
if (select == -1) {
close();
}
return select;
}
public void close() {
sc.close();
}
}
OrdinaryUser :
package bookManagement.User;
import bookManagement.Operation.*;
import java.util.Scanner;
public class OrdinaryUser extends User {
private Scanner sc = new Scanner(System.in);
public OrdinaryUser(String name) {
this.name = name;
operations = new Operation[]{
new showOperation(),
new findOperation(),
new borrowOperation(),
new returnOperation(),
};
}
@Override
public int menu() {
System.out.println("====================================");
System.out.println("欢迎" + this.name + "登录图书管理系统");
System.out.println("1.显示当前书籍列表");
System.out.println("2.查询书籍");
System.out.println("3.借阅书籍");
System.out.println("4.归还书籍");
System.out.println("-1.退出");
System.out.println("请输入您的选择");
int select = sc.nextInt();
System.out.println("====================================");
if (select == -1) {
close();
}
return select;
}
public void close() {
sc.close();
}
}
Main:
package bookManagement;
import bookManagement.Book.bookList;
import bookManagement.User.AdminUser;
import bookManagement.User.OrdinaryUser;
import bookManagement.User.User;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
bookList bookList = new bookList();
User user = login();
while (true) {
int select = user.menu();
if (select == -1) {
System.out.println("Bye~~");
System.exit(0);
}
user.doOperation(select, bookList);
}
}
public static User login() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入您的姓名");
String name = scanner.nextLine();
System.out.println("请输入您的身份,1表示普通用户,0表示管理员");
int who = scanner.nextInt();
if (who == 1) {
return new OrdinaryUser(name);
} else {
return new AdminUser(name);
}
}
}
运行展示: