Question
In this exercise, you'll write a function called combine(), which will take a sorted vector of Transactions and combine the ones whose accounts are the
In this exercise, you'll write a function called combine(), which will take a sorted vector of Transactions and combine the ones whose accounts are the same. See the assignment write-up on D2L for details.
I will supply combine.h. You just need to fill in the details in combine.cpp.
Your combine() should simply use == to determine whether two Transaction objects have the same account numbers, and + (or +=) to combine them.
This version of combine() may specifically combine Transaction objects. In the next section, you'll make this a more generic function that can combine any type that implements == and + (or +=).
TestTransactionCobine.cpp
#include
#include
#include
#include
#include
#include "combine.h"
#include "Transaction.h"
void test1()
{
std::cout << "test1() ";
std::vector tlist =
{
{ "S001", 10 },
{ "S002", 20 },
{ "S003", 30 },
{ "S001", 20 },
{ "S001", 30 },
{ "S002", 40 },
{ "S003", 30 },
{ "S002", 20 }
};
std::sort(begin(tlist), end(tlist));
std::vector tcombined = combine(tlist);
std::copy(begin(tcombined), end(tcombined), std::ostream_iterator(std::cout, " "));
}
void test2()
{
std::cout << "test2() ";
std::random_device r;
std::mt19937 e(r());
std::uniform_int_distribution dist(10,100);
std::vector tlist;
int suma = 0;
int sumb = 0;
for (int i = 0; i < 10; i++)
{
int a = dist(e);
int b = dist(e);
Transaction ta("a", a);
Transaction tb("b", b);
tlist.push_back(ta);
tlist.push_back(tb);
suma += a;
sumb += b;
}
std::sort(begin(tlist), end(tlist));
tlist = combine(tlist);
std::cout << std::fixed << std::setprecision(2);
std::cout << (tlist[0].get_amount() - suma) << ' ';
std::cout << (tlist[1].get_amount() - sumb) << ' ';
}
int main()
{
test1();
test2();
}
combine.h
#pragma once
#include #include
#include "Transaction.h"
std::vector combine(const std::vector & vt);
combine.cpp
#include "combine.h"
#include
using namespace std;
vector<"Transaction"> combine(const vector<"Transaction"> & vt) { vector ret; if (vt.size == 0) return ret; auto iter = begin(vt); Transaction trans = *iter++; while (iter != end(vt)){ if(*iter == trans) trans += *iter++; else{ ret.push_back(trans); trans = *iter++; } } ret.push_back (trans); return ret; }
Transaction.h
#pragma once
#include
#include
class Transaction
{
private:
std::string account;
double amount;
public:
// Create a transaction using the given account number and amount.
Transaction(const std::string & ac, double am);
// Create a transaction with a blank account number and an amount of 0.0.
Transaction();
// Return the acount number.
std::string get_account();
// Return the transaction amount.
double get_amount();
// Add the two transactions. Throw a logic_error if the account
// numbers are not the same
Transaction & operator+=(const Transaction & rhs);
};
// Read a transaction from an input stream.
std::istream & operator>>(std::istream & is, Transaction & trans);
// Write a transaction to an output stream.
std::ostream & operator<<(std::ostream & os, const Transaction & trans);
// Two transactions are equal if they have the same account number.
bool operator==(const Transaction & lhs, const Transaction & rhs);
// The < operator compares the account numbers, not the amounts.
bool operator<(const Transaction & lhs, const Transaction & rhs);
// Add two transactions. Throw a logic_error if the account numbers
// are not the same.
Transaction operator+(Transaction lhs, const Transaction & rhs);
Transaction.cpp
#include
#include "Transaction.h"
/***** member functions *****/
Transaction::Transaction(const std::string & ac, double am)
: /* initialize member variables here */
{
}
Transaction::Transaction()
: /* initialize member variables here */
{
}
/* accessors - Transaction::get_account() and Transaction::get_amount() */
/***** non-member functions *****/
/* Note that operator+= is a member function,
and is therefore called Transaction::operator+= */
Transaction & Transaction::operator+=(const Transaction & rhs)
{
/* your code here */
/* compare account numbers - if not the same, throw a logic_error */
}
std::istream & operator>>(std::istream & is, Transaction & trans)
{
/* I'll give you this one */
std::string account;
double amount;
is >> account >> amount;
trans = Transaction(account, amount);
return is;
}
/* operator<< - you're entirely on your own for this one */
/* operator==, operator<, operator+ */
bool operator==(const Transaction & lhs, const Transaction & rhs)
{
/* your code here - just compare the account numbers */
}
bool operator<(const Transaction & lhs, const Transaction & rhs)
{
/* your code here - just compare the account numbers */
}
/* operator+ - you need to provide everything for this one */
/* see page 219 of the text for a clue as to how to implement
this by using operator+= */
TestTransaction.cpp
#include
#include
#include
#include
#include "Transaction.h"
using namespace std;
template
void print_vector(const vector
{
for (const auto t : v)
std::cout << t << ' ';
}
void test1()
{
std::cout << "test1() ";
// This function tests the constructor and accessors.
vector
{
{ "S001", 10 },
{ "S002", 20 },
{ "S003", 30 },
{ "S001", 20 },
{ "S001", 30 },
{ "S002", 40 },
{ "S003", 30 },
{ "S002", 20 }
};
// This will NOT print out the way that operator<< does;
// it will not show the proper number of decimal places.
for (auto trans : tlist)
std::cout << trans.get_account() << ' ' << trans.get_amount() << ' ';
}
vector
{
std::cout << "test2() ";
// This function tests the input and output operators.
// It returns the vector of Transaction objects read
// from stdin; test3() will use this.
vector
Transaction trans;
while (cin >> trans)
{
tlist.push_back(trans);
}
std::copy(begin(tlist), end(tlist), std::ostream_iterator
return tlist;
}
void test3(vector
{
std::cout << "test3() ";
// This function tests operator<.
std::sort(begin(tlist), end(tlist));
std::copy(begin(tlist), end(tlist), std::ostream_iterator
}
void test4()
{
std::cout << "test4() ";
// This function tests the +=, ==, and + operators.
std::cout << Transaction(Transaction("a", 15) + Transaction("a", 25)) << ' ';
Transaction t("c", 20);
t += Transaction("c", 30);
std::cout << t << ' ';
Transaction t1("x", 10);
Transaction t2("x", 20);
Transaction t3("y", 30);
bool exception_expected = false;
try
{
std::cout << std::boolalpha;
std::cout << (t1 == t2) << ' ' << (t1 == t3) << ' ';
std::cout << (t1 + t2) << ' ';
exception_expected = true;
std::cout << (t1 + t3) << ' ';
std::cout << "Failure: not exception thrown when adding Transactions for different accounts ";
}
catch (std::logic_error & e)
{
if (!exception_expected)
std::cout << "Failure: unexpected logic_error thrown ";
}
catch (...)
{
std::cout << "Failure: unexpected exception type thrown ";
}
}
int main()
{
test1();
vector
test3(tlist);
test4();
}
Creating the Transaction class?
In this lab you will create the implementation for a class called Transaction, which will represent a bank transaction. I will supply the header file; you only have to fill in the details in the Transaction.cpp file.
A Transaction has an account number and an amount, along with simple accessors for both those items. (The accessors in the supplied Transaction.h have a subtle problem you'll need to fix.)
We'll need both a default constructor and one which takes the account number and amount. Both constructors should initial everything in the constructor initializer list; neither should have any code in the body of the constructor.
We'll also need to define several operators for the class:
operator+= should be implemented as a member function. It should make sure the account number for the Transaction being added is the same as the one for the Transaction object being added to, and should throw a std::logic_error exception if not.
operator==`` should returntrueif the twoTransactions have the same account number, andfalse``` otherwise.
operator+ should take two transactions and return a Transaction whose amount is the sum of the two given transactions. This should also throw a std::logic_error exception if the account numbers do not match.
operator< should return true if the account number of the first argument is less than the account number for the second argument, and false otherwise. (Note that std::string has operator< defined to do exactly that.)
operator>> should read a std::string and a double from the given input stream, create a Transaction from those, and assign it to the Transaction object passed in as a function argument.
operator<< should write the given Transaction to the given output stream. The account number should be written first, followed by a space, followed by the amount. The amount must be written with two decimal places - you may want to review a previous assignment of you have forgotten how to do that.
For operator<< and operator>>, don't forget to return the given input or output stream.
I have posted full assignment. This last part ^^ is for the transaction classes above. Transaction.h Transaction.cpp and TestTransaction.cpp
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started