Hello, here is what I have to do, did most of it but stuck on updating the file.
Write a program that will keep track of expense information in a file. The file is the expenses.txt file that can be found in this folder
The user of the application should be allowed to:
* · Update the expense amount for an item
* · Get a report on a particular category
* · Get a total of all expenses
You should print a menu and prompt the user to enter a choice. If appropriate, based on the choice, it should ask for the category and the amount.
NOTE: since this is an expense file, you should be able to add new values to the existing category expense as found in the file
Lines 27-87 is my update menu, just copied the report menu and was just gonna change the elsif's so that the user would pick like A and be able to change the food expense. Does that make sense?
Here is my code:
In my book I kind of know how to update the file but with like me as a programmer telling the program what to change. Question is how do I let a user enter the info that they want to change.
Here is the expense file:
food:10
books:75
clothes:40
car repairs:190
gas:80
movies:30
music:22
Hope I explained that right.
Hmmm looking at it, would just splitting them like I did in getting the running total and then being like $money = <>, would that change $money to the new expense the user enters? The problem I am thinking is will it change the whole file? So if I change FOOD to 20, then run the program and click FOOD under the report will it show 20 instead of 10.
Write a program that will keep track of expense information in a file. The file is the expenses.txt file that can be found in this folder
The user of the application should be allowed to:
* · Update the expense amount for an item
* · Get a report on a particular category
* · Get a total of all expenses
You should print a menu and prompt the user to enter a choice. If appropriate, based on the choice, it should ask for the category and the amount.
NOTE: since this is an expense file, you should be able to add new values to the existing category expense as found in the file
Lines 27-87 is my update menu, just copied the report menu and was just gonna change the elsif's so that the user would pick like A and be able to change the food expense. Does that make sense?
Here is my code:
Code:
open(EXPENSE, "expenses.txt");
@data = <EXPENSE>;
close(EXPENSE);
$input = "";
print "\nExpense Menu\n";
print "=============\n";
print "a - Update expense amount for item\n";
print "b - Report for paricular category\n";
print "c - Total of all expenses\n";
print "d - Print menu\n";
print "q - Quit application\n\n";
while(1)
{
print "Enter your choice: ";
chomp($input = <STDIN>);
if($input eq "q")
{
last;
}
#Update Menu
elsif($input eq "a")
{
$x = "";
print "\nUpdate Menu\n";
print "==============\n";
print "a - Food\n";
print "b - Books\n";
print "c - Clothes\n";
print "d - Car Repairs\n";
print "e - Gas\n";
print "f - Movies\n";
print "g - Music\n";
print "q - Quit report\n";
while(1)
{
print "Enter your choice: ";
chomp($x = <STDIN>);
if($x eq "q")
{
print "\nExpense Menu\n";
print "=============\n";
print "a - Update expense amount for item\n";
print "b - Report for paricular category\n";
print "c - Total of all expenses\n";
print "d - Print menu\n";
print "q - Quit application\n\n";
last;
}
elsif($x eq "a")
{
print "You have spent 10 dollars on food\n";
}
elsif($x eq "b")
{
print "You have spent 75 dollars on books\n";
}
elsif($x eq "c")
{
print "You have spent 40 dollars on clothes\n";
}
elsif($x eq "d")
{
print "You have spent 190 dollars on car repairs\n";
}
elsif($x eq "e")
{
print "You have spent 80 dollars on gas\n";
}
elsif($x eq "f")
{
print "You have spent 30 dollars on movies\n";
}
elsif($x eq "g")
{
print "You have spent 22 dollars on music\n";
}
}
}
#Report Menu
elsif($input eq "b")
{
$x = "";
print "\nReport Menu\n";
print "==============\n";
print "a - Food\n";
print "b - Books\n";
print "c - Clothes\n";
print "d - Car Repairs\n";
print "e - Gas\n";
print "f - Movies\n";
print "g - Music\n";
print "q - Quit report\n";
while(1)
{
print "Enter your choice: ";
chomp($x = <STDIN>);
if($x eq "q")
{
print "\nExpense Menu\n";
print "=============\n";
print "a - Update expense amount for item\n";
print "b - Report for paricular category\n";
print "c - Total of all expenses\n";
print "d - Print menu\n";
print "q - Quit application\n\n";
last;
}
elsif($x eq "a")
{
print "You have spent 10 dollars on food\n";
}
elsif($x eq "b")
{
print "You have spent 75 dollars on books\n";
}
elsif($x eq "c")
{
print "You have spent 40 dollars on clothes\n";
}
elsif($x eq "d")
{
print "You have spent 190 dollars on car repairs\n";
}
elsif($x eq "e")
{
print "You have spent 80 dollars on gas\n";
}
elsif($x eq "f")
{
print "You have spent 30 dollars on movies\n";
}
elsif($x eq "g")
{
print "You have spent 22 dollars on music\n";
}
}
}
elsif($input eq "c")
{
foreach $raw (@data)
{
chomp($raw);
($name,$money)=split(/\:/,$raw);
push(@array, $money);
}
$sum = 0;
foreach (@array) {
$sum += $_;
}
print "The total of all expenses is $sum dollars\n"
}
elsif($input eq "d")
{
print "\nExpense Menu\n";
print "=============\n";
print "a - Update expense amount for item\n";
print "b - Report for paricular category\n";
print "c - Total of all expenses\n";
print "d - Print menu\n";
print "q - Quit application\n\n";
}
else
{
print "Invalid syntax. Type 'd' to get help.\n\n";
}
}
In my book I kind of know how to update the file but with like me as a programmer telling the program what to change. Question is how do I let a user enter the info that they want to change.
Here is the expense file:
food:10
books:75
clothes:40
car repairs:190
gas:80
movies:30
music:22
Hope I explained that right.
Hmmm looking at it, would just splitting them like I did in getting the running total and then being like $money = <>, would that change $money to the new expense the user enters? The problem I am thinking is will it change the whole file? So if I change FOOD to 20, then run the program and click FOOD under the report will it show 20 instead of 10.