java - 代码重构作业?

标签 java c++

这是我必须为作业重构的代码:

if (state == TEXAS) {
    rate = TX_RATE;
    amt = base * TX_RATE;
    calc = 2 * basis(amt) + extra(amt) * 1.05;
} else if ((state == OHIO) || (state == MAINE)) {
    rate = (state == OHIO) ? OH_RATE : MN_RATE;
    amt = base * rate;
    calc = 2 * basis(amt) + extra(amt) * 1.05;
    if (state == OHIO)
        points = 2;
} else {
    rate = 1;
    amt = base;
    calc = 2 * basis(amt) + extra(amt) * 1.05;
}

我做过这样的事

if (state == TEXAS) {
    rate = TX_RATE;
    calculation(rate);
} 
else if ((state == OHIO) || (state == MAINE))
    {
rate = (state == OHIO) ? OH_RATE : MN_RATE;

calculation(rate);

if (state == OHIO)
    points = 2;
}

else {
    rate = 1;
    calculation(rate);
}

function calculation(rate)
{
    amt = base * rate;
    calc = 2 * basis(amt) + extra(amt) * 1.05;
}

我怎样才能做得更好?
编辑 我已完成代码编辑 amt = base * rate;

最佳答案

class State {
private :
  double taxRate;
  int baseWeight;
  int extraWeight;
  string name;
  base;
public:
  State(string name, double taxRate = 1, int point =0, double baseWeight=2, double extraWeight=1.05); //implement the method yourself
  double extra(double base);
  double basis(double base);
  double calculate(double base){
      return baseWeight * basis(base) + baseWeight * extra(base);
  }
  int point(){return point};

};

现在如何使用它:

State ohio ("OHIO", OH_RATE, 2);
cout << "OHIO result:" ohio.calculate() << " point:" << ohio.point() << endl;

关于java - 代码重构作业?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8193026/

相关文章:

c++ - RapidJSON 循环遍历字符串数组?

c++ - 当另一方写入管道时,读取管道是否安全?

c++ - 下界上界给出相同的结果

c++ - 是否可以使 C++ 类成为 Objc 类的委托(delegate)?

java - 并发只写事务

java - JScrollPane 组件在我单击之前是不可见的

java - <error-page> 设置在 Spring MVC 中不起作用

c++ - Model View Controller 设计模式代码示例

java - Apache POI 数据格式

java - Java 中的垃圾收集是否在循环内工作?