Загрузка данных
import sys
import json
from PyQt6.QtWidgets import *
from PyQt6.QtCore import Qt
from main_window_ui import Ui_MainWindow
class Product:
def __init__(self, name, price, year, quantity):
self.name = name
self.price = price
self.year = year
self.quantity = quantity
class Warehouse:
def __init__(self):
self.products = []
def add(self, product):
for p in self.products:
if p.name == product.name:
p.quantity += product.quantity
return
self.products.append(product)
def remove(self, name, quantity):
for p in self.products:
if p.name == name:
if p.quantity >= quantity:
p.quantity -= quantity
return True
else:
print("Ошибка. Такого продукта не существует")
return False
class Supply:
def __init__(self, supplier, products):
self.supplier = supplier
self.products = products
def apply(self, warehouse):
for p in self.products:
warehouse.add(p)
class Request:
def __init__(self, organization, product_name, quantity):
self.organization = organization
self.product_name = product_name
self.quantity = quantity
def process(self, warehouse):
return warehouse.remove(self.product_name, self.quantity)
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.warehouse = Warehouse()
file = open("products.txt", "r", encoding="utf-8")
content = file.read()
file.close()
products = json.loads(content)
for item in products:
self.warehouse.add(Product(item["name"], item["price"], item["year"], item["quantity"]))
supply = Supply("Поставщик", [
Product("Ноутбук", 78000, 2024, 6),
Product("SSD", 8500, 2024, 15)
])
supply.apply(self.warehouse)
self.btn_add.clicked.connect(self.add_supply)
self.btn_refresh.clicked.connect(self.refresh_table)
self.refresh_table()
def refresh_table(self):
self.table_warehouse.setRowCount(len(self.warehouse.products))
self.table_warehouse.setColumnCount(4)
self.table_warehouse.setHorizontalHeaderLabels(["Название", "Цена", "Год", "Количество"])
self.table_warehouse.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeMode.Stretch)
for i, p in enumerate(self.warehouse.products):
self.table_warehouse.setItem(i, 0, QTableWidgetItem(p.name))
self.table_warehouse.setItem(i, 1, QTableWidgetItem(str(p.price)))
self.table_warehouse.setItem(i, 2, QTableWidgetItem(str(p.year)))
self.table_warehouse.setItem(i, 3, QTableWidgetItem(str(p.quantity)))
def add_supply(self):
name = self.input_name.text().strip()
price = self.input_price.text().strip()
year = self.input_year.text().strip()
quantity = self.input_quantity.text().strip()
if not all([name, price, year, quantity]):
QMessageBox.warning(self, "Ошибка", "Заполните все поля!")
return
try:
price = float(price)
year = int(year)
quantity = int(quantity)
except:
QMessageBox.warning(self, "Ошибка", "Неверный формат данных!")
return
self.warehouse.add(Product(name, price, year, quantity))
self.refresh_table()
QMessageBox.information(self, "Успешно", f"Товар '{name}' добавлен!")
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())