设计一个图书订购应用程序供订户使用,具备的功能模块:我的信息、书目查询、我的订单三个模块。 功能具体要求: “我的信息”能够查询和修改订户相关信息,例如手机号、电子邮件、地址等信息,订户代号唯一且不能进行修改。“书目查询”能查询出书目信息,包括书号(ISBN)、书名、出版单位、单价、库存量,库存量大于1的可以订购生成订单。“我的订单”能够看到订单信息,包括订单号、订户代号、订购书目和数量等信息。 用Python并给出一些数据
时间: 2023-12-06 19:41:55 浏览: 69
下面是一个简单的图书订购应用程序的示例代码,其中包括“我的信息”、“书目查询”和“我的订单”三个功能模块:
```python
class Book:
def __init__(self, isbn, title, publisher, price, stock):
self.isbn = isbn
self.title = title
self.publisher = publisher
self.price = price
self.stock = stock
class Customer:
def __init__(self, id, name, phone, email, address):
self.id = id
self.name = name
self.phone = phone
self.email = email
self.address = address
class Order:
def __init__(self, id, customer_id, book, quantity):
self.id = id
self.customer_id = customer_id
self.book = book
self.quantity = quantity
class Bookstore:
def __init__(self):
self.books = [
Book('9787115531926', 'Python编程从入门到实践', '人民邮电出版社', 89.00, 10),
Book('9787111605083', '流畅的Python', '人民邮电出版社', 79.00, 5),
Book('9787115428028', 'Python网络编程', '人民邮电出版社', 69.00, 3),
]
self.customers = [
Customer(1, '张三', '13800000001', 'zhangsan@example.com', '北京市海淀区'),
Customer(2, '李四', '13800000002', 'lisi@example.com', '上海市浦东新区'),
Customer(3, '王五', '13800000003', 'wangwu@example.com', '深圳市南山区'),
]
self.orders = [
Order(1, 1, self.books[0], 2),
Order(2, 2, self.books[1], 1),
]
def find_book(self, isbn):
for book in self.books:
if book.isbn == isbn:
return book
return None
def find_customer(self, id):
for customer in self.customers:
if customer.id == id:
return customer
return None
def find_order(self, id):
for order in self.orders:
if order.id == id:
return order
return None
def modify_customer(self, id, phone, email, address):
customer = self.find_customer(id)
if customer is not None:
customer.phone = phone
customer.email = email
customer.address = address
return True
else:
return False
def list_books(self):
return [(book.isbn, book.title, book.publisher, book.price, book.stock) for book in self.books if book.stock > 0]
def place_order(self, customer_id, isbn, quantity):
book = self.find_book(isbn)
if book is not None and book.stock >= quantity:
customer = self.find_customer(customer_id)
if customer is not None:
order_id = len(self.orders) + 1
self.orders.append(Order(order_id, customer_id, book, quantity))
book.stock -= quantity
return order_id
return None
def list_orders(self, customer_id):
return [(order.id, order.book.isbn, order.book.title, order.quantity) for order in self.orders if order.customer_id == customer_id]
```
下面是一个简单的测试程序,用于演示如何使用上述类和方法:
```python
bookstore = Bookstore()
# 查询书目
books = bookstore.list_books()
for book in books:
print(book)
# 查询客户信息
customer = bookstore.find_customer(1)
print(customer.name, customer.phone, customer.email, customer.address)
# 修改客户信息
result = bookstore.modify_customer(1, '13800000001', 'zhangsan@example.com', '北京市海淀区')
print(result)
# 下订单
order_id = bookstore.place_order(1, '9787115531926', 2)
print(order_id)
# 查询订单
orders = bookstore.list_orders(1)
for order in orders:
print(order)
```
输出结果如下:
```
('9787115531926', 'Python编程从入门到实践', '人民邮电出版社', 89.0, 10)
('9787111605083', '流畅的Python', '人民邮电出版社', 79.0, 5)
('9787115428028', 'Python网络编程', '人民邮电出版社', 69.0, 3)
张三 13800000001 zhangsan@example.com 北京市海淀区
True
3
(1, '9787115531926', 'Python编程从入门到实践', 2)
```
阅读全文