Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Zadaca {
- public static void main(String[] args) {
- Media b1 = new Book("Zoki Poki", "Olivera", 50);
- Media b2 = new Book("Doktor Ofboli", "Ofboli", 105);
- Media b3 = new Book("ABC", "BCD", 300);
- Media m1 = new Movie("Titanik", "Titanik", 2.5);
- Media m2 = new Movie("Movie", "Movie", 3.5);
- Movie m3 = new Movie("XYZ", "XYZ", 15.6);
- Media[] medias = new Media[6];
- medias[0] = b1;
- medias[1] = b2;
- medias[2] = b3;
- medias[3] = m1;
- medias[4] = m2;
- medias[5] = m3;
- Library library = new Library();
- library.displayAllMedia(medias);
- }
- }
- class Media {
- String title;
- String creator;
- public Media(String title, String creator) {
- this.title = title;
- this.creator = creator;
- }
- public String getTitle() {
- return title;
- }
- public String getCreator() {
- return creator;
- }
- public void displayInfo() {
- System.out.println("Title: " + title);
- System.out.println("Creator: " + creator);
- }
- }
- class Book extends Media {
- int numPages;
- public Book(String title, String creator, int numPages) {
- super(title, creator);
- this.numPages = numPages;
- }
- public void readSample() {
- System.out.println("The sample pages are available to read " + numPages);
- }
- @Override
- public void displayInfo() {
- super.displayInfo();
- System.out.println("Number of pages: " + numPages);
- }
- }
- class Movie extends Media {
- double duration;
- public Movie(String title, String creator, double duration) {
- super(title, creator);
- this.duration = duration;
- }
- public void watchTrailer() {
- System.out.println("This trailer is available to watch: " + duration);
- }
- @Override
- public void displayInfo() {
- super.displayInfo();
- System.out.println("Duration: " + duration);
- }
- }
- class Library {
- public void displayAllMedia(Media[] medias) {
- for(int i = 0; i < medias.length; i++) {
- medias[i].displayInfo();
- if(medias[i] instanceof Book) {
- Book book = (Book) medias[i];
- book.readSample();
- }
- else if(medias[i] instanceof Movie) {
- Movie movie = (Movie) medias[i];
- movie.watchTrailer();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement