Last active
August 29, 2015 14:07
-
-
Save palewar/bf74ca669ebd5fde73ff to your computer and use it in GitHub Desktop.
Solution to 'Learn Swift $100 Playground Challenge' by Topcoder. It's a playground file for XCode 6 Beta 6. Comments include instructions and solution follows.
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
// Playground - noun: a place where people can play | |
import UIKit | |
import Foundation | |
// Challenge 21 – Super-Duper Shopping Cart (Use Xcode6-Beta6) | |
/** | |
R1 – Create a constant array of string literals called “types” with these | |
values: book, CD, software | |
**/ | |
// implement code for R1 below | |
let types = ["book", "CD", "software"] | |
/** | |
R2 – Create a “Customer” struct with a “name” and “email” property | |
**/ | |
// implement code for R2 below | |
struct Customer { | |
var name = "" | |
var email = "" | |
} | |
/** | |
R3 – Create a “randomCustomer” function that returns a tuple. Add 3 | |
Customer structs to a dictionary using the customer’s name as the key. | |
This function returns a randomly selected name and email of a Customer | |
from the dictionary. (Hint: you can use arc4random_uniform to | |
randomly select a value.) | |
**/ | |
// implement code for R3 below | |
let customer1 = Customer(name: "Sachin", email:"sachin@somemail.com") | |
let customer2 = Customer(name: "Vandana", email: "vandana@somemail.com") | |
let customer3 = Customer(name: "Gaurav", email: "gaurav@somemail.com") | |
let dictionary = [customer1.name: customer1, customer2.name: customer2, customer3.name: customer3] | |
func randomCustomer() -> (name: String, email: String){ | |
let array = [Customer](dictionary.values) | |
let randomIndex = Int(arc4random_uniform(UInt32(array.count))) | |
return (array[randomIndex].name, array[randomIndex].email) | |
} | |
/** | |
R4 – Create a “Product” class with the following properties: | |
1. id – Int | |
2. name – String | |
3. type – Constant, randomly selected value from “types” array | |
4. price – Double | |
5. discount – Double | |
6. saleStatus – String | |
Create an initializer that set’s the name, price and discount. The | |
discount should be set to 0 if discount is not passed. Set the | |
id to a random number between 1 and 10,000. | |
Create a getter for “saleStatus” that returns the String | |
“Sorry. This product is not on sale.” if dicsount is 0. Else | |
return the interpolated string “This product is on sale. It was | |
[display original price] but with a discount you only pay | |
[display sale price].” | |
**/ | |
// implement code for R4 below | |
class Product { | |
var id: Int | |
var name: String | |
var type = types[1] | |
var price: Double | |
var discount: Double | |
var saleStatus: String { | |
get { | |
if discount == 0 { | |
return "Sorry. This product is not on sale." | |
} else { | |
return "This product is on sale. It was \(price) but with a discount you only pay \(price - price * discount)." | |
} | |
} | |
} | |
init(){ | |
id = 1 | |
name = "Beethoven" | |
price = 300 | |
discount = 0 | |
} | |
} | |
/** | |
R5 – Create an array called “products” with 5 product objects. Their names are | |
irrelevant but chose different prices. Set the discounts to 0, 0.1, 0.2, 0.3 | |
and 0.4 respectively. | |
After adding the items to the array, iterate the collection and println the | |
“saleStatus” for each item. | |
**/ | |
// implement code for R5 below | |
let product1 = Product(); let product2 = Product(); let product3 = Product(); let product4 = Product(); let product5 = Product() | |
product1.name = "Beethoven"; product1.price = 100; product1.discount = 0 | |
product2.name = "A R Rahman's Best"; product2.price = 200; product2.discount = 0.1 | |
product3.name = "Madonna's Best"; product3.price = 300; product3.discount = 0.2 | |
product4.name = "Beyonce's Best"; product4.price = 400; product4.discount = 0.3 | |
product5.name = "Shakira's Best"; product5.price = 500; product5.discount = 0.4 | |
let products = [product1, product2, product3, product4, product5] | |
for product in products { | |
println(product.saleStatus) | |
} | |
/** | |
R6 – Write a simple “Cart” class using Generics with the following properties: | |
1. customerName – String | |
2. customerEmail – String | |
3. items – any array of any type T | |
4. itemCount – Int, getter that returns the count of items in “items”. | |
5. promoCode – Optional String set to nil | |
Create an initializer that set the customerName and customerEmail. | |
Implement the following functions: | |
1. Create an “add” method that appends any type to the “items” array. | |
2. Create a “clear” method that removes all items from the “items” array. | |
3. Create a “remove” method that removes an item from the “items” array | |
based upon its position in the array. | |
4. Create a “getPromoCodeDisplay” method that returns the String “Your | |
promo code is [display promoCode].” if promoCode is not nil. Otherwise, | |
return the String “You do not have a promo code.”. | |
5. Create a “getCartStatus” method that returns the String “You have no | |
items in your cart.” if the number of items in the “items” array is 0. | |
Return the String “You have [display number of items] items in your | |
cart.” if the number of items in the “items” array is 1, 2 or 3. | |
For 4+ items in the “items” array, return the String “You are an | |
awesome customer!!” | |
**/ | |
// implement code for R6 below | |
class Cart<T> { | |
var customerName: String | |
var customerEmail: String | |
var items = [T]() | |
var itemCount: Int { | |
get { | |
return items.count | |
} | |
} | |
var promoCode: String? | |
init(name: String, email: String){ | |
customerName = name | |
customerEmail = email | |
} | |
func add(a: T) { | |
items.append(a) | |
} | |
func clear() { | |
items.removeAll(keepCapacity: false) | |
} | |
func remove(position: Int) { | |
items.removeAtIndex(position-1) | |
} | |
func getPromoCodeDisplay() -> String { | |
if promoCode != nil { | |
return "Your promo code is \(promoCode!)." | |
} else { | |
return "You do not have a promo code." | |
} | |
} | |
func getCartStatus() -> String { | |
if items.count == 0 { | |
return "You have no items in your cart." | |
} else if items.count < 4 { | |
return "You have \(items.count) items in your cart." | |
} else { | |
return "You are an awesome customer!!" | |
} | |
} | |
} | |
/** | |
R7 – Create a “customer” object by calling the “randomCustomer” function. | |
Create a new “cart” object for type Product with the newly created | |
“customer” object’s name and email. Printlnt customer’s name. Println the | |
itemCount (should be 0). Println the getCartStatus which should display | |
“You have no items in your cart.” | |
**/ | |
// implement code for R7 below | |
var customer = randomCustomer() | |
var cart = Cart<Product>(name: customer.name, email: customer.email) | |
println(cart.customerName) | |
println(cart.itemCount) | |
println(cart.getCartStatus()) | |
/** | |
R8 – iterate the “products” array and add all items from the “products” | |
array to the cart except for element 3. Println the itemCount (should be 4), | |
println getStatus() (should display “You are an awesome customer!!”). Assign | |
the customer a promo code. First, println getPromoCodeDisplay (should | |
display “You do not have a promo code.”), then set the promoCode to “1234″, | |
then println the getPromoCodeDisplay again (should display “Your promo code | |
is 1234.”). | |
**/ | |
// implement code for R8 below | |
for (index, value) in enumerate(products) { | |
if index+1 != 3 { | |
cart.add(value) | |
} | |
} | |
println(cart.itemCount) | |
println(cart.getCartStatus()) | |
println(cart.getPromoCodeDisplay()) | |
cart.promoCode = "1234" | |
println(cart.getPromoCodeDisplay()) | |
/** | |
R9 – Remove the first item from the cart, then println the itemCount (should | |
be 3) and println the getCartStatus which should display “You have 3 items | |
in your cart.” | |
**/ | |
// implement code for R9 below | |
cart.remove(1) | |
println(cart.itemCount) | |
println(cart.getCartStatus()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment