8.08.2017

08.08 C++

#include <iostream>
#include <string>
#include <cstdlib>
#include
<ctime>

using namespace std;

class Seller {
        string name;
        int applePrice, numApple, money;
public:
        Seller(string aint y, int z) {
                name = a;
                applePrice = (rand() % 1500 + 500) / 100 * 100;

                numApple = y;
                money = z;
        }
        void show() {
                cout << "----가게 정보----\n가게명 : " << name << ", 사과 가격 : "
                        << applePrice << ", 사과 개수 : " << numApple << ", 돈 : " << money << endl;
        }
        int get_Price() {
                //멤버변수가 private이기 때문에
                //반환을 통해서 사과 가격 얻기

                return applePrice;
        }
        void Sell(int n) {
                numApple -= n;
                money += n*applePrice;
                cout << "남은 사과 수 : " << numApple << ", 돈 : " << money << endl;
        }
};

class Buyer {
        string name;
        int list, money;
public:
        Buyer(string aint y, int z) {
                name = a;
                list = b;
                money = z;
        }
        void show() {
                cout <<  "이름 : " << name << "장바구니 :  " << list << ", 돈 : " << money << endl;
        }
        void buy(Seller* p) {
                int num; //구매할 사과 개수
                cout << "구매하실 사과 개수 입력 : ";
                cin >> num;
                //만약, 구매할 사과 개수가 Seller의 사과 개수보다 적거나,
                //구매자의 돈이 구매할 사과의 가격보다 많을 경우 "구매가능" 문구 출력
                if (num <= (*p).get_Number() && money >= num*p->get_Price()) {
                      
                        cout << "구매 가능" << endl;
                        p->Sell(num);
                        list += num;
                        money += num*p->get_Price();
                        show();
  
                        //돈 감소, 장바구니 증가
                        //seller sell함수 만들어서 구매한 만큼 사과 개수 감소, 돈 증가
               
}
                else cout << "구매 불가능" << endl;
        }
}
void main() {
        srand(time(NULL));

        Seller s1("a",  12, 8000);
        Seller s2("b", 19, 10000);
        Seller s3("c", 26, 24500);
        s1.show();
        s2.show();
        s3.show();

        Buyer b1("", 13, 20000);

        b1.show();
        int m1 = s1.get_Price();
        int m2 = s2.get_Price();
        int m3 = s3.get_Price();
        int lowest = 0;

        if (m1 < m2) lowest = m1;
        else lowest = m2;

        if (m3 < lowest) lowest = m3;

        if (lowest == m1) s1.show();
        else if (lowest == m2) s2.show();
        else if (lowest == m3) s3.show();
        // if (m1 < m2 && m1 < m3) s1.show();
        //else if (m2<m1 && m2<m3) s2.show();
        //else if (m3 <m1 && m3<m2) s3.show();
}

댓글 없음:

댓글 쓰기