import {BudgetItem} from '../budget/budget-item';

export class BudgetService {
    
    getItems() {

        var budgetItems: BudgetItem[];

        if (localStorage.budgetItems) {
            budgetItems = JSON.parse(localStorage.budgetItems) || [];
        } else {
            budgetItems = [];
        }

        return budgetItems;
    }


    addItem(item: BudgetItem) {
        
        var allItems: BudgetItem[];

        try {
            allItems = localStorage.budgetItems ? JSON.parse(localStorage.budgetItems) : [];
        } catch(e) {
            allItems = [];
        } finally {
            allItems.push(item);
            localStorage.budgetItems = JSON.stringify(<Array<BudgetItem>>allItems);
        } 

    }

    fillWithTestData() {
        localStorage.budgetItems = JSON.stringify(<Array<BudgetItem>>budgetItems);
    }

}

var budgetItems: BudgetItem[] = [
    {
        sum: 900,
        description: "Salary"
    },
    {
        sum: -150,
        description: "BB-8 toy"
    },
    {
        sum: -20,
        description: "Groceries"
    },
    {
        sum: -200,
        description: "Emergency"
    },
    {
        sum: -50,
        description: "Superman toy"
    },
    {
        sum: 80,
        description: "Loan returned"
    }
]

import {BudgetItem} from '../budget/budget-item';

export class BudgetService {
    
    private _budgetItems: BudgetItem[];
    
    getItems() {
        this._budgetItems = budgetItems;
        return this._budgetItems;
    }

    addItem(new_sum: number, new_description: string) {
        var item: BudgetItem = {
            sum: new_sum,
            description: new_description
        }
        this._budgetItems.push(item);
    }
}

var budgetItems: BudgetItem[] = [
    {
        sum: 900,
        description: "Salary"
    },
    {
        sum: -150,
        description: "BB-8 toy"
    },
    {
        sum: -20,
        description: "Groceries"
    },
    {
        sum: -200,
        description: "Emergency"
    },
    {
        sum: -50,
        description: "Superman toy"
    },
    {
        sum: 80,
        description: "Loan returned"
    }
]

--- 
+++ 
@@ -2,20 +2,39 @@
 
 export class BudgetService {
     
-    private _budgetItems: BudgetItem[];
-    
     getItems() {
-        this._budgetItems = budgetItems;
-        return this._budgetItems;
+
+        var budgetItems: BudgetItem[];
+
+        if (localStorage.budgetItems) {
+            budgetItems = JSON.parse(localStorage.budgetItems) || [];
+        } else {
+            budgetItems = [];
+        }
+
+        return budgetItems;
     }
 
-    addItem(new_sum: number, new_description: string) {
-        var item: BudgetItem = {
-            sum: new_sum,
-            description: new_description
-        }
-        this._budgetItems.push(item);
+
+    addItem(item: BudgetItem) {
+        
+        var allItems: BudgetItem[];
+
+        try {
+            allItems = localStorage.budgetItems ? JSON.parse(localStorage.budgetItems) : [];
+        } catch(e) {
+            allItems = [];
+        } finally {
+            allItems.push(item);
+            localStorage.budgetItems = JSON.stringify(<Array<BudgetItem>>allItems);
+        } 
+
     }
+
+    fillWithTestData() {
+        localStorage.budgetItems = JSON.stringify(<Array<BudgetItem>>budgetItems);
+    }
+
 }
 
 var budgetItems: BudgetItem[] = [
Add localStorage to store data
