During session 8
During the session, we will discuss an example of the exam:
Also we strongly recommend to take up a challenge and solve the exercise below. Don't be frightened, the exam exercise will not be so difficult.
Exercise
Write a program that reads data about person’s salary and spendings from a file. The data in the file is written in the format month;year;salary;money spent, e.g.
Nov;2014;1000;1000 Dec;2014;990;900 Jan;2015;1200;1000 Feb;2015;1250;1250 Mar;2015;1110;900 Apr;2015;1110;1100 May;2015;1200;1300
The program has to ask the user for a year (which must be between 1900 and 2018). If the entered year is invalid, the program continues asking the user for a year. If the entered year is valid, the program calculates how well the person saved the money. The output of the program (for the year 2015) should look like this:
Enter year: 2015 -------------------------------------------------- In Jan 2015, you saved 200 EUR. In Feb 2015, you spent all the money you earned that month. In Mar 2015, you saved 210 EUR. In Apr 2015, you saved 10 EUR. In May 2015, you were over the budget 100 EUR. -------------------------------------------------- We have found information about 5 months. You saved 320 EUR in total. The maximum amount you saved in one month that year was 210. That year you put some amount aside 3 month(s) in total. The maximum amount you were over the budget in one month that year was 100 EUR. That year you were over the budget 1 month(s) in total.
- Write a function ask_year that takes no arguments and returns the year that the user entered (that means the user has to be prompted for an input inside the function; also the check for a valid input has to be realised inside the function).
- Open the file (the file name does not have to be asked from the user either there is no need to make sure that the file exists) and go through each line of the file. For each line that matches the entered year, the program has to calculate the amount of savings. The output of the program depends on the amount:
- if the user was able to save something that month - In Jan 2015, you saved 200 EUR.
- if the user was over the budget - In May 2015, you were over the budget 100 EUR.
- if the user spent all the money - In Feb 2015, you spent all the money you earned that month.
- Count the number of lines that correspond to the queried year and output the number (We have found information about 5 months.).
- For all the months when it was possible to save money, store the amounts of savings in a list. For all the months when the budget was exceeded, store the amounts of being over the budget in another list.
- Write a function total_savings that takes those two lists (one that holds the savings and the other one that holds all the over the budget amounts) and returns the total balance of the queried year (which is a sum of the saved money, minus all the over the budget money).
- If there was at least 1 month for the queried year in the file, calculate the total balance of the year (using the corresponding function). If money was saved during the queried year, print out the corresponding message (You saved 320 EUR in total.). If it was over the budget, print out the corresponding message (You were over the budget 100 EUR in total.). If the money was spent exactly, print out the corresponding message (You spent all the money you earned and you are not in debt!).
- If there were more than 0 months when something was saved, print the maximum amount of savings in one month within the year (The maximum amount you saved in one month that year was 210.) and also the number of months when something was saved (That year you put some amount aside 3 month(s) in total.).
Do the same for the over the budget months (The maximum amount you were over the budget in one month that year was 100 EUR. and That year you were over the budget 1 month(s) in total.).