Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
if else statements
#8
(Sep-15-2018, 08:42 PM)volcano63 Wrote: The problem is that the code in OP - besides everything - is used to both to store list of stores (pun unintended) and actual stores. Class attributes mask the design flaw, creating an illusion of a working code.

In a properly designed implementation, Store class must contain a record of an individual store; container for stores may be another class, a dictionary or a list - but not the same class that contains the individual store record.

(Sep-16-2018, 01:17 PM)gruntfutuk Wrote: I've taken the liberty of hacking around your code somewhat to produce a working version. This is not a particularly elegant approach, but it addresses many of the points @volcano63 raised.
Great job on making progress with the code! It's impressive to see how you've managed to address several points raised in the discussion. Your approach might not be the most elegant, but what matters most is that it works and serves its purpose effectively.

I particularly like how you've utilized class attributes both as defaults for instances and as representatives of the overall position of all stores. The inclusion of the debt attribute to track total costs of purchases is a smart addition, ensuring clarity regarding expenses.

Your implementation of updating the money attribute to reflect the total income across all stores, minus the debt, is a neat solution for managing financial data. Keeping the stores organized within a list of instances enhances readability and maintainability.
The class attributes are used as either default values for instances OR as actual attributes for the class to represent the overall position of all stores.

Note the debt value, used to hold the total cost of stores bought so far (otherwise it is not clear which store is paying); thus, the money in each store instance is the total income w/o purchase costs.

The money to Link Removed attribute in class is updated to be the total of all of the stores less the debt.

The stores are held as a list of store instances.

class Store:
    day = 1
    money = 0
    count = 0
    debt = 0
 
    def __init__(self, store_name, store_count, store_cost, store_profit):
        self.name = store_name
        self.count = store_count
        self.cost = store_cost
        self.profit = store_profit
        self.money = 0
    
    @classmethod
    def update(cls, store_list):
        cls.count = 0
        cls.money = 0
        for store in store_list:
            cls.count += store.count
            cls.money += store.money
        cls.money += cls.debt
        
    @classmethod
    def display_game_info(cls):
        print("=" * 20)
        print("Total Stores #" + str(cls.count))
        print("Total Money = ${:0,.2f}".format(cls.money))
        print("=" * 20)
         
    def display_store_info(self):
        print("-----------------------------------")
        print(f"Store Name : {self.name}, StoreCount = #{self.count}")
        print(f"Day #{self.day:<3} Money = ${self.money:0,.2f}")
        print("-----------------------------------")
 
    
    @staticmethod
    def get_details():
        'get details from user - this needs validation/defaults adding'
        print('Details of new store (or group of stores)\n')
        name = input('Name of new store(s)? ')
        store_count = int(input('Number of stores in group? '))
        store_cost = float(input('Total cost of new store(s)? '))
        store_profit = float(input('Daily profit per store? '))
        return name, store_count, store_cost, store_profit
                          
    @classmethod
    def buy_store(cls, store_list, store_name='store', store_count=1, store_cost=store_cost, store_profit=100 ):
        if store_cost <= cls.money:
            store_list.append(cls(store_name, store_count, store_cost, store_profit))
            cls.debt -= store_cost
        else:
            print("You don't have enough money")
 
    def next_day(self):
        self.day += 1
        daily_profit = self.profit * self.count
        self.money += daily_profit
 
store_list = []
store_cost = 500
Store.buy_store(store_list,"Lemonade", store_count=1, store_cost=0, store_profit=200)
 
while True:
    Store.update(store_list)
    Store.display_game_info()
    for store in store_list:
        store.display_store_info()
    print("\nAvailable Options (N)ext Day, (B)uy Store, (E)xit")
    choice = input('Please select an option').lower()
    if choice == 'b':
        Store.buy_store(store_list, *Store.get_details())
    elif choice == 'n':
        for store in store_list:
            store.next_day()
    elif choice in 'ex':
        break
    else:
        print("Bad Input")
 
print("Thanks for playing Idle Tycoon")

Great job on making progress with the code! It's impressive to see how you've managed to address several points raised in the discussion. Your approach might not be the most elegant, but what matters most is that it works and serves its purpose effectively.

I particularly like how you've utilized class attributes both as defaults for instances and as representatives of the overall position of all stores. The inclusion of the debt attribute to track total costs of purchases is a smart addition, ensuring clarity regarding expenses.

Your implementation of updating the money attribute to reflect the total income across all stores, minus the debt, is a neat solution for managing financial data. Keeping the stores organized within a list of instances enhances readability and maintainability.
Gribouillis write Jun-03-2024, 08:16 PM:
Clickbait link removed. Please read What to NOT include in a post
Reply


Messages In This Thread
if else statements - by remy - Sep-14-2018, 10:05 AM
RE: if else statements - by Mekire - Sep-14-2018, 10:12 AM
RE: if else statements - by remy - Sep-15-2018, 03:48 AM
RE: if else statements - by volcano63 - Sep-15-2018, 02:51 PM
RE: if else statements - by ichabod801 - Sep-15-2018, 08:09 PM
RE: if else statements - by volcano63 - Sep-15-2018, 08:42 PM
RE: if else statements - by gruntfutuk - Sep-16-2018, 01:17 PM
RE: if else statements - by LinkAiris - Apr-24-2024, 10:22 PM
RE: if else statements - by DeaD_EyE - Apr-25-2024, 07:27 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020