refactoring : divergent change - move function
This commit is contained in:
@@ -0,0 +1,25 @@
|
|||||||
|
package com.example.refactoring._07_divergent_change._25_move_function;
|
||||||
|
|
||||||
|
public class Account {
|
||||||
|
|
||||||
|
private int daysOverdrawn;
|
||||||
|
|
||||||
|
private AccountType type;
|
||||||
|
|
||||||
|
public Account(int daysOverdrawn, AccountType type) {
|
||||||
|
this.daysOverdrawn = daysOverdrawn;
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getBankCharge() {
|
||||||
|
double result = 4.5;
|
||||||
|
if (this.daysOverdrawn() > 0) {
|
||||||
|
result += this.type.overdraftCharge(this.daysOverdrawn());
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int daysOverdrawn() {
|
||||||
|
return this.daysOverdrawn;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package com.example.refactoring._07_divergent_change._25_move_function;
|
||||||
|
|
||||||
|
public class AccountType {
|
||||||
|
private boolean premium;
|
||||||
|
|
||||||
|
public AccountType(boolean premium) {
|
||||||
|
this.premium = premium;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isPremium() {
|
||||||
|
return this.premium;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double overdraftCharge(int daysOverdrawn) {
|
||||||
|
if (this.isPremium()) {
|
||||||
|
final int baseCharge = 10;
|
||||||
|
if (daysOverdrawn <= 7) {
|
||||||
|
return baseCharge;
|
||||||
|
} else {
|
||||||
|
return baseCharge + (daysOverdrawn - 7) * 0.85;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return daysOverdrawn * 1.75;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package com.example.refactoring._07_divergent_change._25_move_function._before;
|
||||||
|
|
||||||
|
public class Account {
|
||||||
|
|
||||||
|
private int daysOverdrawn;
|
||||||
|
|
||||||
|
private AccountType type;
|
||||||
|
|
||||||
|
public Account(int daysOverdrawn, AccountType type) {
|
||||||
|
this.daysOverdrawn = daysOverdrawn;
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getBankCharge() {
|
||||||
|
double result = 4.5;
|
||||||
|
if (this.daysOverdrawn() > 0) {
|
||||||
|
result += this.overdraftCharge();
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int daysOverdrawn() {
|
||||||
|
return this.daysOverdrawn;
|
||||||
|
}
|
||||||
|
|
||||||
|
private double overdraftCharge() {
|
||||||
|
if (this.type.isPremium()) {
|
||||||
|
final int baseCharge = 10;
|
||||||
|
if (this.daysOverdrawn <= 7) {
|
||||||
|
return baseCharge;
|
||||||
|
} else {
|
||||||
|
return baseCharge + (this.daysOverdrawn - 7) * 0.85;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return this.daysOverdrawn * 1.75;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package com.example.refactoring._07_divergent_change._25_move_function._before;
|
||||||
|
|
||||||
|
public class AccountType {
|
||||||
|
private boolean premium;
|
||||||
|
|
||||||
|
public AccountType(boolean premium) {
|
||||||
|
this.premium = premium;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isPremium() {
|
||||||
|
return this.premium;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package com.example.refactoring._07_divergent_change._25_move_function;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class AccountTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void bankCharge() {
|
||||||
|
Account account = new Account(5, new AccountType(true));
|
||||||
|
assertEquals(14.5, account.getBankCharge());
|
||||||
|
|
||||||
|
account = new Account(8, new AccountType(true));
|
||||||
|
assertEquals(15.35, account.getBankCharge());
|
||||||
|
|
||||||
|
account = new Account(8, new AccountType(false));
|
||||||
|
assertEquals(18.5, account.getBankCharge());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user