Class for Credit Cards

I need a little help with this class that I am working on for a group project. Here is the assignment:

Car Rental System
1. Cars should already be available to rent (do not make the user enter in this information).
2. There will be smoking and non-smoking cars; large, mid-sized, and small cars.
3. You do not need to take a deposit to reserve a car rental, but a credit card type and number (with expiration date) is needed.
4. Limit your reservation system to one month out of the year and no more than 10 cars.
5. Clients should be able to make and cancel a reservation. A reservation might extend to 2 days and beyond.
6. If a requested car is not available, display a message denoting that the car is not available.
7. Your system needs to be able to recall and display reservation information.

I am in charge of making the class creditCardType, which should hold the information about the individuals credit card. It must have a variable to hold the card type as well as one to hold the card number, and it must include their name and the card's expiration date. I got started, but I think I'm confusing myself with this. The overloading operators were included as a group decision to compare the cards.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#ifndef H_creditCardType
#define H_creditCardType
#include<iostream>

using namespace std;

class creditCardType
{
	public:
		string getName() const;   //Function to return customer's name
		string getCardNum() const;   //Function to return credit card number
		string getCardType() const;   //Function to return credit card type
		string getCardExp() const;   //Function to return credit card expiration date
		bool operator==(const creditCardType&) const;   //Overload the operator ==
		const creditCardType& operator=(const creditCardType&);	  //Overload the operator =
	private:
		string name;   //Variable to store customer's first and last name
		string cCardNum;   //Variable to store credit card number
		string cCardType;   //Variable to store credit card type
		string cCardExp;   //Variable to store credit card expiration

};

string creditCardType::getName()
{
	return name;
}

string creditCardType::getCardNum()
{
	return cCardNum;
}

string creditCardType::getCardType()
{
	return cCardType;
}

string creditCardType::getCardExp()
{
	return cCardExp;
}



#endif 
Overloading operator= shouldn't be necessary, the default should work fine. What exactly are you having trouble with?
Here's what the person in my group said about it: "As far as overloading operators, if I have a cardType variable and I do something like "thisCard == thatCard", it should compare to make sure that all the variables are the same. Likewise, if I do "thisCard = thatCard", it should automatically set the values on thisCard to the value of it's counterpart in thatCard."


My problem is that I feel there is more to it than just this. I'm having trouble with the implementation of the operators as well.
I added a function to it, but I feel like there is something that is definitely not right. Should I have functions to set initial values as well, like to null? (setName, setCardNum, etc)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#ifndef H_creditCardType
#define H_creditCardType

#include<string>

using namespace std;

class creditCardType
{
	public:
		string getName() const;   //Function to return customer's name
		string getCardNum() const;   //Function to return credit card number
		string getCardType() const;   //Function to return credit card type
		string getCardExp() const;   //Function to return credit card expiration date
		void validCardNum(string cardNum) const;   //Function to validate the credit card number
		bool operator==(const creditCardType&) const;   //Overload the operator ==
		const creditCardType& operator=(const creditCardType&);	  //Overload the operator =
	private:
		string name;   //Variable to store customer's first and last name
		string cCardNum;   //Variable to store credit card number
		string cCardType;   //Variable to store credit card type
		string cCardExp;   //Variable to store credit card expiration
		int length = 16;   //Variable to store correct credit card length

};

string creditCardType::getName()
{
	return name;
}

string creditCardType::getCardNum()
{
	return cCardNum;
}

string creditCardType::getCardType()
{
	return cCardType;
}

string creditCardType::getCardExp()
{
	return cCardExp;
}

void creditCardType::validCardNum(string cardNum)
{
	cout << "Please enter your 16 digit credit card number: " << endl;
	cin >> cardNum;
	
	while (cardNum..length() > 16 || cardNum..length() < 16)
	{
		cout << "Invalid, please try entering your credit card number again: " << endl;
		cin >> cardNum;
	}
		string cardNum = cCardNum;
}



#endif 
Major revisions and additions done:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#ifndef H_creditCardType
#define H_creditCardType
#include<iostream>
#include<string>

using namespace std;

class creditCardType
{
	public:
		creditCardType();   //Constructor
		creditCardType(string name, string cardNum, cardType, cardExp);   //Constructor with parameters
		void setName(const string name);   //Function to set the customer's name
		string getName() const;   //Function to return customer's name
		void setCardNum(const string cardNum);   //Function to set the credit card numer
		string getCardNum() const;   //Function to return credit card number
		void setCardType(const string cardType);   //Function to set the credit card type
		string getCardType() const;   //Function to return credit card type
		void setCardExp(const string cardExp);   //Function to set the credit card expiration date
		string getCardExp() const;   //Function to return credit card expiration date
		bool operator==(const creditCardType&) const;   //Overload the operator ==
		const creditCardType& operator=(const creditCardType&);	  //Overload the operator =
		creditCardType cardInfo();   //Returns name, credit card number, card type, and card expiration
	private:
		string fullName;   //Variable to store customer's first and last name
		string cCardNum;   //Variable to store credit card number
		string cCardType;   //Variable to store credit card type
		string cCardExp;   //Variable to store credit card expiration
};

creditCardType::creditCardType()
{
	setName("");
	setCardNum(0000000000000000);
	setCardType("");
	setCardExp(0000);
}

creditCardType::creditCardType(string name, string cardNum, cardType, cardExp)
{
	setName(name);
	setCardNum(cardNum);
	setCardType(cardType);
	setCardExp(cardExp);
}

void creditCardType::setName(const string name)
{
	name = fullName;
}

string creditCardType::getName()
{
	return fullName;
}

void creditCardType::setCardNum(const string cardNum)
{
	cardNum = cCardNum;
}

string creditCardType::getCardNum()
{
	return cCardNum;
}

void creditCardType::setCardType(const string cardType)
{
	if(cardType = 1)
		cardType = "Visa";
	else if(cardType = 2)
		cardType = "MasterCard";
	else if(cardType = 3)
		cardType = "Discover";
	else(cardType = 4)
		cardType = "American Express"; 
		
	cardType = cCardType;
} 

string creditCardType::getCardType()
{
	return cCardType;
}

void creditCardType::setCardExp(const string cardExp)
{
	cardExp = cCardExp;
}

string creditCardType::getCardExp()
{
	return cCardExp;
}

creditCardType::cardInfo()
{
	string infoName;   //Variable to hold full name
	string infoNum;   //Variable to hold credit card number
	string infoType;   //Variable to hold credit card type
	string infoExp;   //Variable to hold credit card expiration date
	creditCardType info;   //Object
	
	bool finished = false;   //Used for while loop until full name information is successful
	bool finished1 = false;   //Used for while loop until credit card number information is successful
	bool finished2 = false;   //Used for while loop untnil credit card type information is successful
	bool finished3 = false;   //Used for while loop until credit card expiration date is successful
	string error = "Invalid format. Please re-enter your name.";   //Input error for full name
	string error1 = "Invalid card number length. Please re-enter your card number.";   //Input error for credit card number
	string error2 = "Invalid card type. Please re-enter your card type.";   //Input error for credit card type
	string error3 = "Invalid expiration date. Please re-enter the expiration date on your card.";   //Input error for credit card expiration date
	
	cout << "We will now need your credit card information." << endl << endl;
	
	
	//Prompts for the full name and checks validation
	do
	{
		try
		{
			cout << "Please enter your full name: ";
			cin.getline(infoName, '\n');
			
			if (infoName.find_first_of("0123456789") != string npos);
				throw error;
				
			finished = true;
			info.setName(infoName);
			
		}
		catch(string error)
		{
			cout << error << endl;
			cin.clear();
		}
	}
	while(!finished)
	
	
	//Prompts for the credit card number and checks validation
	do
	{
		try
		{
			cout << "Please enter your 16-digit credit card number: ";
			cin >> infoNum;
	
			if (infoNum.length() > 16 || infoNum.length() < 16)
				throw error1;
				
			finished1 = true;
			info.setCardNum(infoNum);			
		}
		catch(string error1);
		{
			cout << error1 << endl;
			cin.clear();
		}	
	}
	while (!finished1);
	
	
	//Prompts for the credit card type and checks validation
	do
	{
		try
		{
			cout << "Please enter your credit card type(1, 2, 3, or 4)." << endl;
			cout << "We only accept (1)Visa, (2)MasterCard, (3)Discover, and (4)American Express: ";
			cin >> infoCard
			
			if (infoCard != 1 || 2 || 3 || 4)
				throw error2;
				
			finished2 = true;
			info.setCardType(infoCard);
		}
		catch(string error2)
		{
			cout << error2 << endl;
			cin.clear();
		}
	}
	while(!finished2)
	
	
	//Prompts for the credit card expiration date and checks validation
	do
	{
		try
		{
			cout << "Please enter the expiration date on your credit card (format 0216 = Feb 2016): ";
			cin >> infoExp;
			
			if (infoExp.length() > 4 || infoExp.length() < 4 || infoExp <= 0514)
				throw error3;
				
			finished3 = true;
			info.setCardExp(infoExp)
		}
		catch(string error3)
		{
			cout << error3 << endl;
			cin.clear();
		}
	}
	while(!finished3)
	
	creditCardType infoGathered(info.getName(), info.getCardNum(), info.getCardType, info.getCardExp());
	return infoGathered;
}

#endif H_creditCardType 
Line 34, 36: The arguments aren't strings.

Line 49, 54, 59, 64,78,88: Your assignments are backwards.

Line 69, 71, 73, 75. 1) You're using the assignment operator (=), not the equality operator (==). 2) You're trying to compare a string to an integer.

Line 209-210: Why not just return info? info.getCardType needs ()



Last edited on
Note, don't forget the credit card's security code. It's a 3-4 digit code located somewhere on the card (varies).

creditCardType::cardInfo() is doing entirely too much for a single method. I'm not even sure what exactly you're wanting to do in that method. Looks like you're taking information for a new card, creating a new card object, then returning a copy of said object? Why?

The name of the class seems weird. You call it creditCardType, but it appears to be storing all information about a credit card. Why not just call it CreditCard?

Topic archived. No new replies allowed.