You are on page 1of 4

Вінницький Національний Технічний Університет

Факультет інформаційних технологій та комп’ютерної інженерії


Кафедра обчислювальної техніки

Лабораторна робота №1
з програмування
Тема: “Прості об’єкти та класи’’

Виконала
студентка гр. КІ-23мсз
Рудь О.К.
Перевірив
Кисюк Д.В.

Вінниця 2024
Варіант 7
7. Product: Найменування, Виробник, Ціна, Термін зберігання, Кількість.
Створити масив об'єктів.
Вивести:
а) список товарів для заданого найменування;
б) список товарів для заданого найменування, ціна яких не перевищує вказаної;
в) список товарів, термін зберігання яких більші за заданий.

Код програми:
#include <iostream>
#include <string>
#include <locale>
#include <windows.h>
using namespace std;

class Product {
private:
string name;
string manufacturer;
double price;
int storage_term;
int quantity;
public:
void set_name(string n) { name = n; }
void set_manufacturer(string m) { manufacturer = m; }
void set_price(double p) { price = p; }
void set_storage_term(int s) { storage_term = s; }
void set_quantity(int q) { quantity = q; }
string get_name() { return name; }
string get_manufacturer() { return manufacturer; }
double get_price() { return price; }
int get_storage_term() { return storage_term; }
int get_quantity() { return quantity; }
void show() {
cout << "Назва: " << name << endl;
cout << "Виробник: " << manufacturer << endl;
cout << "Ціна: " << price << endl;
cout << "Термін зберігання: " << storage_term << endl;
cout << "Кількість: " << quantity << endl;
}
};

int main() {
// Set the locale to Ukrainian
setlocale(LC_CTYPE, "ukr");
SetConsoleOutputCP(1251);
// Create an array of Product objects
Product products[3];

// Set the values of the first product


products[0].set_name("Яблуко");
products[0].set_manufacturer("Apple Inc.");
products[0].set_price(1.0);
products[0].set_storage_term(7);
products[0].set_quantity(10);
// Set the values of the second product
products[1].set_name("Банан");
products[1].set_manufacturer("Chiquita Brands International");
products[1].set_price(0.5);
products[1].set_storage_term(5);
products[1].set_quantity(20);

// Set the values of the third product


products[2].set_name("Апельсин");
products[2].set_manufacturer("Sunkist Growers, Inc.");
products[2].set_price(0.75);
products[2].set_storage_term(10);
products[2].set_quantity(15);

// Print the details of all products


for (int i = 0; i < 3; i++) {
cout << "Товар " << i + 1 << ":" << endl;
products[i].show();
cout << endl;
}

// Print the details of products with a given name


string search_name = "Банан";
cout << "Товари з назвою \"" << search_name << "\":" << endl;
for (int i = 0; i < 3; i++) {
if (products[i].get_name() == search_name) {
products[i].show();
cout << endl;
}
}

// Print the details of products with a given name and price less than or equal to a given value
string search_name2 = "Апельсин";
double max_price = 0.6;
cout << "Товари з назвою \"" << search_name2 << "\" та ціною не більше " << max_price << ":" << endl;
for (int i = 0; i < 3; i++) {
if (products[i].get_name() == search_name2 && products[i].get_price() <= max_price) {
products[i].show();
cout << endl;
}
}

// Print the details of products with a storage term greater than a given value
int min_storage_term = 8;
cout << "Товари з терміном зберігання більшим за " << min_storage_term << ":" << endl;
for (int i = 0; i < 3; i++) {
if (products[i].get_storage_term() > min_storage_term) {
products[i].show();
cout << endl;
}
}

return 0;
}

You might also like