Skip to content

Latest commit

 

History

History
132 lines (103 loc) · 3.86 KB

File metadata and controls

132 lines (103 loc) · 3.86 KB

Designing an Online Auction System

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.

System Requirements

The Online Auction System should:

  1. Auction Management: Create and manage auctions with item details, starting prices, and durations.
  2. User Account Management: Handle user registrations for sellers and bidders.
  3. Bidding Process: Allow users to place and track bids.
  4. Auction Monitoring: Enable users to view ongoing auctions and status.
  5. Transaction Processing: Handle winning bid transactions.

Core Use Cases

  1. Creating and Managing Auctions
  2. Registering and Managing User Accounts
  3. Placing and Tracking Bids
  4. Monitoring Auction Progress
  5. Processing Transactions

Key Classes:

  • OnlineAuctionSystem: Manages the system.
  • User: Represents a system user.
  • Auction: Manages auction details.
  • Bid: Represents a user's bid.

Python3 Implementation

User Class

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 = email

Auction Class

Represents 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_amount

Bid Class

Represents a bid.

class Bid:
    def __init__(self, bidder: User, amount: float):
        self.bidder: User = bidder
        self.amount: float = amount

OnlineAuctionSystem Class

Manages 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 None

Example Usage

def 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()