Untitled

 avatar
unknown
plain_text
6 months ago
7.5 kB
2
Indexable
<?php

namespace Classes;

class Book
{
    private int $id;
    private string $title;
    private float $price;
    private array $authors = [];

    function __construct(int $id, string $title, float $price, array $authors)
    {
        $this->id = $id;
        $this->title = $title;
        $this->price = $price;
        $this->authors = $authors;
    }

    public function getAuthors()
    {
        return $this->authors;
    }

    public function getPrice()
    {
        return $this->price;
    }

    public function getTitle()
    {
        return $this->title;
    }

    public function getId()
    {
        return $this->id;
    }

}

?>
<?php

session_start();

$server = "localhost";
$username = "root";
$password = "";
$dbname = "teszt1";

$conn = new mysqli($server, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} else {
    echo "Csatlakozas az adatbazishoz sikeres!";
}

if (isset($_POST["submit"])) {
    $title = $_POST["title"];
    $price = $_POST["price"];
    $authors = $_POST["authors"];

    $sql = "INSERT INTO books (title, price, authors) VALUES ('$title', '$price', '$authors')";

    if ($conn->query($sql) === TRUE) {
        echo "Sikeres hozzaadas az adatbazishoz!";
        $_SESSION["title"] = $title;
        $_SESSION["price"] = $price;
        $_SESSION["authors"] = $authors;
        header("Location: list.php");
        $conn->close();
    }

}

?>

<form action="book_form.php" method="POST">

    <label for="title">Title:</label>
    <input type="text" name="title" required></input>
    <label for="title">Price:</label>
    <input type="number" name="price" required></input>
    <label for="title">Authors:</label>
    <input type="text" name="authors" required></input>
    <input type="submit" name="submit"></input>

</form>
<?php

namespace Classes;

interface Borrowable
{
    public function borrowBook(Book $book);
    public function returnBook(Book $book);
}

?>
<?php

namespace Classes;

class Library
{
    private array $books = [];
    private array $users = [];

    public function getBooks()
    {
        return $this->books;
    }

    public function getUsers()
    {
        return $this->users;
    }

    public function addBook(Book $book)
    {
        $this->books[] = $book;
    }

    public function addUser(User $user)
    {
        $this->users[] = $user;
    }

    public function searchBooksByAuthor($author)
    {
        $sort = function($book) use ($author)
        {
            $authors = $book->getAuthors();
            return in_array($author, $authors);
        };
    
        return array_filter($this->books, $sort);
    }

    public function listBooksAsJson()
    {
        $list = [];
        foreach ($this->books as $book) {
            $list[] = [
                "id" => $book->getId(),
                "title" => $book->getTitle(),
                "price" => $book->getPrice(),
                "authors" => $book->getAuthors(),
            ];
        }

        return json_encode($list, JSON_PRETTY_PRINT);
    }

    public function calculateTotalBookPrice()
    {
        $jsonArray = $this->listBooksAsJson();
        $array = json_decode($jsonArray, true);
        $totalprice = 0;

        foreach ($array as $book) {
            $totalprice += $book['price'];
        }

        return $totalprice;

    }
    
}


?>
<?php

session_start();

echo "<h1>Newly Added Book</h1>";

echo "Title: {$_SESSION["title"]}" . "<br>";
echo "Authors: {$_SESSION['authors']}" . "<br>";
echo "Price: {$_SESSION['price']}" . "<br>";

?>
<?php

namespace Classes;

include_once "Borrowable.php";
include_once "User.php";
include_once "Book.php";
include_once "Library.php";

use Exception;

class Test
{
    function run()
    {
        $user1 = new User(1, "First");
        $user2 = new User(2, "Second");
        $book1 = new Book(1, "Sdsds", 12.10, ["Author First"]);
        $book2 = new Book(2, "Asd", 11.20, ["Author Second"]);
        $book3 = new Book(3, "Dspwks", 18.10, ["Author Third"]);
        $book4 = new Book(4, "Pdsowkskd", 2.10, ["Author Fourth"]);

        $library = new Library();

        $library->addBook($book1);
        $library->addBook($book2);
        $library->addBook($book3);
        $library->addBook($book4);

        $library->addUser($user1);
        $library->addUser($user2);

        $user1->borrowBook($book1);

        echo "<br>";

        try {
            $user1->returnBook($book1);
            echo "<br>";
        } catch (Exception $e) {
            echo "Hiba: " . $e->getMessage();
            echo "<br>";
        }

        try {
            $user2->borrowBook($book1);
            echo "<br>";
            $user2->borrowBook($book2);
            echo "<br>";
            $user2->borrowBook($book3);
            echo "<br>";
            $user2->borrowBook($book4);
        } catch (Exception $e) {
            echo "Hiba: " . $e->getMessage();
        }
        
        try {
            $user1->returnBook($book4);
            echo "<br>";
        } catch (Exception $e) {
            // Hibaüzenet kiírása
            echo "Hiba: " . $e->getMessage();
            echo "<br>";
        }

        echo "<br>";

        $usersArray = $library->getUsers();
        foreach($usersArray as $user) {
            echo "User: {$user->getName()}" . "<br>";
            $books = $user->getBorrowedBooks();
            foreach ($books as $book) {
                echo "Borrowed Books: " . $book->getTitle() . ", ";
            }
            echo "<br>";
        }

        echo "Books by: 'Author First' ";
        $booksByAuthor = $library->searchBooksByAuthor('Author First');

        foreach($booksByAuthor as $book) {
            echo "<br>";
            echo $book->getTitle();
            echo "<br>";
        }

        echo $library->listBooksAsJson();
        echo "<br>";

        echo $library->calculateTotalBookPrice();
        echo "<br>";
        
    }
}

$test = new Test();
$test->run();

?>
<?php

namespace Classes;

use Exception;

class User implements Borrowable
{
   private int $id;
   private string $name;
   private array $borrowedBooks = [];

   function __construct(int $id, string $name)
   {
        $this->id = $id;
        $this->name = $name;
   }

   public function getBorrowedBooks()
   {
    return $this->borrowedBooks;
   }

   public function getName()
   {
    return $this->name;
   }

   public function borrowBook(Book $book)
   {
    if (count($this->borrowedBooks) >= 3) {
        throw new Exception("Maximum 3 konyv kolcsonozheto!");
    } else {
        echo "{$this->name} borrowed: " . $book->getTitle();
        $this->borrowedBooks[] = $book;
    }
   }

   public function returnBook(Book $book)
   {
       $index = array_search($book, $this->borrowedBooks, true);
       if ($index !== false) {
           echo "{$this->name} returned: " . $book->getTitle() ;
           unset($this->borrowedBooks[$index]);
           $this->borrowedBooks = array_values($this->borrowedBooks); // Újraindexelés
       } else {
           throw new Exception("A könyv nincs kikölcsönözve!");
       }
   }   

}

?>
Editor is loading...
Leave a Comment