본문 바로가기

Studies/A Structured Approach Using C++

Project 4-41 Create customers' bills 영수증 발행

//============================================================================
//  제출일 : 2005. 3. 12
//  작업환경 :  Windows XP SP1, VC++ 6.0, Pentium4
//  연습문제 4-41
//  : Call by Reference를 사용하기 위하여 메인함수에 값을 받아오는 함수를
//    추가하였습니다. 클래스 구조를 사용할 경우 굳이 사용할 필요는 없습니다만
//    주어진 조건에 충실하기 위하여 추가하였습니다.
//============================================================================


#include <iostream.h>
#include <iomanip.h>
#define LABOR 0.35  //고정변수 : Labor cost per sqare fit
#define TAX 8.5 //%  //고정변수 : Tex rate

class Bill    //청구서를 발행하는 클래스
{
 public:
  Bill();
  ~Bill();
  void setData(int _length, int _width, int _discount, float carpet); //사용자로부터 읽어온 값을 클래스의 데이터로 set함
  void calculate(void);            //계산 시작
  void printResult(void);            //결과 출력

 private:
  int length;
  int width;
  int discount;
  int area;
  float carpet;
  float carpet_cost;
  float labor_cost;
  float intalled_price;
  float discount_cost;
  float subtotal;
  float tax;
  float total;

  void calcInstall(void);            //계산에 필요한 세 가지 함수
  void clacSubTotal(void);
  void calcTotal(void);
};

Bill::Bill() //생성자
{}    //Do nothing
 
Bill::~Bill() //소멸자
{}    //Do nothing

void Bill::setData(int _length, int _width, int _discount, float _carpet)  //입력받은 변수를 저장하는 함수
{
 length = _length;
 width = _width;
 discount = _discount;
 carpet = _carpet;
 return;
}

void Bill::calculate(void)              //계산의 시작
{
 calcInstall();
 clacSubTotal();
 calcTotal();
 return;
}

void Bill::calcInstall(void)             //시설비 계산
{
 area = length * width;
 carpet_cost = area * carpet;
 labor_cost = (float)(area * LABOR);
 intalled_price = carpet_cost + labor_cost;
 return;
}

void Bill::clacSubTotal(void)             //SubTotal 계산
{
 discount_cost = intalled_price * discount/100;
 subtotal = intalled_price - discount_cost;
 return;
}

void Bill::calcTotal(void)              //Total 계산
{
 tax = (float)(subtotal * TAX/100);
 total = subtotal + tax;
 return;
}

void Bill::printResult(void)             //결과의 출력
{
 cout << "\n\n\t\tMEASUREMENT\n";
 cout.setf( ios::right); //오른쪽 정렬
 cout <<"Length\t\t\t"<<setw(7)<<setfill(' ')<<length<<"\tfeet\n";
 cout <<"Width\t\t\t"<<setw(7)<<setfill(' ')<<width<<"\tfeet\n";
 cout <<"Area\t\t\t"<<setw(7)<<setfill(' ')<<area<<"\tfeet\n";

 cout << "\t\tCHAREGES\n\n";
 cout << "DESCRIPTION\tCOST/SQ.FT.\tCHARGE/ROOM\n";
 cout << "-----------\t-----------\t-----------\n";
 cout <<"Carpet\t\t"<<setw(11)<<setfill(' ')<< carpet<<"\t"<<setw(11)<<setfill(' ')<<carpet_cost<<"\n";
 cout <<"Labor\t\t"<<setw(11)<<setfill(' ')<< LABOR<<"\t"<<setw(11)<<setfill(' ')<<labor_cost<<"\n";
 cout <<"\t\t\t\t-----------\n";
 cout <<"INSTALLED PRICE\t\t\t"<<setw(11)<<setfill(' ')<<intalled_price<<"\n";
 cout <<"Discount\t"<<setw(10)<<setfill(' ')<< discount<<'%'<<"\t"<<setw(11)<<setfill(' ')<<discount_cost<<"\n";
 cout <<"\t\t\t\t-----------\n";
 cout <<"SUBTOTAL\t\t\t"<<setw(11)<<setfill(' ')<<subtotal<<"\n";
 cout <<"TAX\t\t\t\t"<<setw(11)<<setfill(' ')<<tax<<"\n";
 cout <<"TOTAL\t\t\t\t"<<setw(11)<<setfill(' ')<<total<<"\n";

 return;
}
//END of class Bill

//Subfunction of MAIN
void getData(int &_length, int &_width, int &_discount, float &_cost); //Call by Reference를 사용하기 위하여 추가한 함수

int main()
{
 int _length;              //Call by Reference를 사용하기 위하여 추가한 변수
 int _width;
 int _discount;
 float _carpet;
 getData(_length, _width, _discount, _carpet);      //사용자로부터 값을 입력받음
 Bill bill;               //클래스 생성
 bill.setData(_length, _width, _discount, _carpet);     //데이터 저장
 bill.calculate();             //계산  
 bill.printResult();             //결과 출력
 
 return 0;

}

//Subfunction of MAIN
void getData(int &_length, int &_width, int &_discount, float &_carpet) //Call by Reference를 사용하기 위하여 추가한 함수
{
 cout << "Length of room (feet)? ";
 cin >> _length;
 cout << "Width of room (feet)? ";
 cin >> _width;
 cout << "Customer discount (feet)? ";
 cin >> _discount;
 cout << "Cost per square foot (xxx.xx)? ";
 cin >> _carpet;
}