Description
1 Objectives
This homework aims to help you get familiar with the fundamental C++ programming concepts.
Keywords: Constructor/Copy Constructor/Destructor, Assignment/Move, Operator Overloading, Memory Management
2 Problem Definition
Summary of HW3: Implement the methods in the given 3 classes (Transaction, Account, Bank). Banks have Accounts and Accounts have Transactions. You can find more detail in their respective sections.
In this homework you are a programmer and your supervisors want certain features in their Bank system. Your task is to implement Transaction, Account and Bank systems that are provided you with a header file(You will not edit header files). As situation requested shiny features of the latest C++ is not available to you. Therefore, you have to be careful with your programs memory management.
3 Class Definitions
3.1 Transaction
Transaction is the most basic class in this homework. Basically, it holds a certain amount and date of the Transaction.
class Transaction { private :
double _amount;
time_t _date;
public :
/∗∗
∗ Empty constructor ∗ give −1 to everything
∗/ Transaction() ;
/∗∗
∗ Constructor
∗
∗ @param amount The value of Transaction (Can be
∗ @param date Transaction date
∗/ Transaction( double amount, time_t date) ; /∗∗ ∗ Copy Constructor .
∗
∗ @param rhs The Transaction to be copied .
∗/
Transaction( const Transaction& rhs) ;
/∗∗
∗ Compare two Transaction based on their date
∗
∗ @param rhs Compared Transaction negative or positive )
∗ @return If current Transaction happened before return true
∗ else return false
∗/ bool operator <(const Transaction& rhs) const ;
/∗∗
∗ Compare two Transaction based on their date
∗
∗ @param date Compared date the given Transaction ←-
∗ @return If current Transaction happened after
true
∗ else return false the given Transaction return←-
∗/ bool operator >(const Transaction& rhs) const ;
/∗∗
∗ Compare a Transaction with a given date
∗
∗ @param date Compared date
∗ @returnIfcurrentTransaction happened beforethe given date returntrue
∗ elsereturnfalse
∗/ bool operator <(const time_t date) const ;
/∗∗
∗ Compare a Transaction with a given date
∗
∗ @param date Compared date
∗ @returnIfcurrentTransaction happened afterthe given date returntrue
∗ elsereturnfalse
∗/ bool operator >(const time_t date) const ;
/∗∗
∗ Sum the valueof two Transaction amounts
∗
∗ @param rhs The transactionto sum over
∗ @return The output ofthe summation in double format
∗/ double operator+(const Transaction& rhs) ;
/∗∗
∗ Sum the valueof a Transaction with another double
∗
∗ @param add The amount to sum over
∗ @return The output ofthe summation in double format
∗/ double operator+(const double add) ;
/∗∗
∗ Assignment operator
∗
∗ @param rhs Transaction to assign ∗ @return this Transaction
∗/
Transaction& operator=(const Transaction& rhs) ;
/∗∗
∗ Stream overload
∗
∗ What to stream :
∗ Transaction amount”tab−tab”hour : minute : second−day/month/year ( inlocaltime )
∗
∗ @param os Stream to be used .
∗ @param transactionTransaction to be streamed .
∗ @return the current Stream
∗/ friend std : : ostream& operator <<(std : : ostream& os, const Transaction& ←transaction) ;
};
3.2 Account
The account is defined for a single user and users have their respective ids. Accounts also hold Transaction information of their user in a sorted manner.
class Account { private :
int _id;
Transaction∗∗ _activity; int∗ _monthly_activity_frequency;
public :
/∗∗
∗ Empty constructor
∗ give the id as −1 ∗ give nullptr for pointers
∗/ Account() ;
/∗∗
Constructor
∗ Note : The givenactivityarraywillhave 12 Transaction∗
∗ Each oftheseTransaction∗ willrepresent a month from the 2019
∗ Basicaly activity [0] will represent January activity [11] will represent February activity [11] will represent March
. . .
∗ activity [0] will only contain Transactions happened in January
∗ However , be careful that Transactions inside of activity [ i ] will not be in←sorted order
∗ For Example : We are certain that activity [0] is containing Transactions ←-
happened in January 2019
∗ But we are not sure which of them happenedf i r s t .
∗ I strongly suggest you to use a sorting algorithm while Transaction to your object . storing these ←-
∗ ( Sorting by the date , So that you can directly use them
)
∗ (You can use bubble sort )
∗
∗ @param id id of this Account in stream overload←-
∗ @param activity 2d Transaction array f i r s t layers lenght month is 12 for each ←-
∗ @param monthly activity frequency how many transactions
∗/ made in each month
activity [10] will represent November activity [11] will represent December
Account( int id, Transaction∗∗ const activity,int ∗ monthly_activity_frequency←-
) ;
/∗∗
∗ Destructor
∗
∗ Do not forget to free the space you have created ( This assignment does not ←use smart pointers )
∗/
˜Account() ;
/∗∗
∗ Copy constructor (Deep copy)
∗
∗ @param other The Account to be copied
∗/
Account( const Account& rhs) ;
/∗∗
∗ Copy constructor (Deep copy)
∗
∗ This copy constructorstakes two time telements
∗ Transactions of the old Account will be copied to new Account ∗ i f and only i f they are between these given dates ∗ Given dates will not be included .
∗
∗ @param rhs The Account to be copied
∗ @param start date Starting date for transaction to be copied . ∗ @param end date Ending date for transactions to be copied .
∗/
Account( const Account& rhs, time_t start_date, time_t end_date) ;
/∗∗
∗ Move constructor
∗
∗ @param rhs Account which you
∗/
Account(Account&& rhs) ;
/∗∗
∗ Move assignment operator
∗ will move the resources from
∗ @param rhs Account which you
∗ @return this account will move the resources from
∗/
Account& operator=(Account&& rhs) ;
/∗∗
∗ Assignment operator ∗ deep copy
∗
∗ @param rhs Account to assign ∗ @return this account
∗/ Account& operator=(const Account& rhs) ;
/∗∗
∗ Equality comparison overload
∗
∗ This operatorchecks only idofthe Account
∗
∗ @param rhs The Account to compare
∗ @return returnstruei fboth idsare same falseothervise
∗/ bool operator==(const Account& rhs) const ; /∗∗
∗ Equality comparison overload
∗
∗ This operatorchecks only idofthe Account
∗
∗ @param id to compare
∗ @return returnstruei fboth idsare same falseothervise
∗/ bool operator==(int id) const ;
/∗∗
∗ sum and equaloperator
∗ Add Transactionsof two Accounts
∗ You have to add transactionsincorrectplacesin youractivityarray
∗ Note : Remember that activity [0] is always January and activity [11] is ←always December
∗ ( This informationalsoholdsforevery other month)
∗
∗ You can have Transactions with the same date
∗
∗ @param rhs Account which take new Transactions from ∗ @return this Account after adding new Transactions
∗/
Account& operator+=(const Account& rhs) ;
/∗∗
∗ How much money Account has(Sum ofTransaction amounts)
∗
∗
∗ @returntotal amount ofthe money ofthe account
∗/ double balance() ;
/∗∗
∗ How much money Account has at the end ofgiven date
∗
∗ Given datewillnot be included .
∗ @param end date You will count the amounts until this given date ( not ←inclusive )
∗ @return Total amount the Account hasuntilgiven date
∗/ double balance(time_t end_date) ;
/∗∗
∗ How much money Account between given dates ∗ Given dates will not be included .
∗
∗ @param end date You will count the amounts between given dates (not ←inclusive )
∗ @return Total amount the Account has between givendates
∗ You will only count a Transaction amount i f and only i f it occured between←given dates ∗/ double balance(time_t start_date, time_t end_date) ;
/∗∗ ∗ Stream overload .
∗
∗
∗
∗ What to stream
∗ Id ofthe user
∗ Earliest Transaction amount”tab”−”tab”hour : minute : second−day/month/year ( in←localtime )
∗ Second earliest Transaction amount”tab”−”tab”hour : minute : second−day/month/←year ( in localtime ) ∗ . . .
∗ Latest Transaction amount”tab−tab”hour : minute : second−day/month/year ( in ←localtime ) ∗
∗ Note : activity array will only contain dates from January 2019 to ←December 2019
∗ Note :Transactions should be inorder by date
∗ Note :eitherof monthly activity frequencyor activityisnullptr
∗ you will just stream ∗ −1
∗ @param os Stream to be used .
∗ @param Account to be streamed . ∗ @return the current Stream
∗/
friend std : : ostream& operator <<(std : : ostream& os,const Account& account) ;
};
3.3 Bank
The bank keeps track of accounts.
class Bank { private :
std : : string _bank_name;
int _user_count;
Account∗ _users; public :
/∗∗
∗ Empty constructor
∗ give the bank name as ”not defined”
∗ give nullptr for pointers ∗ give 0 as users count
∗/ Bank() ;
/∗∗
∗ Constructor
∗
∗
∗ @param bankname name ofthis bank
∗ @param userspointer to hold users of this bank ∗ @param usercount number of users this bank has ∗/ Bank(std : : string bank_name, Account∗ const users, int user_count) ;
/∗∗
∗ Destructor
∗
∗ Do not forget to free the space you have created ( This assignment does not ←use smart pointers )
∗/ ˜Bank() ;
/∗∗
∗ Copy constructor (Deep copy)
∗
∗ @param rhs The Bank to be copied
∗/ Bank( const Bank& rhs) ;
/∗∗
∗ You should deep copy the contentofthe second bank
∗ Merge two banks
∗ If both banks has a user with the same id , Transactions of these users ←will be merged in to the same Account ∗ For example :
∗ Bank1 has[1 ,2]idusers
∗ Bank2 has[2 ,3]idusers
∗
∗ Bank1 after += operator will have [1 ,2 ,3] id users ∗ User with id 2 will have its transactions histories merged
∗
∗ Transactions with of the users with the same id should be merged and ←updated
∗ @param rhs Merged Bank ∗ @return this Bank
∗/
Bank& operator+=(const Bank& rhs) ;
/∗∗
∗ Add a new account to Bank
∗
∗ Ifthe newly added useralreadyexistsinthis Bank merge their ←-
Transactions
∗
∗ @param new acc new account to add to bank
∗ @returnthis Bank
∗/
Bank& operator+=(const Account& new_acc) ;
/∗∗ Indexingoperatoroverload
∗
∗ Return the Account with the givenid
∗
∗ Ifthereis no Account with the givenidreturn thef i r s telement
∗
∗ @param account ididofthe Account
∗ @return i f given id exist in the bank return the account , else return the ←f i r s t account
∗
∗/
Account& operator [ ] ( int account_id) ;
∗ Stream overload .
∗ all the accounts will be between 01−01−2019 and 31−12−2019
∗ What to stream
∗ bank name”tab”number of users who are eligible for a loan”tab” total ←-
balanceofthe bank
∗
∗ A user is safe for a loan i f and only i f that user did not negative balance for 2 or more consecutive months have any ←-
∗ For example ,let ‘ s say our bank named as ”banana” has twousers
∗
∗ User A’ s balance for each month is
∗
∗ January − 0
∗ February − 0
∗ March − 100
∗ April − −20
∗ May − −30
∗ June − 40
∗ July − 60
∗ August − 0
∗ September − 0
∗ October − 0
∗ November − 0
∗ December − 0
∗asgiven
∗ This user is not eligible because negative ( consecutive ) in April and May his/her balance was ←-
∗ You s t i l lhave to add 150 to thetotalbalanceofthebank
∗ User B’ s balance for
∗
∗ January − 0
∗ February − 0
∗ March − 100
∗ April − −20
∗ May − 40
∗ June − −30 ∗ July − 60
∗ August − 0
∗ September − 0
∗ October − 0
∗ November − 0
∗ December − 0
∗each month isas given
∗ This user is eligible because negative balances were not consecutive
∗ You will also add 150
∗ to the total balance of the bank
∗ your outputwillbe as
/∗∗
∗ banana1300
∗/ friend std : : ostream& operator <<(std : : ostream& os, const Bank& bank) ;
};
4 Extras
You have to check for memory leaks in your code. Any memory leak in certain class will result in point reduction.
You can test your code for any memory leak by using valgrind. (Provided MakeFile has valgrind option)
You can also test your implementation by using transaction test.cpp, account test.cpp, bank test.cpp, and compare your results with provided corresponding example *.txt files. (Some text editors might display tab character in a different format. To look at the output in the best way use gedit in Ubuntu,
TextEdit in Mac or just open it with vi)
Note: You can test your classes with (ouputs of these runs also given to you)
$ make transaction
$ make run
$ make valgrind
$ make account
$ make run
$ make valgrind
$ make bank
$ make run $ make valgrind
5 Grading
• Full grade for Transaction class implementation 15 points.
• Full grade for Account class implementation 60 points.
• Full grade for Bank class implementation 25 points.
To get a full grade from each part your code should not have any memory leak. This will be checked with Valgrind. If any of your classes have memory leak your grade will be reduced by 10 points from the overall points you get for that class. (Let say your Account class has memory leak because of some of your functions, in this case even if all your functions run correctly and passes all the tests you will get 50) If your code gives segmentation error in any part of the testing process you will not be graded for the remaining parts of the test.
Please remember that these classes require each other to function properly. If your Transaction functions have problems, your Account and Bank Functions can also have problems. This will be effective in the testing process. For example, if your Account constructor does not work in a correct way(can be segmentation or any other run time error) we can not grade you for remaining functions.
6 Regulations
• Programming Language: You must code your program in C++ (11). Your submission will be compiled with g++ with -std=c++11 flag on department lab machines.
• Allowed Libraries: You can only use libraries provided inside of headers. Use of any other library (especially the external libraries found on the internet) is forbidden.
• Memory Management: When an instance of a class is destructed, the instance must free all of its owned/used heap memory. Any heap block, which is not freed at the end of the program will result in grade deduction. Please check your codes using valgrind –leak-check=full for memory-leaks.
• Late Submission: You have a total of 10 days for late submission. You can spend this credit for any of the assignments or distribute it for all. For each assignment, you can use at most 3 days-late. • Cheating: In case of cheating, the university regulations will be applied.
• Newsgroup: It’s your responsibility to follow the cow forums for discussions and possible updates on a daily basis.
7 Submission
Submission will be done via CengClass. Create a zip file named hw3.zip that contains:
• Transaction.cpp
• Account.cpp
• Bank.cpp
Do not submit a file that contains a main function. Such a file will be provided and your code will be compiled with it. Also, do not submit a Makefile.
Note: The submitted zip file should not contain any directories! The following command sequence is expected to run your program on a Linux system:
$ unzip hw3.zip
$ make clean
$ make all
$ make run
$ -optional- make valgrind





