Adding stuff to cart
This commit is contained in:
17
src/Cart.py
17
src/Cart.py
@@ -17,7 +17,7 @@ class ShopItem:
|
||||
"""
|
||||
Represents an item that can be bought from the cinema.
|
||||
"""
|
||||
def __init__(self, type: ShopItemType, quantity: int, movie: Movie | None = None):
|
||||
def __init__(self, shoptype: ShopItemType, quantity: int, movie: Movie | None = None):
|
||||
"""
|
||||
Creates a new ShopItem.
|
||||
Throws:
|
||||
@@ -25,13 +25,13 @@ class ShopItem:
|
||||
Example (creates a ShopItem with type popcorn and quantity 3):
|
||||
ShopItem(ShopItemType.Popcorn, 3)
|
||||
"""
|
||||
self.type = type
|
||||
self.type = shoptype
|
||||
self.movie = movie
|
||||
self.quantity = quantity
|
||||
self.cost: float
|
||||
match type:
|
||||
match shoptype:
|
||||
case ShopItemType.Movie:
|
||||
if movie != None:
|
||||
if movie is not None:
|
||||
self.cost = movie.price
|
||||
else:
|
||||
raise ValueError("ShopItem with type ShopItemType.Movie must provide a movie as an argument")
|
||||
@@ -41,18 +41,21 @@ class ShopItem:
|
||||
case ShopItemType.Frozen_Coke:
|
||||
self.cost = 4.95
|
||||
|
||||
def __repr__(self):
|
||||
return f"ShopItem(type={self.type}, movie={self.movie}, quantity={self.quantity}, cost={self.cost})"
|
||||
|
||||
@final
|
||||
class Cart:
|
||||
def __init__(self):
|
||||
self.contents: list[ShopItem] = []
|
||||
|
||||
def __repr__(self):
|
||||
return f"Cart(contents={self.contents})"
|
||||
|
||||
def addItem(self, item: ShopItem):
|
||||
self.contents.append(item)
|
||||
pass
|
||||
|
||||
cart = Cart()
|
||||
|
||||
|
||||
class CartView(QWidget):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
@@ -1,6 +1,93 @@
|
||||
from PySide6.QtGui import Qt
|
||||
from PySide6.QtWidgets import QHBoxLayout, QPushButton, QSpinBox, QVBoxLayout, QWidget, QGridLayout, QLabel
|
||||
|
||||
from Cart import ShopItem, ShopItemType
|
||||
from Movie import Movie, createImage
|
||||
from PySide6.QtWidgets import QHBoxLayout, QPushButton, QWidget, QGridLayout, QLabel
|
||||
from config import cart
|
||||
|
||||
class MoviePurchase(QWidget):
|
||||
"""
|
||||
Widget that helps the user to select the amount of tickets they would like.
|
||||
Also attempts to upsell the user with popcorn and soft drink.
|
||||
"""
|
||||
def __init__(self, movie: Movie):
|
||||
super().__init__()
|
||||
|
||||
self.movie: Movie = movie
|
||||
|
||||
bodylayout = QVBoxLayout()
|
||||
sidebysidelayout = QHBoxLayout()
|
||||
|
||||
self.setMaximumWidth(800)
|
||||
self.setMaximumHeight(600)
|
||||
|
||||
self.setMinimumWidth(800)
|
||||
self.setMinimumHeight(600)
|
||||
|
||||
# Left layout - contains the box for the quantity of tickets
|
||||
leftlayout = QGridLayout()
|
||||
leftlayout.setAlignment(Qt.AlignTop | Qt.AlignLeft)
|
||||
leftlayout.addWidget(QLabel("Amount of tickets to get:"), 0, 0)
|
||||
|
||||
self.tickets: QSpinBox = QSpinBox()
|
||||
self.tickets.setRange(1, 50)
|
||||
self.tickets.setSingleStep(1)
|
||||
self.tickets.setValue(1)
|
||||
leftlayout.addWidget(self.tickets, 1, 0)
|
||||
|
||||
# Right layout - contains the box for upselling the customer
|
||||
rightlayout = QGridLayout()
|
||||
rightlayout.setAlignment(Qt.AlignTop | Qt.AlignLeft)
|
||||
rightlayout.addWidget(QLabel("The Snackbar"), 0, 0)
|
||||
|
||||
# Popcorn
|
||||
rightlayout.addWidget(QLabel("Popcorn ($8.50 per unit)"), 1, 0)
|
||||
self.popcorn: QSpinBox = QSpinBox()
|
||||
self.popcorn.setRange(0, 50)
|
||||
self.popcorn.setSingleStep(1)
|
||||
self.popcorn.setValue(1)
|
||||
rightlayout.addWidget(self.popcorn, 2, 0)
|
||||
|
||||
# Frozen Coke
|
||||
rightlayout.addWidget(QLabel("Frozen Coke ($4.50 per unit)"), 3, 0)
|
||||
self.frozencoke: QSpinBox = QSpinBox()
|
||||
self.frozencoke.setRange(0, 50)
|
||||
self.frozencoke.setSingleStep(1)
|
||||
self.frozencoke.setValue(1)
|
||||
rightlayout.addWidget(self.frozencoke, 4, 0)
|
||||
|
||||
# Lolly bag
|
||||
rightlayout.addWidget(QLabel("Lolly Bag ($3.50 per unit)"), 5, 0)
|
||||
self.lollybag: QSpinBox = QSpinBox()
|
||||
self.lollybag.setRange(0, 50)
|
||||
self.lollybag.setSingleStep(1)
|
||||
self.lollybag.setValue(1)
|
||||
rightlayout.addWidget(self.lollybag, 6, 0)
|
||||
|
||||
|
||||
sidebysidelayout.addLayout(leftlayout)
|
||||
sidebysidelayout.addLayout(rightlayout)
|
||||
|
||||
# Button to add stuff to cart
|
||||
self.cartButton: QPushButton = QPushButton("Add to Cart")
|
||||
_ = self.cartButton.clicked.connect(self.buttonCallback)
|
||||
|
||||
bodylayout.addLayout(sidebysidelayout)
|
||||
bodylayout.addWidget(self.cartButton)
|
||||
|
||||
self.setLayout(bodylayout)
|
||||
self.setWindowTitle("Adding movie to cart")
|
||||
|
||||
def buttonCallback(self):
|
||||
global cart
|
||||
cart.addItem(ShopItem(ShopItemType.Movie, 1, self.movie))
|
||||
|
||||
print("Yay adding to cart")
|
||||
confirmLayout = QVBoxLayout()
|
||||
confirmLayout.addWidget(QLabel("Added to cart!"))
|
||||
self.setLayout(confirmLayout)
|
||||
|
||||
pass
|
||||
|
||||
class MovieView(QWidget):
|
||||
"""
|
||||
@@ -11,6 +98,10 @@ class MovieView(QWidget):
|
||||
def __init__(self, movie: Movie):
|
||||
super().__init__()
|
||||
|
||||
self.movie: Movie = movie
|
||||
|
||||
self.mp: MoviePurchase = MoviePurchase(self.movie)
|
||||
|
||||
self.setMaximumWidth(800)
|
||||
self.setMaximumHeight(600)
|
||||
self.setWindowTitle("Movie Information")
|
||||
@@ -26,6 +117,7 @@ class MovieView(QWidget):
|
||||
|
||||
button1 = QPushButton("Add to cart")
|
||||
button1.setMaximumWidth(100)
|
||||
_ = button1.clicked.connect(self.buyCallback)
|
||||
|
||||
contentlayout.addWidget(label1, 0, 0, alignment=Qt.AlignmentFlag.AlignTop | Qt.AlignmentFlag.AlignLeft)
|
||||
contentlayout.addWidget(button1, 1, 0, alignment=Qt.AlignmentFlag.AlignBottom | Qt.AlignmentFlag.AlignLeft)
|
||||
@@ -35,4 +127,8 @@ class MovieView(QWidget):
|
||||
|
||||
self.setLayout(bodylayout)
|
||||
|
||||
def buyCallback(self):
|
||||
self.mp = MoviePurchase(self.movie)
|
||||
self.mp.show()
|
||||
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import sys
|
||||
from Movie import movies
|
||||
from MovieView import MovieView
|
||||
from MovieButton import MovieButton
|
||||
from config import cart
|
||||
|
||||
# Main Window
|
||||
@final
|
||||
@@ -56,9 +57,10 @@ class MainWindow(QMainWindow):
|
||||
def buyHairyPotty(self):
|
||||
self.currentMovie = MovieView(movies["hairypotty"])
|
||||
self.currentMovie.show()
|
||||
|
||||
|
||||
|
||||
app = QApplication(sys.argv)
|
||||
window = MainWindow()
|
||||
window.show()
|
||||
sys.exit(app.exec())
|
||||
app.exec()
|
||||
print(cart)
|
||||
|
||||
3
src/config.py
Normal file
3
src/config.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from Cart import Cart
|
||||
|
||||
cart = Cart()
|
||||
Reference in New Issue
Block a user