我可以使用策略模式来实现以下逻辑: 因此,想象学费是根据学生的整体表现来计算的,例如,如果学生的总体平均分数超过70,他的学费将减少70%。等等..如果平均标记是50%折扣50%,在其他情况下没有折扣。因此,有不同的计算折扣的策略因平均分数而异。 如何在Java代码中实现它? 我想学生班将有计算平均分数的方法,界面TuitionFeeCalculator和三个策略:70persentDiscount,50%折扣和noDiscount实施TuitionFeeCalculator。
答案 0 :(得分:1)
假设这只是一个课堂练习,因为你永远不会为简单的事情实施策略模式,但你要找的是:
df[df.column1 > someValue]
答案 1 :(得分:0)
有人可以查看此代码并告诉我是否有错误
public class StrategyDemo
{
public static void main(String[] args)
{
Student s = new Student();
TuitionFee fee = new TuitionFee();
fee.setTuitionFee(5000);
StudentStatus status = new StudentStatus(s);
CalculatorSelectorFactory calc = new CalculatorSelectorFactory(status.getStatus());
TuitionFeeCalculator tfc = calc.getCalculator();
double newFee = tfc.calculate();
}
}
public class StudentStatus
{
private Student student;
public StudentStatus(Student student){
this.student = student;
}
public List<Module> getAllModules(){
//query the database and get all modules a particular student is enrolled for
return modules; //return list of modules
}
public int getStatus(){
int total = 0;
int count = 0;
for(Module m: getAllModules()){
total += m.getMark();
count++;
}
int average = total/count;
return average;
}
}
public class TuitionFee{
private double tuitionFee;
public void setTuitionFee(double tuitionFee){
this.tuitionFee = tuitionFee;
}
public double getTuitionFee(){
return tuitionFee;
}
}
public interface TuitionFeeCalculator{
public double calculate();
}
public class Discount70 implements TuitionFeeCalculator{
@Override
public double calculate(TuitionFee tuitionFee){
double discount = tuitionFee * 0.7; //70% discount
return tuitionFee-discount;
}
}
public class Discount50 implements TuitionFeeCalculator{
@Override
public double calculate(TuitionFee tuitionFee){
double discount = tuitionFee * 0.5; //50% discount
return tuitionFee-discount;
}
}
public class NoDiscount implements TuitionFeeCalculator{
@Override
public double calculate(TuitionFee tuitionFee){
return tuitionFee; //no discount
}
}
public class CalculatorSelectorFactory{
TuitionFeeCalculator tfc;
public CalculatorSelectorFactory(int status){
if(status>=70){
tfc=new Discount70();
}
else if(status>=50){
tfc = new Discount50();
}
else{
tfc = new NoDiscount();
}
}
public TuitionFeeCalculator getCalculator(){
return tfc;
}
}