In: Computer Science
Financial Records
Create a program to store weekly expenses and revenues over a period of time. The program should
store the expenses and revenues in two global array variables. Values stored in the array at index k
represent the total revenues/expenses recorded during the k(th) week. Assume that the maximum number of weeks tracked is 250.
Add the following functions to your program:
-
A function that finds the total of all expenses incurred since week t for a given t.
-
A function that records the total expenses and revenues for the next unrecorded week. It must check if there is still space in the array.
-
A function that prints the number of weeks recorded
-
A function that prints all revenues and expenses recorded
-
A function that returns a pointer to the largest revenue recorded
Organize the program so the function prototypes are presented
first, and the definitions after the main
function. Your program should keep asking the user to select
one of the following options and execute
the corresponding function based on the user choice:
(A)
computed the total of all expenses,
(B)
add a new expense/revenue ,
(C)
print the number of weeks recorded,
(D)
print all revenues and expenses recorded,
(E)
print the largest revenue recorded, or
(F)
exit the program.
FinancialRecords.java
import java.util.Scanner;
class FinancialRecords{
private double expenses[] = new double[250];
private double revenues[] = new double[250];
private int noOfWeeksRecorded=0;//tracks that number
of weeks that have been recorded already
private int largestRevenueIndex;//tracks largest
revenue index that has been recorded
public static void main(String a[]){
FinancialRecords fr = new
FinancialRecords();
Scanner input = new
Scanner(System.in);//to take input from user
String choice;
do{
System.out.println("Enter 'F' to exit");
System.out.println("A-Computed the total of all expenses since
given week\nB-Add a new expense/revenue\nC-print the number of
weeks recorded\nD-print all revenues and expenses recorded\nE-print
the largest revenue recorded\nF-Exit the program");
choice =
input.next();
if(choice.equals("A")){
//asking user to
enter a week number
System.out.println("Enter a week
number:");
int num = input.nextInt();
System.out.println(fr.calculateTotalExpenseSinceT(num));
}
else
if(choice.equals("B")){
//asking user to enter expense and revenue for
next unrecorded week
System.out.println("Enter Expense:");
double expense = input.nextDouble();
System.out.println("Enter Revenue:");
double revenue = input.nextDouble();
fr.recordExpensesAndRevenues(expense,revenue);
}
else
if(choice.equals("C"))
System.out.println("Number of weeks
Recorded:"+fr.calculateNumberOfWeeksRecorded());
else
if(choice.equals("D"))
fr.printAllExpensesAndRevenues();
else
if(choice.equals("E"))
System.out.println("Largest
Revenue:"+fr.revenues[fr.findLargestRevenueRecordIndex()]);
else
if(!choice.equals("F"))
System.out.println("Enter valid Character!!!");//asking user to
enter valid character that are A,B,C,D,E,F;
}
while(!choice.equals("F"));//loop
untill user enters 'F';
}
public double calculateTotalExpenseSinceT(int
weekNumber){
//checking weeknumber is
valid(>0) and must be less than or equal to number of weeks
recorded
if(weekNumber>=1 &&
weekNumber<=calculateNumberOfWeeksRecorded()){
double
total=0.0;
for(int
i=weekNumber-1;i<calculateNumberOfWeeksRecorded();i++){//adding
all expenses since week number
total+=expenses[i];
}
return
total;
}
else{
//user asking
for unrecorded week expenses
System.out.println("This week has not yet been recorded!!!");
return
-1.0;
}
}
public void recordExpensesAndRevenues(double
expense,double revenue){
//checking if there is space in
arrays to record
if(calculateNumberOfWeeksRecorded()<250){
this.expenses[calculateNumberOfWeeksRecorded()] = expense;
this.revenues[calculateNumberOfWeeksRecorded()] = revenue;
//for tracking
largestRevenueIndex
if(revenues[this.largestRevenueIndex]<revenue)
this.largestRevenueIndex = this.noOfWeeksRecorded;
this.noOfWeeksRecorded++;
System.out.println("Record added successfully!");
}
else
System.out.println("Sorry! there is
no space to record!!!");
}
public int calculateNumberOfWeeksRecorded(){
return
this.noOfWeeksRecorded;
}
public void printAllExpensesAndRevenues(){
System.out.println("Expenses
Revenues");
for(int
i=0;i<calculateNumberOfWeeksRecorded();i++){
System.out.println(expenses[i]+" "+revenues[i]);
}
}
public int findLargestRevenueRecordIndex(){//returning
largest revenue index
return
this.largestRevenueIndex;
}
}