Untitled
import csv # Function to read the CSV file into a list of dictionaries def read_csv(filename): musicians = [] with open(filename, mode='r', newline='') as file: reader = csv.DictReader(file) for row in reader: musicians.append(row) return musicians # Function to display a list of musicians def show_musicians(musicians): for index, musician in enumerate(musicians, start=1): print(f"{index}. {musician['Artiestennaam']} - Genre: {musician['Genre']}, Instrument: {musician['Instrument']}, Uurprijs: {musician['uurprijs']}") # Function to show musicians of a specific genre def show_musicians_by_genre(musicians, genre): filtered_musicians = [musician for musician in musicians if musician['Genre'].lower() == genre.lower()] if filtered_musicians: show_musicians(filtered_musicians) else: print(f"No musicians found in the {genre} genre.") # Function to show musicians with a specific instrument def show_musicians_by_instrument(musicians, instrument): filtered_musicians = [musician for musician in musicians if musician['Instrument'].lower() == instrument.lower()] if filtered_musicians: show_musicians(filtered_musicians) else: print(f"No musicians found with {instrument} as their instrument.") # Function to sort musicians by cost in descending order def sort_musicians_by_cost(musicians): sorted_musicians = sorted(musicians, key=lambda x: float(x['uurprijs']), reverse=True) show_musicians(sorted_musicians) # Function to calculate cost based on number of hours and hourly rate for a musician by name def calculate_cost_by_name(musicians, musician_name, hours): found = False for musician in musicians: if musician['Artiestennaam'].lower() == musician_name.lower(): try: hourly_rate = float(musician['uurprijs']) cost = hours * hourly_rate print(f"Cost for {musician['Artiestennaam']} for {hours} hours: {cost}") found = True break except ValueError: print("Invalid hourly rate.") if not found: print(f"Musician with name '{musician_name}' not found.") # Function to add musician(s) def add_musician(musicians): num_musicians_to_add = int(input("Enter the number of musicians to add: ")) for _ in range(num_musicians_to_add): artist_name = input("Enter artist name: ") genre = input("Enter genre: ") instrument = input("Enter instrument: ") uurprijs = input("Enter hourly rate: ") musicians.append({'Artiestennaam': artist_name, 'Genre': genre, 'Instrument': instrument, 'uurprijs': uurprijs}) # Function to delete a musician by name def delete_musician_by_name(musicians, musician_name): found = False for musician in musicians: if musician['Artiestennaam'].lower() == musician_name.lower(): musicians.remove(musician) found = True break if not found: print(f"Musician with name '{musician_name}' not found.") # Function to change instrument of a musician by name def change_instrument_by_name(musicians, musician_name, new_instrument): found = False for musician in musicians: if musician['Artiestennaam'].lower() == musician_name.lower(): musician['Instrument'] = new_instrument found = True break if not found: print(f"Musician with name '{musician_name}' not found.") # Function to change hourly rate of a musician by name def change_hourly_rate_by_name(musicians, musician_name, new_hourly_rate): found = False for musician in musicians: if musician['Artiestennaam'].lower() == musician_name.lower(): musician['uurprijs'] = new_hourly_rate found = True break if not found: print(f"Musician with name '{musician_name}' not found.") # Function to update data to the CSV file def update_data(filename, musicians): with open(filename, mode='w', newline='') as file: fieldnames = ['Artiestennaam', 'Genre', 'Instrument', 'uurprijs'] writer = csv.DictWriter(file, fieldnames=fieldnames) writer.writeheader() writer.writerows(musicians) print("Data updated to the CSV file.") # Main program filename = 'Muziekdata.csv' musicians = read_csv(filename) print("\nChoose an option:") print("1. User") print("2. Admin") print("3. Quit") choice = input("Enter your choice: ") while choice != "3": if choice == '1': print("\nUser functionalities:") print("0. Quit") print("1. Show all musicians") print("2. Show musicians of a specific genre") print("3. Show musicians with a specific instrument") print("4. Sort musicians by cost (descending order)") print("5. Calculate cost for a musician") user_choice = input("Enter your choice (1-5): ") if user_choice == '0': exit() elif user_choice == '1': show_musicians(musicians) elif user_choice == '2': genre = input("Enter the genre: ") show_musicians_by_genre(musicians, genre) elif user_choice == '3': instrument = input("Enter the instrument: ") show_musicians_by_instrument(musicians, instrument) elif user_choice == '4': sort_musicians_by_cost(musicians) elif user_choice == '5': musician_name = input("Enter the name of the musician: ") hours = float(input("Enter the number of hours: ")) calculate_cost_by_name(musicians, musician_name, hours) elif choice == '2': print("\nAdmin functionalities:") print("0. Quit") print("1. Add musician(s)") print("2. Delete musician") print("3. Change instrument") print("4. Change hourly rate") print("5. Update data to CSV") admin_choice = input("Enter your choice (1-5): ") if admin_choice == '0': exit() elif admin_choice == '1': add_musician(musicians) elif admin_choice == '2': musician_name = input("Enter the name of the musician to delete: ") delete_musician_by_name(musicians, musician_name) elif admin_choice == '3': musician_name = input("Enter the name of the musician to change instrument: ") new_instrument = input("Enter the new instrument: ") change_instrument_by_name(musicians, musician_name, new_instrument) elif admin_choice == '4': musician_name = input("Enter the name of the musician to change hourly rate: ") new_hourly_rate = input("Enter the new hourly rate: ") change_hourly_rate_by_name(musicians, musician_name, new_hourly_rate) elif admin_choice == '5': update_data(filename, musicians) elif choice == '3': break # Save changes before exiting update_data(filename, musicians) print("\nGoodbye!")
Leave a Comment