Created
April 25, 2023 19:16
-
-
Save tornikegomareli/fb37d67752b1975b0632f61e11f48838 to your computer and use it in GitHub Desktop.
Playlist structure, enums. Add & filter & remove
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.swift | |
// Structures12Apr2023 | |
// | |
// Created by Tornike on 12/04/2023. | |
// | |
import Foundation | |
//Music Player | |
// | |
//Design a simple music player with an enum MusicGenre that has cases for different music genres (e.g., Pop, Rock, Jazz, Classical, etc.). Create a struct Song that contains properties such as title, artist, and genre (using MusicGenre). Implement a struct Playlist that has an array of Song objects and a name. Add methods to Playlist to add and remove songs, play a specific song, and filter the playlist by genre. | |
enum MusicGenre { | |
case pop | |
case rock | |
case jazz | |
case classical | |
case hiphop | |
} | |
struct Song { | |
let id = UUID() | |
let title: String | |
let artist: String | |
let genre: MusicGenre | |
init(title: String, artist: String, genre: MusicGenre) { | |
self.title = title | |
self.artist = artist | |
self.genre = genre | |
} | |
} | |
struct Playlist { | |
var favoriteTracks: [Song] = [] | |
mutating func addTrackInPlaylist(song: Song) { | |
favoriteTracks.append(song) | |
} | |
mutating func deleteTrackFromPlaylist(song: Song) { | |
var temporaryArray: [Song] = [] | |
for track in favoriteTracks { | |
if track.id != song.id { | |
temporaryArray.append(track) | |
} | |
} | |
favoriteTracks = temporaryArray | |
} | |
func filterWith(genre: MusicGenre) -> [Song] { | |
var temporaryArray: [Song] = [] | |
for track in favoriteTracks { | |
if track.genre == genre { | |
temporaryArray.append(track) | |
} | |
} | |
return temporaryArray | |
} | |
} | |
var travelingPlaylist = Playlist() | |
let mfdoom = Song(title: "Doomsday", artist: "MF Doom", genre: .hiphop) | |
let pinkFloyd = Song(title: "The wall", artist: "Pink Floyd", genre: .rock) | |
let wutang = Song(title: "Protect yea neck", artist: "Wu tang", genre: .hiphop) | |
let jackson = Song(title: "Moondance", artist: "Michael Jackson", genre: .pop) | |
let marvin = Song(title: "Some love", artist: "Marvin guy", genre: .jazz) | |
let bethoven = Song(title: "Moonlight sonata", artist: "Bethoven", genre: .classical) | |
travelingPlaylist.addTrackInPlaylist(song: mfdoom) | |
travelingPlaylist.addTrackInPlaylist(song: pinkFloyd) | |
travelingPlaylist.addTrackInPlaylist(song: wutang) | |
travelingPlaylist.addTrackInPlaylist(song: jackson) | |
travelingPlaylist.addTrackInPlaylist(song: marvin) | |
travelingPlaylist.addTrackInPlaylist(song: bethoven) | |
func printTrackDetails(song: Song) { | |
print("Artist: ", song.artist) | |
print("Track: ", song.title) | |
print("Genre: ", song.genre) | |
} | |
func printPlaylistMusic() { | |
for track in travelingPlaylist.favoriteTracks { | |
printTrackDetails(song: track) | |
} | |
} | |
let filtredHipHop = travelingPlaylist.filterWith(genre: .classical) | |
print(filtredHipHop) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment