-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpense.js
47 lines (46 loc) · 1.63 KB
/
expense.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
var Expense = function(amount, remarks, category, date) {
this.amount = amount;
this.remarks = remarks;
this.category = category;
if(date!=null)this.date = date;
else this.date=new Date();
}
Expense.prototype.save = function() {
var dFormat = this.date.getDate() + "/" + (this.date.getMonth()+1)+"/"+this.date.getFullYear();
return [dFormat, this.amount, this.category, this.remarks].join("$");
}
Expense.load = function(st) {
var tokens = st.split("$");
var dateParams = tokens[0].split("/");
var d = new Date(parseInt(dateParams[2]), parseInt(dateParams[1])-1 , parseInt(dateParams[0]));
return new Expense(parseInt(tokens[1]), tokens[3], tokens[2], d);
}
Expense.threshold = function(expenseList,lowerBound,upperBound) {
//Get expenses bounded by certain specific limits
var finalList = [];
for(var i=0; i!=expenseList.length; i++) {
if(expenseList[i].amount>lowerBound && expenseList[i].amount<upperBound) finalList.push(expenseList[i]);
}
return finalList;
};
Expense.categorize = function(expenseList, cat) {
//Find those expenses from the expenseList that fall under the given category
var finalList = [];
for(var i=0; i!=expenseList.length; i++) {
if(expenseList[i].category==cat) finalList.push(expenseList[i]);
}
return finalList;
};
var canUseDOM = !!(
(typeof window !== 'undefined' &&
window.document && window.document.createElement)
);
if(!canUseDOM) {
exports.Expense = Expense;
}
// Test code
// var e = [];
// e.push(new Expense(1,"hi", "C"));
// e.push(new Expense(11,"h2i", "C"));
// e.push(new Expense(12,"hisa", "B"));
// console.log(Expense.load("1/8/2016$45$FnB$Juice").date.getDay())