Hi Ollie,
Besides initialising the variable 'x', your program is also initializing the String array(records) every time.
Quote:
String[] records;
records = new String[10];
|
Hence, every time the user enters option as 'y', the rateCalculate() method is called again, and the above initialization occurs, which flushes the previosly stored values of records[].
To prevent this, you may intitalise both of these as 'STATIC' during the start of the code and then use the value of 'x' to circulate through the records array.
Also, when finally printing using the option 'r',the for loop needs to compare with x(number of values stored) and not with records.length(which stores the total length of the string array).
The new code is attached for your reference :-
Code:
import java.util.*;
public class sainsburys2 {
static String[] records = new String[100];
static int x=0;
public static void main(String[] args) {
rateCalculate(0);
}
public static void rateCalculate(int x)
{
String name;
int hours;
double pay;
double wage;
Scanner in = new Scanner(System.in);
String answer;
System.out.println("******************************************************************");
System.out.println("********* Welcome to your personal planner for the week **********");
System.out.println("******************************************************************");
System.out.println("Please enter your name to begin");
System.out.print(">");
name = in.next();
if (name.equalsIgnoreCase("Ollie")) {
System.out.println("\nhey Ollie, you made this program =]");
} else {
System.out.println("\nhello " + name.toUpperCase() + "\n");
}
System.out.println("\nPlease enter the amount of hours you will be working this week");
System.out.print(">");
hours = in.nextInt();
if (hours == 0) {
System.out.println("\nlazy goat, you should be shot!\n");
} else if(hours > 40) {
System.out.println("\nYou're working far too many hours!!\n");
}
System.out.println("\nOkay " + name + ", so you'll be working " + hours + " hours this week\n");
System.out.println("\nNow.. if you don't mind me asking, how much do you earn per hour?\n");
System.out.print("> $ ");
pay = in.nextDouble();
if (pay == 0) {
System.out.println("\nGet a different job, they're having a laugh\n");
} else if(pay >= 6) {
System.out.println("\nI wish i was earning as much as you =(\n");
}
System.out.println("\nOkay then, two seconds while i calculate how much you'll earn\n");
wage = pay * hours;
System.out.println("\nThis week you will be earning $" + wage);
System.out.println("\nwowzers!!\n");
records[x]= ("name: " + name.toUpperCase() + "\n hours: " + hours + "\n pay: $" + pay + "\n salary this week: $" + wage + "\n");
System.out.println(records[x]);
System.out.print("would you like to run through again? y or n please or r to view all records so far > ");
answer = in.next();
if (answer.equalsIgnoreCase("y")) {
System.out.println("n\n\n\n");
x++;
rateCalculate(x);
} else if (answer.equalsIgnoreCase("n")) {
System.exit(1);
} else if (answer.equalsIgnoreCase("r")) {
for (int i = 0;i<=x;i++) {
System.out.println(records[i]);
}
}
}
}