快递计价器
分数 20
全屏浏览
切换布局
作者 大数据2021
单位 山东科技大学
现需要编写一个简易快递计价程序。具体来说:
1、抽象快递类Express
,其包含一个属性int weight
表示快递重量(单位为kg),一个方法getWeight()
用于返回快递重量和一个抽象方法getTotal()
用于计算快递运费。
2、两个类继承Express
,分别是:
(a)顺路快递SLExpress
:计价规则为首重(1kg)12元,每增加1kg费用加2元。
(b)地地快递DDExpress
:计价规则为首重(1kg)5元,每增加1kg费用加1元。
3、菜菜驿站类CaicaiStation,提供静态方法 int calculate(Express[] ex) 用于计算所有快递的费用。
输入样例:
6
SL 2
DD 2
SL 1
SL 1
SL 1
DD 3
输入解释:
第1行n表示需要计算的快递件数
第2至n+1表示每个快递信息,即选哪家快递公司 以及快递的重量(单位kg)
输出样例:
63
输出解释:
所有快递总运费。
裁判测试程序样例:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); Express[] ex = new Express[n]; for (int i = 0; i < ex.length; i++) { if (sc.next().equals("SL")) ex[i] = new SLExpress(sc.nextInt()); else ex[i] = new DDExpress(sc.nextInt()); } System.out.println(CaicaiStation.calculate(ex)); sc.close(); } } /* 请在这里填写答案 */
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
abstract class Express {
int weight;
public abstract int getWeight();
public abstract int getTotal();
}
class SLExpress extends Express {
public SLExpress(int weight) {
this.weight = weight;
}
@Override
public int getWeight() {
// TODO Auto-generated method stub
return this.weight;
}
@Override
public int getTotal() {
// TODO Auto-generated method stub
if (this.weight == 1) {
return 12;
} else {
return 12 + 2 * (this.weight - 1);
}
}
}
class DDExpress extends Express {
public DDExpress(int weight) {
this.weight = weight;
}
@Override
public int getTotal() {
if (this.weight == 1) {
return 5;
} else {
return 5 + (this.weight - 1);
}
}
@Override
public int getWeight() {
// TODO Auto-generated method stub
return this.weight;
}
}
class CaicaiStation {
public static int calculate(Express[] ex) {
int sum = 0;
for (Express e : ex) {
sum += e.getTotal();
}
return sum;
}
}