Tutorial :A Simple Expense calculator using Java, Python and C#.
By Praveen Kumar

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.

No alt text provided for this image

Coding Logic:

The underlying logic is same in all languages.

  1. Reading the input file (In this case myexpenses.txt).
  2. Reading each line and breaking up the CSV values.
  3. Adding all values in each lines to get daily expense (stored is totalExpensePerDay).
  4. 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 :

No alt text provided for this image


C#:

No alt text provided for this image

Thats it!



To view or add a comment, sign in

More articles by Praveen A

Explore content categories