refactoring : divergent change - move function

This commit is contained in:
haerong22
2022-03-16 12:59:45 +09:00
parent 826cbf6631
commit 37ba93c045
5 changed files with 123 additions and 0 deletions

View File

@@ -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;
}
}

View File

@@ -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;
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -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;
}
}

View File

@@ -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());
}
}