summaryrefslogtreecommitdiff
path: root/graphs/cpp/my_nfts/auction.cc
diff options
context:
space:
mode:
Diffstat (limited to 'graphs/cpp/my_nfts/auction.cc')
-rw-r--r--graphs/cpp/my_nfts/auction.cc45
1 files changed, 45 insertions, 0 deletions
diff --git a/graphs/cpp/my_nfts/auction.cc b/graphs/cpp/my_nfts/auction.cc
new file mode 100644
index 0000000..534c61d
--- /dev/null
+++ b/graphs/cpp/my_nfts/auction.cc
@@ -0,0 +1,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_;
+} \ No newline at end of file