|
| 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 | +} |
0 commit comments