Creating Methods

1
2
3
4
5
6
7
8
9
10
11
public class Main {

public static void main(String[] args) {
String message = greetUser("Lucas", "YE");
System.out.println(message);
}

public static String greetUser(String firstName, String lastName){
return "Hello " + firstName + " " + lastName;
}
}

Extracting Methords

改变代码的结构却不改变它的行为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public class Main {
public static void main(String[] args) {
int principal = (int) readNumber("Principal (1K - 1M): ", 1000, 1_000_000);
float annualInterest = (float) readNumber("Annual Interest Rate: ", 1, 30);
byte years = (byte) readNumber("Period (Year): ", 1, 30);

double mortgage = getMortgage(principal, annualInterest, years);

String mortgageFormatted = NumberFormat.getCurrencyInstance().format(mortgage);
System.out.print("Mortgage: " + mortgageFormatted);
}

public static double readNumber(String prompt, double min, double max) {
Scanner scanner = new Scanner(System.in);
double value;
while (true) {
System.out.print(prompt);
value = scanner.nextDouble();
if (value >= min && value <= max)
break;
System.out.println("Enter a number between " + min + " and " + max + ".");
}
return value;
}

public static double getMortgage(int principal,
float annualInterest,
byte years) {
final byte MONTHS_IN_YEAR = 12;
final byte PERCENT = 100;

float monthlyInterest = annualInterest / MONTHS_IN_YEAR / PERCENT;
short numberOfPayments = (short) (years * MONTHS_IN_YEAR);

double mortgage = principal
* (monthlyInterest * Math.pow(1 + monthlyInterest, numberOfPayments))
/ (Math.pow(1 + monthlyInterest, numberOfPayments) - 1);
return mortgage;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
public class Main {
final static byte MONTHS_IN_YEAR = 12;
final static byte PERCENT = 100;

public static void main(String[] args) {
int principal = (int) readNumber("Principal (1K - 1M): ", 1000, 1_000_000);
float annualInterest = (float) readNumber("Annual Interest Rate: ", 1, 30);
byte years = (byte) readNumber("Period (Year): ", 1, 30);

printMortgage(principal, annualInterest, years);
printPaymentSchedule(principal, annualInterest, years);
}

private static void printMortgage(int principal, float annualInterest, byte years) {
double mortgage = calculateMortgage(principal, annualInterest, years);
String mortgageFormatted = NumberFormat.getCurrencyInstance().format(mortgage);
System.out.println();
System.out.println("MORTGAGE");
System.out.println("--------");
System.out.println("Monthly Payments: " + mortgageFormatted);
}

private static void printPaymentSchedule(int principal, float annualInterest, byte years) {
System.out.println();
System.out.println("PAYMENT SCHEDULE");
System.out.println("----------------");
short numberOfPayments = (short) (years * MONTHS_IN_YEAR);
for(short numberOfPaymentsMade = 1; numberOfPaymentsMade <= numberOfPayments; numberOfPaymentsMade++) {
double balance = calculateBalance(principal, annualInterest, years, numberOfPaymentsMade);
String balanceFormatted = NumberFormat.getCurrencyInstance().format(balance);
System.out.println(balanceFormatted);
}
}

public static double readNumber(String prompt, double min, double max) {
Scanner scanner = new Scanner(System.in);
double value;
while (true) {
System.out.print(prompt);
value = scanner.nextDouble();
if (value >= min && value <= max)
break;
System.out.println("Enter a number between " + min + " and " + max + ".");
}
return value;
}

public static double calculateMortgage(int principal,
float annualInterest,
byte years) {
float monthlyInterest = annualInterest / MONTHS_IN_YEAR / PERCENT;
short numberOfPayments = (short) (years * MONTHS_IN_YEAR);

double mortgage = principal
* (monthlyInterest * Math.pow(1 + monthlyInterest, numberOfPayments))
/ (Math.pow(1 + monthlyInterest, numberOfPayments) - 1);

return mortgage;
}

public static double calculateBalance(int principal,
float annualInterest,
byte years,
short numberOfPaymentsMade) {
float monthlyInterest = annualInterest / MONTHS_IN_YEAR / PERCENT;
short numberOfPayments = (short) (years * MONTHS_IN_YEAR);

double balance = principal
* (Math.pow(1 + monthlyInterest, numberOfPayments)
- Math.pow(1 + monthlyInterest, numberOfPaymentsMade))
/ (Math.pow(1 + monthlyInterest, numberOfPayments) - 1);

return balance;
}
}