java设计飞机行李托运计费系统。假设飞机上个人托运行李的条件是:行李重量在20公斤以下免费托运;20~30公斤超出部分5元/公斤;30~40公斤超出部分10元/公斤;40~50公斤超出部分15元/
时间: 2024-10-27 11:11:19 浏览: 63
软件工程——航空行李托运费计算.源cpp(可运行)
在Java中设计一个飞机行李托运计费系统的简单思路可以分为以下几个步骤:
1. **创建行李类** (Luggage): 定义一个名为Luggage的类,包含行李的属性如weight(重量),并初始化默认为0。
```java
public class Luggage {
private double weight;
public Luggage() {
this.weight = 0;
}
// getter and setter methods for weight
}
```
2. **计算费用方法** (calculateFee): 在Luggage类中添加一个方法,根据行李重量的不同阶段计算费用。使用if-else或switch-case结构。
```java
public class Luggage {
// ...
public double calculateFee() {
if (weight <= 20) {
return 0;
} else if (weight <= 30) {
return 5 * (weight - 20);
} else if (weight <= 40) {
return 5 * 10 + 10 * (weight - 30);
} else if (weight <= 50) {
return 5 * 10 + 10 * 10 + 15 * (weight - 40);
} else {
// If the luggage is over 50 kg, you can add a condition to handle it.
throw new IllegalArgumentException("Weight exceeds the limit");
}
}
}
```
3. **主程序入口** (Main): 创建Luggage实例,获取用户输入的重量,然后计算并打印费用。
```java
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the luggage weight:");
double luggageWeight = scanner.nextDouble();
Luggage luggage = new Luggage();
luggage.setWeight(luggageWeight);
double fee = luggage.calculateFee();
System.out.printf("The baggage fee is %.2f yuan.\n", fee);
}
}
```
阅读全文