Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 3. public class Expenses{
- 4. public static void main(String[] args){
- 5. String expense[] = {"Coffee", "Snacks", "Tuition", "Gas", "Fun", "Website"};// The names of each expenses
- 6. int expFreq[] = {104, 52, 2, 24, 52, 12};// The frequency of each expense
- 7. double amount[] = {3.62, 12, 6500, 55, 25, 4.99};// The amounts of each expense
- 8. double total = 0;
- 9. System.out.printf("%-15s\t%-15s\t%-10s\t%10s\t%15s\n",
- 10. "Item", "Frequency", "Times", "Amount", "Annual Total");
- 11. for(int i = 0; i < 6; i++){ // do not hard code 6; use array length property
- 12. outputInfo(expense[i], getLabel(expFreq[i]), expFreq[i], amount[i]);
- 13. total += expFreq[i] * amount[i];
- 14. }
- 15. System.out.printf("%-15s\t%-15s\t%-10s\t%10s\t%15.2f\n",
- 16. "Total", " ", " ", " ", total);
- 17. }
- 18. public static String getLabel(int times){
- 19. String freq = "";
- 20. switch(times){// Switch statement to determine a string-value for Frequency
- 21. case 2:
- 22. freq = "Bi-Yearly";
- 23. break;
- 24. case 12:
- 25. freq = "Monthly";
- 26. break;
- 27. case 24:
- 28. freq = "Bi-Monthly";
- 29. break;
- 30. case 52:
- 31. freq = "Weekly";
- 32. break;
- 33. case 104:
- 34. freq = "Bi-Weekly";
- 35. break;
- 36. default:
- 37. freq = "Invalid Frequency";// inconsistent with flowchart
- 38. break;
- 39. }
- 40. return freq;
- 41. }
- 42. public static void outputInfo(String item, String freq, int times, double amount){
- 43. System.out.printf("%-15s\t%-15s\t%-10s\t%10s\t%15s\n",
- 44. item, freq, times, amount, times * amount);
- 45. }// not exactly what was required… in requirement, the method should call getLabel to get label based on times.
- 46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement