-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.js
71 lines (60 loc) · 1.84 KB
/
code.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
class Income {
constructor(date, subject, description, sum){
this.date = date;
this.subject = subject;
this.description = description;
this.sum = sum;
}
}
class Outgoings extends Income{
sum = -1 * this.sum;
}
function add(inc){
if(inc instanceof Outgoings)tab='outgoings';
else tab='income';
storage = localStorage.getItem(tab);
var array = new Array();
if(storage !== null){
array = JSON.parse(storage);
}
array.push(inc);
localStorage.setItem(tab, JSON.stringify(array));
load();
}
function funct(element){
element.innerHTML = "something";
}
function display(tab, array){
var table = '';
for(inc of array)table += '<tr><td>' + inc.date + '</td><td>' + inc.subject +'</td><td>' +inc.sum + '</td></tr>';
document.getElementById(tab).innerHTML=table;
}
function load(){
var storage = localStorage.getItem('income');
var array = new Array();
var sold = 0;
if(storage !== null){
array = JSON.parse(storage);
display('income', array);
for(obj of array)sold+=parseFloat(obj.sum);
}
storage = localStorage.getItem('outgoings');
array = new Array();
if(storage !== null){
array = JSON.parse(storage);
display('outgoings', array);
for(obj of array)sold+=parseFloat(obj.sum);
}
console.log("soldul este " + sold);
document.getElementById('sold').innerHTML = "Soldul este "+sold;
}
function add_income(){
var income = document.getElementById('in').value;
var subject = document.getElementById('subject').value;
var date = document.getElementById('date').value;
var sum = document.getElementById('sum').value;
let obj;
if(income ==='income')obj = new Income(date, subject, '', sum);
else obj = new Outgoings(date, subject, '', sum);
add(obj);
}