I am having trouble writing the individual files for part 1, which is as follows:
part 1
Purpose
This assignment reviews object-oriented programming concepts such as classes, methods,
constructors, accessor methods, and access modifiers. It makes use of an array of objects as a
class data member, and introduces the concept of object serialization or \"binary I/O\".
Set Up
(Yes, these commands are rather tedious to type repeatedly. Part 2 of this assignment introduces
a new technique for compiling and linking your program files called a makefile. Makefiles
require a bit more work up front, but save a lot of typing at the command line once the makefile
has been created.)
As in Assignment 1, you should create a subdirectory to hold your files for Assignment 2.
In that directory, make a symbolic link to the data file for this part of the assignment:
In this assignment, you will be creating several source code and header files, as described below.
You can create each of these files separately using the nano editor, just as you did on Assignment
1.
To compile and link the program you\'ve created, type:
Once you\'ve added the AccountDB class, you should type:
To run the executable file created by the previous command, type:
Program
For this assignment, you will need to write three source code files as well as two header files.
Each of these files is relatively short, but many inexperienced programmers are overwhelmed by
the idea of writing a program as multiple files. \"Where do I start?!!\" is a common refrain. This
assignment sheet attempts to walk you through the steps of writing a multi-file program.
The steps outlined below should not be thought of as a purely linear process, but rather an
iterative one - For example, work a little on Step 1, then a little on Step 2, then test what you\'ve
written (Step 3).
Step 1: Write the CreditAccount class declaration
The CreditAccount class represents information about a credit card account. The code for the
CreditAccount class will be placed in two separate files, which is the norm for non-template C++
classes.
The header file for a class contains the class declaration, including declarations of any data
members and prototypes for the methods of the class. The name of the header file should be of
the form ClassName.h (for example, CreditAccount.h for the header file of theCreditAccount
class).
A skeleton for the CreditAccount.h file is given below. As shown, a header file should begin and
end with header guards to prevent it from accidentally being #included more than once in the
same source code file (which would produce duplicate symbol definition errors). The symbol
name used in the header guards can be any valid C++ name that is not already in use in your
program or the C/C++ libraries. Using a name of the format CLASSNAME_H (like
CREDIT_ACCOUNT_H in the code below) is recommended to avoid naming conflicts.
Data Members
The CreditAccount class should have the following private data members:
an accoun.
I am having trouble writing the individual files for part 1, which i.pdf
1. I am having trouble writing the individual files for part 1, which is as follows:
part 1
Purpose
This assignment reviews object-oriented programming concepts such as classes, methods,
constructors, accessor methods, and access modifiers. It makes use of an array of objects as a
class data member, and introduces the concept of object serialization or "binary I/O".
Set Up
(Yes, these commands are rather tedious to type repeatedly. Part 2 of this assignment introduces
a new technique for compiling and linking your program files called a makefile. Makefiles
require a bit more work up front, but save a lot of typing at the command line once the makefile
has been created.)
As in Assignment 1, you should create a subdirectory to hold your files for Assignment 2.
In that directory, make a symbolic link to the data file for this part of the assignment:
In this assignment, you will be creating several source code and header files, as described below.
You can create each of these files separately using the nano editor, just as you did on Assignment
1.
To compile and link the program you've created, type:
Once you've added the AccountDB class, you should type:
To run the executable file created by the previous command, type:
Program
For this assignment, you will need to write three source code files as well as two header files.
Each of these files is relatively short, but many inexperienced programmers are overwhelmed by
the idea of writing a program as multiple files. "Where do I start?!!" is a common refrain. This
assignment sheet attempts to walk you through the steps of writing a multi-file program.
The steps outlined below should not be thought of as a purely linear process, but rather an
iterative one - For example, work a little on Step 1, then a little on Step 2, then test what you've
written (Step 3).
Step 1: Write the CreditAccount class declaration
The CreditAccount class represents information about a credit card account. The code for the
CreditAccount class will be placed in two separate files, which is the norm for non-template C++
classes.
The header file for a class contains the class declaration, including declarations of any data
members and prototypes for the methods of the class. The name of the header file should be of
the form ClassName.h (for example, CreditAccount.h for the header file of theCreditAccount
2. class).
A skeleton for the CreditAccount.h file is given below. As shown, a header file should begin and
end with header guards to prevent it from accidentally being #included more than once in the
same source code file (which would produce duplicate symbol definition errors). The symbol
name used in the header guards can be any valid C++ name that is not already in use in your
program or the C/C++ libraries. Using a name of the format CLASSNAME_H (like
CREDIT_ACCOUNT_H in the code below) is recommended to avoid naming conflicts.
Data Members
The CreditAccount class should have the following private data members:
an account number (a char array with room for 19 characters PLUS the null character, i.e. 20
elements total)
a customer name (a char array with room for 20 characters PLUS the null character)
a credit limit (a double variable)
a current account balance (a double variable)
Note: Make that sure you code your data members in THE EXACT ORDER LISTED ABOVE
and with THE EXACT SAME DATA TYPES. If you use float instead of double or only make
the name array 20 characters long instead of 21, your final program will not work correctly!
C++11 Initialization Option for Data Members
C++11 adds the ability to initialize the non-static data members of a class at the time you declare
them using a "brace-or-equal" syntax. This is very convenient, and can eliminate most or all of
the code from your default constructor. Here are a few examples of the kind of initializations you
can do in a class declaration:
Feel free to use this option if you want to.
Method Prototypes
The CreditAccount class declaration should (eventually) contain public prototypes for all of the
methods in the CreditAccount.cpp source code file described in Step 2 below.
Step 2: Write the CreditAccount class implementation
The source code file for a class contains the method definitions for the class. The name of the
source code file should be of the form ClassName.cpp or ClassName.cc (for example,
CreditAccount.cpp for the source code file of the CreditAccount class).
The CreditAccount class implementation should (eventually) contain definitions for all of the
methods described below. Make sure to #include "CreditAccount.h" at the top of this file.
CreditAccount default constructor - This "default" constructor for the CreditAccount class takes
no parameters. Like all C++ constructors, it does not have a return data type.
This method should set the account number and customer name data members to "null strings".
This can be done by copying a null string literal ("") into the character array using strcpy() or
3. by setting the first element of the array to a null character ('0'). The credit limit and account
balance data members should be set to 0.
(If you're working in C++11 and you initialized the data members at declaration as described
above under C++11 Initialization Option for Data Members, this method's body can be empty.
You still need to code the method though, even though it won't actually do anything.)
Alternate CreditAccount constructor - Write another constructor for the CreditAccount class that
takes four parameters: 1) a character array that contains a new account number, 2) a character
array that contains a new customer name, 3) a double variable that contains a new credit limit,
and 4) a double variable that contains a new balance. DO NOT GIVE THESE PARAMETERS
THE SAME NAMES AS YOUR DATA MEMBERS.. Like all C++ constructors, this
constructor does not have a return data type.
Use strcpy() to copy the new account number parameter into the account number data member
and the new customer name parameter into the customer name data member. The new credit
limit and new account balance parameters can be assigned to the corresponding data members.
getAccountNumber() - This accessor method takes no parameters. It should return the account
number data member. In C++, the usual return data type specified when you are returning the
name of a character array is char* or "pointer to a character" (since returning an array's name
will convert the name into a pointer to the first element of the array, which in this case is data
type char.
getName() - This accessor method takes no parameters. It should return the customer name data
member.
getLimit() - This accessor method takes no parameters. It will have a return data type of double.
It should return the credit limit data member.
getBalance() - This accessor method takes no parameters. It will have a return data type of
double. It should return the account balance data member.
processPayment() - This method takes one parameter, a double payment amount. It returns
nothing. The payment amount should be subtracted from the current account balance data
member. (Note that it is possible to have a negative balance on an account as a result of
overpayment.)
processCharge() - This method takes one parameter, a double charge amount. It returns a
Boolean value. If the charge amount plus the current account balance is greater than the credit
limit, the method should return false without altering the balance (because this charge has been
declined). Otherwise, the charge amount should be added to the account balance and the method
should return true.
print() - This method takes no parameters and returns nothing. The method should print the
values of the data members for the account in a format similar to the following:
4. If the balance is negative, it should be printed as a positive number but followed by the letters
"CR" (for credit). For example, the sample output shown above is for an account with a balance
of -127.65.
Step 3: Test and debug the CreditAccount class
As you write your declaration and implementation of the CreditAccount class, you should begin
testing the code you've written. Create a basic main program called assign2.cpp that tests your
class. This is not the final version of assign2.cpp that you will eventually submit. In fact, you'll
end up deleting most (or all) of it by the time you're done with the assignment. An example test
program is given below.
You do not have to have written all of the methods for the CreditAccount class before you begin
testing it. Simply comment out the parts of your test program that call methods you haven't
written yet. Write one method definition, add its prototype to the class declaration, uncomment
the corresponding test code in your test program, and then compile and link your program. If you
get syntax errors, fix them before you attempt to write additional code. A larger amount of code
that does not compile is not useful - it just makes debugging harder! The goal here is to
constantly maintain a working program.
Once your CreditAccount class has been thoroughly tested and debugged, it's time to write the
second class for this assignment.
Step 4: Write the AccountDB class declaration
The AccountDB class represents a database of CreditAccount objects. Like the CreditAccount
class, the code for this class will be placed in two separate files.
Place the class declaration in a header file called AccountDB.h. Like the file CreditAccount.h
you wrote in Step 1, this file should begin and end with header guards to prevent it from
accidentally being #included more than once in the same source code file.
After the header guard at the top of the file but before the class definition, make sure to #include
"CreditAccount.h".
Data Members
The AccountDB class should have the following two private data members:
An array of 20 CreditAccount objects
An integer that specifies the number of CreditAccount objects actually stored in the array
Note: Once again, make sure you code your data members in THE EXACT ORDER LISTED
ABOVE and with THE EXACT SAME DATA TYPES.
Method Prototypes
The AccountDB class declaration should (eventually) contain public prototypes for all of the
methods in the AccountDB.cpp source code file described in Step 5 below.
Step 5: Write the AccountDB class implementation
5. The AccountDB class implementation should (eventually) contain definitions for all of the
methods described below. Make sure to #include "AccountDB.h" at the top of this file.
AccountDB default constructor - This "default" constructor for the AccountDB class takes no
parameters. Like all C++ constructors, it does not have a return data type.
This constructor is called to create an empty database, so this method should set the number of
accounts data member to 0.
(As with the CreditAccount class, if you initialize the number of accounts data member to 0
when you declare it, this method's body can be empty. You still need to code the method with an
empty body.)
Alternate AccountDB constructor - Write a second constructor for the AccountDB class that
takes one parameter: a C++ string that contains the name of an existing database file. Like all
C++ constructors, this constructor does not have a return data type.
This constructor should do the following:
Declare an input file stream variable (the code below assumes it is named inFile).
Open the file stream for binary input. Check to make sure the file was opened successfully as
usual.
Read the database file into your AccountDB object. You can do this with a single statement:
Close the file stream.
print() - This method takes no parameters and returns nothing.
This method should first print a descriptive header line (e.g., "Credit Card Account Listing"). It
should then loop through the array of CreditAccount objects and print each of the elements that
contains account data, with a blank line between each account. Here we see some of the power of
object-oriented programming: since each element of the array is an object, we can call a method
for that object. We've already written a print() method for the CreditAccount class, so printing
an element of the account array is as easy as calling print() for the array element. The syntax for
calling a method using an array element that is an object is pretty straightforward:
Step 6: Write the main program
Since most of the logic of the program is embedded in the two classes you wrote, the main()
routine logic is extremely simple.
Create an AccountDB object using the alternate constructor you wrote. Pass the filename string
literal "accounts" as an argument to the constructor.
Call the print() method for the AccountDB object.
here is part 2 and what I have coded for it:
Part 2
6. Purpose
This part of the assignment introduces the insertion sort and binary search algorithms. It also
introduces the use of makefiles for compiling and linking your programs.
Set Up
In your Assignment 2 directory, make a symbolic link to the data file for this part of the
assignment:
In this assignment, you will be creating several source code and header files, as described below.
You can create each of these files separately using the nano editor, just as you did on Assignment
1.
Once you've written a makefile for your program, you can compile and link it by simply typing:
Running the executable file is unchanged from Part 1.
To remove the executable and object code files for your program (conserving disk space), you
can type:
Program
For this part of the assignment, you'll need to write one new file and then modify two of the files
you wrote for Part 1.
Step 1: Write a makefile
The file named makefile tells the make utility how to build the final executable file from your
collection of C++ source code and header files. The makefile for this assignment is given in its
entirety below. Makefiles for future assignments will follow this basic pattern.
IMPORTANT NOTE: The commands that appear in the makefile below MUST be indented as
shown. Furthermore, the indentation MUST be done using a tab character, not spaces. If you
don't indent your makefile commands, or indent using spaces, your makefile WILL NOT
WORK.
Once you've written the file makefile, try using the make utility to compile and link your
program.
Step 2: Add the following methods to the AccountDB class
sortAccounts() - This method takes no parameters and returns nothing.
This method should sort the array of CreditAccount objects in ascending order by account
number using the insertion sort algorithm.
The sort code linked to above sorts an array of integers called numbers of size size. You will
need to make a substantial number of changes to that code to make it work in this program:
This will be a method, not a function. Change the parameters for the method to those described
above.
In the method body, change the data type of bucket to CreditAccount. This temporary storage
will be used to swap elements of the array of CreditAccount objects.
7. In the method body, change any occurrence of numbers to the name of your array of
CreditAccount objects and size to numAccounts (or whatever you called the data member that
tracks the number of valid CreditAccount objects stored in the array).
The comparison of accountArray[j-1] and bucket will need to use the C string library function
strcmp() to perform the comparison. Also, you'll need to use the getAccountNumber() method to
access the accountNumber data member within each CreditAccountobject. The final version of
the inner for loop should look something like this:
It is legal to assign one CreditAccount object to another; you don't need to write code to copy
individual data members.
Add a call to the sortAccounts() method to the end of the alternate constructor you wrote for the
AccountDB class. This will sort the array of CreditAccount objects that were read in from the
input file.
searchForAccount() - This method should take one parameter: a character array containing the
account number code of the CreditAccount to search for (searchNumber). The method should
return an integer.
The logic for this method is a variation of the binary search of a sorted list strategy.
As usual, you'll need to use strcmp() to perform the comparison of account numbers.
processTransactions() - This method should take one parameter: a C++ string containing the
name of a file of credit card transaction data. The method should return nothing.
The method should open the specified transaction file for input (not binary input - this file is a
text file). Make sure to test that the file was opened successfully; if it wasn't, print an error
message and exit the program.
Before reading any transactions, the function should print a report header and column headers.
The function should then read transaction data from the file until end of file is reached. A typical
transaction is shown below. The first field on the transaction record is the date of the transaction,
followed by an account number, then the transaction type ('C' for charge or 'P' for payment),
and finally a transaction amount.
Once all of the data for a given transaction has been read, call the searchForAccount() method to
search the accounts array for the account number given in the transaction. If the account number
is present in the array, then the transaction may be processed. For a payment, simply call the
processPayment() method for the object that contains a matching account number, passing it the
transaction amount. For a charge, call the processCharge() method for the object that contains a
matching account number, passing it the transaction amount.
For each transaction record processed, print a line in a transaction report with the data from the
record and the updated balance for that account. If the balance is negative, it should be printed as
a positive number but followed by the letters "CR". If the transaction account number was not
8. found in the account array or if a charge exceeded the account's credit limit (i.e., if the
processCharge() method returned false), print an appropriate error message instead of the
account balance.
After all transactions have been processed, close the transaction file.
Step 3: Add two method calls to the main program
The main() routine logic will now look like this:
Create an AccountDB object using the alternate constructor you wrote. Pass the filename string
"accounts" as an argument to the constructor.
Call the print() method for the AccountDB object.
Call the processTransactions() method for the AccountDB object. Pass the filename string
"transactions.txt" as an argument to the method.
Call the print() method for the AccountDB object.
And what I have for part 2, though it may not be correct:
# PROGRAM: assign2
# PROGRAMMER:
# LOGON ID:
# DATE DUE:
#
#include
#include
#include
#include
#define SIZE 20
#define ACCOUNTS /home/turing/t90kjm1/CS241/Data/Spring2017/Assign2/accounts
#define TRANS /home/turing/t90kjm1/CS241/Data/Spring2017/Assign2/transactions.txt
using namespace std;
class CreditAccount
{
public:
CreditAccount();
char* get_accountNumber();
float get_balance();
void process_payment( float );