In this article, we delve into the object-oriented design and implementation of an Online Auction System using Python3.
This system allows for the creation and management of auctions, user participation in bidding, and handling transactions.
The Online Auction System should:
- Auction Management: Create and manage auctions with item details, starting prices, and durations.
- User Account Management: Handle user registrations for sellers and bidders.
- Bidding Process: Allow users to place and track bids.
- Auction Monitoring: Enable users to view ongoing auctions and status.
- Transaction Processing: Handle winning bid transactions.
- Creating and Managing Auctions
- Registering and Managing User Accounts
- Placing and Tracking Bids
- Monitoring Auction Progress
- Processing Transactions
OnlineAuctionSystem: Manages the system.User: Represents a system user.Auction: Manages auction details.Bid: Represents a user's bid.
Manages user account information.
class User:
def __init__(self, user_id: str, name: str, email: str):
self.user_id: str = user_id
self.name: str = name
self.email: str = emailRepresents an auction.
from datetime import datetime
from typing import Dict
class Auction:
def __init__(self, auction_id: str, item_description: str, starting_price: float, end_time: datetime, seller: User):
self.auction_id: str = auction_id
self.item_description: str = item_description
self.starting_price: float = starting_price
self.end_time: datetime = end_time
self.seller: User = seller
self.bids: Dict[User, float] = {}
def place_bid(self, bidder: User, bid_amount: float):
if datetime.now() < self.end_time:
self.bids[bidder] = bid_amountRepresents a bid.
class Bid:
def __init__(self, bidder: User, amount: float):
self.bidder: User = bidder
self.amount: float = amountManages the online auction system operations.
from typing import List, Optional
class OnlineAuctionSystem:
def __init__(self):
self.users: List[User] = []
self.auctions: List[Auction] = []
def add_user(self, user: User):
self.users.append(user)
def add_auction(self, auction: Auction):
self.auctions.append(auction)
def place_bid(self, auction_id: str, bidder: User, bid_amount: float):
auction = self.find_auction_by_id(auction_id)
if auction:
auction.place_bid(bidder, bid_amount)
def find_auction_by_id(self, auction_id: str) -> Optional[Auction]:
for auction in self.auctions:
if auction.auction_id == auction_id:
return auction
return Nonedef main():
# Initialize the auction system
system = OnlineAuctionSystem()
# Create users and add them to the system
alice = User("001", "Alice Johnson", "alice@example.com")
bob = User("002", "Bob Smith", "bob@example.com")
system.add_user(alice)
system.add_user(bob)
# Create an auction
auction_end_time = datetime.now() + timedelta(days=1) # Auction ends in 1 day
auction = Auction("A001", "Vintage Clock", 100.0, auction_end_time, alice)
system.add_auction(auction)
# Users place bids
system.place_bid("A001", bob, 120.0)
system.place_bid("A001", alice, 150.0)
# Output the current highest bid
current_auction = system.find_auction_by_id("A001")
if current_auction:
highest_bid = max(current_auction.bids.values())
print(f"Highest bid: {highest_bid}")
if __name__ == "__main__":
main()