This repository has been archived on 2024-10-24. You can view files and clone it, but cannot push or open issues or pull requests.
NG_2024_Stanislav_Mykhailenko/Lesson_5/Task_1/autoservice.cpp

89 lines
3.8 KiB
C++
Raw Normal View History

2024-07-20 11:48:29 +00:00
#include "autoservice.h"
#include "ui_autoservice.h"
AutoService::AutoService(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::AutoService)
{
ui->setupUi(this);
connect(ui->b_filter, &QPushButton::clicked, this, &AutoService::filter);
connect(ui->l_automobiles, &QListWidget::itemClicked, this, &AutoService::select);
2024-07-22 11:42:11 +00:00
generateAutomobiles();
2024-07-20 11:48:29 +00:00
}
AutoService::~AutoService()
{
delete ui;
}
void AutoService::filter()
{
ui->l_automobiles->clear();
ui->e_selected->clear();
2024-07-22 11:42:11 +00:00
bool manufacturerEmpty = ui->e_manufacturer->text().isEmpty();
bool typeEmpty = ui->e_type->text().isEmpty();
bool priceEmpty = ui->e_price->text().isEmpty();
2024-07-20 11:48:29 +00:00
for (int automobile = 0; automobile < m_automobiles.size(); ++automobile) {
2024-07-22 15:45:54 +00:00
bool manufacturerMatch = m_automobiles[automobile]->getManufacturer() == ui->e_manufacturer->text();
bool typeMatch = m_automobiles[automobile]->getType().contains(ui->e_type->text());
bool priceMatch = m_automobiles[automobile]->getPrice() <= ui->e_price->text().toInt();
2024-07-20 11:48:29 +00:00
if (
2024-07-22 15:45:54 +00:00
(manufacturerEmpty || manufacturerMatch)
&& (typeEmpty || typeMatch)
&& (priceEmpty || priceMatch)
2024-07-20 11:48:29 +00:00
)
addAutomobile(m_automobiles[automobile]);
}
}
void AutoService::addAutomobile(ChildAutomobile * automobile)
{
ui->l_automobiles->addItem(automobile->getManufacturer() + " " + automobile->getModel());
}
2024-07-22 11:42:11 +00:00
void AutoService::generateAutomobiles()
{
QStringList manufacturers = {"Audi", "Bentley", "Ford", "Honda", "Hyundai", "JAC", "Jeep", "Rolls-Royce", "Toyota", "Volkswagen"};
QStringList models = {"A3", "Accord", "Atlas", "Bronco", "Cherokee", "Colorado Crew Cab", "Continental GT", "Santa Cruz", "Shuailing T6", "Wraith"};
QStringList types = {"Coupé", "Pickup truck"};
for (int automobile = 0; automobile < 10; ++automobile) {
ChildAutomobile* newAutomobile = new ChildAutomobile();
QString manufacturer = manufacturers[QRandomGenerator::global()->bounded(manufacturers.size())];
newAutomobile->setManufacturer(manufacturer);
int modelNumber = QRandomGenerator::global()->bounded(models.size());
QString model = models[modelNumber];
newAutomobile->setModel(model);
models.removeAt(modelNumber);
QString type = types[QRandomGenerator::global()->bounded(types.size())];
newAutomobile->setType(type);
int price = QRandomGenerator::global()->bounded(10000, 500000);
newAutomobile->setPrice(price);
int seats = QRandomGenerator::global()->bounded(5, 10);
newAutomobile->setSeats(seats);
int colorR = QRandomGenerator::global()->bounded(0, 256);
int colorG = QRandomGenerator::global()->bounded(0, 256);
int colorB = QRandomGenerator::global()->bounded(0, 256);
newAutomobile->setColor(QColor(colorR, colorG, colorB));
m_automobiles.append(newAutomobile);
}
}
2024-07-20 11:48:29 +00:00
void AutoService::select(QListWidgetItem * item)
{
for (int automobile = 0; automobile < m_automobiles.size(); ++automobile) {
if (item->text() == (m_automobiles[automobile]->getManufacturer() + " " + m_automobiles[automobile]->getModel())) {
ui->e_selected->setText("Manufacturer: " + m_automobiles[automobile]->getManufacturer() + "\n"
+ "Model: " + m_automobiles[automobile]->getModel() + "\n"
+ "Type: " + m_automobiles[automobile]->getType() + "\n"
+ "Seats: " + QString::number(m_automobiles[automobile]->getSeats()) + "\n"
+ "Color: " + m_automobiles[automobile]->getColor().name() + "\n"
+ "Price: " + QString::number(m_automobiles[automobile]->getPrice()));
break;
}
}
}