You want to read through this file six lines at a time, and do calculations based on the contents of each set. First, you need a place to put your values, and a static inner class would be useful:
private static class EmployeeData {
final String firstName;
final String lastName;
final int age;
final BigDecimal income;
final BigDecimal savings;
final BigDecimal raise;
public EmployeeData(String firstName, String lastName, int age, BigDecimal income, BigDecimal savings, BigDecimal raise) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.income = income;
this.savings = savings;
this.raise = raise;
}
}
Now you have a useful data structure into which you can put the lines your scanner is reading. (Note we're using BigDecimals instead of doubles, because we're doing financial calculations. That matters.)
Scanner in = new Scanner(new File("midterm.txt"));
while (in.hasNext()){
String firstName = in.nextLine();
String lastName = in.nextLine();
int age = in.nextInt();
BigDecimal income = in.nextBigDecimal();
BigDecimal savings = in.nextBigDecimal();
BigDecimal raise = in.nextBigDecimal();
EmployeeData employeeData = new EmployeeData(firstName, lastName, age, income, savings, raise);
BigDecimal power = calculatePower(employeeData);
}
You're now set up to do your calculations, which I'd put into appropriately named methods:
private static BigDecimal calculatePower(EmployeeData employeeData){
int ageDifference = RETIREMENT_AGE - employeeData.age;
BigDecimal base = employeeData.raise.add(BigDecimal.valueOf(1L));
BigDecimal baseRaised = BigDecimal.valueOf(Math.pow(base.doubleValue(), ageDifference));
return employeeData.income.multiply(baseRaised);
}
And you'll need to specify the constants being used:
private static final BigDecimal INTEREST = BigDecimal.valueOf(.07);
private static final int RETIREMENT_AGE = 70;
private static final BigDecimal WITHDRAWAL_PERCENT = BigDecimal.valueOf(.04);
private static final BigDecimal SAVINGS_RATE = BigDecimal.valueOf(.15);
So what do you think? Does this give you enough of a start? Here's the entire program so far; I've added annotations to explain in more detail what's going on:
import java.math.BigDecimal;
import java.util.*;
// The file name will have to match the class name; e.g. Scratch.java
public class Scratch {
// These are values that are created when your class is first loaded.
// They will never change, and are accessible to any instance of your class.
// BigDecimal.valueOf 'wraps' a double into a BigDecimal object.
private static final BigDecimal INTEREST = BigDecimal.valueOf(.07);
private static final int RETIREMENT_AGE = 70;
private static final BigDecimal WITHDRAWAL_PERCENT = BigDecimal.valueOf(.04);
private static final BigDecimal SAVINGS_RATE = BigDecimal.valueOf(.15);
public static void main(String[] args) {
// midterm.txt will have to be in the working directory; i.e.,
// the directory in which you're running the program
// via 'java -cp . Scratch'
Scanner in = new Scanner(new File("midterm.txt"));
// While there are still more lines to read, keep reading!
while (in.hasNext()){
String firstName = in.nextLine();
String lastName = in.nextLine();
int age = in.nextInt();
BigDecimal income = in.nextBigDecimal();
BigDecimal savings = in.nextBigDecimal();
BigDecimal raise = in.nextBigDecimal();
// Put the data into an EmployeeData object
EmployeeData employeeData = new EmployeeData(firstName, lastName, age, income, savings, raise);
// Calculate power (or whatever you want to call it)
BigDecimal power = calculatePower(employeeData);
System.out.println("power = " + power);
}
}
private static BigDecimal calculatePower(EmployeeData employeeData){
int ageDifference = RETIREMENT_AGE - employeeData.age;
// With big decimals, you can't just use +, you have to use
// .add(anotherBigDecimal)
BigDecimal base = employeeData.raise.add(BigDecimal.valueOf(1L));
BigDecimal baseRaised = BigDecimal.valueOf(Math.pow(base.doubleValue(), ageDifference));
return employeeData.income.multiply(baseRaised);
}
// A static inner class to hold the data
private static class EmployeeData {
// Because this class is never exposed to the outside world,
// and because the values are final, we can skip getters and
// setters and just make the variable values themselves accessible
// directly by making them package-private (i.e., there is no '
// private final' or even 'public final', just 'final')
final String firstName;
final String lastName;
final int age;
final BigDecimal income;
final BigDecimal savings;
final BigDecimal raise;
public EmployeeData(String firstName, String lastName, int age, BigDecimal income, BigDecimal savings, BigDecimal raise) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.income = income;
this.savings = savings;
this.raise = raise;
}
}
}