Building an Automotive Auction App in Python
Are you a car enthusiast looking to dip your toes into the world of programming? Or perhaps you're a developer interested in building a fun project to showcase your skills? In this article, I, Waran Gajan Bilal, will walk you through how to create a simple automotive auction app for bidding using Python.
Introduction
An automotive auction app allows users to bid on cars up for auction. Our app will consist of two main components: a Car
class to represent each car being auctioned, and an Auction
class to manage the auction process.
Building the Car Class
The Car
class will have attributes such as make, model, year, starting price, current bid, and bidder. Here's how we can define the Car
class:
class Car:
def __init__(self, make, model, year, starting_price):
self.make = make
self.model = model
self.year = year
self.starting_price = starting_price
self.current_bid = starting_price
self.bidder = None
def bid(self, amount, bidder):
if amount > self.current_bid:
self.current_bid = amount
self.bidder = bidder
print(f"Bid of ${amount} accepted for {self.make} {self.model} by {bidder}")
else:
print(f"Sorry, your bid of ${amount} is too low for {self.make} {self.model}")
def __str__(self):
return f"{self.year} {self.make} {self.model}, Current Bid: ${self.current_bid}, Bidder: {self.bidder}"
Implementing the Auction Class
The Auction
class will manage the auction process, including starting the auction and accepting bids. Here's how the Auction
class can be implemented:
class Auction:
def __init__(self, car):
self.car = car
def start_auction(self):
print(f"Auction started for {self.car.make} {self.car.model} - Starting price: ${self.car.starting_price}")
while True:
try:
bid_amount = float(input("Enter your bid amount: $"))
bidder_name = input("Enter your name: ")
self.car.bid(bid_amount, bidder_name)
except ValueError:
print("Please enter a valid bid amount.")
except KeyboardInterrupt:
print("\nAuction ended.")
break
Putting It All Together
Now that we have our Car
and Auction
classes defined, we can create instances of the Car
class and initiate auctions using the Auction
class. Here's an example:
car1 = Car("Toyota", "Corolla", 2018, 5000)
auction1 = Auction(car1)
auction1.start_auction()
Conclusion
In this article, I've built a simple automotive auction app for bidding using Python. This project serves as a great starting point for beginners to practice object-oriented programming concepts and develop their skills further. Feel free to expand upon this project by adding features such as multiple cars for auction, a user interface, or persistence using databases.
Happy coding!Feel free to adjust the content as needed before posting it on your Hashnode blog!