parking

mail@pastecode.io avatar
unknown
python
a year ago
10 kB
1
Indexable
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton, \
    QTableWidget, QTableWidgetItem, QHeaderView, QComboBox, QMessageBox, QFrame, QSpacerItem, QSizePolicy, QTextBrowser
from PyQt5.QtGui import QColor
from PyQt5.QtCore import QTimer

class ParkingGUI(QWidget):
    def __init__(self):
        super().__init__()

        self.car_list = []
        self.parked_car = []

        self.init_ui()


    def init_ui(self):
        self.setWindowTitle('Parking System')
        self.setGeometry(100, 100, 1600, 500)

        self.create_widgets()
        self.create_layout()

        self.show()

    def create_widgets(self):
        self.label_license_plate = QLabel('Numer rejestracyjny:')
        self.input_license_plate = QLineEdit()

        self.label_color = QLabel('Kolor:')
        self.input_color = QLineEdit()

        self.label_vehicle_type = QLabel('Typ pojazdu:')
        self.input_vehicle_type = QComboBox()
        self.input_vehicle_type.addItems(['Osobowy', 'Ciężarowy', 'Jednoślad'])

        self.label_engine_capacity = QLabel('Pojemność silnika:')
        self.input_engine_capacity = QLineEdit()

        self.label_status = QLabel('Status:')
        self.input_status = QComboBox()
        self.input_status.addItems(['Niezaparkowany', 'Zaparkowany'])

        self.button_add_car = QPushButton('Dodaj')
        self.button_add_car.clicked.connect(self.add_car)

        self.button_enter_parking = QPushButton('Wjazd na parking')
        self.button_enter_parking.clicked.connect(self.enter_parking)

        self.button_exit_parking = QPushButton('Wyjazd z parkingu')
        self.button_exit_parking.clicked.connect(self.exit_parking)

        self.button_remove_car = QPushButton('Usuń')
        self.button_remove_car.clicked.connect(self.remove_car)

        self.table = QTableWidget()
        self.table.selectionModel().selectionChanged.connect(self.update_car_parking_history)
        self.table.setColumnCount(6)
        self.table.setHorizontalHeaderLabels(['Numer rejestracyjny', 'Kolor', 'Typ pojazdu', 'Pojemność silnika', 'Status', 'Czas parkowania'])
        header = self.table.horizontalHeader()
        header.setSectionResizeMode(QHeaderView.Stretch)

        self.table.setMaximumWidth(900)

        self.parking_history = QTextBrowser()

        self.car_history = QTextBrowser()

        self.timer = QTimer(self)
        self.timer.timeout.connect(self.update_parking_time)
        self.timer.start(10000)

        self.label_total_costs = QLabel('Bieżący utarg: 0')
        self.label_parking_history = QLabel(self.parking_history)
        self.label_parking_history.setText('Historia parkowania')
        self.label_car_history = QLabel('Historia parkowania pojazdu:')

        self.currently_parked_car = None

    def create_layout(self):
        layout_main = QVBoxLayout()

        frame_input = QFrame(self)
        frame_input.setFrameStyle(QFrame.Panel | QFrame.Raised)
        frame_input.setStyleSheet("background-color: #b3d1ff")
        layout_input = QVBoxLayout(frame_input)

        layout_input.addWidget(self.label_license_plate)
        layout_input.addWidget(self.input_license_plate)
        layout_input.addWidget(self.label_color)
        layout_input.addWidget(self.input_color)
        layout_input.addWidget(self.label_vehicle_type)
        layout_input.addWidget(self.input_vehicle_type)
        layout_input.addWidget(self.label_engine_capacity)
        layout_input.addWidget(self.input_engine_capacity)
        layout_input.addWidget(self.label_status)
        layout_input.addWidget(self.input_status)
        spacer_item = QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding)
        layout_input.addItem(spacer_item)
        layout_input.addWidget(self.button_add_car)
        layout_input.addWidget(self.button_remove_car)
        spacer_item = QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding)
        layout_input.addItem(spacer_item)
        layout_input.addWidget(self.button_enter_parking)
        layout_input.addWidget(self.button_exit_parking)

        frame_input.setMaximumWidth(200)
        layout_info = QVBoxLayout()
        layout_info.addWidget(self.label_total_costs)
        layout_table = QHBoxLayout()


        layout_table.addWidget(self.table)
        self.parking_history.setMaximumWidth(200)

        self.car_history.setMaximumWidth(200)





        layout_horizontal = QHBoxLayout()
        layout_horizontal.addWidget(frame_input)
        layout_horizontal.addLayout(layout_table)
        layout_horizontal.addLayout(layout_info)

        layout_main.addLayout(layout_horizontal)

        layout_parkhistory = QVBoxLayout()
        layout_parkhistory.addWidget(self.label_parking_history)
        layout_parkhistory.addWidget(self.parking_history)
        layout_table.addLayout(layout_parkhistory)

        layout_carhistory = QVBoxLayout()
        layout_carhistory.addWidget(self.label_car_history)
        layout_carhistory.addWidget(self.car_history)
        layout_table.addLayout(layout_carhistory)

        self.setLayout(layout_main)

    def add_car(self):
        license_plate = self.input_license_plate.text()
        color = self.input_color.text()
        vehicle_type = self.input_vehicle_type.currentText()
        engine_capacity = self.input_engine_capacity.text()
        status = self.input_status.currentText()

        if license_plate and color and engine_capacity:
            if not any(car['license_plate'] == license_plate for car in self.car_list):
                car = {'license_plate': license_plate, 'color': color, 'vehicle_type': vehicle_type, 'engine_capacity': engine_capacity, 'parking_time': 0, 'total_cost': 0, 'status': status, 'car_parking_history': []}
                self.car_list.append(car)
                self.update_table()

                self.input_license_plate.clear()
                self.input_color.clear()
                self.input_engine_capacity.clear()
            else:
                QMessageBox.warning(self, 'Błąd', 'Taki pojazd już istnieje.')
        else:
            QMessageBox.warning(self, 'Błąd', 'Uzupełnij potrzebne informacje.')

    def enter_parking(self):
        selected_row = self.table.currentRow()
        if selected_row != -1:
            self.currently_parked_car = self.car_list[selected_row]
            if self.currently_parked_car['status'] == 'Niezaparkowany':
                self.car_list[selected_row]['parking_time'] += 1
                self.car_list[selected_row]['status'] = 'Zaparkowany'

            self.update_table()

    def exit_parking(self):
        selected_row = self.table.currentRow()
        if selected_row != -1:
            if self.car_list[selected_row]['status'] == 'Zaparkowany':
                car_parking_history_row = f"{self.car_list[selected_row]['parking_time']}h - {self.car_list[selected_row]['parking_time'] * 10}zł"
                self.car_list[selected_row]['car_parking_history'].append(car_parking_history_row)


                self.car_list[selected_row]['status'] = 'Niezaparkowany'
                self.car_list[selected_row]['total_cost'] += self.car_list[selected_row]['parking_time'] * 10
                self.car_list[selected_row]['parking_time'] = 0
            self.update_table()

    def remove_car(self):
        selected_row = self.table.currentRow()
        if selected_row != -1:
            removed_car = self.car_list.pop(selected_row)
            if removed_car == self.currently_parked_car:
                self.currently_parked_car = None
            self.update_table()

            # Odjęcie kosztu usuniętego pojazdu od całkowitej sumy opłat
            total_costs = sum(car['total_cost'] for car in self.car_list)
            self.label_total_costs.setText(f'Bieżący utarg: {total_costs}')

    def update_table(self):
        self.table.setRowCount(len(self.car_list))
        for row, car in enumerate(self.car_list):
            self.table.setItem(row, 0, QTableWidgetItem(car['license_plate']))
            self.table.setItem(row, 1, QTableWidgetItem(car['color']))
            self.table.setItem(row, 2, QTableWidgetItem(car['vehicle_type']))
            self.table.setItem(row, 3, QTableWidgetItem(car['engine_capacity']))
            self.table.setItem(row, 4, QTableWidgetItem(car['status']))
            self.table.setItem(row, 5, QTableWidgetItem(str(car['parking_time'])))

        total_costs = sum(car['total_cost'] for car in self.car_list)
        self.label_total_costs.setText(f'Bieżący utarg: {total_costs}')

        self.parking_history.clear()
        parking_history_list = self.get_parking_history()
        for string in parking_history_list:
            self.parking_history.append(string)

    def update_car_parking_history(self):
        selected_row = self.table.currentRow()
        license_plate = self.table.item(selected_row, 0)

        for car in self.car_list:
            if car['license_plate'] == license_plate:
                car_parking_history_list = car['car_parking_history']
                break

        self.car_history.clear()




    def update_parking_time(self):
        for car in self.car_list:
            if car['status'] == 'Zaparkowany':
                car['parking_time'] += 1
        self.update_table()


    def get_parking_history(self):
        parking_history_list = []
        for car in self.car_list:
            parking_history_string = f"{car['license_plate']} {car['total_cost']}zł"
            parking_history_list.append(parking_history_string)
        return parking_history_list

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = ParkingGUI()
    sys.exit(app.exec_())
Leave a Comment