Advertisement
eyuprog

Simple Bookstore Application

May 11th, 2015
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.95 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. * README
  5. * ======
  6. *
  7. * This is simple source code for bookstore application.
  8. * The functional requirement that you needed to help you understand the user story has been written.
  9. *
  10. * Your mission is :
  11. *
  12. * 1. Complete these source code at "PLACE YOUR CODE HERE" parts and run them without any error.
  13. * 2. The result has to be:
  14. *    1:true
  15. *    2:true
  16. *    3:true
  17. *    4:true
  18. *    5:true
  19. * 3. Send the source code file with your answers.
  20. */
  21.  
  22.  
  23. // PHP v5.4
  24. $app = new TestApplication;
  25. $app->run();
  26.  
  27.  
  28. $listbook=array();
  29. $listbeli=array();
  30.  
  31. class TestApplication {
  32.  
  33.  
  34.    
  35.     public function run() {
  36.  
  37.         $shop  = new Shop;     
  38.  
  39.         $books = array(
  40.             new Book('The Fellowship of the Ring', 'J.R.R. Tolkien'),
  41.             new Book('The Two Towers', 'J.R.R. Tolkien'),
  42.             new Book('The Return of the King', 'J.R.R. Tolkien'),
  43.             new Book('The Hobbit', 'J.R.R. Tolkien'),
  44.             new Book('Harry Potter and the Sorcerer\'s Stone', 'J.K. Rowling'),
  45.             new Book('Harry Potter and the Chamber of Secrets', 'J.K. Rowling'),
  46.             new Book('Harry Potter and the Prisoner of Azkaban', 'J.K. Rowling'),
  47.             new Book('Harry Potter and the Goblet of Fire', 'J.K. Rowling'),
  48.             new Book('Harry Potter and the Order of the Phoenix', 'J.K. Rowling'),
  49.             new Book('Harry Potter and the Half-Blood Prince', 'J.K. Rowling'),
  50.             new Book('Harry Potter and the Deathly Hallows', 'J.K. Rowling'),
  51.         );
  52.  
  53.  
  54.         foreach ($books as $book) {
  55.             $shop->bookAdd($book);
  56.         }
  57.  
  58.  
  59.  
  60.         // Ahmad goes to the bookstore.
  61.         // He looks for a book title "Two Towers"
  62.         // If the book is there, he puts it on book cart.
  63.  
  64.         $ahmad = new Person('Ahmad Ramadhan');
  65.  
  66.         $available = $shop->bookIsAvailable('The Two Towers');
  67.  
  68.         if ($available) {
  69.  
  70.             $book = $shop->bookGet('The Two Towers');
  71.             $ahmad->addToBag($book);
  72.  
  73.             echo "1: true";
  74.  
  75.         } else {
  76.  
  77.             echo "1: false";
  78.         }
  79.  
  80.         echo "\n";
  81.        
  82.  
  83.         // Ahmad goes to bookcase collection from author J.K. Rowling
  84.         // He looks for a book title "Harry Potter and The Goblet of Fire" from author J.K Rowling
  85.         // If the book is there, he puts it on book cart.
  86.  
  87.  
  88.         $rowlingBooks = $shop->bookListByAuthor('J.K. Rowling');
  89.  
  90.         if (in_array('Harry Potter and the Goblet of Fire', $rowlingBooks)) {
  91.  
  92.             $book = $shop->bookGet('Harry Potter and the Goblet of Fire');
  93.             $ahmad->addToBag($book);
  94.  
  95.             echo "2: true";
  96.  
  97.         } else {
  98.  
  99.             echo "2: false";
  100.         }
  101.  
  102.         echo "\n";
  103.  
  104.  
  105.         // Ahmad has finished choose the books and goes to cashier.
  106.         // He checked the bookcart and sees that there are 2 books on bookcart
  107.  
  108.         if ($ahmad->countBag() == 2) {
  109.  
  110.             echo "3: true";
  111.  
  112.         } else {
  113.  
  114.             echo "3: false";
  115.         }
  116.  
  117.         echo "\n";
  118.  
  119.         // Ahmad finished buy the books.
  120.  
  121.  
  122.  
  123.  
  124.         // Bayu goes to the bookstore.
  125.         // He looks for a book that he only remembers part of the title is "The King" in entire bookcase.
  126.         // After he finds 1 book with title matched, he puts in on bookcart.
  127.  
  128.         $bayu = new Person('Bayu Sakti');
  129.  
  130.         $books_theking = $shop->bookListByTitleContains('The King');
  131.  
  132.         if (count($books_theking) > 0) {
  133.  
  134.             $book = $shop->bookGet($books_theking[0]);
  135.             $bayu->addToBag($book);
  136.  
  137.             echo "4: true";
  138.  
  139.         } else {
  140.  
  141.             echo "4: false";
  142.         }
  143.  
  144.         echo "\n";
  145.  
  146.  
  147.         // Accidentally, He looks new arrival of the newest Harry potter's book series.
  148.         // Then he put it on bookcart and turn book "The Return of The King" back to bookcase.
  149.  
  150.  
  151.         $available = $shop->bookIsAvailable('Harry Potter and the Deathly Hallows');
  152.  
  153.         if ($available) {
  154.  
  155.             $book = $shop->bookGet('Harry Potter and the Deathly Hallows');
  156.             $bayu->addToBag($book);
  157.  
  158.  
  159.             $bayu->removeFromBag('The Return of the King');
  160.  
  161.         }
  162.  
  163.  
  164.  
  165.         // Bayu has finished choose the books and goes to cashier.
  166.         // He checked the bookcart and sees that there is 1 book on bookcart
  167.  
  168.         if ($bayu->countBag() == 1) {
  169.  
  170.             echo "5: true";
  171.  
  172.         } else {
  173.  
  174.             echo "5: false";
  175.         }
  176.  
  177.         echo "\n";
  178.  
  179.         // Bayu finished buy the books.
  180.        
  181.        
  182.     }
  183. }
  184.  
  185.  
  186. class Shop {
  187.  
  188.    // PLACE YOUR CODE HERE
  189.   var $titleBuy;
  190.   function bookAdd()
  191.   {
  192.     global $listbook;  
  193.   }
  194.  
  195.   function bookIsAvailable($title)
  196.   {
  197.     global $listbook;
  198.     if($this->in_array_r($title, $listbook))
  199.     {
  200.         return true;
  201.     }else{
  202.         return false;
  203.     }
  204.   }
  205.  
  206.   function bookGet($title='')
  207.   {
  208.     global $listbook;
  209.     global $listbeli;
  210.     $aa=array();
  211.     foreach($listbook as $key=>$val)
  212.     {
  213.         if($this->in_array_r($title,$listbook))
  214.         {
  215.             if($val['judul']==$title)
  216.             {
  217.                 $aa=array(
  218.                 'judul'=>$val['judul'],
  219.                 'pengarang'=>$val['pengarang'],
  220.                 );
  221.             }
  222.            
  223.         }
  224.     }
  225.     return $aa;
  226.   }
  227.  
  228.   function bookListByAuthor($title)
  229.   {
  230.     global $listbook;
  231.     global $listbeli;
  232.     $aa=array();
  233.     foreach($listbook as $key=>$val)
  234.     {
  235.         if($this->in_array_r($title,$listbook))
  236.         {
  237.            
  238.             if($val['pengarang']==$title)
  239.             {
  240.                 $aa[]=$val['judul'];
  241.             }
  242.         }
  243.     }
  244.     return $aa;
  245.   }
  246.  
  247.   function bookListByTitleContains($title)
  248.   {
  249.     global $listbook;
  250.     global $listbeli;
  251.     $aa=array();
  252.     foreach($listbook as $key=>$val)
  253.     {
  254.         if($this->in_array_r($title,$listbook))
  255.         {
  256.            
  257.             if($val['judul']==$title)
  258.             {
  259.                 $aa[]=$val['judul'];
  260.             }
  261.         }
  262.     }
  263.     return $aa;
  264.   }
  265.  
  266.  
  267.   function in_array_r($needle, $haystack) {
  268.     $found = false;
  269.     foreach ($haystack as $item) {
  270.     if ($item === $needle) {
  271.             $found = true;
  272.             break;
  273.         } elseif (is_array($item)) {
  274.             $found = $this->in_array_r($needle, $item);
  275.             if($found) {
  276.                 break;
  277.             }
  278.         }    
  279.     }
  280.     return $found;
  281.     }
  282. }
  283.  
  284.  
  285. class Book {
  286.  
  287.    // PLACE YOUR CODE HERE
  288.     function __construct($title,$author)
  289.     {
  290.         global $listbook;
  291.         $listbook[]=array(
  292.             'judul'=>$title,
  293.             'pengarang'=>$author,
  294.             );
  295.     }
  296.    
  297. }
  298.  
  299.  
  300.  
  301. class Person {
  302.     var $namaX;
  303.     function __construct($nama)
  304.     {      
  305.         $this->namaX=$nama;
  306.     }
  307.    
  308.     function addToBag($arr)
  309.     {
  310.         global $listbeli;      
  311.         $listbeli[]=array(
  312.             'pembeli'=>$this->namaX,
  313.             'judul'=>$arr,
  314.             );         
  315.         echo '<pre>';
  316.         var_dump($listbeli);
  317.         echo '</pre>';
  318.     }
  319.    
  320.     function ArrayMergeKeepKeys() {
  321.       $arg_list = func_get_args();
  322.       foreach((array)$arg_list as $arg){
  323.           foreach((array)$arg as $K => $V){
  324.               $Zoo[$K]=$V;
  325.           }
  326.       }
  327.     return $Zoo;
  328.     }
  329.    
  330.     function countBag()
  331.     {
  332.         global $listbeli;
  333.         $c=0;
  334.         foreach($listbeli as $key)
  335.         {
  336.             $c+=1;
  337.         }
  338.         $c=$c-1;
  339.         echo $c;       
  340.     }
  341. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement