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.
title, an author, and its publicationYear.title, a director, and its releaseYear.title, an artist, and the album it belongs to.Define an Enum:
enum named Media.book, movie, and musicTrack.Create Some Media:
mediaLibrary.Process and Print the Library:
mediaLibrary array.switch statement to check which type of media each item is.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)
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!