-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExpenseFile.cpp
86 lines (65 loc) · 2.14 KB
/
ExpenseFile.cpp
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
#include "ExpenseFile.h"
Expense ExpenseFile::getExpenseData()
{
Expense expense;
xml.IntoElem(); //Make Expense element current parent position
xml.FindElem("expenseId");
expense.setExpenseId(atoi(xml.GetData().c_str()));
xml.FindElem("userId");
expense.setUserId(atoi(xml.GetData().c_str()));
xml.FindElem("date");
expense.setDate(atoll(xml.GetData().c_str()));
xml.FindElem("item");
expense.setItem(xml.GetData());
xml.FindElem("amount");
expense.setAmount(atof(xml.GetData().c_str()));
xml.OutOfElem(); //Make Expenses element current parent position again (go out from current Expense)
return expense;
}
vector<Expense> ExpenseFile::loadExpensesFromFile(int loggedUserId)
{
if(xml.Load(getFileName()))
{
Expense loadedExpense;
vector<Expense> expenses;
xml.FindElem(); //Sets position to "Expenses" element
xml.IntoElem(); //Make Expenses element current parent position
while(xml.FindElem("Expense"))
{
loadedExpense = getExpenseData();
if(loadedExpense.getUserId() == loggedUserId)
expenses.push_back(loadedExpense);
lastExpenseId = loadedExpense.getExpenseId();
}
return expenses;
}
else
{
cout << "Nie udalo sie zaladowac pliku z wydatkami!";
return vector<Expense>();
}
}
void ExpenseFile::appendExpenseToFile(Expense expense)
{
bool fileExists = xml.Load(getFileName());
if(!fileExists)
{
xml.SetDoc("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
xml.AddElem("Expenses");
}
xml.FindElem(); //Sets position to "Expenses" element
xml.IntoElem(); //Make Expenses element current parent position
xml.AddElem("Expense");
xml.IntoElem();
xml.AddElem("expenseId", expense.getExpenseId());
xml.AddElem("userId", expense.getUserId());
xml.AddElem("date", expense.getDate());
xml.AddElem("item", expense.getItem());
xml.AddElem("amount", to_string(expense.getAmount()));
xml.Save(getFileName());
lastExpenseId++;
}
int ExpenseFile::getLastExpenseId()
{
return lastExpenseId;
}