Java图书管理系统
时间: 2023-11-30 13:42:31 浏览: 89
java图书管理系统
Java图书管理系统是一个用Java语言编写的软件,用于管理图书馆或书店的图书信息。它可以实现管理员和普通用户两种身份的登录和操作,包括增添图书、删除图书、查找图书、展示图书、借阅图书和归还图书等功能。
下面是Java图书管理系统的基本架构:
1. BOOK包:包含Book类,用于表示一本书的信息,如书名、作者、价格、分类等。
2. BOOKLIST包:包含Booklist类,用于管理所有的图书信息,包括增添图书、删除图书、查找图书、展示图书等功能。
3. FUNCTION包:包含Ifunction接口,用于定义管理员和普通用户的操作接口。
4. ADMIN包:包含Admin类,用于实现管理员的操作,包括增添图书、删除图书、查找图书、展示图书和退出系统等功能。
5. USER包:包含User类,用于实现普通用户的操作,包括查找图书、借阅图书、归还图书和退出系统等功能。
下面是Java图书管理系统的代码实现:
1. Book类
```java
package BOOK;
public class Book {
private String name; // 书名
private String author; // 作者
private double price; // 价格
private String category; // 分类
private boolean borrowed; // 是否被借出
public Book(String name, String author, double price, String category, boolean borrowed) {
this.name = name;
this.author = author;
this.price = price;
this.category = category;
this.borrowed = borrowed;
}
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 getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public boolean isBorrowed() {
return borrowed;
}
public void setBorrowed(boolean borrowed) {
this.borrowed = borrowed;
}
}
```
2. Booklist类
```java
package BOOKLIST;
import BOOK.Book;
public class Booklist {
public Book[] books = new Book[10];
private int booknum;
{
books[0] = new Book("java", "张三", 12, "编程", false);
books[1] = new Book("pathon", "lisi", 12, "编程", false);
books[2] = new Book("matlab", "wangwu", 12, "编程", false);
booknum = 3;
}
public int getBooknum() {
return booknum;
}
public void setBooknum(int booknum) {
this.booknum = booknum;
}
public void addBook(Book book) {
books[booknum] = book;
booknum++;
}
public void deleteBook(String name) {
for (int i = 0; i < booknum; i++) {
if (books[i].getName().equals(name)) {
for (int j = i; j < booknum - 1; j++) {
books[j] = books[j + 1];
}
booknum--;
break;
}
}
}
public Book findBook(String name) {
for (int i = 0; i < booknum; i++) {
if (books[i].getName().equals(name)) {
return books[i];
}
}
return null;
}
public void display() {
for (int i = 0; i < booknum; i++) {
System.out.println("书名:" + books[i].getName() + ",作者:" + books[i].getAuthor() + ",价格:" + books[i].getPrice() + ",分类:" + books[i].getCategory() + ",是否被借出:" + books[i].isBorrowed());
}
}
}
```
3. Ifunction接口
```java
package FUNCTION;
public interface Ifunction {
void addBook(); // 增添图书
void deleteBook(); // 删除图书
void findBook(); // 查找图书
void display(); // 展示图书
void exit(); // 退出系统
}
```
4. Admin类
```java
package ADMIN;
import BOOKLIST.Booklist;
import FUNCTION.Ifunction;
import java.util.Scanner;
public class Admin implements Ifunction {
private Booklist booklist;
public Admin(Booklist booklist) {
this.booklist = booklist;
}
@Override
public void addBook() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入书名:");
String name = scanner.next();
System.out.println("请输入作者:");
String author = scanner.next();
System.out.println("请输入价格:");
double price = scanner.nextDouble();
System.out.println("请输入分类:");
String category = scanner.next();
booklist.addBook(new BOOK.Book(name, author, price, category, false));
System.out.println("添加成功!");
}
@Override
public void deleteBook() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除的书名:");
String name = scanner.next();
booklist.deleteBook(name);
System.out.println("删除成功!");
}
@Override
public void findBook() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要查找的书名:");
String name = scanner.next();
BOOK.Book book = booklist.findBook(name);
if (book != null) {
System.out.println("书名:" + book.getName() + ",作者:" + book.getAuthor() + ",价格:" + book.getPrice() + ",分类:" + book.getCategory() + ",是否被借出:" + book.isBorrowed());
} else {
System.out.println("未找到该书!");
}
}
@Override
public void display() {
booklist.display();
}
@Override
public void exit() {
System.exit(0);
}
}
```
5. User类
```java
package USER;
import BOOK.Book;
import BOOKLIST.Booklist;
import FUNCTION.Ifunction;
import java.util.Scanner;
public class User implements Ifunction {
private Booklist booklist;
public User(Booklist booklist) {
this.booklist = booklist;
}
@Override
public void addBook() {
System.out.println("您没有权限进行此操作!");
}
@Override
public void deleteBook() {
System.out.println("您没有权限进行此操作!");
}
@Override
public void findBook() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要查找的书名:");
String name = scanner.next();
Book book = booklist.findBook(name);
if (book != null) {
System.out.println("书名:" + book.getName() + ",作者:" + book.getAuthor() + ",价格:" + book.getPrice() + ",分类:" + book.getCategory() + ",是否被借出:" + book.isBorrowed());
} else {
System.out.println("未找到该书!");
}
}
@Override
public void display() {
booklist.display();
}
@Override
public void exit() {
System.exit(0);
}
}
```
以上是Java图书管理系统的基本实现,你可以根据需要进行修改和扩展。
阅读全文