Question : How do I create a class that handles file I/O for a car rental reservation program?

Basically, I want to add a class that handles all the file input/output of the below program. The program attached is what we have so far in an attempt to let a user rent a car (if it's not already reserved), check a reservation and cancel a reservation. It's still missing some parts but I am concerned with creating a new file handling class. I know we want
-a vector that will hold only ten cars with all variables saved to a simple .txt file INCLUDING vector reservations.
-A constructor with no parameteres other than maybe a .txt name to open fle and load all info into variables.
-Simple accessor functions for car objects
-Function that will accept as a parameter a vector variable and resave all info to the file.

Basically, does this way make sense? And if so, how can I go about doing it?

I'm a newbie at C++ and to this forum so I apologize if anything sounds crazy. (I've also been up coding for 40 hours of the past 48!) Thanks in advance to anyone who takes the time to help!
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:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325:
326:
327:
328:
329:
330:
331:
332:
333:
334:
335:
336:
337:
338:
339:
340:
341:
342:
343:
344:
345:
346:
347:
348:
349:
350:
351:
352:
353:
354:
355:
356:
357:
358:
359:
360:
361:
362:
363:
364:
365:
// .cpp
#include <iostream>
#include <string>
#include <time.h>


#include "car.h"
#include "customer.h"
#include "res.h"

using namespace std;

time_t convTime(int day, int month, int year)
{
   time_t rawtime;
   struct tm * timeinfo;
   time (&rawtime);
   timeinfo = localtime(&rawtime);
   timeinfo->tm_year = year - 1900;
   timeinfo->tm_mon = month - 1;
   timeinfo->tm_mday = day;
   return mktime(timeinfo);
}

time_t askForTime()
{
    int day, month, year;
    cout << "Please enter all fields as numbers." << endl;
    cout << "Day: ";
    cin >> day;
    cout << "Month: ";
    cin >> month;
    cout << "Year: ";
    cin >> year;
    return convTime(day, month, year);
}

Customer createCust()
{
	string cName, cAddress, cccNumber, cccType, cEmail;
    cout<<"Please enter your name and then press enter."<<endl;
	cin >> cName;
	
	cout<<"Please enter your address and then press enter."<<endl;
	cin >> cAddress;
	
	cout<<"Please enter your credit card number and then press enter."<<endl;
	cin >> cccNumber;
	
	cout<<"Please enter your credit card type and then press enter."<<endl;
	cin >> cccType;
	
	cout<<"Please enter your email address and then press enter."<<endl;
	cin >> cEmail;
	
	return Customer(cName, cAddress, cccNumber, cccType, cEmail);
}

int main(void)
{
    vector<Customer> customers;
	Car carArray[10] = {
	    Car(1, "Mid Size", 50.00, false),
		Car(2, "Mid Size", 55.00, true),
		Car(3, "Compact", 40.00, false),
		Car(4, "Compact", 45.00, true),
		Car(5, "Full Size", 55.00, false),
		Car(6, "Full Size", 60.00, true),
		Car(7, "Minivan", 55.00, false),
		Car(8, "Minivan", 60.00, true),
		Car(9, "SUV", 60.00, false),
		Car(10, "SUV", 65.00, true),
    };

	int selection;
	Customer * currentCust;
	bool notDone = true;

	cout << "Please Make a Selection:" << endl;
	cout << "1. I am a new customer. " << endl;
	cout << "2. I am an existing customer. " << endl;
	cout << "1 or 2: ";
	cin >> selection;
	if (selection == 1)
	{
         customers.push_back(createCust());
         currentCust = &customers[customers.size() - 1];
    }
    else if (selection == 2)
    {
         cout << "Which customer are you?" << endl;
         for (int i = 0; i < customers.size(); i++)
             cout << (i+1) << ". " << customers[i].getName() << endl;
         cout << "? ";
         cin >> selection;
         currentCust = &customers[selection];
    }
  while(notDone) {
    cout << endl << endl;
    cout << "Please Make a Selection:" << endl;     
    cout << "1.  Make a Reservation" << endl;
	cout << "2.  View Reservation Schedule" << endl; 
	cout << "3.  Exit" << endl;
    cout << "? ";
	cin >> selection;

	if (selection == 1) 
    {
  		cout << "Please Enter Pick Up Date." << endl;
		time_t outD = askForTime();
		cout << "Please Enter Return Date." << endl;
		time_t inD = askForTime();
		cout << "Please Input Car ID Number (1-10): ";
		int idNum;
        cin >> idNum;
        Reservation r = Reservation(outD, inD, idNum, currentCust);
        cout << "Your reservation has been confirmed.";
    }
    else if (selection == 3)
         notDone = false;
  }

	return 0;
}

// customer.h

#ifndef H_CUSTOMER
#define H_CUSTOMER

#include<string>

using namespace std;

class Customer
{
public:
	void print() const;
	string getName();
	string getAddress();
	string getccNumber();
	string getccType();
	string getEmail();
	Customer(string _name, string _address, string _ccNumber, string _ccType, string _email);

private:
	string name;
	string address;
	string ccNumber;
	string ccType;
	string email;
};

void Customer::print() const
{
	cout<< name <<endl;
	cout<< address <<endl;
	cout<< ccNumber <<endl;
	cout<< ccType <<endl;
	cout<< email <<endl;
}

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

string Customer::getAddress()
{
    return address;
}

string Customer::getccNumber()
{
    return ccNumber;
}

string Customer::getccType()
{
    return ccType;
}

string Customer::getEmail()
{
    return email;
}

Customer::Customer(string _name, string _address, string _ccNumber, string _ccType, string _email)
{
	name = _name;
	address = _address;
	ccNumber = _ccNumber;
	ccType = _ccType;
	email = _email;
}

#endif

// car.h

#ifndef H_CAR_1
#define H_CAR_1

#include <string>
#include <iostream>
#include <vector>

#include "res.h"

using namespace std;

class Car
{
private:
       int idNumber;
       string cSize;
       double pricePerDay;
       bool smoking;
       vector<Reservation> reservations;
public:
       Car(int idN, string s, double p, bool sm);
       bool makeReservation(Reservation *res);              
       void cancelReservation(Reservation *res);
};

Car::Car(int idN, string s, double p, bool sm)
{
     idNumber = idN;
     cSize = s;
     pricePerDay = p;
     smoking = sm;
}

/*

Pending:

        vector<reservation> car::reservations;

        bool reservation::conflict();

*/

bool Car::makeReservation(Reservation *res)
{
    for (int i=0; i < reservations.size(); i++)
    {
        if (reservations[i].conflict(res))  // conflict returns true if the dates conflict. NOT YET WRITTEN
      //     throw ("The reservation dates conflict with another reservation.");
     //   catch (char message[])
        {
              cout << "The reservation dates conflict with another reservation." << endl;
              return false;
        }
    }

    //now we know the date doesn't conflict. just need to add it
    reservations.push_back(*res);
/*
    //now update the reservation file
    cout << "Attempting to open " << FILENAME << endl;
    ifstream inp (FILENAME, ifstream::in); 

    if (!inp.good())
       throw ("There was a problem opening the reservation file.");
    catch (char message[])
    {
        cout << message << endl;
        return false;
    }

    while (inp.good())
    {
          //read the reservations

    }
    */
    return true;
}

void Car::cancelReservation(Reservation * res)
{
     for (int i=0; i < reservations.size(); i++)
         if (res->getidNumber() == idNumber)
              reservations.erase(reservations.begin() + i);
}

#endif

// res.h

#ifndef H_RESERVATION
#define H_RESERVATION

#include <time.h>
#include "car.h"
#include "customer.h"

using namespace std;

class Reservation
{
private:
    time_t dateOut;
    time_t dateIn;
	int carID;
    Customer * custInfo;
    int reservationNumber;
    
    
public:
    Reservation(time_t dOut, time_t dIn, int _carID, Customer * custI);
    
    bool conflict(Reservation *r);
    
    int getidNumber();
    time_t getDateOut();
    time_t getDateIn();
    int getCarReserved();
    Customer* getCustInfo();
    
};

int Reservation::getidNumber()
{
    return reservationNumber;
}

time_t Reservation::getDateOut()
{
       return dateOut;
}

time_t Reservation::getDateIn()
{
       return dateIn;
}

int Reservation::getCarReserved()
{
    return carID;
}

Customer* Reservation::getCustInfo()
{
         return custInfo;
}

bool Reservation::conflict(Reservation *r)
{
     if ((r->getDateIn() < dateOut) || (r->getDateOut() > dateIn))
        return false;
     return true;
}     

Reservation::Reservation(time_t dOut, time_t dIn, int _carID, Customer * custI)
{
   dateOut = dOut;
   dateIn = dIn;
   carID = _carID;
   custInfo = custI;
   
}                             

#endif

Answer : How do I create a class that handles file I/O for a car rental reservation program?

I have a feeling that you may need to break this project type question of design and implementation into multiple questions on a as-needed basis. But, I'll try to get the ball rolling with some suggestions. But in case your question is dealing with file writing techniques, I'd like you to take a look at this link, and see if the ideas there will give you a start.
         http://w.e-e.com/SiwPQt

There is one I/O issue that you should be aware of before moving on to files. When you ask for the customer's address, you are using
      cin >> cAddress;
But if the address has spaces in it, then this only picks up the first word. You should read the following link about getline to resolve this problem:
    http://www.cplusplus.com/reference/iostream/istream/getline/


===========================
Moving on...
>>  vector will hold only ten cars with all variables saved to a simple .txt file INCLUDING vector reservations.

Looks like you are creating your own database. You can take the following ideas and decide what you really have to do for your assignment. So, following some of the db design approach, consider creating not one, but several .txt files:

- lists customers sorted by customer id (a one-up number)
- lists the car inventory sorted by car id (a one-up number)
- lists the car status sorted by car id (i.e., is it in the lot; or if not, include the customer id and when is it due to return)
- lists reservations using a sort criteria (probably car_id with secondary index of reservation start date is not a bad one)
- lists history of billing information

When a customer takes a rental, the reservation entry is removed, and the billing and car status files are updated.
===========================

re: your file class
Are you required to have a file class; or could you embed I/O operations into your individual classes - Car, Customer, and Reservation? In each one, you constructor could include the corresponding filename and open the corresponding file.
Random Solutions  
 
programming4us programming4us