Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
2.3 kB
0
Indexable
Never
class Color:
    def __init__(self, name, price, quantity):
        self.name = name
        self.price = price
        self.quantity = quantity

class ColorTradingApp:
    def __init__(self):
        self.colors = []

    def add_color(self, name, price, quantity):
        color = Color(name, price, quantity)
        self.colors.append(color)

    def buy_color(self, name, quantity):
        for color in self.colors:
            if color.name == name:
                if color.quantity >= quantity:
                    total_price = color.price * quantity
                    color.quantity -= quantity
                    return f"Bought {quantity} {color.name} for ${total_price}"
                else:
                    return f"Not enough {color.name} in stock."
        return "Color not found."

    def sell_color(self, name, quantity):
        for color in self.colors:
            if color.name == name:
                color.quantity += quantity
                return f"Sold {quantity} {color.name}."
        return "Color not found."

    def display_colors(self):
        for color in self.colors:
            print(f"{color.name}: ${color.price} ({color.quantity} available)")

# Sample usage
if __name__ == "__main__":
    app = ColorTradingApp()
    app.add_color("Red", 10, 50)
    app.add_color("Blue", 8, 70)
    app.add_color("Green", 12, 30)

    print("Welcome to Color Trading App!")
    while True:
        print("\nMenu:")
        print("1. Buy color")
        print("2. Sell color")
        print("3. Display available colors")
        print("4. Exit")
        choice = input("Enter your choice: ")

        if choice == "1":
            color_name = input("Enter color name: ")
            quantity = int(input("Enter quantity: "))
            print(app.buy_color(color_name, quantity))
        elif choice == "2":
            color_name = input("Enter color name: ")
            quantity = int(input("Enter quantity: "))
            print(app.sell_color(color_name, quantity))
        elif choice == "3":
            app.display_colors()
        elif choice == "4":
            print("Exiting...")
            break
        else:
            print("Invalid choice. Please choose again.")
Leave a Comment