Sign in with
Sign up | Sign in
Your question

Perl Project

Last response: in Applications
Share
December 9, 2010 9:16:32 AM

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:
  1. open(EXPENSE, "expenses.txt");
  2. @data = <EXPENSE>;
  3. close(EXPENSE);
  4.  
  5.  
  6. $input = "";
  7.  
  8. print "\nExpense Menu\n";
  9. print "=============\n";
  10. print "a - Update expense amount for item\n";
  11. print "b - Report for paricular category\n";
  12. print "c - Total of all expenses\n";
  13. print "d - Print menu\n";
  14. print "q - Quit application\n\n";
  15.  
  16. while(1)
  17. {
  18. print "Enter your choice: ";
  19. chomp($input = <STDIN>);
  20.  
  21. if($input eq "q")
  22. {
  23. last;
  24. }
  25.  
  26.  
  27. #Update Menu
  28. elsif($input eq "a")
  29. {
  30. $x = "";
  31.  
  32. print "\nUpdate Menu\n";
  33. print "==============\n";
  34. print "a - Food\n";
  35. print "b - Books\n";
  36. print "c - Clothes\n";
  37. print "d - Car Repairs\n";
  38. print "e - Gas\n";
  39. print "f - Movies\n";
  40. print "g - Music\n";
  41. print "q - Quit report\n";
  42.  
  43. while(1)
  44. {
  45. print "Enter your choice: ";
  46. chomp($x = <STDIN>);
  47.  
  48. if($x eq "q")
  49. {
  50. print "\nExpense Menu\n";
  51. print "=============\n";
  52. print "a - Update expense amount for item\n";
  53. print "b - Report for paricular category\n";
  54. print "c - Total of all expenses\n";
  55. print "d - Print menu\n";
  56. print "q - Quit application\n\n";
  57.  
  58. last;
  59. }
  60. elsif($x eq "a")
  61. {
  62. print "You have spent 10 dollars on food\n";
  63. }
  64. elsif($x eq "b")
  65. {
  66. print "You have spent 75 dollars on books\n";
  67. }
  68. elsif($x eq "c")
  69. {
  70. print "You have spent 40 dollars on clothes\n";
  71. }
  72. elsif($x eq "d")
  73. {
  74. print "You have spent 190 dollars on car repairs\n";
  75. }
  76. elsif($x eq "e")
  77. {
  78. print "You have spent 80 dollars on gas\n";
  79. }
  80. elsif($x eq "f")
  81. {
  82. print "You have spent 30 dollars on movies\n";
  83. }
  84. elsif($x eq "g")
  85. {
  86. print "You have spent 22 dollars on music\n";
  87. }
  88. }
  89. }
  90.  
  91.  
  92.  
  93.  
  94. #Report Menu
  95. elsif($input eq "b")
  96. {
  97. $x = "";
  98.  
  99. print "\nReport Menu\n";
  100. print "==============\n";
  101. print "a - Food\n";
  102. print "b - Books\n";
  103. print "c - Clothes\n";
  104. print "d - Car Repairs\n";
  105. print "e - Gas\n";
  106. print "f - Movies\n";
  107. print "g - Music\n";
  108. print "q - Quit report\n";
  109.  
  110. while(1)
  111. {
  112. print "Enter your choice: ";
  113. chomp($x = <STDIN>);
  114.  
  115. if($x eq "q")
  116. {
  117. print "\nExpense Menu\n";
  118. print "=============\n";
  119. print "a - Update expense amount for item\n";
  120. print "b - Report for paricular category\n";
  121. print "c - Total of all expenses\n";
  122. print "d - Print menu\n";
  123. print "q - Quit application\n\n";
  124.  
  125. last;
  126. }
  127. elsif($x eq "a")
  128. {
  129. print "You have spent 10 dollars on food\n";
  130. }
  131. elsif($x eq "b")
  132. {
  133. print "You have spent 75 dollars on books\n";
  134. }
  135. elsif($x eq "c")
  136. {
  137. print "You have spent 40 dollars on clothes\n";
  138. }
  139. elsif($x eq "d")
  140. {
  141. print "You have spent 190 dollars on car repairs\n";
  142. }
  143. elsif($x eq "e")
  144. {
  145. print "You have spent 80 dollars on gas\n";
  146. }
  147. elsif($x eq "f")
  148. {
  149. print "You have spent 30 dollars on movies\n";
  150. }
  151. elsif($x eq "g")
  152. {
  153. print "You have spent 22 dollars on music\n";
  154. }
  155. }
  156. }
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165. elsif($input eq "c")
  166. {
  167.  
  168. foreach $raw (@data)
  169. {
  170. chomp($raw);
  171. ($name,$money)=split(/\:/,$raw);
  172. push(@array, $money);
  173. }
  174. $sum = 0;
  175. foreach (@array) {
  176. $sum += $_;
  177. }
  178. print "The total of all expenses is $sum dollars\n"
  179. }
  180.  
  181. elsif($input eq "d")
  182. {
  183. print "\nExpense Menu\n";
  184. print "=============\n";
  185. print "a - Update expense amount for item\n";
  186. print "b - Report for paricular category\n";
  187. print "c - Total of all expenses\n";
  188. print "d - Print menu\n";
  189. print "q - Quit application\n\n";
  190. }
  191. else
  192. {
  193. print "Invalid syntax. Type 'd' to get help.\n\n";
  194. }
  195. }



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.

More about : perl project

December 9, 2010 11:53:54 AM

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:

  1. open(EXPENSE, "expenses.txt");
  2. @data = <EXPENSE>;
  3. close(EXPENSE);
  4.  
  5. foreach $raw (@data)
  6. {
  7. chomp($raw);
  8. ($name,$money)=split(/\:/,$raw);
  9. push(@array, $money);
  10. }
  11.  
  12.  
  13. $input = "";
  14.  
  15. print "\nExpense Menu\n";
  16. print "=============\n";
  17. print "a - Update expense amount for item\n";
  18. print "b - Report for paricular category\n";
  19. print "c - Total of all expenses\n";
  20. print "d - Print menu\n";
  21. print "q - Quit application\n\n";
  22.  
  23. while(1)
  24. {
  25. print "Enter your choice: ";
  26. chomp($input = <STDIN>);
  27.  
  28. if($input eq "q")
  29. {
  30. last;
  31. }
  32.  
  33.  
  34. #Update Menu
  35. elsif($input eq "a")
  36. {
  37. $x = "";
  38.  
  39. print "\nUpdate Menu\n";
  40. print "==============\n";
  41. print "a - Food\n";
  42. print "b - Books\n";
  43. print "c - Clothes\n";
  44. print "d - Car Repairs\n";
  45. print "e - Gas\n";
  46. print "f - Movies\n";
  47. print "g - Music\n";
  48. print "q - Quit report\n";
  49. while(1)
  50. {
  51. print "Enter your choice: ";
  52. chomp($x = <STDIN>);
  53.  
  54. if($x eq "q")
  55. {
  56. print "\nExpense Menu\n";
  57. print "=============\n";
  58. print "a - Update expense amount for item\n";
  59. print "b - Report for paricular category\n";
  60. print "c - Total of all expenses\n";
  61. print "d - Print menu\n";
  62. print "q - Quit application\n\n";
  63.  
  64. last;
  65. }
  66.  
  67. elsif($x eq "a")
  68. {
  69. print "Please enter the new expense record for food: ";
  70. $money[0]=<>;
  71. }
  72. elsif($x eq "b")
  73. {
  74. print "Please enter the new expense record for books: ";
  75. $money[1]=<>;
  76. }
  77. elsif($x eq "c")
  78. {
  79. print "Please enter the new expense record for clothes: ";
  80. $money[2]=<>;
  81. }
  82. elsif($x eq "d")
  83. {
  84. print "Please enter the new expense record for car repairs: ";
  85. $money[3]=<>;
  86. }
  87. elsif($x eq "e")
  88. {
  89. print "Please enter the new expense record for gas: ";
  90. $money[4]=<>;
  91. }
  92. elsif($x eq "f")
  93. {
  94. print "Please enter the new expense record for movies: ";
  95. $money[5]=<>;
  96. }
  97. elsif($x eq "g")
  98. {
  99. print "Please enter the new expense record for music: ";
  100. $money[6]=<>;
  101. }
  102. }
  103. }
  104. #Report Menu
  105. elsif($input eq "b")
  106. {
  107. $x = "";
  108.  
  109. print "\nReport Menu\n";
  110. print "==============\n";
  111. print "a - Food\n";
  112. print "b - Books\n";
  113. print "c - Clothes\n";
  114. print "d - Car Repairs\n";
  115. print "e - Gas\n";
  116. print "f - Movies\n";
  117. print "g - Music\n";
  118. print "q - Quit report\n";
  119.  
  120. while(1)
  121. {
  122. print "Enter your choice: ";
  123. chomp($x = <STDIN>);
  124.  
  125. if($x eq "q")
  126. {
  127. print "\nExpense Menu\n";
  128. print "=============\n";
  129. print "a - Update expense amount for item\n";
  130. print "b - Report for paricular category\n";
  131. print "c - Total of all expenses\n";
  132. print "d - Print menu\n";
  133. print "q - Quit application\n\n";
  134.  
  135. last;
  136. }
  137.  
  138. elsif($x eq "a")
  139. {
  140. print "You have spent ";
  141. print $money[0];
  142. print " dollars on food\n";
  143. }
  144. elsif($x eq "b")
  145. {
  146. print "You have spent ";
  147. print $money[1];
  148. print " dollars on books\n";
  149. }
  150. elsif($x eq "c")
  151. {
  152. print "You have spent ";
  153. print $money[2];
  154. print " dollars on clothes\n";
  155. }
  156. elsif($x eq "d")
  157. {
  158. print "You have spent ";
  159. print $money[3];
  160. print " dollars on car repairs\n";
  161. }
  162. elsif($x eq "e")
  163. {
  164. print "You have spent ";
  165. print $money[4];
  166. print " dollars on gas\n";
  167. }
  168. elsif($x eq "f")
  169. {
  170. print "You have spent ";
  171. print $money[5];
  172. print " dollars on movies\n";
  173. }
  174. elsif($x eq "g")
  175. {
  176. print "You have spent ";
  177. print $money[6];
  178. print " dollars on music\n";
  179. }
  180. }
  181. }
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190. elsif($input eq "c")
  191. {
  192.  
  193. $sum = 0;
  194. foreach (@array) {
  195. $sum += $_;
  196. }
  197. print "The total of all expenses is $sum dollars\n"
  198. }
  199.  
  200. elsif($input eq "d")
  201. {
  202. print "\nExpense Menu\n";
  203. print "=============\n";
  204. print "a - Update expense amount for item\n";
  205. print "b - Report for paricular category\n";
  206. print "c - Total of all expenses\n";
  207. print "d - Print menu\n";
  208. print "q - Quit application\n\n";
  209. }
  210.  
  211. else
  212. {
  213. print "Invalid syntax. Type 'd' to get help.\n\n";
  214. }
  215. }

m
0
l
December 10, 2010 12:45:12 PM

Figured it out. Can close thread.
m
0
l
!