Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CS160 Computer Science Lab 11 Objectives Work with dictionaries Work with functions Work with files Overview This lab will have you store the birthdays for

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed
CS160 Computer Science Lab 11 Objectives Work with dictionaries Work with functions Work with files Overview This lab will have you store the birthdays for a group of people. This program will store the information with the focus on the date of the month and not on the individual. The point of this program is to be able to see who has a birthday on a given day or to create a table of birthdays, in order of occurrence, for a given month. To do you will need a dictionary that uses the birth date for the key and the name for the value. To ensure that every gets recognized on their birthday, since someone always brings in treats or cake on a birthday, we will only allow for a single birthday per day. If a specific day is already taken then that person should pick a different date. Specifics To get started create a text file using any text editor that will store a series of names and their birthdays. Each line will have a name, a colon separator, and a date. There is no need to store the month, all entries in any given file will be from the same month. An example of a data file, such as "DecemberBirthdays . txt", might be: Scott : 1 Bob : 10 Sue : 12 Alex : 14 Alice: 14 Fred : 22 Use the provided code to display a menu. This will accept the user's choice of what they wish to do within the program and return the integer value associated with the menu choice. The menu options will be opening a file, adding a name, searching by date, displaying all birthdays for the month, and exiting the program. On screen the menu might look like: What would you like to do? 1. Open a data file 2. Add new name 3. Search by date 4. Display all birthdays for the month 5. Exit program Your choice?The menu will allow the user to enter a value between 1 and 5. After each valid entry, perform the specified action and then present the menu again. The primed loop example used for data entry will work well for this as well. The pseudo-code for using the input returned from the menu might be: choice = showMenu() while choice != 5 if choice == 1: open file and return a dictionary with the info elif choice == 2: allow the user to add new name/birthday elif choice == 3: ask for a date and display the associated name elif choice == 4: display all datesames for the month choice = showMenu() write the dictionary back to the original file Specifics for menu options Each menu option (1-4) should be implemented in a function. When reading a data file, ask for the name of a file and then add that information from the file into a dictionary. Ask for the file name in any manner you have done so this year. Ask for the file BEFORE calling the function which reads the data file. When adding new entries, ask for a name and a date and then add them to the dictionary. If the date entered already has a name, state that the date is taken and that the info will not be added to the dictionary and exit the function. Only add one name/birthday per call of the function - no loop needed in this function. When searching by date, allow the user to enter the date. Then display the name associated with that specified date. If no one has a birthday on that date display a message stating so. Continue to perform searches until the user enters a date of 0. You will need a loop in this function. When printing the list of all birthdays in the month, make sure that the name column is left justified and the date column is right justified. Make sure to allow for a reasonable amount of space for the name column (at least 10 characters) Finally, when the user chooses to exit the program write all the information from the dictionary out to the filename specified earlier in the program. Write out one entry per person. Don't ask for a new file name, use the file name the user provided when opening the file.An example of running the program might be something like the following. The program output is in blue. This is not actual output. And remember that the dates will not necessarily be in order. We've discussed in class how to print a dictionary in sorted order. Take that as a challenge, but NOT A REQUIREMENT for the lab. If you want to have a little more fun. offer the choice of printing out the information sorted by name or date. Again. not a requirement. but an interesting chaenge. what would you like to do? 1. Open a data file 2. Add new name 3. Search by date 4. Display all birthdays For the month 5 Exit program Your choice? 1 user selects a file using input() or FileUtils.select0penFi1e ())> what would you like to do? . Open a data file Add new name . Search by date . Display all birthdays For the month Exit program who-IMP Your choice? 4 Tom 1 Bob 16 Scott 29 what would you like to do? 1. Open a data file Add new name . Search by date Display all birthdays For the month Exit program unhuuN Your choice? 2 Name to add: Angie Date for Angie: 39 Angie has been added to the birthday list what would you like to do? 1. Open a data file 2. Add new name 3. Search by date 4. Display all birthdays For the month 5 Exit program Your choice? 4 Tom 1 Bob 16 Scott 28 Angie 39 what would you like to do? 1. Open a data file Add new name 2 3. Search by date 4. Display all birthdays for the month 5 Exit program Your choice? 2 Name to add: Fred Date for Fred: 38 Sorry, that date is taken. Try again with a different date. what would you like to do? Open a data file Add new name Search by date Display all birthdays For the month Exit program Ulla-LUMP Your choice? 2 Name to add: Fred Date For Fred: 31 Fred has been added to the birthday list what would you like to do? 1 Open a data file 2. Add new name 3. Search by date 4 Display all birthdays For the month 5 Exit program Your choice? 5 > Tomzl Bobzla Scott:2a Angie:3@ Fredz31 Code for showing the menu and getting the user's choice: def printMenuChoices ( ) : print ("What would you like to do?") print (" 1. Open a data file") print (" 2. Add new name" ) print (" 3. Search by date" ) print (" 4. Display all birthdays for the month") print (" 5. Exit program") print () def showMenu () : printMenuChoices ( ) menuChoice = int (input ("Your choice? ")) while menuChoice 5: print ("Please enter a valid choice (1-5)") print () menuChoice = int ( input ("Your choice? ")) return menuChoice option = showMenu( ) X O'

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Android 4 Application Development

Authors: Reto Meier

3rd Edition

1118223853, 9781118223857

More Books

Students also viewed these Programming questions