-1

this is the code i used in the LibraryCustomers.py module:

class Customer:

    """
    A class that represents the Customer object
    """

    
    def __init__(self,customer_id,customer_name,customer_city,customer_age):

"""
A function that contains all the relevant information of customers
:param customer_id: Customer's ID
:param customer_name: Customer's name
:param customer_city: Customer's city of living
:param customer_age: Customer's age'
"""

        

        self.customer_id = customer_id
        self.customer_name = customer_name
        self.customer_city = customer_city
        self.customer_age = customer_age
    
    def add_new_customer(self,customer_id, customer_name, customer_city, customer_age):


        """
        A function that add new customer to the Library
        :param customer_id: Customer's ID'
        :param customer_name: Customer's name'
        :param customer_city: Customer's city'
        :param customer_age: Customer's age'
        """


        new_customer = {'customer id':{customer_id}, 'customer name':{customer_name}, 'customer city':{customer_city}, 'customer age':{customer_age}}
        return new_customer

this is the code i used in the main.py: Adding a new customer (based on the input)

            customer_id_input = input("Enter customer's ID: ")
      " adding customer id "
            customer_name_input = input("Enter customer's name: ")
        " adding customer name"
            customer_city_input = input("Enter customer's city: ")
         " adding customer city"
            customer_age_input = input("Enter customer's age: ")
          " adding customer age"



            new_customers = Customer.add_new_customer(customers_library,customer_id_input,customer_name_input,customer_city_input,customer_age_input)
            customers_library.update(new_customers)
            # updated_customers_library = json.dumps(customers_library)
            print("Added customer...")
            print("Done!, Customer added successfully")
            print(f"\n{customers_library}")

every time i run this code it doesn't add the new customer to the customers dict in the main.py

001
  • 13,291
  • 5
  • 35
  • 66
dorbtz
  • 47
  • 5
  • `update` is used to insert into an existing dictionary. For a new item, add it to the dictionary with a key. But in your case, maybe `customers_library` should be a `list` of customers, not a dictionary? – 001 Dec 20 '21 at 15:54
  • @JohnnyMopp , thanks for helping. i'll try with list as well. if i have a dict and i just want to add the new customer by the keys of the dict, how can i do that? – dorbtz Dec 20 '21 at 15:59
  • Do you want more than one customer? Then you can use a dictionary of dictionaries. You can use one of the fields (ex: `customer_id`) as the key: `customers_library[customer_id] = new_customers`. – 001 Dec 20 '21 at 16:27
  • Yes sir, I have a project In this project i need to implement a simple system to manage books library: my system should be able to manage the following entities: Books (Add a new book) Customers (Add a new customer Loans (I will get to that later.) I want to add customers and books to my Library program, and I have separated them to 3 different files so in the main.py file I could write the main menu and get the functions that I need from the files. so in that case what do you think i should use? dict or still to try with LIST? – dorbtz Dec 20 '21 at 17:11
  • @JohnnyMopp ^^^ – dorbtz Dec 20 '21 at 19:51
  • It's really up to you. Use a dictionary of dictionaries if there is one main key (for example `customer_id` or `book_title`). With a list there is no main key so all keys are treated the same. It just depends on how you are going to use it. In my answer I use a list, but I'll update to use a dict to help you decide. – 001 Dec 20 '21 at 20:13
  • @JohnnyMopp Thank you for helping me understanding more, I wanna use dict so I could later use customer_name to find names from the dict or list, also with books I want to use function to find books by name from the list/dict I have. in that case i understand it better to use dict, I have the full code with the changes I made in GitHub, I would like if you take a look – dorbtz Dec 20 '21 at 20:23
  • [link](https://github.com/dorbtz/LibraryBooksManaging.git) - my github link. – dorbtz Dec 20 '21 at 20:29
  • I'm not sure you understand how dictionaries work. Each item in a dictionary has a key and a value. You cannot have 2 items with the same key. Given that you are using classes, I don't think you need dictionaries at all. The library should have 2 lists: a list of `Book` objects and a list of `Customer` objects. – 001 Dec 21 '21 at 12:56
  • Here's a stripped down version of your project with only lists: https://replit.com/@JohnnyMopp/FlimsyImprobableIde – 001 Dec 21 '21 at 13:16

1 Answers1

1

update is used to update/insert into an existing dictionary. For a new item, add it to the dictionary with a key. But in your case, if you plan on having more than one customer, maybe customers_library should be a list of customers, not a dictionary?

class Customer:
    def __init__(self, customer_id, customer_name, customer_city, customer_age):
        self.customer_id = customer_id
        self.customer_name = customer_name
        self.customer_city = customer_city
        self.customer_age = customer_age

    @staticmethod
    def create():
        customer_id_input = input("Enter customer's ID: ")
        customer_name_input = input("Enter customer's name: ")
        customer_city_input = input("Enter customer's city: ")
        customer_age_input = input("Enter customer's age: ")
        return Customer(customer_id_input,
                        customer_name_input,
                        customer_city_input,
                        customer_age_input)

    def __str__(self):
        return f"{self.customer_id},{self.customer_name},{self.customer_city},{self.customer_age}"

class Library:
    def __init__(self):
        self.customers = []

    def new_customer(self):
        self.customers.append(Customer.create())

    def __str__(self):
          return '\n'.join((str(c) for c in self.customers))
customers_library = Library()
print("Added customer...")
customers_library.new_customer()
customers_library.new_customer()
print("Done!, Customer added successfully")
print(customers_library)

To use a dictionary instead of a list, change the Library class to:

class Library:
    def __init__(self):
        self.customers = {}

    def new_customer(self):
        customer = Customer.create()
        # Here, the key is the id, but can be anything
        self.customers[customer.customer_id] = customer

    def __str__(self):
          return '\n'.join((str(c) for c in self.customers.values()))
001
  • 13,291
  • 5
  • 35
  • 66