Instructions
Objective
Write a C++ assignment program to manipulate linked list for a ledger system.
Requirements and Specifications
Source Code
#include "Ledger.h"
#include "Transaction.h"
#include "State.h"
Ledger::Ledger() {
head = nullptr;
tail = nullptr;
size = 0;
}
Ledger::~Ledger() {
Node* curr = head;
while(curr != nullptr) {
Node* tmp = curr->getNext();
delete curr;
curr = tmp;
}
}
Ledger& Ledger::operator+=(const Transaction& t) {
Node* tNode = new Transaction(t.getFromName(), t.getToName(), t.getAmount());
if (tail == nullptr) {
head = tNode;
tail = tNode;
size = 1;
}
else {
tail->setNext(tNode);
tail = tNode;
if (size % STEP == STEP - 1) {
std::map result = calc();
Node* sNode = new State(result);
tail->setNext(sNode);
tail = sNode;
}
size++;
}
return *this;
}
Ledger& Ledger::operator-=(const Transaction& t) {
int steps = size / STEP;
Node* curr = head;
Node* prev = nullptr;
for(int i = 0; i
for (int j = 0; j
curr = curr->getNext();
}
if (i == steps - 1) {
prev = curr;
curr = curr->getNext();
break;
}
curr = curr->getNext();
}
while(curr != nullptr) {
Transaction* tNode = dynamic_cast(curr);
if (tNode->getFromName() == t.getFromName() && tNode->getToName() == t.getToName() && tNode->getAmount() == t.getAmount()) {
if (prev == nullptr) {
head = tNode->getNext();
if (head == nullptr) {
tail = nullptr;
}
}
else {
prev->setNext(tNode->getNext());
if (prev->getNext() == nullptr) {
tail = prev;
}
}
delete tNode;
size--;
break;
}
prev = curr;
curr = curr->getNext();
}
return *this;
}
void Ledger::Clear() {
size = 0;
head = nullptr;
}
void Ledger::Settle() {
std::map result = calc();
std::map::iterator it;
for (it = result.begin(); it != result.end(); it++) {
std::cout << it->first << ": " << it->second << std::endl;
}
}
std::map Ledger::calc() {
int steps = size / STEP;
Node* curr = head;
std::map result;
for(int i = 0; i
for (int j = 0; j
curr = curr->getNext();
}
if (i == steps - 1) {
result = (dynamic_cast(curr))->getAll();
curr = curr->getNext();
break;
}
curr = curr->getNext();
}
while(curr != nullptr) {
std::string from = (dynamic_cast(curr))->getFromName();
std::string to = (dynamic_cast(curr))->getToName();
double amount = (dynamic_cast(curr))->getAmount();
if (from != EXTERNAL_ACCOUNT) {
if (result.find(from) != result.end()) {
result[from] -= amount;
}
else {
result[from] = -amount;
}
}
if (to != EXTERNAL_ACCOUNT) {
if (result.find(to) != result.end()) {
result[to] += amount;
}
else {
result[to] = amount;
}
}
curr = curr->getNext();
}
return result;
}
void Ledger::InTheBlack() {
std::map result = calc();
std::map::iterator it;
for (it = result.begin(); it != result.end(); it++) {
if (it->second > 0.0) {
std::cout << it->first << ": " << it->second << std::endl;
}
}
}
void Ledger::InTheRed() {
std::map result = calc();
std::map::iterator it;
for (it = result.begin(); it != result.end(); it++) {
if (it->second < 0.0) {
std::cout << it->first << ": " << it->second << std::endl;
}
}
}
std::ostream& operator<<(std::ostream& os, const Ledger& l) {
Node* curr = l.head;
int counter = 0;
while(curr != nullptr) {
if (counter % l.STEP == 0) {
curr = curr->getNext();
}
os << curr->to_string() << std::endl;
curr = curr->getNext();
counter++;
}
return os;
}
Contact Details
Related Samples
At ProgrammingHomeworkHelp.com, we provide comprehensive assignment support tailored to students' needs. Our C++ samples showcases expertly crafted examples, helping students understand complex concepts and coding techniques. Each sample is designed to provide clear and practical insights, guiding students through their assignments with ease. Whether you're tackling object-oriented programming, data structures, or algorithms, our C++ samples offer the perfect resource to enhance your learning and boost your grades. Explore our website to access these valuable tools and succeed in your programming journey.
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++
C++