Tutorial :A Simple Expense calculator using Java, Python and C#.
Wrote a simple expense calculator using 3 different languages such as,
- Python
- Java
- C#
Out of all the 3 languages, I prefer python due to its simplicity.😃
Overview:
Input : A text file containing the expenses for each day in CSV format as below,
mon,100,200,300,400 tue,50,40,30,20 wed,1,2,3,4,5 thu,1,2,3,4,5 fri,1,1,1,1,1 sat,0 sun,5
Output : The program calculates the total expenses for each day and finally prints the sum of all expenses in the week.
Coding Logic:
The underlying logic is same in all languages.
- Reading the input file (In this case myexpenses.txt).
- Reading each line and breaking up the CSV values.
- Adding all values in each lines to get daily expense (stored is totalExpensePerDay).
- Finally, adding all the expenses of every day of week ( stored in weeklyExpense).
Python :
weeklyExpense = 0
txtFile = open("d://myexpenses.txt", "r")
txtLines = txtFile.readlines()
print("\n\nMy Expense Calculator ( Using Python ): \n======================================\n\n");
print("\t DAY \tTotal Expenses ");
print("\t --- --------------");
for singleLine in txtLines:
#Print the day
extractData = singleLine.split(",")
print("\t ", extractData[0], end="")
#Calculate the sum of all expenses
totalExpensePerDay = 0;
for expenseCount in range(1 ,len(extractData)):
totalExpensePerDay += int(extractData[expenseCount])
print("\t ", totalExpensePerDay)
weeklyExpense += totalExpensePerDay
print("\n\nYou have spent Rs: ",weeklyExpense," this week!")
print("\n\nEnd of program....")
Java :
C#:
Thats it!