Language: Python First, define a Product class that includes all the information about a single product. a. You will need a constructor to initialize new instances. Objects of this class should have three instance variables: name, price, and quantity in-stock. b. Add a method to your class that takes an integer count and determines whether that many of the product are in stock. c. Add another method that takes a count and returns the total cost of that many of the product. d. Finally, add a method that takes a count and removes that many of the product from the stock. Next, replace the three product lists with a single list of Product instances. Modify the rest of the code to correctly use the attributes and methods of the Products in the list. product_names = ["Ultrasonic range finder", "Servo motor", "Servo controller", "Microcontroller Board", "Laser range finder", "Lithium polymer battery" ] product_prices = [2.50, 14.99, 44.95, 34.95, 149.99, 8.99] product_quantities = [4, 10, 5, 7, 2, 8] def print_stock(): print() print("Available Products") print("------------------") for i in range(0, len(product_names)): if product_quantities[i] > 0: print(str(i)+")", product_names[i], "$", product_prices[i]) print() def main(): cash = float(input("How much money do you have? $")) while cash > 0: print_stock() vals = input( "Enter product ID followed by the quantity you wish to buy (or 'quit' to exit): ").split(" ") if vals[0] == "quit": break prod_id = int(vals[0]) count = int(vals[1]) if product_quantities[prod_id] >= count: if cash >= product_prices[prod_id] * count: product_quantities[prod_id] -= count cash -= product_prices[prod_id] * count print("You purchased", count, product_names[prod_id] + ".") print(f"You have ${cash:.2f} remaining.") else: print("Sorry, you cannot afford that product.") else: print("Sorry, we are sold out of", product_names[prod_id]) if __name__ == '__main__': main()
Language: Python
- First, define a Product class that includes all the information about a single product. a. You will need a constructor to initialize new instances. Objects of this class should have three instance variables: name, price, and quantity in-stock. b. Add a method to your class that takes an integer count and determines whether that many of the product are in stock. c. Add another method that takes a count and returns the total cost of that many of the product. d. Finally, add a method that takes a count and removes that many of the product from the stock.
- Next, replace the three product lists with a single list of Product instances.
- Modify the rest of the code to correctly use the attributes and methods of the Products in the list.
product_names = ["Ultrasonic range finder", "Servo motor", "Servo controller", "Microcontroller Board", "Laser range finder", "Lithium polymer battery"
]
product_prices = [2.50, 14.99, 44.95, 34.95, 149.99, 8.99]
product_quantities = [4, 10, 5, 7, 2, 8]
def print_stock():
print()
print("Available Products")
print("------------------")
for i in range(0, len(product_names)):
if product_quantities[i] > 0:
print(str(i)+")", product_names[i], "$", product_prices[i])
print()
def main():
cash = float(input("How much money do you have? $"))
while cash > 0:
print_stock()
vals = input(
"Enter product ID followed by the quantity you wish to buy (or 'quit' to exit): ").split(" ")
if vals[0] == "quit":
break
prod_id = int(vals[0])
count = int(vals[1])
if product_quantities[prod_id] >= count:
if cash >= product_prices[prod_id] * count:
product_quantities[prod_id] -= count
cash -= product_prices[prod_id] * count
print("You purchased", count, product_names[prod_id] + ".")
print(f"You have ${cash:.2f} remaining.")
else:
print("Sorry, you cannot afford that product.")
else:
print("Sorry, we are sold out of", product_names[prod_id])
if __name__ == '__main__':
main()
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 4 images