$24
• Objectives
This homework aims to help you get familiar with the fundamental C++ programming concepts.
Keywords: Constructor/Copy Constructor/Destructor, Assignment/Move, Operator Overloading, Mem-ory Management
• Problem De nition
Summary of HW3: Implement the methods in the given 3 classes (Transaction, Account, Bank). Banks have Accounts and Accounts have Transactions. You can nd 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 le(You will not edit header les). 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.
1
• Class De nitions
3.1 Transaction
Transaction is the most basic class in this homework. Basically, it holds a certain amount and date of the Transaction.
c l a s s Transaction f
p r i v a t e :
d o u b l e _amount ;
time_t _date ;
p u b l i c :
• ∗ ∗
∗ Empty c o n s t r u c t o r
g i v e 1 t o e v e r y t h i n g
∗/
Transaction ( ) ;
• ∗ ∗
∗ C o n s t r u c t o r
∗
∗ @param amount The v a l u e o f T r a n s a c t i o n (Can be n e g a t i v e o r p o s i t i v e )
∗ @param d a t e T r a n s a c t i o n d a t e
∗/
Transaction( d o u b l e amount , time_t date) ;
• ∗ ∗
∗ Copy C o n s t r u c t o r .
∗
∗ @param r h s The T r a n s a c t i o n t o be c o p i e d .
∗/
Transaction( c o n s t Transaction& rhs) ;
• ∗ ∗
∗ Compare two T r a n s a c t i o n based on t h e i r d a t e ∗
∗ @param r h s Compared T r a n s a c t i o n
∗ @return I f c u r r e n t T r a n s a c t i o n happened b e f o r e t h e g i v e n T r a n s a c t i o n -
r e t u r n t r u e
e l s e r e t u r n f a l s e
∗/
b o o l o p e r a t o r <( c o n s t Transaction& rhs) c o n s t ;
• ∗ ∗
∗ Compare two T r a n s a c t i o n based on t h e i r d a t e ∗
∗ @param d a t e Compared d a t e
∗
@return I f c u r r e n t T r a n s a c t i o n happened a f t e r t h e g i v e n T r a n s a c t i o n r e t u r n -
t r u e
∗
e l s e r e t u r n f a l s e
∗/
b o o l o p e r a t o r >( c o n s t Transaction& rhs) c o n s t ;
• ∗ ∗
∗ Compare a T r a n s a c t i o n with a g i v e n d a t e
∗
∗ @param d a t e Compared d a t e
2
∗ @return I f c u r r e n t T r a n s a c t i o n happened b e f o r e t h e g i v e n d a t e r e t u r n t r u e ∗ e l s e r e t u r n f a l s e
∗/
b o o l o p e r a t o r <( c o n s t time_t date) c o n s t ;
• ∗ ∗
∗ Compare a T r a n s a c t i o n with a g i v e n d a t e ∗
∗ @param d a t e Compared d a t e
∗ @return I f c u r r e n t T r a n s a c t i o n happened a f t e r t h e g i v e n d a t e r e t u r n t r u e ∗ e l s e r e t u r n f a l s e
∗/
b o o l o p e r a t o r >( c o n s t time_t date) c o n s t ;
• ∗ ∗
∗
Sum t h e
v a l u e
o f two
T r a n s a c t i o n amounts
∗
∗
@param
r h s
The
t r a n s a c t i o n
t o sum o v e r
∗
@return
The
output o f
t h e
summation i n
d o u b l e format
∗/
d o u b l e o p e r a t o r +( c o n s t Transaction& rhs) ;
• ∗ ∗
∗ Sum t h e v a l u e o f a T r a n s a c t i o n with a n o t h e r d o u b l e ∗
∗ @param add The amount t o sum o v e r
∗ @return The output o f t h e summation i n d o u b l e format
∗/
d o u b l e o p e r a t o r +( c o n s t d o u b l e add) ;
• ∗ ∗
∗ Assignment o p e r a t o r
∗
∗ @param r h s T r a n s a c t i o n t o a s s i g n
∗ @return t h i s T r a n s a c t i o n
∗/
Transaction& o p e r a t o r =( c o n s t Transaction& rhs) ;
• ∗ ∗
∗ Stream o v e r l o a d
∗
∗ What t o stream :
∗
T r a n s a c t i o n amount"tab
tab"hour : minute : second day/month/ y e a r ( i n l o c a l t i m e )
∗
∗
@param
o s Stream t o
be
used .
∗
@param
t r a n s a c t i o n
T r a n s a c t i o n t o be streamed .
∗ @return t h e c u r r e n t Stream
∗/
f r i e n d std : : ostream& o p e r a t o r <<(std : : ostream& os , c o n s t Transaction& - transaction) ;
• ;
3
3.2 Account
The account is de ned for a single user and users have their respective ids. Accounts also hold Transaction information of their user in a sorted manner.
c l a s s Account f
p r i v a t e :
i n t _id ;
Transaction∗∗ _activity ;
i n t ∗ _monthly_activity_frequency ;
p u b l i c :
• ∗ ∗
∗ Empty c o n s t r u c t o r
g i v e t h e i d a s 1
g i v e n u l l p t r f o r p o i n t e r s
∗/
Account ( ) ;
• ∗ ∗
∗ C o n s t r u c t o r
∗
∗
∗
Note : The g i v e n
a c t i v i t y
a r r a y
w i l l have
12
T r a n s a c t i o n ∗
∗
Each o f
t h e s e
T r a n s a c t i o n ∗ w i l l
r e p r e s e n t
a
month
from t h e
2019
∗
B a s i c a l y
a c t i v i t y [ 0 ]
w i l l
r e p r e s e n t January
∗
a c t i v i t y [ 1 1 ]
w i l l r e p r e s e n t
February
∗
a c t i v i t y [ 1 1 ]
w i l l r e p r e s e n t
March
∗
. . .
∗
a c t i v i t y [ 1 0 ]
w i l l r e p r e s e n t
November
∗
a c t i v i t y [ 1 1 ]
w i l l r e p r e s e n t
December
∗
a c t i v i t y [ 0 ]
w i l l o n l y
c o n t a i n T r a n s a c t i o n s happened i n January
∗
However ,
be
c a r e f u l
t h a t
T r a n s a c t i o n s
i n s i d e
o f
a c t i v i t y [ i ]
w i l l
not be
i n
-
s o r t e d o r d e r
∗
For Example : We
a r e
c e r t a i n t h a t
a c t i v i t y [ 0 ]
i s
c o n t a i n i n g
T r a n s a c t i o n s
-
happened i n January 2019
∗
But we a r e
not
s u r e which
o f
them
happened
f i r s t .
∗
I s t r o n g l y
s u g g e s t
you t o
u s e
a
s o r t i n g a l g o r i t h m
w h i l e
s t o r i n g
t h e s e
-
T r a n s a c t i o n t o
your
o b j e c t .
∗
( S o r t i n g
by
t h e
date ,
So
t h a t
you
can
d i r e c t l y
u s e
them
i n
stream
o v e r l o a d
-
)
∗
(You can
u s e
bubble
s o r t )
∗
∗
@param i d i d
o f
t h i s
Account
∗
@param
a c t i v i t y
2d
T r a n s a c t i o n
a r r a y
f i r s t
l a y e r s
l e n g h t
i s
12 f o r
each
-
month
∗
@param
m o n t h l y
a c t i v i t y f r e q u e n c y
how
many
t r a n s a c t i o n s
made i n
each month
∗/
Account( i n t id , Transaction∗∗ c o n s t activity , i n t ∗ monthly_activity_frequency -
) ;
• ∗ ∗
∗ D e s t r u c t o r
∗
∗ Do not f o r g e t t o f r e e t h e s p a c e you have c r e a t e d ( This a s s i g n m e n t d o e s not -
u s e smart p o i n t e r s )
∗/
4
~Account ( ) ;
• ∗ ∗
∗ Copy c o n s t r u c t o r ( Deep copy )
∗
∗ @param o t h e r The Account t o be c o p i e d
∗/
Account( c o n s t Account& rhs) ;
• ∗ ∗
∗
Copy
c o n s t r u c t o r ( Deep copy )
∗
∗
This
copy c o n s t r u c t o r s
t a k e s two
t i m e
t e l e m e n t s
∗
T r a n s a c t i o n s
o f
t h e
o l d
Account
w i l l
be c o p i e d
t o
new
Account
∗
i f and
o n l y
i f
they
a r e
between
t h e s e
g i v e n d a t e s
∗
Given
d a t e s
w i l l not
be
i n c l u d e d .
∗
∗
@param r h s The Account t o be c o p i e d
∗
@param
s t a r t
d a t e S t a r t i n g
d a t e
f o r t r a n s a c t i o n
t o
be
c o p i e d .
∗
@param
e n d d a t e
Ending
d a t e
f o r
t r a n s a c t i o n s t o
be
c o p i e d .
∗/
Account( c o n s t Account& rhs , time_t start_date , time_t end_date) ;
• ∗ ∗
∗ Move c o n s t r u c t o r
∗
∗ @param r h s Account which you w i l l move t h e r e s o u r c e s from
∗/
Account(Account&& rhs) ;
• ∗ ∗
∗ Move a s s i g n m e n t o p e r a t o r
∗
∗ @param r h s Account which you w i l l move t h e r e s o u r c e s from
∗/
Account& o p e r a t o r =(Account&& rhs) ;
• ∗ ∗
∗ Assignment o p e r a t o r ∗ deep copy
∗
∗ @param r h s Account t o a s s i g n
∗ @return t h i s a c c o u n t
∗/
Account& o p e r a t o r =( c o n s t Account& rhs) ;
• ∗ ∗
∗ E q u a l i t y comparison o v e r l o a d
∗
∗ This o p e r a t o r c h e c k s o n l y i d o f t h e Account ∗
∗ @param r h s The Account t o compare
∗ @return r e t u r n s t r u e i f both i d s a r e same f a l s e o t h e r v i s e
∗/
b o o l o p e r a t o r==(c o n s t Account& rhs) c o n s t ;
• ∗ ∗
∗ E q u a l i t y comparison o v e r l o a d
5
∗
∗ This o p e r a t o r c h e c k s o n l y i d o f t h e Account
∗
∗ @param i d t o compare
∗ @return r e t u r n s t r u e i f both i d s a r e same f a l s e o t h e r v i s e
∗/
b o o l o p e r a t o r==(i n t id) c o n s t ;
• ∗ ∗
∗
sum and
e q u a l o p e r a t o r
∗
Add T r a n s a c t i o n s
o f
two Accounts
∗
You have
t o add
t r a n s a c t i o n s i n
c o r r e c t
p l a c e s i n
your
a c t i v i t y a r r a y
∗
Note : Remember t h a t
a c t i v i t y [ 0 ]
i s always January
and
a c t i v i t y [ 1 1 ] i s -
always
December
∗
( This i n f o r m a t i o n a l s o h o l d s f o r
e v e r y
o t h e r month)
∗
∗
You can
have T r a n s a c t i o n s with t h e same
d a t e
∗
∗
@param r h s Account which t a k e new T r a n s a c t i o n s from
∗
@return
t h i s Account
a f t e r adding new T r a n s a c t i o n s
∗/
Account& o p e r a t o r+=(c o n s t Account& rhs) ;
• ∗ ∗
∗ How much money Account has (Sum o f T r a n s a c t i o n amounts )
∗
∗
∗ @return t o t a l amount o f t h e money o f t h e a c c o u n t
∗/
d o u b l e balance ( ) ;
• ∗ ∗
∗ How much money Account has a t t h e end o f g i v e n d a t e
∗
∗ Given d a t e w i l l not be i n c l u d e d .
∗ @param e n d d a t e You w i l l count t h e amounts u n t i l t h i s g i v e n d a t e ( not -
i n c l u s i v e )
∗ @return T o t a l amount t h e Account has u n t i l g i v e n d a t e
∗/
d o u b l e balance(time_t end_date) ;
• ∗ ∗
∗ How much money Account between g i v e n d a t e s
∗ Given d a t e s w i l l not be i n c l u d e d .
∗
∗ @param e n d d a t e You w i l l count t h e amounts between g i v e n d a t e s ( not -
i n c l u s i v e )
∗ @return T o t a l amount t h e Account has between g i v e n d a t e s
∗ You w i l l o n l y count a T r a n s a c t i o n amount i f and o n l y i f i t o c c u r e d between -
g i v e n d a t e s
∗/
d o u b l e balance(time_t start_date , time_t end_date) ;
6
• ∗ ∗
∗ Stream o v e r l o a d .
∗
∗
∗
∗ What t o stream
∗ Id o f t h e u s e r
E a r l i e s t T r a n s a c t i o n amount"tab" "tab"hour : minute : second day/month/ y e a r ( i n -
l o c a l t i m e )
∗ Second e a r l i e s t T r a n s a c t i o n amount"tab" "tab"hour : minute : second day/month/ -
y e a r ( i n l o c a l t i m e )
∗ ...
∗ L a t e s t T r a n s a c t i o n amount"tab tab"hour : minute : second day/month/ y e a r ( i n - l o c a l t i m e )
∗
∗
Note :
a c t i v i t y
a r r a y w i l l o n l y
c o n t a i n
d a t e s
from January
2019 t o -
December
2019
∗
Note :
T r a n s a c t i o n s s h o u l d
be i n
o r d e r by
d a t e
∗
Note :
e i t h e r
o f
m o n t h l y
a c t i v i t y f r e q u e n c y o r
a c t i v i t y i s
n u l l p t r
∗
you w i l l
j u s t
stream
∗1
∗ @param o s Stream t o be used .
∗ @param Account t o be streamed . ∗ @return t h e c u r r e n t Stream
∗/
f r i e n d std : : ostream& o p e r a t o r <<(std : : ostream& os , c o n s t Account& account) ;
• ;
3.3 Bank
The bank keeps track of accounts.
c l a s s Bank f
p r i v a t e :
std : : string _bank_name ;
i n t _user_count ;
Account∗ _users ;
p u b l i c :
• ∗ ∗
∗ Empty c o n s t r u c t o r
g i v e t h e bank name a s " n o t d e f i n e d "
g i v e n u l l p t r f o r p o i n t e r s
g i v e 0 a s u s e r s c o u n t
∗/
Bank ( ) ;
• ∗ ∗
∗ C o n s t r u c t o r
∗
∗
∗
@param
bank name name o f
t h i s
bank
∗
@param
u s e r s
p o i n t e r t o
h o l d
u s e r s
o f
t h i s
bank
∗
@param
u s e r
c o u n t number
o f u s e r s
t h i s
bank
has
∗/
7
Bank(std : : string bank_name , Account∗ c o n s t users , i n t user_count) ;
• ∗ ∗
∗ D e s t r u c t o r
∗
∗ Do not f o r g e t t o f r e e t h e s p a c e you have c r e a t e d ( This a s s i g n m e n t d o e s not - u s e smart p o i n t e r s )
∗/
~Bank ( ) ;
• ∗ ∗
∗ Copy c o n s t r u c t o r ( Deep copy )
∗
∗ @param r h s The Bank t o be c o p i e d
∗/
Bank( c o n s t Bank& rhs) ;
• ∗ ∗
∗ You s h o u l d deep copy t h e c o n t e n t o f t h e s e co n d bank
∗ Merge two banks
∗ I f both banks has a u s e r with t h e same id , T r a n s a c t i o n s o f t h e s e u s e r s - w i l l be merged i n t o t h e same Account
∗ For example :
∗
Bank1
has
[1 ,2]
i d u s e r s
∗
Bank2
has
[2 ,3]
i d u s e r s
∗
∗
Bank1
a f t e r
+=
o p e r a t o r
w i l l have [ 1 , 2 , 3 ]
i d u s e r s
∗
User
with
i d
2
w i l l
have
i t s t r a n s a c t i o n s
h i s t o r i e s merged
∗
∗
T r a n s a c t i o n s
with o f
t h e
u s e r s with t h e same i d s h o u l d be merged and -
updated
∗ @param r h s Merged Bank
∗ @return t h i s Bank
∗/
Bank& o p e r a t o r+=(c o n s t Bank& rhs) ;
• ∗ ∗
∗ Add a new a c c o u n t t o Bank
∗
I f t h e newly added u s e r a l r e a d y e x i s t s i n t h i s Bank merge t h e i r -
T r a n s a c t i o n s
∗
∗ @param new acc new a c c o u n t t o add t o bank
∗ @return t h i s Bank
∗/
Bank& o p e r a t o r+=(c o n s t Account& new_acc) ;
/∗ ∗ I n d e x i n g o p e r a t o r o v e r l o a d
∗
∗
Return t h e
Account with
t h e
g i v e n i d
∗
∗
I f t h e r e
i s
no Account
with
t h e g i v e n
i d r e t u r n
t h e f i r s t
e l e m e n t
∗
∗
@param a c c o u n t i d
i d
o f
t h e
Account
∗
@return
i f
g i v e n
i d
e x i s t i n
t h e bank
r e t u r n t h e
account ,
e l s e r e t u r n an -
a c c o u n t with i d= 1
∗
∗/
Account& o p e r a t o r [ ] ( uint account_id) ;
8
• ∗ ∗
∗ Stream o v e r l o a d .
a l l t h e a c c o u n t s w i l l be between 01 01 2019 and 31 12 2019
∗ What t o stream
∗
bank name"tab"number
o f u s e r s
who
a r e
e l i g i b l e
f o r
a l o a n "tab" t o t a l
-
b a l a n c e
o f
t h e
bank
∗
∗
A u s e r
i s
s a f e f o r
a
l o a n
i f
and
o n l y
i f
t h a t
u s e r
d i d
not
have any
-
n e g a t i v e b a l a n c e
f o r
2 o r more
c o n s e c u t i v e months
∗
For
example ,
l e t ' s
say our
bank
named
a s
"banana"
has
two
u s e r s
∗
∗
User
A ' s
b a l a n c e
f o r
each
month
i s
a s
g i v e n
∗
∗
January
0
∗
February
0
∗
March
100
∗
A p r i l
20
∗
May
30
∗
June
40
∗
J u l y
60
∗
August
0
∗
September
0
∗
October
0
∗
November
0
∗
December
0
∗
∗
This
u s e r
i s
not
e l i g i b l e
b e c a u s e
i n
A p r i l and
May
h i s / h e r
b a l a n c e
was -
n e g a t i v e ( c o n s e c u t i v e )
∗
You
s t i l l
have
t o
add
150
t o
t h e t o t a l
b a l a n c e
o f
t h e
bank
∗
User
B ' s
b a l a n c e
f o r
each
month i s a s
g i v e n
∗
∗
January
0
∗
February
0
∗
March
100
∗
A p r i l
20
∗
May
40
∗
June
30
∗
J u l y
60
∗
August
0
∗
September
0
∗
October
0
∗
November
0
∗
December
0
∗
∗
This u s e r
i s e l i g i b l e
b e c a u s e
n e g a t i v e
b a l a n c e s
were
not c o n s e c u t i v e
∗
You
w i l l
a l s o
add
150
t o
t h e
t o t a l b a l a n c e o f t h e
bank
∗
∗ your output w i l l be a s
∗ banana 1 300
∗/
f r i e n d std : : ostream& o p e r a t o r <<(std : : ostream& os , c o n s t Bank& bank) ;
• ;
9
• 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 les. (Some text editors might display tab character in a di erent 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
• 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 e ective 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.
• Regulations
◦ Programming Language: You must code your program in C++ (11). Your submission will be compiled with g++ with -std=c++11 ag 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.
10
• 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.
• Submission
Submission will be done via CengClass. Create a zip le named hw3.zip that contains:
• Transaction.cpp
• Account.cpp
• Bank.cpp
Do not submit a le that contains a main function. Such a le will be provided and your code will be compiled with it. Also, do not submit a Make le.
Note: The submitted zip le 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
11