Skip to content

Commit 44a1876

Browse files
committedSep 30, 2023
First commit
0 parents  commit 44a1876

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed
 

‎Product.php

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
class Product
4+
{
5+
private string $file_name;
6+
7+
public function __construct($file_name)
8+
{
9+
$this->file_name = $file_name;
10+
}
11+
12+
public function setFileName($file_name)
13+
{
14+
$this->file_name = $file_name;
15+
}
16+
17+
public function getFileName()
18+
{
19+
return $this->file_name;
20+
}
21+
22+
public function add($product, $price)
23+
{
24+
$data = "$product - $price";
25+
26+
$file = fopen($this->file_name, 'a');
27+
28+
if (filesize($this->file_name) !== 0) {
29+
fwrite($file, " $data");
30+
} else {
31+
fwrite($file, $data);
32+
}
33+
34+
fclose($file);
35+
}
36+
37+
public function updateName($old_name, $new_name)
38+
{
39+
if (!file_exists($this->file_name)) {
40+
echo 'File not found. Please create your file and try again!';
41+
return;
42+
}
43+
44+
$file = fopen($this->file_name, 'r');
45+
46+
if (filesize($this->file_name) !== 0) {
47+
$file_data = fread($file, filesize($this->file_name));
48+
49+
$pos = strpos($file_data, $old_name);
50+
51+
if ($pos !== false) {
52+
for ($i = $pos; $i < strlen($file_data); $i++) {
53+
if ($file_data[$i] == ' ' && $file_data[$i + 1] == '-') {
54+
$new_data = substr($file_data, 0, $pos - 1) . " $new_name " . substr($file_data, $i + 1, strlen($file_data) - $i);
55+
56+
$file = fopen($this->file_name, 'w');
57+
fwrite($file, $new_data);
58+
fclose($file);
59+
60+
return;
61+
}
62+
}
63+
} else {
64+
echo 'Product not found!';
65+
}
66+
} else {
67+
echo 'Product not found!';
68+
}
69+
70+
fclose($file);
71+
}
72+
73+
public function remove($product)
74+
{
75+
if (!file_exists($this->file_name)) {
76+
echo "This file '$this->file_name' not found!";
77+
return;
78+
}
79+
80+
$file = fopen($this->file_name, 'r');
81+
82+
if (filesize($this->file_name) !== 0) {
83+
$file_data = fread($file, filesize($this->file_name));
84+
85+
$pos = strpos($file_data, $product);
86+
87+
if ($pos !== false) {
88+
for ($i = $pos; $i < strlen($file_data); $i++) {
89+
if ($file_data[$i] >= '0' && $file_data[$i] <= '9' && ($file_data[$i + 1] == ' ' || $i === strlen($file_data) - 1)) {
90+
$new_data = substr($file_data, 0, $pos - 1) . substr($file_data, $i + 1, strlen($file_data) - $i);
91+
92+
$file = fopen($this->file_name, 'w');
93+
fwrite($file, $new_data);
94+
fclose($file);
95+
96+
return;
97+
}
98+
}
99+
} else {
100+
echo 'Product not found!';
101+
}
102+
} else {
103+
echo 'Product not found!';
104+
}
105+
106+
fclose($file);
107+
}
108+
}

‎file.txt

Whitespace-only changes.

‎index.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
include 'Product.php';
4+
5+
// Написать консольную команду, которая получает два аргумента — «Имя файла», «Действие». Файл, который нужно открыть, содержит строки, имеющие формат: «Наименование» — «Цена» Например: Огурцы — 50 Помидоры — 40 Масло — 40 Реализовать следующие действия: Добавить в список Изменить запись в списке Удалить из списка Вычесть общую сумму В решении желательно использовать ООП подход.
6+
7+
$filename = readline('Enter file name: ');
8+
$action = readline("Actions (1: Add, 2: Update, 3: Delete): ");
9+
10+
$product = new Product($filename);
11+
12+
if ($action == 1) {
13+
$product_name = readline('Enter product name: ');
14+
$product_price = readline('Enter product price: ');
15+
$product->add($product_name, $product_price);
16+
} else if ($action == 2) {
17+
$old_name = readline('Enter old product name: ');
18+
$new_name = readline('Enter new product name: ');
19+
$product->updateName($old_name, $new_name);
20+
} else if ($action == 3) {
21+
$product_name = readline('Enter product name: ');
22+
$product->remove($product_name);
23+
}

0 commit comments

Comments
 (0)
Please sign in to comment.