第二版,第一章中的,java的重构的代码
package com.ruoyi.blog.controller;
import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class RebuildC1 {
private Invoice invoice;
public RebuildC1 () {
List<Performance> performances = new LinkedList<>();
performances.add(new Performance("hamlet", 12));
performances.add(new Performance("as-like", 35));
performances.add(new Performance("othello", 40));
invoice = new Invoice("BigCo", performances);
}
public static void main (String[] args) {
RebuildC1 rebuildC1 = new RebuildC1();
rebuildC1.statement();
}
public void statement () {
StringBuilder result = new StringBuilder("Statement for ").append(invoice.customer).append("\n");
invoice.performances.forEach(performance -> {
result.append(" play name :").append(performance.playID).append(";");
result.append(" audience is :").append(performance.audience).append(";");
result.append(" credits is :").append(getThisCredit(performance)).append(";");
result.append(" cost is :").append(getThisAmount(performance)).append(";").append("\n");
});
result.append("total cost :").append(forTotalAmount(invoice.performances)).append("\n");
result.append("total earn credits :").append(forTotalCredits(invoice.performances));
log.info(result.toString());
}
private int forTotalCredits (List<Performance> performances) {
AtomicInteger volumeCredits = new AtomicInteger();
performances.forEach(performance -> {
volumeCredits.addAndGet(getThisCredit(performance));
});
return volumeCredits.get();
}
private int forTotalAmount (List<Performance> performances) {
AtomicInteger totalAmount = new AtomicInteger();
performances.forEach(performance -> {
totalAmount.addAndGet(getThisAmount(performance));
});
return totalAmount.get();
}
private int getThisCredit (Performance performance) {
int credits = Math.max(performance.audience - 30, 0);
if ("comedy".equals(Plays.getTypeByName(performance.playID))) {
credits += (int) Math.floor(new BigDecimal(performance.audience).divide(new BigDecimal(5)).intValue());
}
return credits;
}
private int getThisAmount (Performance aPerformance) {
int result = 0;
switch (Plays.getTypeByName(aPerformance.playID)) {
case "tragedy":
result = 40000;
if (aPerformance.audience > 30) {
result += 1000 * (aPerformance.audience - 30);
}
break;
case "comedy":
result = 30000;
if (aPerformance.audience > 20) {
result += 500 * (aPerformance.audience - 20);
}
break;
default:
log.error("unknown plays type:{}", Plays.getTypeByName(aPerformance.playID));
}
return result;
}
@Data
private static class Invoice {
Invoice (String customer, List<Performance> performances) {
this.customer = customer;
this.performances = performances;
}
String customer;
List<Performance> performances;
}
@Data
private static class Performance {
Performance (String playID, int audience) {
this.playID = playID;
this.audience = audience;
}
String playID;
int audience;
}
private enum Plays {
HAMLET("Hamlet", "tragedy"),
AS_LIKE("as-like", "comedy"),
OTHELLO("Othello", "tragedy");
String name;
String type;
Plays (String name, String type) {
this.name = name;
this.type = type;
}
public static String getTypeByName (String name) {
for (Plays plays : values()) {
if (plays.name.equals(name)) {
return plays.type;
}
}
return "comedy";
}
}
}
网友评论