网上书店管理系统 java
时间: 2023-10-23 14:10:25 浏览: 119
java的网上书店管理系统
5星 · 资源好评率100%
网上书店管理系统是一个电子商务平台,用户可以在该平台上购买图书、管理个人信息、查询订单等。以下是一个简单的网上书店管理系统的Java代码示例:
```
import java.util.ArrayList;
import java.util.Scanner;
class Book {
private String name;
private String author;
private double price;
private int num;
public Book(String name, String author, double price, int num) {
this.name = name;
this.author = author;
this.price = price;
this.num = num;
}
public String getName() {
return name;
}
public String getAuthor() {
return author;
}
public double getPrice() {
return price;
}
public int getNum() {
return num;
}
public void setPrice(double price) {
this.price = price;
}
public void setNum(int num) {
this.num = num;
}
}
class User {
private String username;
private String password;
public User(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}
public class OnlineBookstore {
private ArrayList<Book> books;
private ArrayList<User> users;
public OnlineBookstore() {
books = new ArrayList<>();
users = new ArrayList<>();
}
public void login() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入用户名:");
String username = scanner.nextLine();
System.out.print("请输入密码:");
String password = scanner.nextLine();
for (User user : users) {
if (user.getUsername().equals(username) && user.getPassword().equals(password)) {
System.out.println("登录成功!");
return;
}
}
System.out.println("用户名或密码错误,请重新输入!");
login();
}
public void addBook() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入书名:");
String name = scanner.nextLine();
System.out.print("请输入作者:");
String author = scanner.nextLine();
System.out.print("请输入价格:");
double price = scanner.nextDouble();
System.out.print("请输入数量:");
int num = scanner.nextInt();
books.add(new Book(name, author, price, num));
System.out.println("添加成功!");
}
public void editBook() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要修改的书名:");
String name = scanner.nextLine();
for (Book book : books) {
if (book.getName().equals(name)) {
System.out.print("请输入修改后的价格:");
double price = scanner.nextDouble();
System.out.print("请输入修改后的数量:");
int num = scanner.nextInt();
book.setPrice(price);
book.setNum(num);
System.out.println("修改成功!");
return;
}
}
System.out.println("没有找到该书籍!");
}
public void searchBook() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要查询的书名:");
String name = scanner.nextLine();
for (Book book : books) {
if (book.getName().equals(name)) {
System.out.println("书名:" + book.getName());
System.out.println("作者:" + book.getAuthor());
System.out.println("价格:" + book.getPrice());
System.out.println("数量:" + book.getNum());
return;
}
}
System.out.println("没有找到该书籍!");
}
public void displayBooks() {
for (Book book : books) {
System.out.println("书名:" + book.getName());
System.out.println("作者:" + book.getAuthor());
System.out.println("价格:" + book.getPrice());
System.out.println("数量:" + book.getNum());
System.out.println();
}
}
public static void main(String[] args) {
OnlineBookstore bookstore = new OnlineBookstore();
bookstore.users.add(new User("admin", "admin"));
bookstore.login();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("1. 添加书籍");
System.out.println("2. 修改书籍");
System.out.println("3. 查询书籍");
System.out.println("4. 显示所有书籍");
System.out.println("5. 退出");
System.out.print("请输入操作编号:");
int choice = scanner.nextInt();
switch (choice) {
case 1:
bookstore.addBook();
break;
case 2:
bookstore.editBook();
break;
case 3:
bookstore.searchBook();
break;
case 4:
bookstore.displayBooks();
break;
case 5:
System.exit(0);
default:
System.out.println("输入错误,请重新输入!");
}
}
}
}
```
该示例实现了以下功能:
1. 用户登录(默认用户名为admin,密码为admin)。
2. 添加书籍。
3. 修改书籍。
4. 查询书籍。
5. 显示所有书籍。
注意:该示例仅供参考,实际项目中需要根据具体需求进行相应的修改和完善。
阅读全文