Skip to content

Refactor the view table methods #4

@AkunoCode

Description

@AkunoCode

The view_products(), view_sales(), view_customers(), view_admins() methods could be refactored by creating a new display_table() method which takes a table name and a list of column names as arguments

Sample code from chatGPT:

def display_table(self, table_name, columns):
    """Display data from the specified table"""

    # Query the table and fetch the data
    self.cursor.execute(f"SELECT * FROM {table_name}")
    data = self.cursor.fetchall()

    # Display the data in a tabular format
    labels = "\t".join(columns)
    print(labels)

    for row in data:
        values = "\t".join(str(x) for x in row)
        print(values)

    input("Press Enter to continue...\n")

def view_products(self):
    """Display all products in the database"""

    # Define the columns to display
    columns = ["Product ID", "Name", "Price", "Quantity in Stock"]

    # Call the display_table function with the appropriate arguments
    self.display_table("products", columns)

Metadata

Metadata

Assignees

Labels

documentationImprovements or additions to documentationenhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions