Created
October 13, 2016 01:38
-
-
Save jmarkstar/918b545c4c8b521ddf5534ed6a7b4b72 to your computer and use it in GitHub Desktop.
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
/* | |
I want to create a Cooler that supports many kind of drinks but the principals drinks are Jolt energy drinks for mine and caffeine free diet coke for my girlfriend. | |
If I took an incorrect drink, I will put it again in the cooler, If I took the correct one, I will drink it. | |
*/ | |
//using enums to limit the size of cans | |
enum DrinkSize { | |
case Can12 | |
case Can16 | |
case Can24 | |
case Can32 | |
} | |
//Inheritance for differents drinks | |
class Drink { | |
var volume:Double | |
var caffeine:Double | |
var temperature:Double | |
var drinkSize:DrinkSize | |
var description:String | |
init(volume:Double, caffeine:Double, temperature:Double, drinkSize:DrinkSize){ | |
self.volume = volume | |
self.caffeine = caffeine | |
self.temperature = temperature | |
self.description = "Drink base class" | |
self.drinkSize = drinkSize | |
} | |
func drinking(amount:Double){ | |
self.volume -= amount | |
} | |
func temperatureChange(change:Double){ | |
self.temperature += change | |
} | |
} | |
class Jolt: Drink { | |
init(temperature:Double) { | |
super.init(volume: 23.5, caffeine: 280, temperature: temperature, drinkSize: DrinkSize.Can24) | |
self.description = "Jolt energy drink" | |
} | |
} | |
class CaffeineFreeDietCoke:Drink { | |
init(volume:Double, temperature:Double, drinkSize:DrinkSize){ | |
super.init(volume: volume, caffeine: 0, temperature: temperature, drinkSize: drinkSize) | |
self.description = "Caffeine Free Diet coke" | |
} | |
} | |
//My Cooler class. I am using polymorphism to add and remove any kind of drinks from the cooler. | |
class Cooler { | |
var temperature:Double | |
var cansOfDrinks = [Drink]() | |
var maxCans:Int | |
init(temperature:Double, maxCans:Int) { | |
self.temperature = temperature | |
self.maxCans=maxCans | |
} | |
func addDrink(drink:Drink) -> Bool{ | |
if cansOfDrinks.count<maxCans { | |
cansOfDrinks.append(drink) | |
return true | |
} else { | |
return false | |
} | |
} | |
func removeDrink() -> Drink? { | |
if cansOfDrinks.count > 0 { | |
return cansOfDrinks.removeFirst() | |
} else { | |
return nil | |
} | |
} | |
} | |
//Creating a cooler and adding cans of drinks | |
var cooler = Cooler(temperature: 38.0, maxCans:24) | |
for _ in 0...5 { | |
let can = Jolt(temperature: 45.1) | |
let _ = cooler.addDrink(drink: can) | |
} | |
for _ in 0...5{ | |
let can = CaffeineFreeDietCoke(volume: 15.5, temperature: 45, drinkSize: DrinkSize.Can16) | |
let _ = cooler.addDrink(drink: can) | |
} | |
let jolt = cooler.removeDrink() | |
cooler.cansOfDrinks.count | |
jolt?.drinking(amount: 5) | |
print("Jolt Left in can: \(jolt?.volume)") | |
var foundCan = false | |
//My girlfriend wants a caffeite free diet coke | |
var girlfriendDrink:Drink? | |
while !foundCan { | |
if let can = cooler.removeDrink() { | |
//I am using 'IS' to compare the type of the instance | |
if can is CaffeineFreeDietCoke { | |
foundCan = true | |
girlfriendDrink = can | |
} else { | |
cooler.addDrink(drink: can) | |
} | |
} | |
} | |
if let drink = girlfriendDrink { | |
print("Got: \(drink.description)") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment