1.一个运输公司从网上得到订单,订单上标有货物重量和运输里程,该公司可以使用3种运输工具:卡车、火车、飞机。编写运输接口,声明3个接口常量,表示运输工具,声明一个计算运费的方法,参数是重量和里程。
提示:
(1).飞机常量:public static final int PLANE = 1;//火车 TRAIN = 2 卡车 CAR = 3
(2).运输方法:public double cost(double weight, double space); //weight 是重量(单位:t),space是距离(单位:km)
2.卡车、火车、飞机分别实现运输接口,计算运费的方法如下:
(1).卡车:运费 = 重量 * 距离 * 120,当距离大于1000(KM)或重量大于60(t)的时候拒载,返回-1。
(2).火车:当距离在900(KM)内(包含)时,运费=重量 * 距离 * 250,大于900(KM)运费= 重量 * 距离 * 300
(3).飞机:当距离大于500(KM)时,运费=重量*距离*750,否则拒载,返回-1。
3.编写管理员类,编写main方法使用运输接口和实现类。
下面是代码:
//创建接口
public interface Method {
public static final int Truck =1;
public static final int Train =2;
public static final int Plane =3;
public void Truck();
public void Train();
public void Plane();
}
import java.util.Scanner;
//实现方式
public class Transport implements Method {
public void method() {
while (true) {
Scanner input = new Scanner(System.in);
System.out.println("请按键选择运输方式:1.卡车运输,2.火车运输,3.飞机运输,4.退出系统。");
int a = input.nextInt();
if (a == 1) {
this.Truck();
} else if (a == 2) {
this.Train();
} else if (a == 3) {
this.Plane();
} else if (a == 4) {
break;
} else {
System.out.print("输入有误,请重新输入:");
}
}
}
// 卡车计费方式:
public void Truck() {
System.out.println("卡车:运费=重量x距离x120,当距离大于1000(KM)或重量大于60(t)的时候拒载");
Scanner input = new Scanner(System.in);
System.out.println("请输入重量:");
double weight = input.nextDouble();
System.out.println("请输入距离:");
double distance = input.nextDouble();
double count = weight * distance * 120;
if (distance > 1000) {
System.out.println("距离太远了,拒载!");
} else if (weight > 60) {
System.out.println("重量太重了,拒载!");
} else {
System.out.println("运费是:" + count);
}
}
// 火车计费方式:
public void Train() {
System.out
.println("火车:当距离在900(KM)内(包含)时,运费=重量x距离x250,大于900(KM)运费=重量x距离x300");
Scanner input = new Scanner(System.in);
System.out.println("请输入重量:");
double weight = input.nextDouble();
System.out.println("请输入距离:");
double distance = input.nextDouble();
if (distance <= 900) {
double count = weight * distance * 250;
System.out.println("运费是:" + count);
} else {
double count = weight * distance * 300;
System.out.println("运费是:" + count);
}
}
// 飞机计费方式:
public void Plane() {
System.out.println("飞机:当距离大于500(KM)时,运费=重量x距离x750,否则拒载");
Scanner input = new Scanner(System.in);
System.out.println("请输入重量:");
double weight = input.nextDouble();
System.out.println("请输入距离:");
double distance = input.nextDouble();
if (distance > 500) {
double count = weight * distance * 750;
System.out.println("运费是:" + count);
} else {
System.out.println("距离太近了,拒载!");
}
}
// 测试
public static void main(String[] args) {
Transport m = new Transport();
m.method();
}
}