Perl Project

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
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:
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.
 

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
Alright I did some work on it and got it so when you change the number in the expense sheet it changes it when you get the report, but the thing is if you run the program and just go right to the report it says it has no values. I guess since I haven't put any in, how do I get the report to have the beginning values in it at first.

Here's my new code:

Code:
open(EXPENSE, "expenses.txt");
@data = <EXPENSE>;
close(EXPENSE);

 foreach $raw (@data)
        {
        chomp($raw);
        ($name,$money)=split(/\:/,$raw);
        push(@array, $money);
        }


$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 "Please enter the new expense record for food: ";
    $money[0]=<>;
  }
 elsif($x eq "b")
  {
    print "Please enter the new expense record for books: ";
    $money[1]=<>;
  }
 elsif($x eq "c")
  {
    print "Please enter the new expense record for clothes: ";
    $money[2]=<>;
  }
 elsif($x eq "d")
  {
    print "Please enter the new expense record for car repairs: ";
    $money[3]=<>;
  }
 elsif($x eq "e")
  {
    print "Please enter the new expense record for gas: ";
    $money[4]=<>;
  }
 elsif($x eq "f")
  {
    print "Please enter the new expense record for movies: ";
    $money[5]=<>;
  }
 elsif($x eq "g")
  {
    print "Please enter the new expense record for music: ";
    $money[6]=<>;
  }
}
}
#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 ";
    print $money[0];
    print " dollars on food\n";
  }
 elsif($x eq "b")
  {
    print "You have spent ";
    print $money[1];
    print " dollars on books\n";
  }
 elsif($x eq "c")
  {
    print "You have spent ";
    print $money[2];
    print " dollars on clothes\n";
  }
 elsif($x eq "d")
  {
    print "You have spent ";
    print $money[3];
    print " dollars on car repairs\n";
  }
 elsif($x eq "e")
  {
    print "You have spent ";
    print $money[4];
    print " dollars on gas\n";
  }
 elsif($x eq "f")
  {
    print "You have spent ";
    print $money[5];
    print " dollars on movies\n";
  }
 elsif($x eq "g")
  {
    print "You have spent ";
    print $money[6];
    print " dollars on music\n";
  }
}
}








elsif($input eq "c")
  {

      $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";
  }
}