Write data to Json file with UUID
This commit is contained in:
@@ -35,6 +35,7 @@ class ShopItem:
|
||||
case ShopItemType.Movie:
|
||||
if movie is not None:
|
||||
self.cost = movie.price
|
||||
self.movie = movie
|
||||
else:
|
||||
raise ValueError("ShopItem with type ShopItemType.Movie must provide a movie as an argument")
|
||||
case ShopItemType.Popcorn:
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
import json
|
||||
import uuid
|
||||
|
||||
from PySide6.QtWidgets import QWidget, QVBoxLayout, QLabel, QLineEdit, QPushButton
|
||||
|
||||
class Payment(QWidget):
|
||||
@@ -10,6 +13,8 @@ class Payment(QWidget):
|
||||
def __init__(self, cart):
|
||||
super().__init__()
|
||||
|
||||
self.cart = cart
|
||||
|
||||
self.setWindowTitle("Payment")
|
||||
|
||||
layout = QVBoxLayout()
|
||||
@@ -49,6 +54,7 @@ class Payment(QWidget):
|
||||
self.widget.setWindowTitle("Payment")
|
||||
layout = QVBoxLayout()
|
||||
layout.addWidget(QLabel("Please enter your name"))
|
||||
self.widget.setLayout(layout)
|
||||
self.widget.show()
|
||||
return
|
||||
if self.number.text() == "":
|
||||
@@ -56,6 +62,7 @@ class Payment(QWidget):
|
||||
self.widget.setWindowTitle("Payment")
|
||||
layout = QVBoxLayout()
|
||||
layout.addWidget(QLabel("Please enter your credit card number"))
|
||||
self.widget.setLayout(layout)
|
||||
self.widget.show()
|
||||
return
|
||||
if self.cvv.text() == "":
|
||||
@@ -63,6 +70,7 @@ class Payment(QWidget):
|
||||
self.widget.setWindowTitle("Payment")
|
||||
layout = QVBoxLayout()
|
||||
layout.addWidget(QLabel("Please enter your CVV"))
|
||||
self.widget.setLayout(layout)
|
||||
self.widget.show()
|
||||
return
|
||||
if self.expiry.text() == "":
|
||||
@@ -70,17 +78,45 @@ class Payment(QWidget):
|
||||
self.widget.setWindowTitle("Payment")
|
||||
layout = QVBoxLayout()
|
||||
layout.addWidget(QLabel("Please enter your expiry date"))
|
||||
self.widget.setLayout(layout)
|
||||
self.widget.show()
|
||||
return
|
||||
|
||||
# If this was a real payment system, we would check all the details here
|
||||
# Luckily, this is just an assignment, so we'll just assume it's valid
|
||||
|
||||
paymentId = uuid.uuid4()
|
||||
|
||||
self.widget = QWidget()
|
||||
self.widget.setWindowTitle("Payment")
|
||||
layout = QVBoxLayout()
|
||||
layout.addWidget(QLabel("Payment Successful!"))
|
||||
layout.addWidget(QLabel(f"Payment Successful!\n\nYour payment ID is: {paymentId}"))
|
||||
self.widget.setLayout(layout)
|
||||
self.widget.show()
|
||||
self.close()
|
||||
|
||||
# JSONify all the details
|
||||
dictionary = {
|
||||
"cardInfo": {
|
||||
"name": self.name.text(),
|
||||
"number": self.number.text(),
|
||||
"cvv": self.cvv.text(),
|
||||
"expiry": self.expiry.text()
|
||||
},
|
||||
"cart": []
|
||||
}
|
||||
for item in self.cart.contents:
|
||||
dictionary["cart"].append({
|
||||
"type": item.type.name,
|
||||
"quantity": item.quantity,
|
||||
"total": item.total(),
|
||||
})
|
||||
if item.movie is not None:
|
||||
dictionary["cart"][-1]["movie"] = item.movie.name
|
||||
|
||||
string = json.dumps(dictionary, indent=4)
|
||||
with open(f"payment-{paymentId}.json", "w") as f:
|
||||
f.write(string)
|
||||
|
||||
self.cart.contents.clear()
|
||||
|
||||
|
||||
15
src/app.py
15
src/app.py
@@ -1,4 +1,6 @@
|
||||
from PySide6.QtWidgets import QApplication, QMainWindow, QWidget, QHBoxLayout, QVBoxLayout, QPushButton, QLabel
|
||||
from PySide6.QtGui import QCloseEvent
|
||||
from PySide6.QtWidgets import QApplication, QMainWindow, QWidget, QHBoxLayout, QVBoxLayout, QPushButton, QLabel, \
|
||||
QMessageBox
|
||||
from typing import final
|
||||
import sys
|
||||
|
||||
@@ -77,6 +79,17 @@ class MainWindow(QMainWindow):
|
||||
self.currentMovie = MovieView(movies["hairypotty"])
|
||||
self.currentMovie.show()
|
||||
|
||||
def closeEvent(self, event: QCloseEvent):
|
||||
global cart
|
||||
if len(cart.contents) == 0:
|
||||
event.accept()
|
||||
else:
|
||||
reply = QMessageBox.question(self, 'Items in cart', "Your cart is not empty! It will be lost if you close the app.\n\nAre you sure you want to close the app?", buttons=QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No, defaultButton=QMessageBox.StandardButton.No)
|
||||
if reply == QMessageBox.StandardButton.Yes:
|
||||
event.accept()
|
||||
else:
|
||||
event.ignore()
|
||||
|
||||
|
||||
app = QApplication(sys.argv)
|
||||
window = MainWindow(cart)
|
||||
|
||||
Reference in New Issue
Block a user