您想一次通读此文件六行,并根据每组的内容进行计算。首先,您需要一个放置值的地方,并且一个静态内部类将很有用:
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;
}
}
现在,您有了一个有用的数据结构,可以在其中放入扫描仪正在读取的行。 (请注意,我们正在使用BigDecimals而不是double,因为我们正在进行财务计算。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);
}
您现在已准备好进行计算,我已将其放入适当命名的方法中:
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);
}
您将需要指定要使用的常量:
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);
那你怎么看?这是否给您足够的开始?到目前为止,这是整个程序;我添加了注释以更详细地说明发生了什么:
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,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,BigDecimal raise) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.income = income;
this.savings = savings;
this.raise = raise;
}
}
}
本文链接:https://www.f2er.com/2539784.html