AIwinter

🦴🦴

Nov 16th, 2022
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 11.00 KB | None | 0 0
  1. Uses Crt;
  2.  
  3. type
  4.   Items = Record
  5.     Id: word;
  6.     Name: string[20];
  7.     Manufacturer: string[20];
  8.     Size: byte;
  9.     Date: string[8];
  10.     Price: longword;
  11.   end;
  12.  
  13.   Orders = Record
  14.     Id: word;
  15.     Item: word;
  16.     Name: string[20];
  17.     Adress: string[20];
  18.     Count: byte;
  19.     Date: string[8];
  20.     IsPaid: boolean;
  21.   end;
  22.  
  23. var
  24.   Fv1: file of Items;
  25.   ItemsTable: Items;
  26.   ItemsEmpty: Items;
  27.  
  28.   Fv2: file of Orders;
  29.   OrdersTable: Orders;
  30.  
  31.   temp: longword;
  32.  
  33.   ItemsToDelete: array[0..3] of word := (word.MaxValue,
  34.   word.MaxValue, word.MaxValue, word.MaxValue);
  35.  
  36. const
  37.   ItemsName = 'Items.rost';
  38.   ItemsBackup = 'ItemsBackUp.rost';
  39.   OrdersName = 'Orders.rost';
  40.   YesChar = 'yYуУ';
  41.   NoChar = 'nNиИ';
  42.  
  43. procedure AddItem;
  44. var
  45.   input: string;
  46.  
  47. begin
  48.  
  49.   if (FileSize(Fv1) > 0) then
  50.  
  51.   begin
  52.     Seek(Fv1, FileSize(Fv1) - 1);
  53.     Read(Fv1, ItemsTable);
  54.     ItemsTable.Id := ItemsTable.Id + 1;
  55.   end
  56.  
  57.   else ItemsTable.Id := 0;
  58.  
  59.   ItemsTable.Size := 0;
  60.  
  61.   repeat
  62.     Write('Имя товара: ');
  63.     Readln(input);
  64.   until (input.Length <= 20) and (input <> ''.ToString());
  65.   ItemsTable.Name := input;
  66.  
  67.   repeat
  68.     Write('Производитель: ');
  69.     Readln(input);
  70.   until (input.Length <= 20) and (input <> ''.ToString());
  71.   ItemsTable.Manufacturer := input;
  72.  
  73.   Write('Есть ли размер S (y or n): ');
  74.   Readln(input);
  75.   if YesChar.Contains(input) then ItemsTable.Size += 1;
  76.   Write('Есть ли размер M (y or n): ');
  77.   Readln(input);
  78.   if YesChar.Contains(input) then ItemsTable.Size += 2;
  79.   Write('Есть ли размер L (y or n): ');
  80.   Readln(input);
  81.   if YesChar.Contains(input) then ItemsTable.Size += 4;
  82.   Write('Есть ли размер XL (y or n): ');
  83.   Readln(input);
  84.   if YesChar.Contains(input) then ItemsTable.Size += 8;
  85.  
  86.   repeat
  87.     Write('Дата: ');
  88.     Readln(input);
  89.     input.Replace('.', '')
  90.   until (input.Length = 8) and longword.TryParse(input, temp);
  91.  
  92.   ItemsTable.Date := input;
  93.  
  94.   repeat
  95.     Write('Стоимость: ');
  96.     Readln(input);
  97.   until longword.TryParse(input, ItemsTable.Price);
  98.  
  99.   Write(Fv1, ItemsTable);
  100. end;
  101.  
  102.  
  103.  
  104. procedure Show(Table: Items);
  105. begin
  106.   if Table.Name = '' then
  107.   begin
  108.     exit;
  109.   end;
  110.   var size: string = ((Table.Size and 8) = 8 ? ' XL;' : '') + ((Table.Size and 4) = 4 ? ' L;' : '') + ((Table.Size and 2) = 2 ? ' M;' : '') + ((Table.Size and 1) = 1 ? ' S;' : '');
  111.   WriteLn('Id:' + Table.Id.ToString() + ' Name:' + Table.Name + ' Manuf:' + Table.Manufacturer +
  112.     ' Size:' + size + ' Price:' + Table.Price.ToString() + ' Date:' + Table.Date.ToString() + 'To Delete: ' + ItemsToDelete.Contains(Table.Id).ToString);
  113. end;
  114.  
  115. procedure ListItems;
  116. begin
  117.   Seek(Fv1, 0);
  118.   while not (EOF(Fv1)) do
  119.   begin
  120.     Read(Fv1, ItemsTable);
  121.     Show(ItemsTable);
  122.   end;
  123. end;
  124.  
  125.  
  126.  
  127. function FindById(Id: word): Items;
  128. begin
  129.  
  130.   ItemsTable.Id := MaxLongWord;
  131.   Seek(Fv1, 0);
  132.   while (not EoF(Fv1)) and (ItemsTable.Id <> Id) do
  133.   begin
  134.     Read(Fv1, ItemsTable);
  135.   end;
  136.   if ItemsTable.Id = Id then
  137.   begin
  138.     Result := ItemsTable;
  139.   end
  140.   else
  141.   begin
  142.     WriteLn('Не нашелся объект с таким Id');
  143.     Result := ItemsEmpty;
  144.   end;
  145. end;
  146.  
  147. function FindById: Items;
  148. begin
  149.   var Id: word;
  150.   var input: string;
  151.   repeat
  152.     Write('Введите Id записи: ');
  153.     ReadLn(input);
  154.   until word.TryParse(input, Id);
  155.  
  156.   Result := FindById(Id);
  157. end;
  158.  
  159. procedure FindByName;
  160. var
  161.   name: string;
  162.   count: word = 0;
  163. begin
  164.   Write('Введите название товара: ');
  165.   ReadLn(name);
  166.   name := name.ToLowerInvariant;
  167.   Seek(Fv1, 0);
  168.   ItemsTable.Name := #13;
  169.   while (not EoF(Fv1)) do
  170.   begin
  171.     Read(Fv1, ItemsTable);
  172.    
  173.     if Pos(name, ItemsTable.Name.ToLowerInvariant) = 1 then
  174.     begin
  175.       show(ItemsTable);
  176.       count := count + 1;
  177.     end
  178.   end;
  179.   if count = 0 then WriteLn('Не нашелся объект с таким именем');
  180. end;
  181.  
  182. procedure AddOrder;
  183. var
  184.   input: string;
  185. begin
  186.   if (FileSize(Fv2) > 0) then
  187.  
  188.   begin
  189.     Seek(Fv2, FileSize(Fv2) - 1);
  190.     Read(Fv2, OrdersTable);
  191.     OrdersTable.Id := OrdersTable.Id + 1;
  192.   end
  193.   else OrdersTable.Id := 0;
  194.  
  195.   repeat
  196.     Write('Id товара: ');
  197.     Readln(input);
  198.   until (word.TryParse(input, OrdersTable.Item)) and (FindById(OrdersTable.Item) <> ItemsEmpty);
  199.  
  200.   repeat
  201.     Write('Имя: ');
  202.     Readln(input);
  203.   until (input.Length <= 20) and (input <> ''.ToString());
  204.   OrdersTable.Name := input;
  205.  
  206.   repeat
  207.     Write('Адрес: ');
  208.     Readln(input);
  209.   until (input.Length <= 20) and (input <> ''.ToString());
  210.   OrdersTable.Adress := input;
  211.  
  212.   repeat
  213.     Write('Количество: ');
  214.     Readln(input);
  215.   until byte.TryParse(input, OrdersTable.Count) and (OrdersTable.Count > 0);
  216.  
  217.   repeat
  218.     Write('Дата заказа: ');
  219.     Readln(input);
  220.     input.Replace('.', '')
  221.   until (input.Length = 8) and longword.TryParse(input, temp);
  222.   OrdersTable.Date := input;
  223.  
  224.   Write('Заказ оплачен?(y or n): ');
  225.   Readln(input);
  226.   OrdersTable.IsPaid := YesChar.Contains(input) ? True : False;
  227.  
  228.   Write(Fv2, OrdersTable);
  229. end;
  230.  
  231. procedure Show(Table: Orders);
  232. begin
  233.   if Table.Name = '' then
  234.   begin
  235.     exit;
  236.   end;
  237.   WriteLn('Id:' + Table.Id.ToString() + ' ItemId:' + Table.Item.ToString() + ' ItemsName:' + FindById(Table.Item).Name + ' Name:' + Table.Name + ' Adress:' + Table.Adress +
  238.     ' Count:' + Table.Count.ToString() + ' Date:' + Table.Date.ToString());
  239. end;
  240.  
  241. procedure ListOrders;
  242. begin
  243.   Seek(Fv2, 0);
  244.   while not (EOF(Fv2)) do
  245.   begin
  246.     Read(Fv2, OrdersTable);
  247.     Show(OrdersTable);
  248.   end;
  249. end;
  250.  
  251. procedure Delete;
  252. var
  253.   input: string;
  254. begin
  255.   ItemsTable := FindById;
  256.   Show(ItemsTable);
  257.   if ItemsTable.Id <> ItemsEmpty.Id then begin
  258.     if ItemsToDelete.Contains(ItemsTable.Id) then begin
  259.       Write('Вы хотите отменить удаление? (y or n): ');
  260.       ReadLn(input);
  261.       if YesChar.Contains(input) then begin
  262.         ItemsToDelete[ItemsToDelete.FindIndex(x -> x = ItemsTable.Id)] := word.MaxValue;
  263.       end;
  264.     end
  265.     else
  266.     begin
  267.       Write('Вы хотите удалить? (y or n): ');
  268.       ReadLn(input);
  269.       if YesChar.Contains(input) then begin
  270.         for var i := 0 to ItemsToDelete.Length - 1 do
  271.         begin
  272.           if ItemsToDelete[i] = word.MaxValue then begin
  273.             ItemsToDelete[i] := ItemsTable.Id;
  274.           end;
  275.         end;
  276.       end;
  277.     end;
  278.   end;
  279. end;
  280.  
  281.  
  282. procedure Zap;
  283. var
  284.   Fv3: file of Items;
  285. begin
  286.   Assign(Fv3, ItemsBackup);
  287.   Rewrite(Fv3);
  288.   Seek(Fv1, 0);
  289.   while not (EOF(Fv1)) do
  290.   begin
  291.     Read(Fv1, ItemsTable);
  292.     if not ItemsToDelete.Contains(ItemsTable.Id) then
  293.       Write(Fv3, ItemsTable);
  294.   end;
  295.   Close(Fv1);
  296.   Erase(Fv1);
  297.   Close(Fv3);
  298.   Rename(Fv3, ItemsName);
  299.   Reset(Fv1);
  300. end;
  301.  
  302. procedure Edit;
  303. type
  304.   Data = (Name, Manufacturer, Size, Date, Price, Сохранить);
  305. begin
  306.   var input: string;
  307.   var choice := Data.Name;
  308.   ItemsTable := FindById;
  309.   Show(ItemsTable);
  310.   var c: char;
  311.   while choice <> Data.Сохранить do
  312.   begin
  313.     ClearLine;
  314.     Console.SetCursorPosition(0, Console.CursorTop);
  315.     Write('Что хотите изменить?: ');
  316.     Write(choice.ToString);
  317.     c := Readkey();
  318.     if c = #0 then begin
  319.       c := ReadKey();
  320.       case c of
  321.         #39: choice := Data(Min(ord(choice) + 1, 5));
  322.         #37: choice := Data(Max(ord(choice) - 1, 0));
  323.       end;
  324.     end
  325.     else
  326.     begin
  327.       if c <> #13 then continue;
  328.       WriteLn('');
  329.       case choice of
  330.         Data.Name:
  331.           begin
  332.             repeat
  333.               Write('Имя товара: ');
  334.               Readln(input);
  335.             until (input.Length <= 20) and (input <> ''.ToString());
  336.             ItemsTable.Name := input;
  337.           end;
  338.         Data.Date:
  339.           begin
  340.             repeat
  341.               Write('Дата: ');
  342.               Readln(input);
  343.               input.Replace('.', '')
  344.             until (input.Length = 8) and longword.TryParse(input, temp);
  345.             ItemsTable.Date := input;
  346.           end;
  347.         Data.Size:
  348.           begin
  349.             ItemsTable.Size := 0;
  350.             Write('Есть ли размер S (y or n): ');
  351.             Readln(input);
  352.             if YesChar.Contains(input) then ItemsTable.Size += 1;
  353.             Write('Есть ли размер M (y or n): ');
  354.             Readln(input);
  355.             if YesChar.Contains(input) then ItemsTable.Size += 2;
  356.             Write('Есть ли размер L (y or n): ');
  357.             Readln(input);
  358.             if YesChar.Contains(input) then ItemsTable.Size += 4;
  359.             Write('Есть ли размер XL (y or n): ');
  360.             Readln(input);
  361.             if YesChar.Contains(input) then ItemsTable.Size += 8;
  362.           end;
  363.         Data.Price:
  364.           begin
  365.             repeat
  366.               Write('Стоимость: ');
  367.               Readln(input);
  368.             until longword.TryParse(input, ItemsTable.Price);
  369.           end;
  370.         Data.Manufacturer:
  371.           begin
  372.             repeat
  373.               Write('Производитель: ');
  374.               Readln(input);
  375.             until (input.Length <= 20) and (input <> ''.ToString());
  376.             ItemsTable.Manufacturer := input;
  377.           end;
  378.         Data.Сохранить:
  379.           begin
  380.             Seek(Fv1, ItemsTable.Id);
  381.             Write(Fv1, ItemsTable);
  382.             Show(ItemsTable);
  383.           end;
  384.      
  385.       end;
  386.     end;
  387.    
  388.   end;
  389. end;
  390.  
  391. procedure Test;
  392. begin
  393.   while True do
  394.   begin
  395.     WriteLn(ord(ReadKey()));
  396.   end;
  397. end;
  398.  
  399. procedure OpenDB;
  400. begin
  401.   Assign(Fv1, ItemsName);
  402.   Assign(Fv2, OrdersName);
  403.  
  404.   if not FileExists(ItemsName) then Rewrite(Fv1)
  405.   else ReSet(Fv1);
  406.  
  407.   if not FileExists(OrdersName) then Rewrite(Fv2)
  408.   else ReSet(Fv2);
  409.  
  410.   ItemsEmpty.Id := word.MaxValue;
  411. end;
  412.  
  413. procedure CloseDB;
  414. begin
  415.   Close(Fv1);
  416.   Close(Fv2);
  417. end;
  418.  
  419. procedure Menu;
  420. var
  421.   input: string;
  422. begin
  423.   WriteLn('Для списка команд введите help');
  424.   while true do
  425.   begin
  426.     write('Введите команду: ');
  427.     ReadLn(input);
  428.     case input of
  429.       'add items': AddItem;
  430.       'add orders': AddOrder;
  431.       'list items': ListItems;
  432.       'list orders': ListOrders;
  433.       'quit': exit;
  434.       'find id': Show(FindById);
  435.       'find name': FindByName;
  436.       'clear': clrscr;
  437.       'edit': edit;
  438.       {'delete': delete;}
  439.       'zap': zap;
  440.       'help':
  441.         WriteLn('add items/orders - добавить значение в таблицу Товары/Заказы'
  442.               + NewLine + 'list items/orders - вывести таблицу Товары/Заказы' + NewLine  + 'find name - поиск по имени' + NewLine
  443.               + 'find id - поиск по id' + NewLine + 'clear - отчистить консоль' + NewLine + 'quit - выйти');
  444.     end;
  445.   end;
  446. end;
  447.  
  448.  
  449.  
  450.  
  451. begin
  452.   OpenDB;
  453.   Menu;  
  454.   CloseDB;
  455. end.
Add Comment
Please, Sign In to add comment