rainbowpride89

C++ help

Mar 26th, 2012
790
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.15 KB | None | 0 0
  1. I need some help with a code that I am working on. Here is what it is suppose to do:
  2.     Description of the program: Write an application that serves as a personal account manager that manages multiple accounts for the user. To keep multiple accounts, you are required to use a list.
  3. Specifications:-** Modify the Account class.** - add an additional data number that represents the (unique) account number. This data member will be used as the key to locate the account. Next modify the constructor(s) and display method. - add a new method getNumber to return the account number. - overload the pipe operator (<<) to display the account information. - overload the equal-to operator (==) to compare two accounts based on the account number. - overload the assignment operator (=) to assign (copy) one account to another.
  4. This application should also have a menu-driven interface. The application is responsible to produce proper messages and inform the user whether the particular transaction is successful or not. Can define own fuctions to facilitate this application. The menu should contain the following options: Exit: terminate the application Open an account: user will be asked for the opening balance (with interest rate fixed) and the (unique) account number, and a new account will be created. If there is no room in the list, try to double the size of the list and open the account again. Check an account: user will be prompted for the account number, and the information of the account (if exists) is displayed. Close an account: user will be prompted for the account number and the account will be closed. Check all accounts: all accounts' information will be displayed. Deposit: user will be asked for a specific account and the amount to deposit.
  5. Withdraw: user will be asked for a specific account and the amount to withdraw. Transfer: user will be prompted for the account number to transfer from, the account number to transfer to, and the amount of the transfer.
  6. Need to use list using dynamic array (ListDA) to implement this application. Start with the default size(CAPACITY) of 2. Need to create a new solution (Personal Account Manager Solution), a new project ( Personal Account Manager Project), and a source file ( main.cpp) that uses Account and ListDA objects. In order to use those objects need to manually copy the class files (header and implementation files) of both classes into project.
  7.  
  8. I have five files with code. I will post them and the code in each now.
  9.  
  10. main.cpp contains this code:
  11.  
  12.     #include "stdafx.h"
  13. #include <iostream>
  14. #include <iomanip>
  15. #include "Account.h"
  16. #include "ListDA.h"
  17. using namespace std;
  18.  
  19.  
  20. int main()
  21. {
  22.     int choice;
  23.     int openBal;
  24.     int accountNum;
  25.     int accountNums;
  26.     int depositAmt;
  27.     int withdrawAmt;
  28.     int transferAmt;
  29.  
  30.  
  31.     cout << "1. Open an account. " << endl;
  32.     cout << "2. Check an account. " << endl;
  33.     cout << "3. Close an account. " << endl;
  34.     cout << "4. Check all accounts. " << endl;
  35.     cout << "5. Deposit. " << endl;
  36.     cout << "6. Withdraw. " << endl;
  37.     cout << "7. Transfer. " << endl;
  38.     cout << "8. Exit. " << endl;
  39.    
  40.     cout << " Please make a selcetion from the choices above (1-8): ";
  41.     cin >> choice;
  42.     cout << endl;
  43.  
  44. switch (choice){
  45.     case 1:
  46.         cout << "Please enter the opening balence: $ ";
  47.         cin >> openBal;
  48.         cout << endl;
  49.         cout << "Please enter the account number: ";
  50.         cin >> accountNum;
  51.         cout << endl;
  52.         break;
  53.     case 2:
  54.         cout << "Please enter the account number: ";
  55.         cin >> accountNum;
  56.         cout << endl;
  57.         break;
  58.     case 3:
  59.         cout << "Please enter the account number: ";
  60.         cin >> accountNum;
  61.         cout << endl;
  62.         break;
  63.     case 4:
  64.         cout << "Please enter all the account numbers to show: ";
  65.         cin >> accountNums;
  66.         cout << endl;
  67.         break;
  68.     case 5:
  69.         cout << "Please enter the account number you wish to deposit money into: ";
  70.         cin >> accountNum;
  71.         cout << endl;
  72.         cout << "Please enter how much money you want to deposit into the account: $ ";
  73.         cin >> depositAmt;
  74.         cout << endl;
  75.         break;
  76.     case 6:
  77.         cout << "Please enter the account number you wish to make a withdraw from: ";
  78.         cin >> accountNum;
  79.         cout << endl;
  80.         cout << "Please enter the amount you wish to withdraw from the account: $ ";
  81.         cin >> withdrawAmt;
  82.         cout << endl;
  83.         break;
  84.     case 7:
  85.         cout << "Please enter the account number you wish to transfer money from: ";
  86.         cin >> accountNum;
  87.         cout << endl;
  88.         cout << "Please enter the account number you wish to transfer the money to: ";
  89.         cin >> accountNum;
  90.         cout << endl;
  91.         cout << "Please enter the amount of money you want to transfer: $ ";
  92.         cin >> transferAmt;
  93.         cout << endl;
  94.         break;
  95.     case 8:
  96.         cout << "Thank you! Come back soon. ";
  97.         cout << endl;
  98.         break;
  99.     default:
  100.         cout << "That was an invaild choice. " << endl;
  101.         cout << "Please make a selection (1-8): ";
  102.         cin >> choice;
  103.         cout << endl;
  104. } //end case
  105.  
  106. system ("pause");
  107.  
  108. return 0;
  109.  
  110. } // end main
  111.  
  112. Account.h contains this code:
  113.  
  114.     #pragma once
  115. #ifndef _ACCOUNT_
  116. #define _ACCOUNT_
  117.  
  118. // default values
  119. #define START_BALANCE 500.0
  120. #define START_INTEREST_RATE 0.0175
  121. // define Account class
  122. class Account
  123. {
  124. private:
  125.     double balance;
  126.     double interestRate;
  127. public:
  128.     // constructor with default values
  129.     // set to default values if value(s) is/are not valid
  130.     Account(double initialBalance, double initialInterestRate);
  131.     // observers
  132.     double getBalance() const; // return balance
  133.     double getInterestRate() const; // return interestRate
  134.     void displayAccountInformation() const; // display formatted account information through cout
  135.     // transformers
  136.     bool deposit(double amount);
  137.     // disposit amount into account
  138.     // return false if unsuccessful (amount invalid, balance not changed)
  139.     //        true otherwise
  140.     bool withdraw(double amount);
  141.     // withdraw amount from account
  142.     // return false if unsuccessful (amount invalid, balance not changed)
  143.     //        true otherwise
  144.     void applyInterest(); // apply interestRate to balance
  145.     bool setInterestRate(double newRate);
  146.     // change interestRate to newRate
  147.     // return false if unsuccessful (newRate invalid, interestRate not changed)
  148.     //        true otherwise
  149.     bool setBalance(double newBalance);
  150.     // change balance to newBalance
  151.     // return false if unsuccessful (newBalance invalid, balance not changed)
  152.     //        true otherwise
  153. }; // end Account
  154.  
  155. #endif
  156.  
  157. Account.cpp contains this code:
  158.  
  159.     #include "stdafx.h"
  160. #include <iostream>
  161. #include <iomanip>
  162. #include "Account.h"
  163. #include "ListDA.h"
  164. using namespace std;;
  165.  
  166. // constructor with default values
  167. // set to default values if value(s) is/are not valid
  168. Account::Account(double initialBalance = START_BALANCE, double initialInterestRate = START_INTEREST_RATE)
  169. {
  170.     if (initialBalance >= 0)
  171.         balance = initialBalance;
  172.     else // initialBalance < 0
  173.         balance = START_BALANCE;
  174.     if (initialInterestRate >= 0 && initialInterestRate < 1)
  175.         interestRate = initialInterestRate;
  176.     else // initialInterestRate >= 1 or initialInterestRate < 0
  177.         interestRate = START_INTEREST_RATE;
  178. } // end Account
  179.  
  180. // observers
  181. double Account::getBalance() const
  182. // return balance
  183. {
  184.     return balance;
  185. } // end getBalance
  186.  
  187. double Account::getInterestRate() const
  188. // return interestRate
  189. {
  190.     return interestRate;
  191. } // end getInterestRate
  192.  
  193. void Account::displayAccountInformation() const
  194. // display formatted account information through cout
  195. {
  196.     cout << "Account Information" << endl
  197.          << "Current Balance: $" << fixed << setprecision(2) << balance << endl
  198.          << "Interest Rate: " << interestRate * 100 << "%" << endl;
  199. } // end displayAccountInformation
  200.  
  201. // transformers
  202. bool Account::deposit(double amount)
  203. // disposit amount into account
  204. // return false if unsuccessful (amount invalid, balance not changed)
  205. //        true otherwise
  206. {
  207.     if (amount < 0) // invalid amount
  208.         return false;
  209.     balance += amount;
  210.     return true;
  211. } // end deposit
  212.  
  213. bool Account::withdraw(double amount)
  214. // withdraw amount from account
  215. // return false if unsuccessful (amount invalid, balance not changed)
  216. //        true otherwise
  217. {
  218.     if (amount < 0 || amount > balance) // invalid amount
  219.         return false;
  220.     balance -= amount;
  221.     return true;
  222. } // end withdraw
  223.  
  224. void Account::applyInterest()
  225. // apply interestRate to balance
  226. {
  227.     balance *= 1.0 + interestRate;
  228. } // end applyInterest
  229.  
  230. bool Account::setInterestRate(double newRate)
  231. // change interestRate to newRate
  232. // return false if unsuccessful (newRate invalid, interestRate not changed)
  233. //        true otherwise
  234. {
  235.     if (newRate < 0 || newRate >= 1) // invalid newRate
  236.         return false;
  237.     interestRate = newRate;
  238.     return true;
  239. } // end setInterestRate
  240.  
  241. bool Account::setBalance(double newBalance)
  242. // change balance to newBalance
  243. // return false if unsuccessful (newBalance invalid, balance not changed)
  244. //        true otherwise
  245. {
  246.     if (newBalance < 0) // invalid balance
  247.         return false;
  248.     balance = newBalance;
  249.     return true;
  250. } // end setBalance
  251.  
  252. ListDA.h contains this code:
  253.  
  254.     // basic assumptions
  255. // 1. no duplication in the list
  256. // 2. user is resposible to check the status of the list
  257. //    before using any transformer method
  258.  
  259. #pragma once
  260. #ifndef _LIST_DA_
  261. #define _LIST_DA_
  262.  
  263. #include <iostream>
  264. #include <iomanip>
  265. using namespace std;
  266.  
  267. const int CAPACITY = 2; // default size
  268.  
  269. // flexible element type
  270. // user is responsible for overloading
  271. // assignment (=), equal (==), and pipe (<<) operators
  272. typedef int elemType;
  273.  
  274. class ListDA
  275. {
  276. private:
  277.     elemType* list; // pointer to the array to hold items
  278.     int size; // number of items in this list
  279.     int maxSize; // capacity of the array
  280.  
  281.     // utility methods
  282.     int find(const elemType& item) const;
  283.     // returns position of item in this list if present
  284.     //         -1 otherwise
  285. public:
  286.     // constructors
  287.     ListDA(void); // default constructor
  288.     ListDA(int size); // general constructor
  289.     ListDA(const ListDA& aList); // copy constructor
  290.    
  291.     // observers
  292.     bool isEmpty() const;
  293.     // returns true if this list is empty
  294.     //         false otherwise
  295.  
  296.     bool isFull() const;
  297.     // returns true if this list is full
  298.     //         false otherwise
  299.  
  300.     int getSize() const;
  301.     // returns the number of items in this list
  302.  
  303.     void display(ostream& out) const;
  304.     // displays every item in this list through out
  305.  
  306.     bool retrieve(elemType& item) const;
  307.     // retrieves item from this list
  308.     // returns true if item is present in this list and
  309.     //              element in this list is copied to item
  310.     //         false otherwise
  311.  
  312.     // transformers
  313.     void insert(const elemType& item);
  314.     // inserts item into this list
  315.     // preconditions: list is not full and
  316.     //                item not present in this list
  317.     // postcondition: item is in this list
  318.  
  319.     void remove(elemType& item);
  320.     // removes item from this list
  321.     // postcondition: item is no longer in this list
  322.  
  323.     bool changeSize(int newSize);
  324.     // changes the capacity to newSize
  325.     // all items in this list is retained
  326.     // returns true if successful
  327.     // returns false if no memory is available, or
  328.     //                  the newSize is smaller than size
  329.  
  330.     // destructor
  331.     // default destructor
  332.     ~ListDA();
  333. }; // end ListSA
  334.  
  335. #endif
  336.  
  337. ListDA.cpp contains this code:
  338.  
  339.     #include "stdafx.h"
  340. #include <iostream>
  341. #include <iomanip>
  342. #include "Account.h"
  343. #include "ListDA.h"
  344. using namespace std;
  345.  
  346. // utility methods
  347. int ListDA::find(const elemType& item) const
  348. // returns position of item in this list if present
  349. //         -1 otherwise
  350. {
  351.     int pos = -1;
  352.     for (int i = 0; i < size && pos < 0; i++)
  353.         if (list[i] == item)
  354.             pos = i;
  355.     return pos;
  356. } // end
  357.  
  358. // constructors
  359. ListDA::ListDA(void) // default constructor
  360. {
  361.     list = new elemType[CAPACITY];
  362.     maxSize = CAPACITY;
  363.     size = 0;
  364. } // default constructor
  365.  
  366. ListDA::ListDA(int aSize) // general constructor
  367. {
  368.     list = new elemType[aSize];
  369.     maxSize = aSize;
  370.     size = 0;
  371. }
  372.  
  373. ListDA::ListDA(const ListDA& aList) // copy constructor
  374. {
  375.     maxSize = aList.maxSize;
  376.     list = new elemType[maxSize];
  377.     size = aList.size;
  378.     for (int i = 0; i < size; i++)
  379.         list[i] = aList.list[i];
  380. } // copy constructor
  381.  
  382. // observers
  383. bool ListDA::isEmpty() const
  384. // returns true if this list is empty
  385. //         false otherwise
  386. {
  387.     return size == 0;
  388. } // end isEmpty
  389.  
  390. bool ListDA::isFull() const
  391. // returns true if this list is full
  392. //         false otherwise
  393. {
  394.     return size == maxSize;
  395. } // end isFull
  396.  
  397. int ListDA::getSize() const
  398. // returns the number of items in this list
  399. {
  400.     return size;
  401. } // end getSize
  402.  
  403. void ListDA::display(ostream& out) const
  404. // displays every item in this list through out
  405. {
  406.     for (int i = 0; i < size; i++)
  407.         out << setw(3) << (i+1) << ". " << list[i] << endl;
  408. } // end display
  409.  
  410. bool ListDA::retrieve(elemType& item) const
  411. // retrieves item from this list
  412. // returns true if item is present in this list and
  413. //              element in this list is copied to item
  414. //         false otherwise
  415. {
  416.     int pos = find(item);
  417.     if (pos < 0)
  418.         return false;
  419.     else // post >= 0
  420.     {
  421.         item = list[pos];
  422.         return true;
  423.     }
  424. } // end retrieve
  425.  
  426. // transformers
  427. void ListDA::insert(const elemType& item)
  428. // inserts item into this list
  429. // preconditions: list is not full and
  430. //                item not present in this list
  431. // postcondition: item is in this list
  432. {
  433.     list[size++] = item;
  434. } // end insert
  435.  
  436. void ListDA::remove(elemType& item)
  437. // removes item from this list
  438. // postcondition: item is no longer in this list
  439. {
  440.     int pos = find(item);
  441.     if (pos >= 0)
  442.         list[pos] = list[--size];
  443. } // end remove
  444.  
  445. bool ListDA::changeSize(int newSize)
  446. // changes the capacity to newSize
  447. // all items in this list is retained
  448. // returns true if successful
  449. // returns false if no memory is available, or
  450. //                  the newSize is smaller than size
  451. {
  452.     if (size > newSize) // newSize cannot hold all items
  453.         return false;
  454.     if (maxSize == newSize)
  455.         return true;
  456.     elemType* newList = new elemType[newSize];
  457.     if (newList == 0) // memory allocation failed
  458.         return false;
  459.     for (int i = 0; i < size; i++) // move items
  460.         newList[i] = list[i];
  461.     maxSize = newSize;
  462.     delete [] list; // get rid of old space
  463.     list = newList; // point to new list
  464.     return true;
  465. } // end changeSize
  466.  
  467. // destrcutor
  468. // default destructor
  469. ListDA::~ListDA()
  470. {
  471.     delete [] list;
  472. } // end ~ListDA
  473.  
  474.  
  475. So there is all my code and at the top is what I am trying to get the program to do. Please help.
Advertisement
Add Comment
Please, Sign In to add comment