Skip to content

#11 - php #3681

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 134 additions & 0 deletions Roadmap/11 - MANEJO DE FICHEROS/php/gabrielmoris.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php
/*
* IMPORTANT: You should only upload the code file as part of the exercise.
*
* EXERCISE:
* Develop a program capable of creating a file named after
* your GitHub username and has the .txt extension.
* Add several lines to that file:
* - Your name.
* - Age.
* - Favorite programming language.
* Print the content.
* Delete the file.
*/


function file_creator($content, $name)
{
$checkedName = "";

if (substr($name, -4) !== '.txt') {
$checkedName = $name . ".txt";
} else {
$checkedName = $name;
}

file_put_contents($checkedName, $content, FILE_APPEND | LOCK_EX);
echo "File " . $checkedName . " created.\n";
}

function file_reader($name)
{
$checkedName = "";
if (substr($name, -4) !== '.txt') {
$checkedName = $name . ".txt";
} else {
$checkedName = $name;
}

$contents = file_get_contents($checkedName);
echo $contents . "\n";
}

function file_deleter($name)
{
$checkedName = "";
if (substr($name, -4) !== '.txt') {
$checkedName = $name . ".txt";
} else {
$checkedName = $name;
}

if (file_exists($checkedName)) {
if (unlink($checkedName)) {
echo "File " . $checkedName . " deleted.\n";
} else {
echo "File " . $checkedName . " couldn't be deleted.\n";
}
} else {
echo "File " . $checkedName . " doens't exist.\n";
}
}

$content = "Name: Gabriel Moris \nAge: 34\nFavourite programming language: English.";
$fileName = 'gabrielmoris.txt';
file_creator($content, $fileName);
file_reader($fileName);
file_deleter($fileName);


/* EXTRA DIFFICULTY (optional):
* Develop a sales management program that stores its data in a
* .txt file.
* - Each product is saved on a line of the file in the following way:
* [product_name], [quantity_sold], [price].
* - Following that format, and through terminal, it should allow adding, consulting,
* updating, deleting products and exiting.
* - It should also have options to calculate the total sale and by product.
* - The exit option deletes the .txt.
*/

function sales_management_system()
{
echo "
===== PRINTER =====
1.- New Document
2.- Read Document
3.- Delete Document
4.- Exit
==================
\n";

do {
$selection = readline("Select an option\n");
switch ($selection) {
case 1:
new_document();
break;
case 2:
read_document();
break;
case 3:
delete_document();
break;
case 4:
echo "\033c";
echo "Good bye 👋 🌐\n";
exit;
default:
echo "This option doesn't exist.\n";
}
} while (true);
}

function new_document()
{
$name = readline("Write the name of the document without extenstion (.txt only):\n");
$content = readline("Write the content of the document without extenstion (.txt only):\n");
file_creator($content, $name);
}

function read_document()
{
$name = readline("Write the name of the document without extenstion (.txt only):\n");
file_reader($name);
}

function delete_document()
{
$name = readline("Write the name of the document without extenstion (.txt only):\n");
file_deleter($name);
}

sales_management_system();