본문 바로가기

카테고리 없음

POS Project complete version

#include "stdafx.h"

#include <iostream>

#include <string>


struct Object {

std::string objName;

unsigned int objPrice;

};


struct Order {

Object *pList;

unsigned int count;


void AddObject(Object *pObj);

void DeleteObject(int n);

void Free(void);

};


void Run(bool bPrint);


Order *pList;

unsigned int count;

unsigned int total;


Object objList[] = {

"사과", 100,

"배", 200

};


void PrintOrder(void);

void AddOrder(void);

void EditOrder(void);

void CalculateOrder(void);


int main(void) {

using namespace std;


cout << "POS Project 1.0" << endl;


Run(true);


return 0;

}


void Run(bool bPrint)

{

using namespace std;


int menu = 0;


if (bPrint) {

cout << "----------------------------" << endl;

cout << "| 0. 프로그램 메뉴 다시 출력." << endl;

cout << "| 1. 현재 주문 조회." << endl;

cout << "| 2. 새 주문 추가." << endl;

cout << "| 3. 주문 수정." << endl;

cout << "| 4. 주문 정산." << endl;

cout << "| 5. 현재 총 매출액." << endl;

cout << "| 6. 프로그램 종료." << endl;

cout << "----------------------------" << endl;

}


cout << "메뉴 번호 ";

cin >> menu;


if (menu < 0 || menu > 6)

{

cout << "잘못 입력하셨습니다." << endl;

}

else {

switch (menu) {

case 0 :

return Run(true);

case 1:

PrintOrder();

break;

case 2:

AddOrder();

break;

case 3:

EditOrder();

break;

case 4:

CalculateOrder();

break;

case 5:

cout << endl << "현재 총 매출액 : " << ::total << "원." << endl << endl;

break;

case 6:

return;

}

}

return Run(false);

}


void Order::AddObject(Object *pObj)

{

Object *pList = 0;


//pList = (Object *)malloc(sizeof(Object) * (count + 1));

pList = new Object[count+1];


for (int n = 0; n < count; n++)

{

pList[n].objName = this->pList[n].objName;

pList[n].objPrice = this->pList[n].objPrice;


}


pList[count].objName = pObj->objName;

pList[count].objPrice = pObj->objPrice;


//free(this->pList);

delete[] this->pList;


this->pList = pList;


count += 1;


return;

}


void Order::DeleteObject(int n)

{

Object *pList = 0;


if (n >= count) {

return;

}


//pList = (Object *)malloc(sizeof(Object) * (count - 1));

pList = new Object[count - 1];


for (int i = 0; i < n; i++) {

pList[i] = this->pList[i];

}


for (int i = n; i < count - 1; i++)

{

pList[i] = this->pList[i + 1];

}


//free(this->pList);

delete[] this->pList;


this->pList = pList;


count -= 1;


return;

}


void Order::Free(void)

{

//free(pList);

delete[] pList;


return;

}


void PrintOrder(void)

{

using namespace std;


int total = 0;


if (::count)

{

for (int n = 0; n < ::count; n++)

{

cout << endl << "주문 : " << n + 1 << ": " << endl;


for (int i = 0; i < ::pList[n].count; i++) {

cout << "\t이름: " << pList[n].pList[i].objName << endl;

cout << "\t가격: " << pList[n].pList[i].objPrice << endl;


total += pList[n].pList[i].objPrice;

}


cout << "\t총 가격: " << total << endl;

total = 0;

}

}

else {

cout << endl << "현재 주문이 없습니다." << endl;

}

cout << endl;


return;

}


void AddOrder(void)

{

using namespace std;


Order *pList = 0;

Order order = { 0, };


cout << endl;


cout << "주문을 추가합니다." << endl;


while (true)

{

string name;

int count = 0;


int n = 0;


cout << "주문에 추가할 물품의 정보(이름,개수)를 입력해주세요 : ";


cin >> name;

if (name == "0") break;


cin >> count;


if (count == 0) break;


for (n = 0; n < 2; n++) {

if (::objList[n].objName == name)

{

break;

}

}

if (n == 2)

{

cout << "물품 이름을 찾지 못하였습니다." << endl;

break;

}


for (int i = 0; i < count; i++) {

order.AddObject(&::objList[n]);

}

}


//pList = (Order *)malloc(sizeof(Order) * (::count + 1));

pList = new Order[::count + 1];


for (int i = 0; i < ::count; i++)

{

pList[i].pList = ::pList[i].pList;

pList[i].count = ::pList[i].count;

}


pList[::count].pList = order.pList;

pList[::count].count = order.count;


//free(::pList);

delete[] ::pList;

::pList = pList;


::count += 1;

cout << endl << "주문이 추가되었습니다." << endl << endl;


return;

}


void EditOrder(void)

{

using namespace std;


int index = 0;


string act;

string name;


cout << endl << "주문을 수정합니다." << endl;

cout << endl << "수정할 주문의 번호를 입력하십시오 : ";

cin >> index;


if (index <= 0 || index > ::count)

{

cout << "잘못된 주문 번호입니다." << endl << endl;

return;

}


index -= 1;


cout << "주문 " << index << ": " << endl;

for (int n = 0; n < ::pList[index].count; n++)

{

cout << "\t이름: " << ::pList[index].pList[n].objName << endl;

}

cout << "추가/제거와 상품 이름을 입력하십시요 : ";

cin >> act >> name;


if (act == "추가")

{

int n = 0;


for (n = 0; n < 2; n++)

{

if (name == ::objList[n].objName)

{

break;

}

}

if (n == 2)

{

cout << "잘못된 물품 이름입니다. " << endl << endl;

return;

}


::pList[index].AddObject(&::objList[n]);

}

else if (act == "제거")

{

int n = 0;


for (n = 0; n < ::pList[index].count; n++)

{

if (name == ::pList[index].pList[n].objName) {

break;

}

}


if (n == ::pList[index].count)

{

cout << "물품이 주문에 존재하지 않습니다." << endl << endl;

return;

}


::pList[index].DeleteObject(n);

}

else {

cout << "잘못된 입력값입니다." << endl << endl;

return;

}


cout << endl << "수정이 완료되었습니다." << endl << endl;


return;

}


void CalculateOrder(void)

{

using namespace std;


int index = 0;

int total = 0;

int amount = 0;


Order *pList = 0;


cout << endl << "주문을 정산합니다." << endl << endl;


cout << "정산할 주문의 번호를 입력해주세요: ";

cin >> index;


if (index <= 0 || index > ::count)

{

cout << "잘못된 주문 번호입니다. " << endl << endl;

return;

}


index -= 1;


cout << "주문 " << index << ": " << endl;

for (int n = 0; n < ::pList[index].count; n++)

{

cout << "\t이름: " << ::pList[index].pList[n].objName << endl;

total += ::pList[index].pList[n].objPrice;

}

cout << "받아야 할 금액 : " << total << endl << endl;


cout << "받은 금액을 입력해주세요: ";

cin >> amount;


if (total > amount)

{

cout << "받은 금액이 받아야 할 총 금액보다 적습니다." << endl << endl;

return;

}


::total += total;


cout << "남은 금액은 " << amount - total << "원 입니다." << endl << endl;


pList = new Order[::count - 1];

for (int n = 0; n < index; n++)

{

pList[n] = ::pList[n];

}


for (int n = index; n < ::count - 1; n++)

{

pList[n] = ::pList[n + 1];

}


delete[] ::pList;

::pList = pList;

::count -= 1;


cout << "정산이 완료되었습니다. " << endl << endl;


return;

}