py设计一个超市进销存管理系统
时间: 2023-08-27 09:06:55 浏览: 131
好的,一个超市进销存管理系统主要包括以下模块:
1. 商品管理模块:包括添加商品、修改商品、删除商品、查询商品等操作;
2. 库存管理模块:包括商品入库、商品出库、库存查询等操作;
3. 销售管理模块:包括销售记录、销售统计等操作;
4. 用户管理模块:包括添加用户、修改用户、删除用户、查询用户等操作;
5. 权限管理模块:包括用户权限分配、角色管理等操作。
下面是一个简单的示例代码:
```python
import json
class Product:
def __init__(self, pid, name, price, quantity):
self.id = pid
self.name = name
self.price = price
self.quantity = quantity
def __str__(self):
return f'{self.id}\t{self.name}\t{self.price}\t{self.quantity}'
class Inventory:
def __init__(self, filename):
self.filename = filename
self.products = []
self.load()
def load(self):
try:
with open(self.filename, 'r') as f:
data = json.load(f)
for p in data:
product = Product(p['id'], p['name'], p['price'], p['quantity'])
self.products.append(product)
except FileNotFoundError:
pass
def save(self):
with open(self.filename, 'w') as f:
data = []
for p in self.products:
data.append({'id': p.id, 'name': p.name, 'price': p.price, 'quantity': p.quantity})
json.dump(data, f)
def add_product(self, product):
self.products.append(product)
self.save()
def remove_product(self, pid):
for p in self.products:
if p.id == pid:
self.products.remove(p)
self.save()
return True
return False
def update_product(self, pid, name, price, quantity):
for p in self.products:
if p.id == pid:
p.name = name
p.price = price
p.quantity = quantity
self.save()
return True
return False
def search_product(self, pid=None, name=None):
result = []
for p in self.products:
if pid and p.id == pid:
result.append(p)
elif name and p.name == name:
result.append(p)
return result
def get_all_products(self):
return self.products
def add_quantity(self, pid, quantity):
for p in self.products:
if p.id == pid:
p.quantity += quantity
self.save()
return True
return False
def remove_quantity(self, pid, quantity):
for p in self.products:
if p.id == pid:
if p.quantity >= quantity:
p.quantity -= quantity
self.save()
return True
else:
return False
return False
class Sale:
def __init__(self, pid, quantity, total):
self.pid = pid
self.quantity = quantity
self.total = total
def __str__(self):
return f'{self.pid}\t{self.quantity}\t{self.total}'
class SalesRecord:
def __init__(self, filename):
self.filename = filename
self.sales = []
self.load()
def load(self):
try:
with open(self.filename, 'r') as f:
data = json.load(f)
for s in data:
sale = Sale(s['pid'], s['quantity'], s['total'])
self.sales.append(sale)
except FileNotFoundError:
pass
def save(self):
with open(self.filename, 'w') as f:
data = []
for s in self.sales:
data.append({'pid': s.pid, 'quantity': s.quantity, 'total': s.total})
json.dump(data, f)
def add_sale(self, sale):
self.sales.append(sale)
self.save()
def get_sales(self):
return self.sales
def get_total_sales(self):
total = 0
for s in self.sales:
total += s.total
return total
class User:
def __init__(self, username, password, role):
self.username = username
self.password = password
self.role = role
def __str__(self):
return f'{self.username}\t{self.role}'
class UserManager:
def __init__(self, filename):
self.filename = filename
self.users = []
self.load()
def load(self):
try:
with open(self.filename, 'r') as f:
data = json.load(f)
for u in data:
user = User(u['username'], u['password'], u['role'])
self.users.append(user)
except FileNotFoundError:
pass
def save(self):
with open(self.filename, 'w') as f:
data = []
for u in self.users:
data.append({'username': u.username, 'password': u.password, 'role': u.role})
json.dump(data, f)
def add_user(self, user):
self.users.append(user)
self.save()
def remove_user(self, username):
for u in self.users:
if u.username == username:
self.users.remove(u)
self.save()
return True
return False
def update_user(self, username, password, role):
for u in self.users:
if u.username == username:
u.password = password
u.role = role
self.save()
return True
return False
def search_user(self, username=None):
result = []
for u in self.users:
if username and u.username == username:
result.append(u)
return result
def get_all_users(self):
return self.users
class Role:
ADMIN = 'admin'
CASHIER = 'cashier'
class Supermarket:
def __init__(self):
self.inventory = Inventory('inventory.json')
self.sales = SalesRecord('sales.json')
self.users = UserManager('users.json')
self.current_user = None
def login(self, username, password):
users = self.users.search_user(username)
if users:
user = users[0]
if user.password == password:
self.current_user = user
return True
return False
def logout(self):
self.current_user = None
def is_admin(self):
return self.current_user and self.current_user.role == Role.ADMIN
def add_product(self, pid, name, price, quantity):
product = Product(pid, name, price, quantity)
self.inventory.add_product(product)
def remove_product(self, pid):
if self.inventory.remove_product(pid):
self.sales.sales = [s for s in self.sales.sales if s.pid != pid]
def update_product(self, pid, name, price, quantity):
self.inventory.update_product(pid, name, price, quantity)
def search_product(self, pid=None, name=None):
return self.inventory.search_product(pid, name)
def get_all_products(self):
return self.inventory.get_all_products()
def add_quantity(self, pid, quantity):
if self.inventory.add_quantity(pid, quantity):
product = self.search_product(pid=pid)[0]
total = product.price * quantity
sale = Sale(pid, quantity, total)
self.sales.add_sale(sale)
def remove_quantity(self, pid, quantity):
if self.inventory.remove_quantity(pid, quantity):
product = self.search_product(pid=pid)[0]
total = product.price * quantity
sale = Sale(pid, quantity, total)
self.sales.add_sale(sale)
def get_sales(self):
return self.sales.get_sales()
def get_total_sales(self):
return self.sales.get_total_sales()
def add_user(self, username, password, role):
user = User(username, password, role)
self.users.add_user(user)
def remove_user(self, username):
self.users.remove_user(username)
def update_user(self, username, password, role):
self.users.update_user(username, password, role)
def search_user(self, username=None):
return self.users.search_user(username)
def get_all_users(self):
return self.users.get_all_users()
```
上面的代码中,我们定义了 `Product`、`Inventory`、`Sale`、`SalesRecord`、`User`、`UserManager`、`Supermarket` 等类来实现超市进销存管理系统的各项操作,其中:
- `Product` 类表示商品信息,包括商品编号、商品名称、商品单价和库存数量;
- `Inventory` 类表示库存管理模块,包括添加商品、删除商品、修改商品、查询商品、商品入库、商品出库等操作;
- `Sale` 类表示销售记录,包括商品编号、销售数量和销售总额;
- `SalesRecord` 类表示销售管理模块,包括销售记录和销售统计等操作;
- `User` 类表示用户信息,包括用户名、密码和角色;
- `UserManager` 类表示用户管理模块,包括添加用户、删除用户、修改用户、查询用户等操作;
- `Supermarket` 类表示超市进销存管理系统,包括系统登录、系统登出、商品管理、库存管理、销售管理、用户管理等操作。
我们可以通过以下代码来测试上面的超市进销存管理系统:
```python
# 创建一个超市进销存管理系统
supermarket = Supermarket()
# 添加商品
supermarket.add_product('001', '可乐', 2.5, 100)
supermarket.add_product('002', '薯片', 3.0, 200)
supermarket.add_product('003', '巧克力', 5.0, 150)
# 查询商品
print('所有商品:')
for p in supermarket.get_all_products():
print(p)
print()
print('查询商品:')
for p in supermarket.search_product(name='可乐'):
print(p)
print()
# 商品入库
supermarket.add_quantity('001', 50)
# 商品出库
supermarket.remove_quantity('002', 100)
# 查询销售记录
print('销售记录:')
for s in supermarket.get_sales():
print(s)
print()
# 查询销售总额
print(f'销售总额:{supermarket.get_total_sales()}')
print()
# 添加用户
supermarket.add_user('admin', 'admin123', Role.ADMIN)
supermarket.add_user('cashier', 'cashier123', Role.CASHIER)
# 查询用户
print('所有用户:')
for u in supermarket.get_all_users():
print(u)
print()
print('查询用户:')
for u in supermarket.search_user(username='admin'):
print(u)
print()
# 修改用户
supermarket.update_user('admin', 'admin456', Role.CASHIER)
# 删除用户
supermarket.remove_user('cashier')
```
阅读全文