用java写一个网上书店管理系统
时间: 2024-05-07 19:19:52 浏览: 80
网上书店信息管理系统加代码加数据库表模板.doc
好的,下面是一个简单的网上书店管理系统的Java代码,包含书籍信息的添加、删除、修改和查询功能:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class OnlineBookstore {
private ArrayList<Book> books;
private Scanner scanner;
public OnlineBookstore() {
books = new ArrayList<>();
scanner = new Scanner(System.in);
}
public void addBook() {
System.out.print("请输入书名:");
String name = scanner.nextLine();
System.out.print("请输入作者:");
String author = scanner.nextLine();
System.out.print("请输入价格:");
double price = scanner.nextDouble();
scanner.nextLine();
Book book = new Book(name, author, price);
books.add(book);
System.out.println("添加成功!");
}
public void deleteBook() {
System.out.print("请输入要删除的书名:");
String name = scanner.nextLine();
for (Book book : books) {
if (book.getName().equals(name)) {
books.remove(book);
System.out.println("删除成功!");
return;
}
}
System.out.println("没有找到该书!");
}
public void modifyBook() {
System.out.print("请输入要修改的书名:");
String name = scanner.nextLine();
for (Book book : books) {
if (book.getName().equals(name)) {
System.out.print("请输入修改后的书名:");
String newName = scanner.nextLine();
System.out.print("请输入修改后的作者:");
String author = scanner.nextLine();
System.out.print("请输入修改后的价格:");
double price = scanner.nextDouble();
scanner.nextLine();
book.setName(newName);
book.setAuthor(author);
book.setPrice(price);
System.out.println("修改成功!");
return;
}
}
System.out.println("没有找到该书!");
}
public void searchBook() {
System.out.print("请输入要查询的书名:");
String name = scanner.nextLine();
for (Book book : books) {
if (book.getName().equals(name)) {
System.out.println("查询结果:");
System.out.println("书名:" + book.getName());
System.out.println("作者:" + book.getAuthor());
System.out.println("价格:" + book.getPrice());
return;
}
}
System.out.println("没有找到该书!");
}
public void displayBooks() {
System.out.println("所有书籍信息:");
for (Book book : books) {
System.out.println("书名:" + book.getName());
System.out.println("作者:" + book.getAuthor());
System.out.println("价格:" + book.getPrice());
System.out.println("---------------");
}
}
public static void main(String[] args) {
OnlineBookstore bookstore = new OnlineBookstore();
while (true) {
System.out.println("请选择操作:");
System.out.println("1.添加书籍");
System.out.println("2.删除书籍");
System.out.println("3.修改书籍");
System.out.println("4.查询书籍");
System.out.println("5.显示所有书籍信息");
System.out.println("6.退出");
int choice = bookstore.scanner.nextInt();
bookstore.scanner.nextLine();
switch (choice) {
case 1:
bookstore.addBook();
break;
case 2:
bookstore.deleteBook();
break;
case 3:
bookstore.modifyBook();
break;
case 4:
bookstore.searchBook();
break;
case 5:
bookstore.displayBooks();
break;
case 6:
System.exit(0);
default:
System.out.println("请输入正确的选项!");
}
}
}
}
class Book {
private String name;
private String author;
private double price;
public Book(String name, String author, double price) {
this.name = name;
this.author = author;
this.price = price;
}
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;
}
}
```
注意,这只是一个简单的示例,实际开发中还需要考虑到更多的功能和异常处理。
阅读全文