-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustomer.cpp
More file actions
135 lines (104 loc) · 3.33 KB
/
customer.cpp
File metadata and controls
135 lines (104 loc) · 3.33 KB
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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Customer {
public:
// Constructor
Customer () : cust_name(""), cust_address(""), acct_number(0)
{
}
Customer(string name, string address, int number) : cust_name(name), cust_address(address), acct_number(number)
{
}
// Accessor to get the account number
int getAcctNumber() const { return acct_number; }
// Accessor to get the customer name
string getCustName() const { return cust_name; }
// Accessor to get the customer address
string getCustAddress() const { return cust_address; }
// Set a customer name and address
void set(string name, string address);
// Set a customer address
void setAddress(string address) { cust_address = address; }
// Set a customer acct number
void setAcct_number(int number) { acct_number = number; }
// Get the next account number for the next customer.
const unsigned long getNextAcctNumber() { return acct_number++; }
// input operator
friend Customer operator>> (istream& ins, Customer &cust);
// output operator
friend void operator<< (ostream& outs, const Customer &cust);
private:
string cust_name; // customer name
unsigned long acct_number; // account number
string cust_address; // customer address
};
const int MAX_CUSTOMERS = 20; // Change this to a smaller number to test.
// Declare the class variable nextAcctNum
//unsigned long Customer::nextAcctNum=10000;
// set the customer name and address
// @param name: the customer name
// @param address: the account address
void Customer::set(string name, string address)
{
this->cust_name = name;
this->cust_address = address;
}
// input operator reads customer as a name and address on two separate lines.
// name
// address1
Customer operator >>(istream& ins, Customer& cust)
{
getline(ins, cust.cust_name);
getline(ins, cust.cust_address);
return cust;
}
// output operator
void operator <<( ostream& out, const Customer& cust)
{
out << cust.acct_number << ": " << cust.cust_name << "\n" << cust.cust_address << endl;
}
int main()
{
Customer custList[MAX_CUSTOMERS];
ifstream infile;
string filename;
cout << "Enter the name of the file for input: ";
cin >> filename;
infile.open(filename);
if (infile.fail()) {
cout << "Error: failed to open file: " << filename;
exit(1);
}
// Read in customer list.
int size = 0;
while (!infile.eof())
{
infile >> custList[size];
custList[size].setAcct_number(10001 + size);
if (size == MAX_CUSTOMERS)
break;
size++;
}
// Get customer address changes
for (int i = 0; i < size; i++)
{
char answer;
string input;
cout << "Address change for " << custList[i].getAcctNumber() << "? (y or n) : ";
cin >> answer;
if (answer == 'y') {
cin.ignore(); // newline
getline(cin, input);
custList[i].setAddress(input);
break;
}
}
// Write out customer list.
for (int i = 0; i < size - 1; i++)
{
cout << custList[i];
}
return 0;
}