I am trying to write an object-oriented restaurant order management system, and I have started by creating two classes in separate modules: CreateFile and Employee.
The Employee class is responsible for storing information about employees in a dictionary, such as their id ,name, phone number, and role.
The CreateFile class handles the tasks of saving, creating, loading, and updating files that are stored in a JSON format. The file will contain the information of the class instances. For example, if we have an instance x of the Employee class with the following information: Employee(4245, "John", "Smith", 07956, "waiter"), this information will be passed to the Employee class, assembled into a dictionary, and then stored in a JSON file.
I have successfully implemented the loading and saving of data. However, I am currently facing challenges with the "update" functionality. My goal is to retrieve the dictionary specific to a given instance and update it. This way, when I call the update method, I can pass any class instance, load the data, and update the corresponding dictionary.
the code for both classes is as follows:
import os
import json
class CreateFile:
    def __init__(self, data, file_name, key="", value=""):
        self.data = data
        self.file_name = file_name
        self.key = key
        self.value = value
    def create_file(self, data, file_name):
        info_to_write = json.dumps(data) + "\n"
        json_file = file_name
        if os.path.exists(json_file):
            with open(json_file, "a") as file:
                file.write(info_to_write)
        else:
            with open(json_file, "w") as file:
                file.write(info_to_write)
    
    def update_file(self, key, update_value, file_name, instance):
        loaded_data = self.load(file_name)
        for dicts in loaded_data:
            if dicts.get("emp_id") == instance.get_id():
                dicts[key] = update_value
        with open(file_name, 'w') as file:
            json.dump(loaded_data, file, indent=4)
    def load(self, file_name):
        with open(file_name, 'r') as file:
            content = file.read()
            data = json.loads(content)
        return data
Json_loading import CreateFile
class Employee:
    def __init__(self, emp_id, first_name, last_name, phone_num, role):
        self.emp_id = emp_id
        self.first_name = first_name
        self.last_name = last_name
        self.phone_num = phone_num
        self.role = role
        self.loader = CreateFile({}, "employee_data.json")
    def add_employee(self):  # should i use kwarggs?
        emp_dict = {
            "emp_id": self.emp_id,
            "first_name": self.first_name,
            "last_name": self.last_name,
            "phone_num": self.phone_num,
            "role": self.role
        }
        self.loader.create_file(emp_dict, "employee_data.json")
    def update_employee(self, key, new_value, file_name, instance):
        self.loader.update_file(key, new_value, file_name, instance)
    def get_info(self):
        return self.loader.load("employee_data.json")
I attempted to load the JSON file into a list of dictionaries and iterate over the list to find the dictionary that has the same key-value pair at index 0 as the instance I'm passing in as an argument (the instance is stored as a dictionary).
However, I encountered difficulties with this approach, as it seemed to be complex and not yielding the desired results. As a result, I was unable to successfully implement this functionality.
please note that each class is in a serrate module name Employee,Json_loading. if my approach is flawed or there is a better one i appreciate any and all feedback
