使用Java实现 图书管理后台功能 1.支持用户注册与登录 如果登录失败或者注册失败,可以重新登录或注册。要求注册的账号密码不为空。登录三次失败,则关闭系统。 2.登录成功后,支持用户进行图书的增删改查操作 输入1,查询所有数据 输入2,根据id查询单条数据 输入3,新增一条数据,需要管理员录入书名、价格(元),id是唯一主键,支持自增 输入4,管理员录入id、书名、价格(元),如果id在库中不存在,则提示该书不存在,修改失败 输入5,管理员输入id,根据id删除库中对应条目。如果id在库中不存在,则提示该书不存在,删除失败 输入0,退出账号并关闭系统
时间: 2024-03-26 18:39:50 浏览: 209
这是一个比较复杂的功能,需要编写多个类和方法。下面是一个简单的Java实现,仅供参考。
Book类:
```java
public class Book {
private int id;
private String name;
private double price;
public Book(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
```
BookManager类:
```java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class BookManager {
private List<Book> books = new ArrayList<>();
private boolean loggedIn = false;
private int loginAttempts = 0;
public void run() {
Scanner scanner = new Scanner(System.in);
while (true) {
if (!loggedIn) {
System.out.println("请选择操作:1.注册 2.登录");
int choice = scanner.nextInt();
if (choice == 1) {
register(scanner);
} else if (choice == 2) {
login(scanner);
} else {
System.out.println("输入错误,请重新选择!");
}
} else {
System.out.println("请选择操作:1.查询所有数据 2.根据id查询单条数据 3.新增一条数据 4.修改一条数据 5.删除一条数据 0.退出账号");
int choice = scanner.nextInt();
switch (choice) {
case 1:
listBooks();
break;
case 2:
searchBookById(scanner);
break;
case 3:
addBook(scanner);
break;
case 4:
modifyBook(scanner);
break;
case 5:
deleteBook(scanner);
break;
case 0:
logout();
return;
default:
System.out.println("输入错误,请重新选择!");
break;
}
}
}
}
private void register(Scanner scanner) {
System.out.println("请输入用户名和密码(以空格分隔):");
String username = scanner.next();
String password = scanner.next();
if (username.isEmpty() || password.isEmpty()) {
System.out.println("用户名和密码不能为空!");
} else {
System.out.println("注册成功,请登录!");
}
}
private void login(Scanner scanner) {
System.out.println("请输入用户名和密码(以空格分隔):");
String username = scanner.next();
String password = scanner.next();
if (username.isEmpty() || password.isEmpty()) {
System.out.println("用户名和密码不能为空!");
} else if (loginAttempts >= 3) {
System.out.println("登录失败次数过多,系统已关闭!");
System.exit(0);
} else {
System.out.println("登录成功!");
loggedIn = true;
loginAttempts = 0;
}
}
private void logout() {
loggedIn = false;
System.out.println("已退出账号!");
}
private void listBooks() {
System.out.println("所有图书:");
for (Book book : books) {
System.out.println(book.getId() + "\t" + book.getName() + "\t" + book.getPrice());
}
}
private void searchBookById(Scanner scanner) {
System.out.println("请输入图书id:");
int id = scanner.nextInt();
for (Book book : books) {
if (book.getId() == id) {
System.out.println("查询结果:");
System.out.println(book.getId() + "\t" + book.getName() + "\t" + book.getPrice());
return;
}
}
System.out.println("该图书不存在!");
}
private void addBook(Scanner scanner) {
System.out.println("请输入书名和价格(以空格分隔):");
String name = scanner.next();
double price = scanner.nextDouble();
int id = books.size() + 1;
Book book = new Book(id, name, price);
books.add(book);
System.out.println("添加成功!");
System.out.println(book.getId() + "\t" + book.getName() + "\t" + book.getPrice());
}
private void modifyBook(Scanner scanner) {
System.out.println("请输入图书id:");
int id = scanner.nextInt();
for (Book book : books) {
if (book.getId() == id) {
System.out.println("请输入书名和价格(以空格分隔):");
String name = scanner.next();
double price = scanner.nextDouble();
book.setName(name);
book.setPrice(price);
System.out.println("修改成功!");
System.out.println(book.getId() + "\t" + book.getName() + "\t" + book.getPrice());
return;
}
}
System.out.println("该图书不存在!");
}
private void deleteBook(Scanner scanner) {
System.out.println("请输入图书id:");
int id = scanner.nextInt();
for (Book book : books) {
if (book.getId() == id) {
books.remove(book);
System.out.println("删除成功!");
return;
}
}
System.out.println("该图书不存在!");
}
}
```
Main类:
```java
public class Main {
public static void main(String[] args) {
BookManager bookManager = new BookManager();
bookManager.run();
}
}
```
这个实现还有很多不足之处,例如没有持久化数据、没有输入验证等。但是,这个实现可以作为一个简单的参考,帮助你了解如何使用Java实现图书管理后台功能。
阅读全文