Over the limit
[C++] 재고 관리 프로그램 본문
클래스, 상속, 접근제한, 함수 중복을 활용하고
객체지향의 특성을 알고 제대로 적용하는 것을 주안으로 작성한 재고 관리 프로그램이다.
#include<iostream>
#include<string>
using namespace std;
void f(char c = ' ', int line = 1); //함수 중복
void f(char c, int line) { //함수 중복
for (int i = 0; i < line; i++) {
for (int j = 0; j < 35; j++)
cout << c;
cout << endl;
}
}
class Size { //클래스
protected: //접근 제한
string name;
int amount;
bool empty;
public:
Size() { empty = false; }
void decrease() { //사이즈를 선택하면 재고가 하나씩 줄 수 있는 함수
amount = amount - 1;
if (amount == 0) empty = true;
}
int getAmount(){ return amount; }
void setAmount(int amount) { this->amount = amount; }
string getName() { return name; }
bool getempty() { return empty; }
};
class small : public Size{ //Size 클래스와 상속관계
public:
small() { this->name = "small"; this->amount = 5; }
};
class medium : public Size {//Size 클래스와 상속관계
public:
medium() { this->name = "medium"; this->amount = 5; }
};
class large : public Size { //Size 클래스와 상속관계
public:
large() { this->name = "large"; this->amount = 5; }
};
class Store {
Size* Size[4]; //포인터 객체 배열 선언
public:
Store() {
Size[0] = new small();
Size[1] = new medium();
Size[2] = new large();
}
void order(int type);
bool checkempty(int type);
void run();
void show();
int select();
};
bool Store::checkempty(int type) { //재고가 소진되었는지 확인
if (Size[0]->getempty() || Size[1]->getempty() || Size[2]->getempty())
return true;
}
void Store::order(int type) { //주문. 키보드를 누르면 값이 줄어든다
if (checkempty(type)) cout << "재고 소진. 안녕히 가세요." << endl;
else {
switch (type) {
case 0:cout << "small 사이즈 출고합니다." << endl;
Size[0]->decrease(); //small사이즈 감소
break;
case 1:cout << "medium 사이즈 출고합니다." << endl;
Size[1]->decrease(); //medium사이즈 감소
break;
case 2:cout << "large 사이즈 출고합니다." << endl;
Size[2]->decrease(); //large사이즈 감소
break;
default: cout << "다시 입력해주세요." << endl;
}
}
}
void Store::run() { //재고 관리 프로그램을 동작하게 한다.
while (true) {
show();
int menu = select();
switch (menu) {
case 0: case 1: case 2: order(menu); break;
case 3: return;
}
}
}
int Store::select() { //재고 관리 프로그램의 선택 입력창을 구현한다.
cout << endl;
cout << "small : 0, medium : 1, large : 2, 종료 : 3>>";
int choice = 0;
cin >> choice;
return choice;
}
void Store::show() { //재고가 몇개 남았는지 for문을 사용하여 알려준다.
for (int i = 0; i < 3; i++) {
cout << Size[i]->getName() << ":";
for (int amount = 0; amount < Size[i]->getAmount(); amount++) {
cout << "O";
}
cout << endl;
}
}
int main() {
f('=', 3); //함수 중복
f(); //함수 중복
cout << "------- 재고 관리 프로그램------" << endl;
cout << "모든 사이즈가 존재할 때만 영업합니다." << endl;
f(); //함수 중복
f('=', 3); //함수 중복
Store st; //객체 생성
st.run(); //run을 이용하여 재고 관리 프로그램 실행
return 0;
}
'Algorithm > Algorithm 공부' 카테고리의 다른 글
[JAVA] DFS 구현 (0) | 2021.07.11 |
---|---|
[JAVA] ArrayList 개념 (0) | 2021.07.10 |
해시(Hash)는 무엇인가? 간단한 해시 테이블 설명까지! (0) | 2021.06.30 |
스택(Stack) 큐(Queue) 개념 비교 (1) | 2021.06.12 |
AES-128 Decryption Algorithm (1) | 2021.05.31 |