Skip to content

Commit 92faca0

Browse files
committed
llv22 - swift learning in topcoder
1 parent 6312fd5 commit 92faca0

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='3.0' sdk='macosx'>
3+
<sections>
4+
<code source-file-name='section-1.swift'/>
5+
</sections>
6+
<timeline fileName='timeline.xctimeline'/>
7+
</playground>
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
// Playground - noun: a place where people can play
2+
// Learn swift from http://tc-learnswift.herokuapp.com/problems?handle=llv22
3+
4+
import Foundation
5+
6+
// Challenge 21 – Super-Duper Shopping Cart (Use Xcode6-Beta6)
7+
8+
/**
9+
R1 – Create a constant array of string literals called “types” with these
10+
values: book, CD, software
11+
**/
12+
// implement code for R1 below
13+
let types = ["book", "CD", "software"];
14+
15+
16+
/**
17+
18+
R2 – Create a “Customer” struct with a “name” and “email” property
19+
**/
20+
// implement code for R2 below
21+
struct Customer{
22+
23+
}
24+
25+
26+
/**
27+
28+
R3 – Create a “randomCustomer” function that returns a tuple. Add 3
29+
30+
Customer structs to a dictionary using the customer’s name as the key.
31+
This function returns a randomly selected name and email of a Customer
32+
from the dictionary. (Hint: you can use arc4random_uniform to
33+
randomly select a value.)
34+
**/
35+
// implement code for R3 below
36+
37+
38+
39+
/**
40+
41+
R4 – Create a “Product” class with the following properties:
42+
1. id – Int
43+
2. name – String
44+
3. type – Constant, randomly selected value from “types” array
45+
4. price – Double
46+
5. discount – Double
47+
6. saleStatus – String
48+
49+
Create an initializer that set’s the name, price and discount. The
50+
discount should be set to 0 if discount is not passed. Set the
51+
id to a random number between 1 and 10,000.
52+
53+
Create a getter for “saleStatus” that returns the String
54+
“Sorry. This product is not on sale.” if dicsount is 0. Else
55+
return the interpolated string “This product is on sale. It was
56+
[display original price] but with a discount you only pay
57+
[display sale price].”
58+
**/
59+
// implement code for R4 below
60+
61+
62+
63+
/**
64+
65+
R5 – Create an array called “products” with 5 product objects. Their names are
66+
irrelevant but chose different prices. Set the discounts to 0, 0.1, 0.2, 0.3
67+
and 0.4 respectively.
68+
69+
After adding the items to the array, iterate the collection and println the
70+
“saleStatus” for each item.
71+
**/
72+
// implement code for R5 below
73+
74+
75+
76+
/**
77+
78+
R6 – Write a simple “Cart” class using Generics with the following properties:
79+
1. customerName – String
80+
2. customerEmail – String
81+
3. items – any array of any type T
82+
4. itemCount – Int, getter that returns the count of items in “items”.
83+
5. promoCode – Optional String set to nil
84+
85+
Create an initializer that set the customerName and customerEmail.
86+
87+
Implement the following functions:
88+
89+
1. Create an “add” method that appends any type to the “items” array.
90+
2. Create a “clear” method that removes all items from the “items” array.
91+
3. Create a “remove” method that removes an item from the “items” array
92+
based upon its position in the array.
93+
4. Create a “getPromoCodeDisplay” method that returns the String “Your
94+
promo code is [display promoCode].” if promoCode is not nil. Otherwise,
95+
return the String “You do not have a promo code.”.
96+
5. Create a “getCartStatus” method that returns the String “You have no
97+
items in your cart.” if the number of items in the “items” array is 0.
98+
Return the String “You have [display number of items] items in your
99+
cart.” if the number of items in the “items” array is 1, 2 or 3.
100+
For 4+ items in the “items” array, return the String “You are an
101+
awesome customer!!”
102+
**/
103+
// implement code for R6 below
104+
105+
106+
107+
/**
108+
109+
R7 – Create a “customer” object by calling the “randomCustomer” function.
110+
Create a new “cart” object for type Product with the newly created
111+
“customer” object’s name and email. Printlnt customer’s name. Println the
112+
itemCount (should be 0). Println the getCartStatus which should display
113+
“You have no items in your cart.”
114+
**/
115+
// implement code for R7 below
116+
117+
118+
119+
/**
120+
121+
R8 – iterate the “products” array and add all items from the “products”
122+
array to the cart except for element 3. Println the itemCount (should be 4),
123+
println getStatus() (should display “You are an awesome customer!!”). Assign
124+
the customer a promo code. First, println getPromoCodeDisplay (should
125+
display “You do not have a promo code.”), then set the promoCode to “1234″,
126+
then println the getPromoCodeDisplay again (should display “Your promo code
127+
is 1234.”).
128+
**/
129+
// implement code for R8 below
130+
131+
132+
133+
/**
134+
R9 – Remove the first item from the cart, then println the itemCount (should
135+
be 3) and println the getCartStatus which should display “You have 3 items
136+
in your cart.”
137+
**/
138+
// implement code for R9 below
139+
140+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Timeline
3+
version = "3.0">
4+
<TimelineItems>
5+
</TimelineItems>
6+
</Timeline>

0 commit comments

Comments
 (0)