mirror of
https://github.com/Dichgrem/Java.git
synced 2026-02-04 22:01:56 -05:00
73 lines
1.8 KiB
Java
73 lines
1.8 KiB
Java
package com.study.demo13;
|
|
|
|
interface ScoreCalculator {
|
|
double calculate();
|
|
String getLevel();
|
|
}
|
|
|
|
abstract class AbstractCourse implements ScoreCalculator {
|
|
protected double score;
|
|
|
|
public AbstractCourse(double score) {
|
|
this.score = score;
|
|
}
|
|
|
|
protected String levelByScore(double s) {
|
|
if (s >= 75) {
|
|
return "A";
|
|
} else if (s >= 60) {
|
|
return "B";
|
|
} else if (s >= 60) {
|
|
return "C";
|
|
} else {
|
|
return "D";
|
|
}
|
|
}
|
|
}
|
|
|
|
class TheoryCourse extends AbstractCourse {
|
|
public TheoryCourse(double score) {
|
|
super(score);
|
|
}
|
|
|
|
@Override
|
|
public double calculate() {
|
|
return score;
|
|
}
|
|
|
|
@Override
|
|
public String getLevel() {
|
|
return levelByScore(score);
|
|
}
|
|
}
|
|
|
|
class LabCourse extends AbstractCourse {
|
|
public LabCourse(double score) {
|
|
super(score);
|
|
}
|
|
|
|
@Override
|
|
public double calculate() {
|
|
return score * 0.9;
|
|
}
|
|
|
|
@Override
|
|
public String getLevel() {
|
|
return levelByScore(calculate());
|
|
}
|
|
}
|
|
|
|
public class GradeService {
|
|
public static void main(String[] args) {
|
|
ScoreCalculator theory = new TheoryCourse(85);
|
|
ScoreCalculator lab = new LabCourse(85);
|
|
ScoreCalculator theoryLow = new TheoryCourse(55);
|
|
ScoreCalculator labMid = new LabCourse(70);
|
|
|
|
System.out.println("TheoryCourse: score = " + theory.calculate() + ", level = " + theory.getLevel());
|
|
System.out.println("LabCourse: score = " + lab.calculate() + ", level = " + lab.getLevel());
|
|
System.out.println("TheoryCourse(low): score = " + theoryLow.calculate() + ", level = " + theoryLow.getLevel());
|
|
System.out.println("LabCourse(mid): score = " + labMid.calculate() + ", level = " + labMid.getLevel());
|
|
}
|
|
}
|