[please] About Classes I

please give me some advice what should I make with this coding.
I have a question below



Design and implement a class dayType that implements the day of the week in a program. The class dayType should store the day, such as Sun for Sunday. The program should be able to perform the following operations on an object of type dayType:
a. Set the day
b. Print the day
c. Return the day
d. Return the next day
e. Return the previous day
f. Calculate and return the day by adding certain days to the current day. For example if the current day is Monday and we add 4 days, the day to be returned is Friday. Similarly, if today is Tuesday and we add 13 days, the day to be returned is Monday.
g. Add the appropriate constructors.



and this is my answer


#include <iostream>
#include <cassert>
#include <string>


using namespace std;

class DayType {
private:
static const int DAYS = 7;
static string weekDays[DAYS];
string weekDay;

public:

DayType(string const & day = weekDays[0]);

// Accessor and Mutator
string getDay() const;
void setDay(string const & );

// Services
void print() const;
string nextDay() const;
string prevDay() const;
string addDay(int) const;

private:
size_t getDayNumber(void) const; // translate day name to day number
// private use only
};

//end header file






string DayType::weekDays[DayType::DAYS] =
{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};

DayType::DayType(string const & day) {
setDay(day); // assure that day is valid
}

string DayType::getDay() const {
return weekDay;
}

void DayType::setDay(string const & day) {
assert ( day == weekDays[0] || day == weekDays[1] ||
day == weekDays[2] || day == weekDays[3] ||
day == weekDays[4] || day == weekDays[5] ||
day == weekDays[6]); // assure that day is valid
weekDay = day;
}

void DayType::print() const {
cout << getDay();
}

string DayType::nextDay() const {
return weekDays[(getDayNumber() + 1) % DAYS];
}

string DayType::prevDay() const {
return weekDays[(getDayNumber() + 6) % DAYS];
}

string DayType::addDay(int days) const {
if (days < 0) days = DAYS + days; // (= 7 - abs(Days))
return weekDays[(getDayNumber() + days) % DAYS];
}

size_t DayType::getDayNumber(void) const {
// use the logic of search()
size_t i = 0;
for (; i < DAYS && weekDays[i] != getDay(); i++);
return i; // must exist
}

void testConstructors();
void testNextPrevDays(DayType const &);
void testAddDays(DayType const &, int);

int main() {

testConstructors();

const int DAYS = 7;
string weekDays[DAYS] = {"Sun", "Mon", "Tue", "Wed",
"Thu", "Fri", "Sat"};
for (size_t i = 0; i < DAYS; i++)
testNextPrevDays(DayType(weekDays[i]));

testAddDays(DayType("Sun"), -6);
testAddDays(DayType("Mon"), 5);
testAddDays(DayType("Tue"), 13);

system("pause");
return EXIT_SUCCESS;
}

//end main file




void testConstructors() {
DayType day0; // test the default constructor
assert (day0.getDay() == "Sun");

DayType day1("Mon"); // test the other constructor
assert (day1.getDay() == "Mon");

DayType day2(day1); // test the copy constructor
assert (day2.getDay() == "Mon");
}

void testNextPrevDays(DayType const & day) {
day.print(); cout << endl;
cout << "Its previous day:" << day.prevDay() << endl;
cout << "Its next day:" << day.nextDay() << endl;
}

void testAddDays(DayType const & day, int days) {
day.print(); cout << "+ " << days << " is ";
cout << day.addDay( days ) << endl;;
}





Please give me some advice,

how do I make a user to input his own day that he wants calculated without making an error?

What code should I use to change it?
What steps should I do to change it?

Your help is greatly appreciated
Last edited on
you need code indent tag to write your code here. you will get more help if your codes are easy to read, sorry (not advice)
Topic archived. No new replies allowed.