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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
#include <iostream>
#include <fstream>
#include <ctime>
#include <string>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <list>
#include <vector>
using namespace std;
class Transaction
{
public:
//time_t Time;
string Time;
string Kind;
string Description;
double Amount;
double EndBalance;
};
class Account
{
private:
int fileLength;
public:
friend class Transaction;
string Name;
vector<Transaction> Transactions;
float CurrentBalance;
void ReadAccount(string);
void WriteAccount();
void UpdateAccountFile();
void AddTransaction(Transaction);
void Deposit(std::string,double);
void Withdraw(std::string,double);
void TransferTo(std::string,std::string,double);
};
string ReadTillComma(ifstream &ImputFile)
{
string myString;
getline(ImputFile, myString, ',' );
return myString;
}
Transaction ReadTransaction(ifstream &ImputFile)
{
Transaction tran;
string end;
tran.Time = ReadTillComma(ImputFile);
tran.Kind = ReadTillComma(ImputFile);
tran.Description = ReadTillComma(ImputFile);
tran.Amount = atof(ReadTillComma(ImputFile).c_str());
getline(ImputFile, end);
tran.EndBalance = atof(end.c_str());
return tran;
}
void Account::ReadAccount(string name)
{
Name = name;
ifstream csvRead((name +".csv").c_str());
while(csvRead.tellg() =! -1)
{
Transaction tran = ReadTransaction(csvRead);
Transactions.push_back(tran);
}
csvRead.close();
CurrentBalance = Transactions.back().EndBalance;
}
void Account::WriteAccount()
{
ofstream csvWrite((Name +".csv").c_str());
for (int i = 0; i < Transactions.size(); i++)
{
csvWrite << Transactions[i].Time << "," << Transactions[i].Kind << ","
<< Transactions[i].Description << "," << Transactions[i].Amount << ","
<< Transactions[i].EndBalance << endl;
}
csvWrite.close();
}
void Account::UpdateAccountFile()
{
ofstream csvWrite((Name +".csv").c_str(), ios::binary | ios::ate);
csvWrite << Transactions.back().Time << "," << Transactions.back().Kind << ","
<< Transactions.back().Description << "," << Transactions.back().Amount << ","
<< Transactions.back().EndBalance << endl;
csvWrite.close();
}
void Account::AddTransaction(Transaction tran)
{
Transactions.push_back(tran);
UpdateAccountFile();
}
void Account::Deposit(string desc, double amnt)
{
Transaction tran;
tran.Time = "Now"; //Fix later
tran.Kind = "DEPOSIT";
tran.Description = desc;
tran.Amount = amnt;
tran.EndBalance = CurrentBalance + amnt;
AddTransaction(tran);
CurrentBalance = tran.EndBalance;
}
void Account::Withdraw(string desc, double amnt)
{
Transaction tran;
tran.Time = "Now"; //Fix later
tran.Kind = "WITHDRAWAL";
tran.Description = desc;
tran.Amount = amnt;
tran.EndBalance = CurrentBalance - amnt;
AddTransaction(tran);
CurrentBalance = tran.EndBalance;
}
void Account::TransferTo(string destinationAccount, string desc, double amnt)
{
}
/*
//No sense in doing this anyway since date is never altered and strptime doesnt work for windows c++
time_t StringToTime(string s)
{
struct tm myTM;
strptime(s.c_str(), "%Y-%m-%d %H:%M:%S",&myTM);
time_t time = mktime(&myTM);
return time;
}
string TimeToString(time_t timeRaw)
{
struct tm * timeinfo;
char timeString [80];
time ( &timeRaw);
timeinfo = localtime ( &timeRaw );
strftime (timeString, 80, "%Y-%m-%d %H:%M:%S",timeinfo);
}
*/
void PrintTransaction(Transaction t)
{
cout << t.Time << " ";
cout << t.Kind << " ";
cout << t.Description << " ";
cout << t.Amount << " ";
cout << t.EndBalance << endl;
}
float getCurrentBalance (string accountName)
{
list<Transaction> transactions;
ifstream csvRead((accountName +".csv").c_str());
while(csvRead.tellg() != -1)
{
transactions.push_back(ReadTransaction(csvRead));
//PrintTransaction(transactions.back());
}
return transactions.back().EndBalance;
}
int main()
{
Account myA;
myA.ReadAccount("lab3");
cout << myA.CurrentBalance;
//cout << getCurrentBalance("lab3");
}
|