Skip to content

Commit e210c6f

Browse files
authored
Merge pull request mouredev#3681 from gabrielmoris/11-php
#11 - php
2 parents 994a1b2 + b14e1dc commit e210c6f

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
/*
3+
* IMPORTANT: You should only upload the code file as part of the exercise.
4+
*
5+
* EXERCISE:
6+
* Develop a program capable of creating a file named after
7+
* your GitHub username and has the .txt extension.
8+
* Add several lines to that file:
9+
* - Your name.
10+
* - Age.
11+
* - Favorite programming language.
12+
* Print the content.
13+
* Delete the file.
14+
*/
15+
16+
17+
function file_creator($content, $name)
18+
{
19+
$checkedName = "";
20+
21+
if (substr($name, -4) !== '.txt') {
22+
$checkedName = $name . ".txt";
23+
} else {
24+
$checkedName = $name;
25+
}
26+
27+
file_put_contents($checkedName, $content, FILE_APPEND | LOCK_EX);
28+
echo "File " . $checkedName . " created.\n";
29+
}
30+
31+
function file_reader($name)
32+
{
33+
$checkedName = "";
34+
if (substr($name, -4) !== '.txt') {
35+
$checkedName = $name . ".txt";
36+
} else {
37+
$checkedName = $name;
38+
}
39+
40+
$contents = file_get_contents($checkedName);
41+
echo $contents . "\n";
42+
}
43+
44+
function file_deleter($name)
45+
{
46+
$checkedName = "";
47+
if (substr($name, -4) !== '.txt') {
48+
$checkedName = $name . ".txt";
49+
} else {
50+
$checkedName = $name;
51+
}
52+
53+
if (file_exists($checkedName)) {
54+
if (unlink($checkedName)) {
55+
echo "File " . $checkedName . " deleted.\n";
56+
} else {
57+
echo "File " . $checkedName . " couldn't be deleted.\n";
58+
}
59+
} else {
60+
echo "File " . $checkedName . " doens't exist.\n";
61+
}
62+
}
63+
64+
$content = "Name: Gabriel Moris \nAge: 34\nFavourite programming language: English.";
65+
$fileName = 'gabrielmoris.txt';
66+
file_creator($content, $fileName);
67+
file_reader($fileName);
68+
file_deleter($fileName);
69+
70+
71+
/* EXTRA DIFFICULTY (optional):
72+
* Develop a sales management program that stores its data in a
73+
* .txt file.
74+
* - Each product is saved on a line of the file in the following way:
75+
* [product_name], [quantity_sold], [price].
76+
* - Following that format, and through terminal, it should allow adding, consulting,
77+
* updating, deleting products and exiting.
78+
* - It should also have options to calculate the total sale and by product.
79+
* - The exit option deletes the .txt.
80+
*/
81+
82+
function sales_management_system()
83+
{
84+
echo "
85+
===== PRINTER =====
86+
1.- New Document
87+
2.- Read Document
88+
3.- Delete Document
89+
4.- Exit
90+
==================
91+
\n";
92+
93+
do {
94+
$selection = readline("Select an option\n");
95+
switch ($selection) {
96+
case 1:
97+
new_document();
98+
break;
99+
case 2:
100+
read_document();
101+
break;
102+
case 3:
103+
delete_document();
104+
break;
105+
case 4:
106+
echo "\033c";
107+
echo "Good bye 👋 🌐\n";
108+
exit;
109+
default:
110+
echo "This option doesn't exist.\n";
111+
}
112+
} while (true);
113+
}
114+
115+
function new_document()
116+
{
117+
$name = readline("Write the name of the document without extenstion (.txt only):\n");
118+
$content = readline("Write the content of the document without extenstion (.txt only):\n");
119+
file_creator($content, $name);
120+
}
121+
122+
function read_document()
123+
{
124+
$name = readline("Write the name of the document without extenstion (.txt only):\n");
125+
file_reader($name);
126+
}
127+
128+
function delete_document()
129+
{
130+
$name = readline("Write the name of the document without extenstion (.txt only):\n");
131+
file_deleter($name);
132+
}
133+
134+
sales_management_system();

0 commit comments

Comments
 (0)