summaryrefslogtreecommitdiff
path: root/graphs/cpp/my_nfts/auction.cc
blob: 534c61d1bdcc67053f04ddcda2cffb0070db3b46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "auction.hh"

#include <algorithm>
Auction::Auction(Person& organizer, NFT nft, uint initial_bid)
    : organizer_{ organizer }
    , nft_{ std::move(nft) }
    , highest_bidder_{ nullptr }
    , highest_bid_{ initial_bid }
{
    if (!nft_ || nft_->empty())
        throw std::invalid_argument{ "Invalid nft!" };
}
Auction::~Auction()
{
    if (highest_bidder_ == nullptr)
        organizer_.add_nft(std::move(nft_));
    else
    {
        highest_bidder_->add_nft(std::move(nft_));
        organizer_.add_money(highest_bid_);
    }
}
bool Auction::bid(Person& person, uint money)
{
    if (money > highest_bid_)
    {
        if (highest_bidder_ == nullptr)
            highest_bidder_ = &person;
        else
            highest_bidder_->add_money(highest_bid_);
        highest_bid_ = money;
        highest_bidder_ = &person;
        highest_bidder_->remove_money(money);
        return true;
    }
    return false;
}
const std::string& Auction::get_nft_name() const
{
    return *nft_;
}
uint Auction::get_highest_bid() const
{
    return highest_bid_;
}