Open menu with table of contents Catalog Your Media Library
Logo of Stuttgart Media University for light theme Logo of Stuttgart Media University for dark theme
Mobile Application Development 2

Catalog Your Media Library

Stuttgart Media University

1 The Goal

Your task is to build a simple system to catalog items in a personal media library. The library can contain three different types of media: Books, Movies, and Music Tracks.

Each type of media has its own unique set of properties that you need to store.

2 Requirements

  1. Book: Needs a title, an author, and its publicationYear.
  2. Movie: Needs a title, a director, and its releaseYear.
  3. Music Track: Needs a title, an artist, and the album it belongs to.

3 Steps

  1. Define an Enum:

    • Create a Swift enum named Media.
    • This enum should have three cases: book, movie, and musicTrack.
    • For each case, add the required properties as associated values.
  2. Create Some Media:

    • Create at least one constant for each type of media (one book, one movie, one song).
    • Store these instances in an array called mediaLibrary.
  3. Process and Print the Library:

    • Loop through the mediaLibrary array.
    • Inside the loop, use a switch statement to check which type of media each item is.
    • For each case, extract the associated values and print a descriptive string to the console.

4 Code

import Foundation

// Step 1: Define the Media enum with associated values
// Each case can store different types of information specific to that media type
enum Media {
    case Book(title: String, author: String, publicationYear: Int)
    case Movie(title: String, director: String, releaseYear: Int)
    case MusicTrack(title: String, artist: String, album: String)
    // case Game(title: String, releaseYear: Int) // If included the switch case will break because it's no longer acknowleges all the cases
}
// Step 2: Create instances of different media types
let terminator2 = Media.Movie(title: "Terminator2", director: "James Cameron", releaseYear:1991)
let harryPotter = Media.Book(title: "Harry Potter und der Stein der weisen Eltern", author: "J. K. Rowling", publicationYear: 2000)
let aroundTheWorld = Media.MusicTrack(title: "Around the World", artist: "Daft Punk", album: "Discovery")

// Store all media items in a single array
// Swift allows this because they're all of type Media
let mediaArray = [terminator2, harryPotter, aroundTheWorld]


// Step 3: Loop through and process each media item
for mediaItem in mediaArray {
   //print(mediaItem)
   
   // Use switch to handle each media type differently
   // The 'let' keyword extracts the associated values
   switch mediaItem {
   case .Book(let title, let author, let publicationYear):
       print("Book \(title) from \(author) published at \(publicationYear)")
   case .Movie(let title, let director, let releaseYear):
       print("The Movie \(title) was directed by \(director) and was released in \(releaseYear)")
   case .MusicTrack(let title, let artist, let album):
       print("The Track \(title) is from the Artist \(artist) and is on the Album \(album)")
//    default: // Default not required if switch case is exhaustive
//        print("Unknown Type")
   }
}

// Bonus: Defining the Function
// This function filters movies by release year using pattern matching
func printMoviesNewerThan(year: Int, over library: [Media]){
   
   for mediaItem in library {
       // 'if case' lets us check if an item matches a specific enum case
       if case .Movie(let title, let director, let releaseYear) = mediaItem {
           
           if releaseYear > year {
               print(mediaItem)
           } else {
               print("Movie too old!")
           }
       }
   }
}

// Calling the function
printMoviesNewerThan(year: 2000, over: mediaArray)

5 Expected Output

Book Harry Potter und der Stein der weisen Eltern from J. K. Rowling published at 2000
The Movie Terminator2 was directed by James Cameron and was released in 1991
The Track Around the World is from the Artist Daft Punk and is on the Album Discovery
Movie too old!