Narzędzie konsolowe w PHP do robienia zakupów
|Narzędzie konsolowe w PHP do robienia zakupów
Bot PHP
Zastanawiałeś się może czy nie napisać jakiegoś prostego bota takie narzędzie konsolowe w PHP do robienia zakupów?
Narzędzie konsolowe w PHP
Z pewnością jak o takim czymś myślałeś to ten programik jest dla ciebie. Pokazuje jak można prostym nakładem sił stworzyć narzędzie konsolowe do obsługi np. koszyka zakupowego i to właśnie jest jego zadaniem.
Singleton i interface
Zademonstrowałem w nim także, jak można i nawet trzeba w tym przypadku zastosować singletona i ponadto, jak korzystać z interfejsów w konsoli powershell. Index.php sprowadza się do takiego menagera obsługującego poszczególne instancje i interfejsy. To w nim wszystko się dzieje i pobiera odpowiednie rzeczy do działania. Wywołanie instancji jako singletona jest tutaj konieczna ponieważ chcemy zapisywać stan naszego koszyka. Wywołanie instancji jako singletona jest tutaj konieczna ponieważ chcemy zapisywać stan naszego koszyka. Dobrą praktyką według mnie jest tutaj dołączenie samych funkcji w indeksie (menager) możemy wtedy w łatwy sposób rozbudowywać narzędzie o inne dodatkowe możliwości, a tym samym używać już istniejących rozwiązań. Przedstawiony sposób rozwiązuje kilka kluczowych kwestii w łączeniu klas interfejsów i funkcji razem jeden do jednego.
Nie będę się rozpisywał zobacz sam jakie to proste.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | declare(strict_types=1); use ConsoleShopPHP\ClassApp\Product; use ConsoleShopPHP\ClassApp\Items; use ConsoleShopPHP\ClassApp\Cash; use ConsoleShopPHP\appInterface\Shop\ShopInterface; use ConsoleShopPHP\appInterface\Exception\ProgramsException; use ConsoleShopPHP\appInterface\Items\ItemsInterface; use ConsoleShopPHP\appInterface\Cash\CashInterface; try{ echo "Enter the Product? Product <--> A 0.65 $, Product <--> B 1 $, Product <--> C 2,5 $, Product <--> D 5.75 $, Exit <--> E of e: \n"; echo "Input: "; $tabLineProduct = ["A"=>true, "B"=>true, "C"=>true, "D"=>true]; do{ $line = trim(handleLine()); // Wyjscie exitMachine($line); // Wszystkie symbole oprócz tablicy if(@is_null($tabLineProduct[$line])){ $tabLineProduct[$line] = false; // throw new ProgramsException("empty index"); } if($tabLineProduct[$line] === true){ $product = new Product($line); stringoTo("The cost of the product is: ", $product); textCash(); }else{ echo "There is no such product symbol ABORTING!\n"; echo "Enter the correct symbol A, B, C, D !\n"; echo "Enter the Product? Product <--> A 0.65 $, Product <--> B 1 $, Product <--> C 2,5 $, Product <--> D 5.75 $ Exit <--> E of e: \n"; } }while( $tabLineProduct[$line] !== true ); $cash = Cash::getInstance(); $items = Items::getInstance(); $tabLineMoney = ["P"=>true, "J"=>true, "T"=>true, "F"=>true, "D"=>true,"RM"=>false]; do{ echo "Input: "; $line = trim(handleLine()); // Wyjscie exitMachine($line); // Wszystkie symbole oprócz tablicy if(@is_null($tabLineMoney[$line])){ $tabLineMoney[$line] = -1; // throw new ProgramsException("empty index"); } // RETURN-MONEY RM usun monety if($tabLineMoney[$line] === false){ emptyCash($cash); emptyItems($items); echo "Coins cleared! \n"; echo "Enter the symbol correctly or remove the coins\n"; }else{ if($tabLineMoney[$line] === true){ $cash->addTypeCash($line); $items->addTypeItems($line); // Dodaje z automatu addCash($cash); echo sumCash($cash)." $ "; addItems($items); echo allItems($items)." \n"; if($product->typeProduct > sumCash($cash)){ echo "not enough! \n\n"; } }else{ echo "No such cash symbol ABORTING!\n"; textCash(); } } }while($product->typeProduct > sumCash($cash)); $c = (int)(sumCash($cash)*1000); $p = (int)($product->typeProduct*1000); echo 'The product purchased '.$product->typeProduct." $ and the return to the account is: ".(($c - $p)/1000)." $"; echo "\n"; echo "Thank you...\n\n\n\n\n"; }catch(ProgramsException $e){ echo $e->getMessage(); } function stringoTo($text, ShopInterface $value ):void{ print $text.$value." $"; } function addCash(CashInterface $cash):void{ $cash->add(); } function sumCash(CashInterface $cash):float{ return $cash->sum(); } function emptyCash(CashInterface $cash):void{ $cash->empty(); } function addItems(ItemsInterface $items){ $items->add(); } function allItems(ItemsInterface $items):string{ return $items->itemAll(); } function emptyItems(ItemsInterface $items){ $items->empty(); } function exitMachine($line):void { if($line == 'E' || $line == 'e'){ echo "ABORTING!\n\n\n\n\n"; exit; } } function textCash(){ echo " Enter the cash correctly or remove it money: 0,05 $ --> P, 0,1 $ --> J, 0,25 $ --> T, 0,5 $ --> F, 1,00 $ --> D RETURN-MONEY --> RM Empty money Cash. I'm waiting? ;) : \n\n"; } |
Co znajdziesz w poście:
A tutaj do pobrania git All: https://github.com/ber34/console-Shop-To-PHP