Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
Anonymous User
Assignment 2 questuin 1
July 30, 2011 03:17PM
This is quite a monster of a question. the part where I have to check if there is a vacant rooms blew me.

the function vacancy takes two two dates as parameter(arrival and departure date),the pat where I have to check if atlist one room is available on each of the dates from
the arrival to departure by calling roomsAvailable() is where I struggle. may someone give me an idea how to tackle this one? how do you increase dates from arrival to departure as I think we have to use a loop
avatar Re: Assignment 2 questuin 1
July 30, 2011 06:10PM
Could you cut and paste the question, please? If you do so, students who've done this course before might also be able to help you.
Anonymous User
Re: Assignment 2 questuin 1
August 01, 2011 11:56AM
I did not include the question UML but I hope the description is enough to assist me. how do I implement vacancy function?


vacancy() tests takes two dates as parameters (intended to represent the arrival and departure dates, respectively), tests whether the two dates are valid (e.g. 32/12/2011 is not valid), tests whether the arrival date precedes the departure date, and if so, checks whether there is at least one room available on each of the dates from the arrival date to the day before the departure date (by calling roomsAvailable() for each of these dates).

• roomsAvailable() takes a date as parameter and returns the number of rooms available on that date. It calculates this by going through the entire list of bookings and counting how many of them include the given date (and subtracts this from the total number of rooms). Hint: Use the booked() member function of the Booking class.
avatar Re: Assignment 2 questuin 1
August 01, 2011 01:15PM
Could you email me your tut letter please, there's not enough context yet.
Anonymous User
Re: Assignment 2 questuin 1
August 01, 2011 02:52PM
Sent
Re: Assignment 2 questuin 1
August 03, 2011 02:56PM
same here, please send me the tut to 42677718@mylife.unisa.ac.za

--------------------------------------------------------------------
In a long journey, even a matchstick is heavy...
avatar Re: Assignment 2 questuin 1
August 03, 2011 06:24PM
@crunx

Sorry for the late reply. I've looked through your assignment. Nice questions.

As to your original question, yes, you are most certainly going to have to iterate over dates and a for() loop should do it. If you want your counter variable to move from one day to the next between the arrival date and the departure date, you're best using the QDate:: addDays() function.

Have a look at it and also the QtAssistant reference documentation. You'll find a lot of useful stuff in there as well as descriptions of all the classes and how to use them.
Anonymous User
Re: Assignment 2 questuin 1
August 04, 2011 05:32PM
Thanks, nice one there. let me try QDate::addDays(). It never crossed my mind
Anonymous User
Re: Assignment 2 questuin 1
August 04, 2011 07:38PM
Ooops, Not as easy as I thought.

Not quite sure if this makes sense

bool vacancy(QDate a, QDate d)
{
for(QDate date = a; date <= d; date.addDays(1))

if date == roomsAvailable(date)
return true;
}

Nope, the QtAssistant reference documentation didn't help.
avatar Re: Assignment 2 questuin 1
August 04, 2011 09:36PM
@crunx

The QDate:: addDays() function won't change the internal value of the object, it returns a new QDate, offset by the number of days you specify, that you can manipulate somewhere else. In your for loop, you need to record this new date.

*Style Note: Don't be afraid of longer variable names. It makes your code easier to read. You may get penalised by the lecturers.
Language: C++ (QT)
bool vacancy(QDate arrivalDate, QDate departureDate) { for(QDate currentDate = arrivalDate; currentDate <= departure; currentDate = currentDate.addDays(1)){   if (currentDate == roomsAvailable(currentDate)) return true; } return false; }
Anonymous User
Re: Assignment 2 questuin 1
August 05, 2011 02:22PM
@Robanaurochs

Your counsel is appreciated. Thanks very much.
Anonymous User
Re: Assignment 2 questuin 1
August 12, 2011 11:40AM
Seriously, this abstract concept is messing up with my head. Since Booking class is an abstract class, constructing addBooking has been complex. Had booking class not been abstract, addBooking could have been easier. Eg void BookingList::addBooking(Booking* b)
{
If (vacancy(arrivalDate, departureDate))
Append(p);
}
But since Booking class abstract, you cannot initiated it, and I tried this below code .
First I had to create instances of single and Sharing.

Booking* BookingList::addBooking(Person contactName, QDate arrivalDate, QDate departureDate,
Person * guest1, Person* guest2)
{
If (vacancy(arrivalDate, departureDate))
{
// create instances of single and sharing class
Single *singleGuest = new Single(contactPerson, arrivalDate, departureDate, guest);
Sharing *sharingGest= newSharing(contactPerson, arrivalDate, DepartureDate, guest1, guest2);
BookingList.append(addBooking(contactName, departureDate , arrivalDate, guest1, guest2));
}
Return BookingList;
}

My question what is wrong with my code? May someone there explain how to implement this addbookining class.
avatar Re: Assignment 2 questuin 1
August 12, 2011 12:53PM
You're not quite understanding the usefulness of abstraction.

Take your bookingList class for instance. The addBooking() function must determine how many guests there are and then decide what kind of concrete booking to make. Since you need to pass through two guest Person instances, the easiest way to check what kind of booking to create is to check whether the second guest is a valid pointer or not. You do this by checking whether the pointer address is zero or not.

Language: C++
Booking *newBooking; If (guest2 == 0){ newBooking = new Single(contactPerson, arrivalDate, departureDate, guest1); } else { newBooking = new Sharing(contactPerson, arrivalDate, departureDate, guest1, guest2); }   append(newBooking);   return newBooking;
Anonymous User
Re: Assignment 2 questuin 1
August 12, 2011 04:44PM
@robanaurochs

axactly how I wanted to code it. It looks simple but it never clicked for me. I definately understand the usefulness of abstraction however, I think my limited knowledge of c++ did not help to apply the concept.
I have learnt a lot and thanks again robanaurochs
Re: Assignment 2 questuin 1
August 23, 2011 10:29AM
hi guys!can anyone pliz help me with the implementation of the void BookingList::roomsAvailable(QDate d)????

blv
Anonymous User
Re: Assignment 2 questuin 1
August 23, 2011 05:50PM
Hi boby love,

How about posting your attempt so that people can help you basing on that? in that way, the assistance can enhance your limitations , again, you have a sense of ownership of your solution hence improving your confidence. I hope you get my point
Re: Assignment 2 questuin 1
August 24, 2011 02:17PM
Hi there! I'm new here!

I'm trying to figure out as to how to solve this little problem. I want the m_Contact = c statement inside the booking constructor definition (see below) to work properly.

class Booking // ABSTRACT CLASS

booking.h

Language: C++ (QT)
{ public:   Booking(Person c, QDate a, QDate d); QString toString(); virtual double rate() = 0; bool booked(QDate d); double SINGLE_PPPN; double SHARING_PPPN;   private: Person m_Contact; QDate m_ArrivalDate; QDate m_DepartureDate; };

booking.cpp
Language: C++ (QT)
#include<QString> #include<QDate>   #include "booking.h" #include "person.h"   Booking::Booking(Person c, QDate a, QDate d)   { m_Contact = c; // IT DOESN'T WORK!!!!! m_ArrivalDate = a; m_DepartureDate = d;   SINGLE_PPPN = 50.0; SHARING_PPPN = 100.0; }   QString Booking::toString()   { return (m_Contact.toString() + ", Arrival Date: " + m_ArrivalDate + ", Departure Date: " + m_DepartureDate); }   bool Booking::booked(QDate d)   { if ((m_ArrivalDate <= d) && (d < m_DepartureDate))   return true;   else   return false; }

This is the most boring programming module angry smiley
avatar Re: Assignment 2 questuin 1
August 24, 2011 03:17PM
The problem here is that you're attempting to assign the speed of light in a vacuum to some variable. What probably happened is that you picked some person faster than the speed of light, and so your computer baulks at this physical impossibility...

Which is a roundabout way of saying: "That's a pretty weird error."

It looks like it should work just fine.

Have you tried the initialization list alternative? (I see it's a constructor).
Re: Assignment 2 questuin 1
August 24, 2011 03:45PM
Whoa! Now it works oO Thanks man!!!! You really saved my life! I have been so frustated by this stupid problem. Can't believe that I didn't consider using the initialization list (facepalm)


Again, thanks man smiling smiley Now going to finish this boring assignment (It's late, I know sad smiley)
avatar Re: Assignment 2 questuin 1
August 24, 2011 04:20PM
smile It only occurred to me because I've probably stared at something similar for a similar number of hours at some quite recent point in time. Glad to see the shot in the dark somehow hit the mark, anyway.
Re: Assignment 2 questuin 1
August 24, 2011 07:33PM
Can someone please help with iterating the Booking List with a foreach loop.

My code looks like this:

Language: C++ (QT)
int BookingList::roomsAvailable(QDate d){ int roomsOccupied = 0; foreach (Booking, BookingList);{ if (Booking::booked(d)) roomsOccupied++; } return NO_OF_ROOMS - roomsOccupied; }

I'm truly lost with using pointers, inherited classes and Lists all at once, please help.
avatar Re: Assignment 2 questuin 1
August 24, 2011 10:22PM
Are you passing an instance as the first argument to your foreach loop?

Looks to me like it's just the class of that object. Maybe you can do that, but I suspect not. Look up foreach in the Qt docs and make sure, I reckon. That might sort you out.
Re: Assignment 2 questuin 1
August 25, 2011 08:35AM
xtoor Wrote:
-------------------------------------------------------
> Can someone please help with iterating the Booking
> List with a foreach loop.
>
> My code looks like this:
>
>
> int BookingList::roomsAvailable(QDate d){
> int roomsOccupied = 0;
> foreach (Booking, BookingList);{
> if (Booking::booked(d))
> roomsOccupied++;
> }
> return NO_OF_ROOMS - roomsOccupied;
> }
>
>
> I'm truly lost with using pointers, inherited
> classes and Lists all at once, please help.

Use *this instead of BookingList as 2nd argument of foreach. It's safe.

Well, guys, I have another problem, I can't initialize NO_OF_ROOMS variable of int type inside the public section of BookingList class. I get an error message saying it's forbid to initialize data members inside the class. I want it to be initialized everytime an instance of BookingList is created. The question paper doesn't mention that you should add any constructor to BookingList class.
Anonymous User
Re: Assignment 2 questuin 1
August 25, 2011 10:32AM
@Spectre

I advice you to understand how foreach loop works

this how I did it
Language: C++ (QT)
foreach(Booking * b, *this){ NO_OF_ROOMS++; if ((b->booked(d)) { roomsOccupied ++ } return NO_OF_ROOMS - roomsOccupied; }
Anonymous User
Re: Assignment 2 questuin 1
August 25, 2011 10:36AM
Sorry I made a mistake there.
is No_OF_Rooms++ just after foreach loop instead of rooms Occupied++
Re: Assignment 2 questuin 1
August 25, 2011 12:11PM
crunx Wrote:
-------------------------------------------------------
> Sorry I made a mistake there.
> is No_OF_Rooms++ just after foreach loop instead
> of rooms Occupied++

Hey, I have figured out how to use a foreach loop properly, thanks to QtAssistance.

As for your roomsAvailable code, Didn't you specify the maximum number of rooms in your code? How do you tell if your rooms are all fully booked? Mine is different from yours. I initialize NO_OF_ROOMS to something like 10 everytime an instance of BookingList is created.
Anonymous User
Re: Assignment 2 questuin 1
August 25, 2011 02:17PM
the statement No_OF_Rooms++ just after foreach loop is intended to count the number of rooms

Language: C++ (QT)
if ((b->booked(d)) { roomsOccupied ++ }

is to count the number of booked rooms.

so to answer your question, my program count the number of rooms so I id not initialize No_OF_Rooms. Initializing it is another way of doing it and there is nothing wrong with that.
Re: Assignment 2 questuin 1
August 25, 2011 09:04PM
crunx Wrote:
-------------------------------------------------------
> the statement No_OF_Rooms++ just after foreach
> loop is intended to count the number of rooms
>
>
> if ((b->booked(d))
> {
> roomsOccupied ++
> }
>
>
>
> is to count the number of booked rooms.
>
> so to answer your question, my program count the
> number of rooms so I id not initialize
> No_OF_Rooms. Initializing it is another way of
> doing it and there is nothing wrong with that.

Yeah, I got it.

Good luck guys smiling smiley
Re: Assignment 2 questuin 1
August 28, 2011 11:18PM
Quote
slow eddy
Are you passing an instance as the first argument to your foreach loop?

Looks to me like it's just the class of that object. Maybe you can do that, but I suspect not. Look up foreach in the Qt docs and make sure, I reckon. That might sort you out.

slow eddy,
I did the same thing that u suggested to Spectre but no luck
Re: Assignment 2 questuin 1
August 28, 2011 11:20PM
I get undefined reference to 'vtable for Booking', anyone help!!!angry smiley
Sorry, only registered users may post in this forum.

Click here to login