#ifndef __STATE_H__
#define __STATE_H__
#include "context.h"
#include <iostream>
using namespace std;
typedef enum
{
CHILDREN = 1,
PRIMARY_SCHOOL_STUDENT,
MIDDLE_SCHOOL_STUDENT,
UNIVERSITY_SCHOOL_STUDENT,
GRADUATE_STUDENT,
PROGRAMER,
} EDUCATION_TYPE_EN;
class Life;
class State
{
public:
State() {}
virtual void Operation(Life *life) {};
};
class Children : public State
{
public:
Children(){}
void Operation(Life *life);
};
class PrimarySchoolStudent : public State
{
public:
void Operation(Life *life);
};
class MiddleSchoolStudent : public State
{
public:
void Operation(Life *life);
};
class UniversitySchoolStudent : public State
{
public:
void Operation(Life *life);
};
class GraduateStudent : public State
{
public:
void Operation(Life *life);
};
class Programmer : public State
{
public:
void Operation(Life *life);
};
#endif /* __STATE_H__ */
#include "stdafx.h"
#include "state.h"
void Children::Operation(Life *life)
{
if (life->GetAge() < 0)
{
cout << life->GetAge() << "岁时" << "未出生" << endl;
}
else if (life->GetAge() >= 0 && life->GetAge() < 6)
{
cout << life->GetAge() << "岁时" << "未上学" << endl;
}
else
{
life->SetState(new PrimarySchoolStudent);
life->ShowState();
}
}
void PrimarySchoolStudent::Operation(Life *life)
{
if (life->GetAge() >= 6 && life->GetAge() < 12)
{
cout << life->GetAge() << "岁时" << "读小学" << endl;
}
else
{
life->SetState(new MiddleSchoolStudent);
life->ShowState();
}
}
void MiddleSchoolStudent::Operation(Life *life)
{
if (life->GetAge() >= 12 && life->GetAge() < 18)
{
cout << life->GetAge() << "岁时" << "读中学" << endl;
}
else
{
life->SetState(new UniversitySchoolStudent);
life->ShowState();
}
}
void UniversitySchoolStudent::Operation(Life *life)
{
if (life->GetAge() >= 18 && life->GetAge() < 22)
{
cout << life->GetAge() << "岁时" << "读大学" << endl;
}
else
{
life->SetState(new GraduateStudent);
life->ShowState();
}
}
void GraduateStudent::Operation(Life *life)
{
if (life->GetAge() >= 22 && life->GetAge() < 25)
{
cout << life->GetAge() << "岁时" << "读研究生" << endl;
}
else
{
life->SetState(new Programmer);
life->ShowState();
}
}
void Programmer::Operation(Life *life)
{
if (life->GetAge() >= 25)
{
cout << life->GetAge() << "岁时" << "成为码农,开始工作" << endl;
}
else
{
cout << "状态异常" << endl;
}
}