Home > Blockchain >  How to Get Specific Variables from One Class to Another?
How to Get Specific Variables from One Class to Another?

Time:09-19

I am currently in the processing of self making an inventory system, and am trying to better organize my code in specific classes and methods. Currently, I am trying to organize a class that does user input, sends that input to another class to use.

Here is my relevant code:

class user_interaction(object):

    def __init__(self):
        self.name = self.ask()
        self.options = self.ask_options()
        self.add_item_num = None
        self.add_item_price = None
        self.add_item_quant = None
        self.add_item_name = None

    def ask(self):
        while 1:
            name = input("What is your name?\n")
            if name == "":
                print("Ha! You have to enter a name!")
            else:
                print("Welcome to the Shepherdstown Bake Shop "   name)
                return name
            
        
                
    def ask_options(self):
        while 1:
            option = input('''What would you like to do? \n1. Add a Item: \n2. Delete a Item:\n3. Edit an Item: \n4. View Inventory \n5. End Program\n''')
            if option == '1':       
                print("Welcome to the adding process "   self.name)
                ????
                break

     def enter_data(message, typ):
            while True:
                try:
                    v = typ(input(message))
                except ValueError:
                    print(f"Thats not an {typ}!")
                    continue
                else:
                    break
            return v

   def add_item_interaction(self):
            self.add_item_num = enter_data("What is the items #?\n", int)
            self.add_item_price = enter_data("What is the items price?\n", float)
            self.add_item_quant = enter_data("What is the items quantity?\n", int)

            while True:
                self.add_name = enter_data("What is the items name?\n", str)
                if name == "":
                    print("Ha! You have to enter a name!")
                    continue
                break

and my other class

class DataBase_Management(object):
    
    def __init__(self):
        self.result = []


    def make_dict_items(self):
        with open("Items2.csv") as fp:
            reader = csv.reader(fp)
            labels = next(reader, None)
            result = []
            for row in reader:
                if row:
                    row[0] = int(row[0])
                    row[1] = float(row[1])
                    row[2] = int(row[2])
                    pairs = zip(labels, row)
                    self.result.append(dict(pairs))
    
    def add_item(self):
        #Make Entries the variables in new_row???
        new_row = [item_num, price, quant, name]
        with open("Items2.csv", "a ") as  fp:
           reader = csv.reader(fp)
           fp.seek(0)
           labels = next(reader, None)
           writer = csv.writer(fp)
           new_record = dict(zip(labels, new_row))
           self.result.append(new_record)
           writer.writerow(new_record.values())
           print("Item Added! Check Inventory Again to see!")

How would I make it so the entries gotten in class User_interaction, in the method add_item_interaction, are sent to the add_item method in the DataBase_Management class? For example, I am trying to make it where self.add_item_num = item_num in the add_item method in the database management class?

CodePudding user response:

It sounds like the User_Interaction class should have its own Database_Management instance and use it when necessary:

class User_Interaction:

    def __init__(self):
        self.database = DataBase_Management()
        ...

   def add_item_interaction(self):
            add_item_num = self.enter_data("What is the items #?\n", int)
            add_item_price = self.enter_data("What is the items price?\n", float)
            add_item_quant = self.enter_data("What is the items quantity?\n", int)

            while True:
                add_name = self.enter_data("What is the items name?\n", str)
                if add_name == "":
                    print("Ha! You have to enter a name!")
                    continue
                break
            self.database.add_item(add_item_num, add_item_price, add_item_quant, add_name)
    ...

class DataBase_Management:    
    def __init__(self):
        self.result = []

    
    def add_item(self, item_num, price, quant, name):
        ...
  • Related