(1) 乘客可以免费托运重量不超过30kg(含30kg)的行李。
(2) 假设行李超过30kg,其收费标准为:
- 头等舱国内乘客,超重部分每千克收费4元。
- 其他舱国内乘客,超重部分每千克收费6元。
- 外国乘客:超重部分每千克比国内乘客多1倍。
- 残疾乘客:为正常价格的半价。
(3) 行李重量超出部分,不满1kg的按照1kg计算。
代码实现如下:
1、main方法:
package Ticket;public class Test {public static void main(String[] args) {ICalcTicket ct = new CalcTicketImpl();ct.CalcTicket();}
}
2、接口( ICalcTicket.java ):
package Ticket;public interface ICalcTicket {void CalcTicket();int levelselect(int x, int y, int level, int moreweight);int TotalMoney(double weight, int nation, int health, int level);int IsTrue(int flag);}
3、接口实现类( CalcTicketImpl.java ):
package Ticket;import java.util.Scanner;import static java.lang.Math.ceil;public class CalcTicketImpl implements ICalcTicket {private int flag = 0;private int money = 0;private double weight;private int moreweight;Scanner scanner = null;int[] a = new int[3];FormatJudge fj = new FormatJudge();public void CalcTicket() {while (true) {scanner = new Scanner(System.in);System.out.println("Please enter weight in KG (with two decimal places):");try {weight = scanner.nextDouble();break;} catch (Exception e) {System.out.println("输入信息不符合要求,请重新输入!");}}if (weight < 30) {System.out.println("Your luggage will be free to carry!");} else {//调用IsTrue方法输入国籍,健康状况,舱位等级,并将返回值存入数组,作为调用TotalMoney时传入的三个参数for (flag = 0; flag <= 2; flag++) {int input = IsTrue(flag);a[flag] = input;}money = TotalMoney(weight, a[0], a[1], a[2]);System.out.println("You should pay ¥ " + money + " .");}}//舱位等级判断public int LevelSelect(int x, int y, int level, int moreWeight) {switch (level) {case 1: //first classmoney = moreWeight * x;break;case 2: //other classesmoney = moreWeight * y;break;default:break;}return money;}//计算运费public int TotalMoney(double weight, int nation, int health, int level) {moreweight = (int) ceil(weight - 30);if (nation == 1) { //domesticif (health == 1) { //disabledmoney = LevelSelect(2, 3, level, moreweight);} else { //healthymoney = LevelSelect(4, 6, level, moreweight);}} else { //foreignerif (health == 1) { //disabledmoney = LevelSelect(4, 6, level, moreweight);} else { //healthymoney = LevelSelect(8, 12, level, moreweight);}}return money;}//输入国籍,健康状况,舱位等级三个参数并捕捉输入异常public int IsTrue(int flag) {int input;this.flag = flag;while (true) {scanner = new Scanner(System.in);if (flag == 0) {System.out.println("Please enter '1' or '2' (1:domestic, 2:foreigner):");} else if (flag == 1) {System.out.println("Please enter '1' or '2' (1:disabled, 2:healthy):");} else if (flag == 2) {System.out.println("Please enter '1' or '2' (1:first class, 2:other classes):");}//捕捉两个异常,一个是scanner.nextInt异常,一个是自定义异常try {input = scanner.nextInt();fj.judge(input);break;} catch (Exception e) {System.out.println("输入信息不符合要求,请重新输入!");}}return input;}
}
4、自定义异常类( FormatException.java ):
package Ticket;//自定义异常
public class FormatException extends Exception {public FormatException() {super();}
}//将输入的值是int类型但不是1或2的异常抛出
class FormatJudge {public void judge(int m) throws FormatException {if (m != 1 && m != 2) {throw new FormatException();}}
}