Advertisement
Josif_tepe

Untitled

Nov 7th, 2024
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.54 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class Zadaca {
  3.  
  4.     public static void main(String[] args) {
  5.         Media b1 = new Book("Zoki Poki", "Olivera", 50);
  6.         Media b2 = new Book("Doktor Ofboli", "Ofboli", 105);
  7.         Media b3 = new Book("ABC", "BCD", 300);
  8.  
  9.         Media m1 = new Movie("Titanik", "Titanik", 2.5);
  10.         Media m2 = new Movie("Movie", "Movie", 3.5);
  11.         Movie m3 = new Movie("XYZ", "XYZ", 15.6);
  12.  
  13.         Media[] medias = new Media[6];
  14.         medias[0] = b1;
  15.         medias[1] = b2;
  16.         medias[2] = b3;
  17.  
  18.         medias[3] = m1;
  19.         medias[4] = m2;
  20.         medias[5] = m3;
  21.         Library library = new Library();
  22.         library.displayAllMedia(medias);
  23.        
  24.     }
  25.    
  26. }
  27.  
  28. class Media {
  29.     String title;
  30.     String creator;
  31.  
  32.     public Media(String title, String creator) {
  33.         this.title = title;
  34.         this.creator = creator;
  35.     }
  36.  
  37.     public String getTitle() {
  38.         return title;
  39.     }
  40.     public String getCreator() {
  41.         return creator;
  42.     }
  43.  
  44.     public void displayInfo() {
  45.         System.out.println("Title: " + title);
  46.         System.out.println("Creator: " + creator);
  47.     }
  48.  
  49. }
  50.  
  51. class Book extends Media {
  52.     int numPages;
  53.  
  54.     public Book(String title, String creator, int numPages) {
  55.         super(title, creator);
  56.         this.numPages = numPages;
  57.     }
  58.     public void readSample() {
  59.         System.out.println("The sample pages are available to read " + numPages);
  60.     }
  61.  
  62.     @Override
  63.     public void displayInfo() {
  64.         super.displayInfo();
  65.         System.out.println("Number of pages: " + numPages);
  66.     }
  67.  
  68. }
  69.  
  70. class Movie extends Media {
  71.     double duration;
  72.  
  73.     public Movie(String title, String creator, double duration) {
  74.         super(title, creator);
  75.         this.duration = duration;
  76.     }
  77.     public void watchTrailer() {
  78.         System.out.println("This trailer is available to watch: " + duration);
  79.        
  80.     }
  81.  
  82.     @Override
  83.     public void displayInfo() {
  84.         super.displayInfo();
  85.         System.out.println("Duration: " + duration);
  86.     }
  87. }
  88.  
  89. class Library {
  90.     public void displayAllMedia(Media[] medias) {
  91.         for(int i = 0; i < medias.length; i++) {
  92.             medias[i].displayInfo();
  93.             if(medias[i] instanceof Book) {
  94.                 Book book = (Book) medias[i];
  95.                 book.readSample();
  96.             }
  97.             else if(medias[i] instanceof Movie) {
  98.                 Movie movie = (Movie) medias[i];
  99.                 movie.watchTrailer();
  100.             }
  101.         }
  102.  
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement