Skip to content

Instantly share code, notes, and snippets.

@andriiburka
Last active September 16, 2020 19:07
Show Gist options
  • Save andriiburka/3fafe448e05376787e07a323a9494f49 to your computer and use it in GitHub Desktop.
Save andriiburka/3fafe448e05376787e07a323a9494f49 to your computer and use it in GitHub Desktop.
//Homework 2 - "Pet shop tasks"
protocol FeedProtocol {
func feeding()
}
protocol JailProtocol {
func needsCage()
}
protocol TankProtocol {
func needsTank()
}
protocol WalkProtocol {
func walking()
}
protocol SwimProtocol {
func swimming()
}
protocol CleanProtocol {
func cleaning()
}
//MARK: --- Structures
///The pet shop duties can be broken down into these tasks:
///• All pets need to be fed.
///• Pets that can fly need to be caged.
///• Pets that can swim need to be put in a tank.
struct Cat: FeedProtocol, WalkProtocol{
func feeding() { print(FeedProtocol.self) }
func walking() { print(WalkProtocol.self) }
}
struct Dog: FeedProtocol, WalkProtocol{
func walking() { print(FeedProtocol.self) }
func feeding() { print(WalkProtocol.self) }
}
struct Fish: FeedProtocol, CleanProtocol, TankProtocol {
func needsTank() { print(TankProtocol.self) }
func feeding() { print(FeedProtocol.self) }
func cleaning() { print(CleanProtocol.self) }
}
struct Bird: FeedProtocol, JailProtocol, CleanProtocol {
func feeding() { print(FeedProtocol.self) }
func needsCage() { print(JailProtocol.self) }
func cleaning() { print(CleanProtocol.self) }
}
//MARK: --- Arrays
///2. Create homogeneous arrays for animals that need to be fed, caged, cleaned, walked, and tanked. Add the appropriate animals to these arrays. The arrays should be declared using the protocol as the element type, for example var caged: [Cageable]
var cleanedPlaceOfPet: [CleanProtocol] = []
var swimmingPets: [SwimProtocol] = []
var walkingPets: [WalkProtocol] = []
var tankedPets: [TankProtocol] = []
var cagedPets: [JailProtocol] = []
var fedPets: [FeedProtocol] = []
var cats = Cat()
var dogs = Dog()
var fish = Fish()
var bird = Bird()
cleanedPlaceOfPet.append(fish)
cleanedPlaceOfPet.append(bird)
fedPets.append(cats)
fedPets.append(dogs)
fedPets.append(fish)
fedPets.append(bird)
cagedPets.append(bird)
tankedPets.append(fish)
walkingPets.append(cats)
walkingPets.append(dogs)
//MARK: --- LOOP
///3. Write a loop that will perform the proper tasks (such as feed, cage, walk) on each element of each array.
var allDaTings: [Any] = [cleanedPlaceOfPet, walkingPets, tankedPets, cagedPets, fedPets]
for currentCollection in allDaTings {
print(currentCollection)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment