Part C - Encapsulation

Constructors

Workshop 4


In this workshop, you are to code a class that represents a credit card and processes transactions and payments on the card.


Learning Outcomes

Upon successful completion of this workshop, you will have demonstrated the abilities to

  • code a class for simple objects
  • define a constructor that sets an object to a safe empty state
  • overload a constructor to receive information from a client

Credit Card - All Groups

CreditCard Class

Define a class named CreditCard that holds and updates information about a single credit card.  The information consists of

  • a long long integer holding the credit card number
  • a C-style null-terminated string holding the name of the credit card holder (max 20 characters)
  • a double holding the current balance on the credit card

Include two constructors in your definition: a no-argument constructors that sets the object to a safe empty state and a two-argument constructor that receives the credit card number and the address of a C-style string that contians the holder's name. 

Include the following public member functions also:

  • void charge(double c) - charges the amount c to the credit card
  • void pay(double p) - pays down the credit card by the amount p
  • long long number() const - returns the the credit card number
  • double balance() const - returns the balance owing on the credit card
  • const char* name() const - returns the name of the credit card holder

Select your own safe empty state.  Ensure that your member functions behave well if the object on which they are called is in a safe empty state.

Place your class definition in a header file named CreditCard.h and your member function definitions in an implementation file named CreditCard.cpp

Client Module

The implementation file for the main module, which uses your class, is listed on the left.  The results using your class look something like those shown on the right.

 // Workshop 4 - Constructors
 // w4.cpp

 #include <iostream>
 using namespace std;
 #include "CreditCard.h"

 int main() {
     CreditCard card(1234, "John Doe"), empty;
     double x;
     char option;

     do {
         cout << "Select (C, P or Q) : ";
         cin >> option;
         switch (option) {
         case 'C':
         case 'c':
             cout << "Charge : ";
             cin >> x;
             card.charge(x);
             break;
         case 'P':
         case 'p':
             cout << "Payment : ";
             cin >> x;
             card.pay(x);
             break;
         }
     } while (option != 'Q' && option != 'q');

     cout << "Number  " << card.number()  << endl;
     cout << "Name    " << card.name()    << endl;
     cout << "Balance " << card.balance() << endl;

     cout << "Number  " << empty.number()  << endl;
     cout << "Name    " << empty.name()    << endl;
     cout << "Balance " << empty.balance() << endl;
 }













 Select (C, P or Q) : c
 Charge : 45.32

 Select (C, P or Q) : c
 Charge : 7.45

 Select (C, P or Q) : c
 Charge : 105.46

 Select (C, P or Q) : p
 Payment : 100

 Select (C, P or Q) : c
 Charge : 87.50

 Select (C, P or Q) : q


 Number  1234
 Name    John Doe
 Balance 145.73

 Number  0
 Name
 Balance 0.0

Note the sections of your code that required special attention.  Post advice to the forum letting other students know where they should take care.


Validation - Group Y

Design and code the CreditCard class described above.  Complete the w4.cpp implementation file.  Upgrade this file to include validate the data received from the client.  If the charge is negative, the payment is negative or the object is in a safe empty state do nothing.  If the two argument constructor receives an invalid credit card number or the string with the holder's name is too long set the object to a safe empty state.  Find and use the Mastercard algorithm to validate the number.


SUBMISSION

Typescript

Create a typescript of your complete solution using the following commands: 

 + At the prompt, type: script w4.txt
 + At the prompt, type: whoami
 + At the prompt, type: cat CreditCard.h CreditCard.cpp
 + At the prompt, type: g++ -o w4 w4.cpp CreditCard.cpp 
 + At the prompt, type: w4
 + At the input prompt, enter test data
 + At the prompt type: exit

These commands will produce a file named w4.txt.  Download this file to your local computer. 

Moodle

Upload your typescript file to Moodle: 

  • Login to Moodle
  • Select your course code
  • Select W4 under Assignments and Tasks
  • Upload w4.txt
  • Write a short note to your instructor
    • press "Edit"
    • enter your conclusions about this workshop in the notes textbox
    • press "Save Changes"
  • When ready to submit, press "Send for Marking"

Other Submissions

Submit your typescript file as your instructor has specified.







  Designed by Chris Szalwinski   Copying From This Site   
Logo
Creative Commons License