-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddtostaff.cpp
More file actions
124 lines (104 loc) · 3.84 KB
/
addtostaff.cpp
File metadata and controls
124 lines (104 loc) · 3.84 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
#include "addtostaff.h"
#include "ui_addtostaff.h"
// Constructor
AddToStaff::AddToStaff(QWidget *parent)
: QDialog(parent) // Initialize QDialog with parent widget
, ui(new Ui::AddToStaff) // Initialize UI
{
ui->setupUi(this); // Set up the user interface for this dialog
database = QSqlDatabase::database("DB"); // Get the database instance named "DB"
qDebug() << "Database initialized successfully";
}
// Destructor
AddToStaff::~AddToStaff()
{
delete ui; // Delete the UI
}
// Function to validate user input
bool AddToStaff::validateUserInput() {
qDebug() << "Validating user input";
// Get the name, phone, and wage text from the UI
QString name = ui->txtName->text();
QString phone = ui->txtPhone->text();
QString wageText = ui->txtWage->text();
// Check if any of the fields are empty
if (name.isEmpty() || phone.isEmpty() || wageText.isEmpty()) {
QMessageBox::warning(this, "Input Error", "Name, Phone, and Wage should not be empty.");
qDebug() << "Name, Phone, or Wage is empty";
return false;
}
// Check if the phone number starts with 0 and has a length of 10
if (!phone.startsWith('0') || phone.length() != 10) {
QMessageBox::warning(this, "Input Error", "Phone should start with 0 and have a length of 10.");
qDebug() << "Phone number is not valid";
return false;
}
// Check if the phone number contains only numeric characters
for (QChar c : phone) {
if (!c.isDigit()) {
QMessageBox::warning(this, "Input Error", "Phone should contain only numeric characters.");
qDebug() << "Phone number contains non-numeric characters";
return false;
}
}
// Check if the wage is a valid number
bool ok;
double wage = wageText.toDouble(&ok);
if (!ok || wage < 0) {
QMessageBox::warning(this, "Input Error", "Wage should be a valid positive number.");
qDebug() << "Wage is not a valid number";
return false;
}
// Check if the name contains only alphabetic characters
for (QChar c : name) {
if (!c.isLetter()) {
QMessageBox::warning(this, "Input Error", "Name should contain only alphabetic characters.");
qDebug() << "Name contains non-alphabetic characters";
return false;
}
}
qDebug() << "User input validated successfully";
return true;
}
// Function to reset the input fields
void AddToStaff::reset() {
ui->txtName->clear(); // Clear the name field
ui->txtPhone->clear(); // Clear the phone field
ui->txtWage->clear(); // Clear the wage field
qDebug() << "Input fields reset";
}
// Function to handle the add button click event
void AddToStaff::on_addBtn_clicked()
{
qDebug() << "Add button clicked";
// Validate the user input
if (!validateUserInput()) {
return;
}
// Get the name, phone, and wage from the UI
QString name = ui->txtName->text();
QString phone = ui->txtPhone->text();
int wage = ui->txtWage->text().toInt();
QSqlQuery query(database); // Create a query
// Prepare the query
query.prepare("INSERT INTO Staff (Name, Phone, Wage) VALUES (:Name, :Phone, :Wage)");
// Bind the values to the query
query.bindValue(":Name", name);
query.bindValue(":Phone", phone);
query.bindValue(":Wage", wage);
// Execute the query
if (!query.exec()) {
QMessageBox::critical(this, "Database Error", query.lastError().text());
qDebug() << "Failed to insert staff into database";
return;
}
emit staffAdded(); // Emit the staffAdded signal
QMessageBox::information(this, "Success", "Staff information added successfully.");
qDebug() << "Staff information added successfully";
reset(); // Reset the input fields
close();
}
void AddToStaff::on_cancelBtn_clicked()
{
close();
}