This repository was archived by the owner on Apr 17, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVideoRentalSystem.cpp
More file actions
227 lines (185 loc) · 10 KB
/
VideoRentalSystem.cpp
File metadata and controls
227 lines (185 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include "VideoRentalSystem.h"
#include <QPushButton>
#include <QLabel>
#include <QVBoxLayout>
#include <QString>
#include <QFile>
#include <QApplication>
#include <QDebug>
VideoRentalSystem::VideoRentalSystem(QWidget *parent) : QMainWindow(parent) {
// Initialize the main widget and layout
QWidget *mainWidget = new QWidget(this);
layout = new QVBoxLayout(mainWidget);
initializeRentalsFile();
// Create labels and buttons
title = new QLabel("Rent-A-Video");
returnNum = new QLabel("53");
upcomingReturnsLabel = new QLabel("Upcoming returns");
inventoryNum = new QLabel("66");
videosInventoryLabel = new QLabel("Videos in inventory");
rentVideoButton = new QPushButton("Rent Video");
returnVideoButton = new QPushButton("Rental Return");
manageUsersButton = new QPushButton("Manage Users");
manageInventoryButton = new QPushButton("Manage Inventory");
rentVideoButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
returnVideoButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
manageUsersButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
manageInventoryButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
title->setStyleSheet("QLabel {width: 596px; height: 90px; color: #1C0F13; font-family: Garamond; font-size: 80px; font-style: normal; font-weight: 600; line-height: normal;}");
returnNum->setStyleSheet("QLabel {width: 596px; height: 90px; color: #14293E; font-family: Inter; font-size: 180px; font-style: normal; font-weight: 600; line-height: normal;}");
upcomingReturnsLabel->setStyleSheet("QLabel {width: 596px; height: 90px; color: #14293E; font-family: Inter; font-size: 65px; font-style: normal; font-weight: 600; line-height: normal;}");
inventoryNum->setStyleSheet("QLabel {width: 596px; height: 90px; color: #14293E; font-family: Inter; font-size: 180px; font-style: normal; font-weight: 600; line-height: normal;}");
videosInventoryLabel->setStyleSheet("QLabel {width: 596px; height: 90px; color: #14293E; font-family: Inter; font-size: 65px; font-style: normal; font-weight: 600; line-height: normal;}");
rentVideoButton->setStyleSheet("QPushButton {width: 550px; min-width: 550px; height: 115px; min-height: 115px; border-radius: 57px; background: #81B2D9; color: #F5F5F5; font-family: Inter; font-size: 55px; font-style: normal; font-weight: 600; line-height: normal;}""QPushButton:hover {background: #629FD0;}");
returnVideoButton->setStyleSheet("QPushButton {width: 550px; min-width: 550px; height: 115px; min-height: 115px; border-radius: 57px; background: #81B2D9; color: #F5F5F5; font-family: Inter; font-size: 55px; font-style: normal; font-weight: 600; line-height: normal;}""QPushButton:hover {background: #629FD0;}");
manageUsersButton->setStyleSheet("QPushButton {width: 550px; min-width: 550px; height: 115px; min-height: 115px; border-radius: 57px; background: #81B2D9; color: #F5F5F5; font-family: Inter; font-size: 55px; font-style: normal; font-weight: 600; line-height: normal;}""QPushButton:hover {background: #629FD0;}");
manageInventoryButton->setStyleSheet("QPushButton {width: 550px; min-width: 550px; height: 115px; min-height: 115px; border-radius: 57px; background: #81B2D9; color: #F5F5F5; font-family: Inter; font-size: 55px; font-style: normal; font-weight: 600; line-height: normal;}""QPushButton:hover {background: #629FD0;}");
// Create layouts for buttons
QHBoxLayout *leftButtonLayout = new QHBoxLayout;
QHBoxLayout *rightButtonLayout = new QHBoxLayout;
QHBoxLayout *topButtonLayout = new QHBoxLayout;
//QHBoxLayout *titleLayout = new QHBoxLayout;
// Add buttons to layouts
leftButtonLayout->addWidget(rentVideoButton);
leftButtonLayout->addWidget(manageUsersButton);
rightButtonLayout->addWidget(returnVideoButton);
rightButtonLayout->addWidget(manageInventoryButton);
// Create and add spacers for top layout
QSpacerItem *spacerLeft = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Fixed);
QSpacerItem *spacerRight = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Fixed);
topButtonLayout->addItem(spacerLeft);
topButtonLayout->addItem(spacerRight);
topButtonLayout->setContentsMargins(0, 0, 0, 0);
// Add layouts to main layout
// layout->setSpacing(15);
layout->addWidget(title);
layout->addWidget(returnNum);
layout->addWidget(upcomingReturnsLabel);
layout->addWidget(inventoryNum);
layout->addWidget(videosInventoryLabel);
layout->addLayout(topButtonLayout);
layout->addLayout(leftButtonLayout);
layout->addLayout(rightButtonLayout);
// Set the main widget and layout
mainWidget->setLayout(layout);
setCentralWidget(mainWidget);
// Set the window title and size
setWindowTitle("Rent-A-Video");
resize(1920, 1080);
setStyleSheet("QMainWindow { background-color: #E2E2E2; }");
connect(manageInventoryButton, &QPushButton::clicked, this, &VideoRentalSystem::onManageInventoryClicked);
// Connect signals and slots
connect(rentVideoButton, &QPushButton::clicked, this, &VideoRentalSystem::onRentVideoClicked);
connect(returnVideoButton, &QPushButton::clicked, this, &VideoRentalSystem::onReturnVideoClicked);
connect(manageUsersButton, &QPushButton::clicked, this, &VideoRentalSystem::onManageUsersClicked);
connect(manageInventoryButton, &QPushButton::clicked, this, &VideoRentalSystem::onManageInventoryClicked);
userManagementDialog = nullptr;
}
void VideoRentalSystem::onRentVideoClicked() {
RentalManagementDialog *rentalDialog = new RentalManagementDialog(this);
connect(rentalDialog, &RentalManagementDialog::dataChanged, this, &VideoRentalSystem::updateDataDisplay);
rentalDialog->setAttribute(Qt::WA_DeleteOnClose);
rentalDialog->show();
}
void VideoRentalSystem::onReturnVideoClicked() {
ReturnVideoDialog *dialog = new ReturnVideoDialog(this);
connect(dialog, &ReturnVideoDialog::dataChanged, this, &VideoRentalSystem::updateDataDisplay);
dialog->setAttribute(Qt::WA_DeleteOnClose); // Ensure dialog is deleted after closing
dialog->show();
}
void VideoRentalSystem::onManageUsersClicked() {
if (userManagementDialog == nullptr) {
userManagementDialog = new UserManagementDialog(this);
userManagementDialog->setAttribute(Qt::WA_DeleteOnClose);
// Connect the finished signal to a new slot to handle dialog closure
connect(userManagementDialog, &UserManagementDialog::finished,
this, &VideoRentalSystem::onUserManagementDialogClosed);
}
userManagementDialog->show();
userManagementDialog->raise();
userManagementDialog->activateWindow();
}
void VideoRentalSystem::onUserManagementDialogClosed() {
userManagementDialog = nullptr; // Reset the pointer to avoid dangling reference
}
void VideoRentalSystem::onManageInventoryClicked() {
static InventoryManagementDialog *inventoryDialog = nullptr;
if (!inventoryDialog) {
inventoryDialog = new InventoryManagementDialog(this);
inventoryDialog->setAttribute(Qt::WA_DeleteOnClose);
// Ensure this connection is made
connect(inventoryDialog, &InventoryManagementDialog::dataChanged, this, &VideoRentalSystem::updateDataDisplay);
connect(inventoryDialog, &QObject::destroyed, []() {
inventoryDialog = nullptr;
});
inventoryDialog->show();
} else {
inventoryDialog->raise();
inventoryDialog->activateWindow();
}
}
void VideoRentalSystem::initializeRentalsFile() {
QString rentalsFilePath = "rentals.csv";
QFile rentalsFile(rentalsFilePath);
// Check if the file already exists
if (!rentalsFile.exists()) {
// If not, create the file and add a header
if (!rentalsFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
QMessageBox::critical(this, "Error", "Unable to create the rentals file: " + rentalsFile.errorString());
return;
}
QTextStream out(&rentalsFile);
out << "Rental ID,Customer ID,Movie ID,Rental Date,Return Date,Status\n"; // Example header
rentalsFile.close();
}
}
int VideoRentalSystem::calculateUpcomingReturns() {
int upcomingReturns = 0;
QFile rentalFile("rentals.csv");
if (rentalFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&rentalFile);
QString line = in.readLine(); // Skip the header line
while (!in.atEnd()) {
line = in.readLine();
QStringList fields = line.split(',');
// Check if we have enough fields to access index 5
if (fields.size() > 5 && fields[5] != "Returned") {
upcomingReturns++;
}
}
rentalFile.close();
} else {
QMessageBox::warning(this, "Warning", "Cannot open rentals file.");
}
return upcomingReturns;
}
int VideoRentalSystem::calculateInventoryTotal() {
int inventoryTotal = 0;
QFile inventoryFile("inventory.csv");
if (inventoryFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&inventoryFile);
QString line = in.readLine(); // Skip the header line
while (!in.atEnd()) {
line = in.readLine();
QStringList fields = line.split(',');
// Check if we have enough fields to access index 3
if (fields.size() > 3) {
inventoryTotal += fields[3].toInt(); // Assuming that the quantity is at index 3
}
}
inventoryFile.close();
} else {
QMessageBox::warning(this, "Warning", "Cannot open inventory file.");
}
return inventoryTotal;
}
void VideoRentalSystem::showEvent(QShowEvent *event) {
QMainWindow::showEvent(event);
// Update the labels with the latest data
returnNum->setText(QString::number(calculateUpcomingReturns()));
inventoryNum->setText(QString::number(calculateInventoryTotal()));
}
void VideoRentalSystem::updateDataDisplay() {
returnNum->setText(QString::number(calculateUpcomingReturns()));
inventoryNum->setText(QString::number(calculateInventoryTotal()));
}