Untitled

 avatar
unknown
plain_text
19 days ago
7.1 kB
2
Indexable
<?

namespace Classes;

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

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

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

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

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

    public function getAuthors(): array
    {
        return $this->authors;
    }
}
?>
<?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;
use Exception;
use Classes\Book;
use Classes\User;


class Library{

    private array $books=[];
    private array $users=[];




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

    }

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




    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()
{
    $totalPrice = 0;
    foreach ($this->books as $book) {
        $totalPrice += $book->getPrice();
    }
    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;
use Exception;
require_once "Book.php";
require_once "Borrowable.php";
require_once "Library.php";
require_once "User.php";
use Classes\Book;
use Classes\Library;
use Classes\User;

class Test{


    public function run()

    {
        
        $book1=new Book(1,"csosztokos",123,["Kocsog Csaba"]);
        $book2=new Book(2, "josok", 999.9,["Kovacs Szilard"]);
        $book3=new Book(3,"ass",1221,["nenez00"]);
        $book4=new Book(1,"szaiamiau",1223,["Tibikevagyokxd "]);


        $user1=new User(1,"csaba");
        $user2=new User(2,"tibike");

        $library=new Library();


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



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



        $user1->borrowBook($book1);

        try{

            $user1->returnBook($book2);
        }catch(Exception $e)
        {
            echo $e->getMessage();
        }



        try{
            $user2->borrowBook($book3);
            $user2->borrowBook($book2);
            $user2->borrowBook($book4);
            $user2->borrowBook($book1);

        }catch(Exception $e)
        {
            echo $e->getMessage();

        }
        $userArrays=$library->getUsers();
        foreach($userArrays as $user)
        {
            echo "User {$user->getName()}";
            $books=$user->getBorrowedBooks();
            foreach($books as $book)
            {
                echo "Borrowedbooks".$book->getTitle();

            }

        }
        $booksByAuthor = $library->searchBooksByAuthor('Author First');


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

        }

       echo $library->listBooksAsJson();

       echo"<br>";

       echo $library->calculateTotalBookPrice();

    }

}

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







?>
<?php
namespace Classes;
use Exception;
require_once "Book.php";
require_once "Borrowable.php";
require_once "Library.php";
require_once "User.php";
class User implements Borrowable
{

    private int $id;
    private string $name;
    private array $borrowedBooks=[];


    public 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("max 3 konyvet kolcsonozhet ki");
        } else {

            echo "{this->name} returned ". $book->getTitle();
            $this->borrowedBooks[]=$book;

        }

    }
    public function returnBook (Book $book)
    {
        $index=array_search($book, $this->getBorrowedBooks(),TRUE);
        if ($index === false){
            throw new Exception("ez a konyv nincs kikolcsonozve");
        }else{
            echo "{$this->name}vissza hozta:".$book->getTitle();
            unset($this->borrowedBooks[$index]);
            $this->borrowedBooks=array_values($this->borrowedBooks);
        }
    }





}

?>

Leave a Comment