Java接口:

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,当距离大于1000KM)或重量大于60t)的时候拒载,返回-1

2.火车:当距离在900KM)内(包含)时,运费=重量 * 距离 * 250,大于900KM)运费= 重量 * 距离 * 300

3.飞机:当距离大于500KM)时,运费=重量*距离*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();

}

}






  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值