-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
90 lines (68 loc) · 1.98 KB
/
main.js
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
function Goverment(income, departments)
{
this.debt = 0
this.income = income
this.outgoing = 0
this.departments = departments
this.step = function()
{
this.outgoing = 0
this.debt = 0
var complaints = []
var complaint = 0
for (i in this.departments)
{
this.outgoing += this.departments[i].amount
complaint = this.departments[i].get_complaints()
if (complaint != 0)
{
complaints[complaints.length] = complaint
}
}
if (this.outgoing > this.income)
{
this.debt = this.outgoing - this.income
}
return complaints
}
this.toString = function()
{
return "Goverment(debt="+this.debt.toString()+" pounds income="+this.income.toString()+" pounds outgoing="+this.outgoing.toString()+" pounds)"
}
return this
}
function Department(name, amount)
{
this.name = name
this.amount = amount
this.start_amount = amount
this.get_complaints = function()
{
if (this.amount < this.start_amount/2)
{
return "Too little money being invested in " + this.name
} else
{
return 0
}
}
this.toString = function()
{
return "Department(name=\""+this.name+"\", amount="+this.amount.toString()+" pounds)"
}
return this
}
function loadJSON(income, json_data)
{
var data = JSON.parse(json_data)
var keys = Object.keys(data)
var departments = []
for (i in keys)
{
departments[departments.length] = new Department(keys[i], parseFloat(data[keys[i]]))
}
return Goverment(income, departments)
}
//eval(loadJSON(0, data_load()).departments);
//console.log(data_load().departments.toString());
//console.log(loadJSON(0, "{\"abc\":15, \"pP\":70}").departments.toString())