-
Notifications
You must be signed in to change notification settings - Fork 1
/
Swift_Class.swift
135 lines (113 loc) · 2.55 KB
/
Swift_Class.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
class Plan
{
var all_purchases : [String : [Purchase]] = [
'transport’: [String](),
'education’: [String](),
'health’: [String](),
'shopping’: [String](),
'entertainment’: [String](),
'food’: [String]()
]
var category_budget : [String : Int] = [
'transport’: 0,
'education’: 0,
'health’: 0,
'shopping’: 0,
'entertainment’: 0,
'food’: 0
]
init(budget: Int)
{
self.budget = budget
self.balance = budget
}
func add_purchase(purchase: [Purchase])
{
self.all_purchases[purchase.category].append(purchase)
self.balance = self.balance - purchase.price
}
func add_category(new_category: String)
{
all_purchases[new_category] = [String]()
category_budget[new_category] = 0
}
}
class Category_Plan: Plan
{
init(budget: Int)
{
self.budget = budget
self.balance = budget
}
}
class Next_Plan: Plan
{
init(current_plan: Plan, budget: Int = -1)
{
if budget > 0
{
self.budget = current_plan.budget
}
else
{
self.budget = budget
}
}
}
func make_estimation()
{
var estimation = [String : Double]()
for category in self.all_purchases.keys
{
for purchase in self.all_purchases[category]
{
var category_score = estimation.get(category, 0)
category_score = category_score + purchase.impulse_proportion
}
estimation[category] = category_score
}
var score_sum = estimation.values.reduce(0, +)
for category in estimation.keys
{
category_budget[category] = budget * estimation[category] / score_sum
}
}
class Purchase
{
var rated = false
var imp_type: String? = nil
init (date: NSDate? , category: String, price: Double, current_plan: Plan)
{
self.date = date
self.category = category
self.price = price
current_plan.add_purchase(self)
}
func apply_emo(emo_index: Int)
{
self.emo = emo_index
}
func apply_feedback(fb_index: Int = 0)
{
self.feedback = fb_index
self.rated = True
}
func measure_type() -> String
{
self.impulsion = (self.emo - self.feedback + 10) / 20
self.impulse_proportion = self.impulsion * self.price
if self.impulsion > 0.5
{
self.imp_type = "impulsion"
}
else if self.impulsion == 0.5
{
self.imp_type = "neutral"
}
else
{
self.imp_type = "positive"
}
return self.imp_type
}
}