Professional Documents
Culture Documents
ЛР №1
ЛР №1
Лабораторна робота №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];
// 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;
}