papa
papaprogram ProductList;
type
Category = (Computers, Peripherals);
SubCategory = (None, Desktops, Laptops, Printers, Scanners);
Product = record
KOD: Integer;
NAME: string[10];
MARK: string[10];
COST: Integer;
end;
const
FILE_PATH = 'D:\txt\book.txt';
procedure PrintProductList(products: array of Product);
var
i: Integer;
begin
if Length(products) = 0 then
begin
WriteLn('Нет товаров в данной категории.');
Exit;
end;
WriteLn('Список товаров:');
for i := 0 to Length(products) - 1 do
begin
WriteLn(products[i].NAME, ' - ', products[i].COST, ' руб.');
end;
end;
function GetProductsFromFile(filePath: string): array of Product;
var
fileText: TextFile;
line: string;
product: Product;
products: array of Product;
begin
Assign(fileText, filePath);
Reset(fileText);
SetLength(products, 0);
while not Eof(fileText) do
begin
ReadLn(fileText, line);
if Length(line) > 0 then
begin
with product do
begin
KOD := StrToInt(Trim(Copy(line, 1, 2)));
NAME := Trim(Copy(line, 4, 10));
MARK := Trim(Copy(line, 16, 5));
COST := StrToInt(Trim(Copy(line, 22)));
end;
SetLength(products, Length(products) + 1);
products[High(products)] := product;
end;
end;
Close(fileText);
Result := products;
end;
function FilterProductsByCategory(products: array of Product; category: Category): array of Product;
var
i: Integer;
filteredProducts: array of Product;
begin
SetLength(filteredProducts, 0);
for i := 0 to Length(products) - 1 do
begin
if (category = Computers) and (products[i].KOD = 1) then
begin
SetLength(filteredProducts, Length(filteredProducts) + 1);
filteredProducts[High(filteredProducts)] := products[i];
end
else if (category = Peripherals) and (products[i].KOD = 2) then
begin
SetLength(filteredProducts, Length(filteredProducts) + 1);
filteredProducts[High(filteredProducts)] := products[i];
end;
end;
Result := filteredProducts;
end;
function FilterProductsBySubCategory(products: array of Product; subCategory: SubCategory): array of Product;
var
i: Integer;
filteredProducts: array of Product;
begin
SetLength(filteredProducts, 0);
for i := 0 to Length(products) - 1 do
begin
case subCategory of
None:
begin
SetLength(filteredProducts, Length(filteredProducts) + 1);
filteredProducts[High(filteredProducts)] := products[i];
end;
Desktops, Printers:
if (products[i].KOD = 1) then
begin
SetLength(filteredProducts, Length(filteredProducts) + 1);
filteredProducts[High(filteredProducts)] := products[i];
end;
Laptops, Scanners:
if (products[i].KOD = 2) then
begin
SetLength(filteredProducts, Length(filteredProducts) + 1);
filteredProducts[High(filteredProducts)] := products[i];
end;
end;
end;
Result := filteredProducts;
end;
procedure Main();
var
categoryChoice: Integer;
subCategoryChoice: Integer;
category: Category;
subCategory: SubCategory;
productList: array of Product;
begin
productList := GetProductsFromFile(FILE_PATH);
WriteLn('Выберите категорию:');
WriteLn('1) Компьютеры');
WriteLn('2) Периферия');
ReadLn(categoryChoice);
case categoryChoice of
1: category := Computers;
2: category := Peripherals;
else
begin
WriteLn('Неверный выбор категории.');
Exit;
end;
end;
if category = Computers then
begin
WriteLn('Выберите подкатегорию:');
WriteLn('1) Настольные компьютеры');
WriteLn('2) Ноутбуки');
ReadLn(subCategoryChoice);
case subCategoryChoice of
1: subCategory := Desktops;
2: subCategory := Laptops;
else
begin
WriteLn('Неверный выбор подкатегории.');
Exit;
end;
end;
end
else if category = Peripherals then
begin
WriteLn('Выберите подкатегорию:');
WriteLn('1) Принтеры');
WriteLn('2) Сканеры');
ReadLn(subCategoryChoice);
case subCategoryChoice of
1: subCategory := Printers;
2: subCategory := Scanners;
else
begin
WriteLn('Неверный выбор подкатегории.');
Exit;
end;
end;
end;
if subCategory = None then
productList := FilterProductsByCategory(productList, category)
else
productList := FilterProductsBySubCategory(productList, subCategory);
PrintProductList(productList);
end;
begin