Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create 3.6 Animal Shelter.swift #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
enum AnimalType: Int {
case cat = 0
case dog = 1
}


public class Animal {

let animalType: AnimalType

init(animalType: AnimalType) {
self.animalType = animalType
}
}

extension Animal: Equatable {
public static func == (lhs: Animal, rhs: Animal) -> Bool {
return lhs.animalType == rhs.animalType
}
}

public class AnimalQueue {
private var items: [Animal] = []

func enqueue(_ item: Animal) {
items.append(item)
}

func dequeue() -> Animal? {
guard items.count > 0 else { return nil }
return items.removeFirst()
}

func dequeue(animalType: AnimalType) -> Animal? {
guard items.count > 0 else { return nil }
for index in 0..<items.count {
if items[index].animalType == animalType {
let firstOfType = items[index]
items.remove(at: index)
return firstOfType
}
}
return nil
}

func isEmpty() -> Bool {
return items.isEmpty
}
}