一、项目场景
我们寄快递时,快递公司都会根据所寄货物的重量和目的地来计算运费,收费标准如下:
区域编号 | 快件到达地址 | 首重1KG/元 | 续重1KG/元 |
---|---|---|---|
1 | 广西、广东 | 10 | 5 |
2 | 安微、湖南、江苏、浙江、上海、江西、贵州、湖北、山西、福建、河南、云南、重庆、四川 | 12 | 8 |
3 | 北京、天津、陕西、河北、山东、海南 | 15 | 10 |
4 | 青海、宁夏、甘肃、辽宁、吉林、黑龙江、内蒙古 | 18 | 12 |
5 | 西藏、新疆 | 30 | 25 |
二、项目目标
根据上术场景,设计一个“快递运费计算程序”,使程序的操作人员(如收费员等)可以通过简单地输入货物的重量和目的地,即可计算出快递的费用。
三、项目设计要求
- 能够对操作人员的身分进行验证(最多多输入 3 次)
- 能计算若干个快递货物的费用,直到操作人员让它停止为止
- 能够方便地录入货物信息
- 能够自动识别出货物所在的收费段,计算出运费
- 能够显示货物信息及其运费
四、项目程序模块结构
程序总体结构如下:
五、项目程序实现
快递信息类 Cargo
成员变量名称 | 数据类型 | 描述 |
---|---|---|
no | long | 编号 |
name | String | 寄件人姓名 |
phone | long | 寄件人电话 |
receiverAddress | String | 收件地址 |
receiverName | String | 收件人姓名 |
receiverPhone | long | 收件人电话 |
weight | double | 重量 |
charge | double | 快递运费 |
保鲜快递信息类 FreshCargo ,继承 Cargo 类
成员变量名称 | 数据类型 | 描述 |
---|---|---|
hours | int | 保鲜小时数 |
主类 ExpressSystem
成员变量名称 | 数据类型 | 描述 |
---|---|---|
cargoHandler | CargoHandler | 快递处理 |
成员方法名称 | 功能 | 备注 |
---|---|---|
login | 操作员登录 | |
showMenu | 显示菜单 | |
main | 运行项目 | 主方法,程序入口 |
(1)login 方法
方法中需设置的变量有:
操作密码(常量):PASSWORD=1234
操作员输入的密码:password
密码正确标志:flag
操作员密码输入次数:n
请定义各变量的类型并给这些变量赋初值。
要求输入操作密码,与原始密码(假设为 1234)相核对,如果输入正确,提示密码正
确;若输入错误,提示密码输入错误,程序允许操作员输入三次密码,若三次均不正确,则
返回 0。
在程序中设置常量 final int PASSWORD=1234
(2)showMenu方法
在屏幕上显示如下信息:
求操作员选择要执行程序的编号,如果输入数据不在 0~4,给出错误提示,要求重新输
入。根据用户的选择执行相关程序 (可用 switch 或 if-else-if 实现)
快递处理类 CargoHandler
成员变量名称 | 数据类型 | 描述 |
---|---|---|
cargo | Cargo | 快递信息 |
area | int | 目的地区域 |
成员方法名称 | 功能 | 备注 |
---|---|---|
CargoHandler | 构造方法 | |
inputData | 货物数据录入 | |
computeCharge | 计算运费 | |
showMessage | 显示货物信息 | |
showCharge | 显示运费 |
(1)inputData 方法
要求输入货物编号、寄件人信息、收件人信息、收货地址及货物重量,选择收货区域以
便计算运费
(2)computeCharge 方法
根据货物的重量及所在区域计算货物的运费,并将计算结果保存到货物信息中。
区域编号 | 快件到达地址 | 首重1KG/元 | 续重1KG/元 |
---|---|---|---|
1 | 广西、广东 | 10 | 5 |
2 | 安微、湖南、江苏、浙江、上海、江西、贵州、湖北、山西、福建、河南、云南、重庆、四川 | 12 | 8 |
3 | 北京、天津、陕西、河北、山东、海南 | 15 | 10 |
4 | 青海、宁夏、甘肃、辽宁、吉林、黑龙江、内蒙古 | 18 | 12 |
5 | 西藏、新疆 | 30 | 25 |
运费=(int)重量*续重单价+首重单价
(3)showMessage 方法
将货物编号、货物所有者、货物重量以及货物运费在屏幕上显示出来
(4)showCharge 方法
将货物运费在屏幕上显示出来
保鲜快递处理类 FreshCargoHandler ,继承 CargoHandler 类
(1)inputData 方法
重写父类方法,增加“提示用户并输入保鲜时间”
(2)computeCharge 方法
重写父类方法,运费=(int)重量续重单价+首重单价+保鲜时间10
(3)showMessage 方法
重写父类方法,增加将保鲜时间在屏幕上显示出来
六、代码部分
ExpressSystem
package src;
import java.util.Scanner;
public class ExpressSystem {
private CargoHandler cargoHandler;
private FreshCargoHandler freshCargoHandler;
private static final int PASSWORD = 1234;
private String password;
private boolean flag;
private int n;
private int type;
private int house;
public ExpressSystem() {
this.cargoHandler = new CargoHandler();
this.freshCargoHandler = new FreshCargoHandler();
}
public void login() {
Scanner scanner = new Scanner(System.in);
flag = false;
n = 0;
while (n < 3 && !flag) {
System.out.print("请输入操作密码: ");
password = scanner.nextLine();
if (password.equals(String.valueOf(PASSWORD))) {
flag = true;
System.out.println("密码正确!");
} else {
n++;
System.out.println("密码输入错误,请重新输入!");
}
}
if (!flag) {
System.out.println("三次密码输入错误,程序退出: 0");
System.exit(0);
}
// scanner.close();
}
public void showMenu() {
Scanner scanner = new Scanner(System.in);
int choice;
do {
System.out.println("\n---快递运费费用计算程序---");
System.out.println("1. 选择货物类型");
System.out.println("2. 货物数据录入");
System.out.println("3. 计算运费");
System.out.println("4. 货物信息显示");
System.out.println("0. 退出程序");
System.out.print("输入选择:");
choice = scanner.nextInt();
System.out.println();
switch (choice) {
case 0:
System.out.println("退出程序。");
break;
case 1:
Scanner sc1 = new Scanner(System.in);
System.out.print("1. 无需保鲜\n2. 需要保鲜\n请选择货物类型:");
int sc6 = sc1.nextInt();
if (sc6 != 1 && sc6 != 2) {
System.out.print("输入错误");
showMenu();
break;
} else {
type = sc6;
showMenu();
break;
}
case 2:
if (type == 1) {
Scanner sc2 = new Scanner(System.in);
System.out.print("请输入货物编号:");
long no = sc2.nextLong();
System.out.print("请输入寄件人姓名:");
String name = sc2.next();
System.out.print("请输入寄件人电话:");
long phone = sc2.nextLong();
System.out.print("请输入收件地址:");
String receiverAddress = sc2.next();
System.out.print("请输入收件人姓名:");
String receiverName = sc2.next();
System.out.print("请输入收件人电话:");
long receiverPhone = sc2.nextLong();
System.out.print("请输入货物重量:");
double weight = sc2.nextDouble();
cargoHandler.inputData(no, name, phone, receiverAddress, receiverName, receiverPhone, weight);
} else {
Scanner sc2 = new Scanner(System.in);
System.out.print("请输入货物编号:");
long no = sc2.nextLong();
System.out.print("请输入寄件人姓名:");
String name = sc2.next();
System.out.print("请输入寄件人电话:");
long phone = sc2.nextLong();
System.out.print("请输入收件地址:");
String receiverAddress = sc2.next();
System.out.print("请输入收件人姓名:");
String receiverName = sc2.next();
System.out.print("请输入收件人电话:");
long receiverPhone = sc2.nextLong();
System.out.print("请输入货物重量:");
double weight = sc2.nextDouble();
System.out.print("请输入货物保鲜小时:");
int house = sc2.nextInt();
freshCargoHandler.inputData(no, name, phone, receiverAddress, receiverName, receiverPhone, weight, house);
}
showMenu();
break;
case 3:
if (type == 1) {
cargoHandler.computeCharge(cargoHandler.cargo.weight, cargoHandler.cargo.receiverAddress);
cargoHandler.showCharge();
} else {
freshCargoHandler.computeCharge(freshCargoHandler.cargo.weight, freshCargoHandler.cargo.receiverName, house);
freshCargoHandler.showCharge();
}
showMenu();
break;
case 4:
if (type == 1) {
cargoHandler.showMessage();
} else {
freshCargoHandler.showMessage();
}
showMenu();
break;
default:
System.out.println("输入错误,请重新输入。");
break;
}
} while (choice < 0 || choice > 4);
scanner.close();
}
public static void main(String[] args) {
ExpressSystem expressSystem = new ExpressSystem();
expressSystem.login();
expressSystem.showMenu();
}
}
Cargo
package src;
public class Cargo {
long no, phone, receiverPhone;
String name, receiverAddress, receiverName;
double weight, charge;
}
CargoHandler
package src;
import java.util.List;
public class CargoHandler {
Cargo cargo;
int area;
public CargoHandler() {
cargo = new Cargo();
}
public void inputData(long no, String name, long phone, String receiverAddress, String receiverName, long receiverPhone, double weight) {
// 要求输入货物编号、寄件人信息、收件人信息、收货地址及货物重量,选择收货区域以便计算运费
cargo.no = no;
cargo.name = name;
cargo.phone = phone;
cargo.receiverAddress = receiverAddress;
cargo.receiverName = receiverName;
cargo.receiverPhone = receiverPhone;
cargo.weight = weight;
computeCharge(weight, receiverAddress);
}
public void computeCharge(double weight, String receiverAddress) {
// 根据货物的重量及所在区域计算货物的运费,并将计算结果保存到货物信息中
List<String> parts = List.of(
"广西", "广东", // 0 -1
"安徽", "湖南", "江苏", "浙江", "上海", "江西", "贵州", "湖北", "山西", "福建", "河南", "云南", "重庆", "四川", // 2-15
"北京", "天津", "陕西", "河北", "山东", "海南", // 16-21
"青海", "宁夏", "甘肃", "辽宁", "吉林", "黑龙江", "内蒙古", // 22-28
"西藏", "新疆" // 29-30
);
int partsChoice = parts.indexOf(receiverAddress);
if (partsChoice == 0 || partsChoice == 1) {
if (weight > 1) {
cargo.charge = 10 + (weight - 1) * 5;
} else {
cargo.charge = 10;
}
} else if (partsChoice >= 2 && partsChoice <= 15) {
if (weight > 1) {
cargo.charge = 12 + (weight - 1) * 8;
} else {
cargo.charge = 12;
}
} else if (partsChoice >= 16 && partsChoice <= 21) {
if (weight > 1) {
cargo.charge = 15 + (weight - 1) * 10;
} else {
cargo.charge = 15;
}
} else if (partsChoice >= 22 && partsChoice <= 28) {
if (weight > 1) {
cargo.charge = 18 + (weight - 1) * 12;
} else {
cargo.charge = 18;
}
} else if (partsChoice >= 29 && partsChoice <= 30) {
if (weight > 1) {
cargo.charge = 30 + (weight - 1) * 25;
} else {
cargo.charge = 30;
}
}
}
public void showMessage() {
// 将货物编号、货物所有者、货物重量以及货物运费在屏幕上显示出来
System.out.println(
"货物编号:" + cargo.no + "\n" +
"货物寄件人:" + cargo.name + "\n" +
"货物收货人:" + cargo.receiverName + "\n" +
"货物重量:" + cargo.weight + "\n" +
"货物运费:" + cargo.charge
);
}
public void showCharge() {
System.out.println("运费为:" + cargo.charge);
}
}
FreshCargoHandler
package src;
import java.util.List;
public class FreshCargoHandler extends CargoHandler{
int house;
public FreshCargoHandler(){
cargo = new Cargo();
}
public void inputData(long no, String name, long phone, String receiverAddress, String receiverName, long receiverPhone, double weight, int house) {
super.inputData(no, name, phone, receiverAddress, receiverName, receiverPhone, weight);
computeCharge(weight, receiverAddress, house);
}
public void computeCharge(double weight, String receiverAddress, int house) {
List<String> parts = List.of(
"广西", "广东", // 0-1
"安徽", "湖南", "江苏", "浙江", "上海", "江西", "贵州", "湖北", "山西", "福建", "河南", "云南", "重庆", "四川", // 2-15
"北京", "天津", "陕西", "河北", "山东", "海南", // 16-21
"青海", "宁夏", "甘肃", "辽宁", "吉林", "黑龙江", "内蒙古", // 22-28
"西藏", "新疆" // 29-30
);
int partsChoice = parts.indexOf(receiverAddress);
if (partsChoice == 0 || partsChoice == 1) {
if (weight > 1) {
cargo.charge = 10 + (weight - 1) * 5 + house * 10;
} else {
cargo.charge = 10;
}
} else if (partsChoice >= 2 && partsChoice <= 15) {
if (weight > 1) {
cargo.charge = 12 + (weight - 1) * 8 + house * 10;
} else {
cargo.charge = 12;
}
} else if (partsChoice >= 16 && partsChoice <= 21) {
if (weight > 1) {
cargo.charge = 15 + (weight - 1) * 10 + house * 10;
} else {
cargo.charge = 15;
}
} else if (partsChoice >= 22 && partsChoice <= 28) {
if (weight > 1) {
cargo.charge = 18 + (weight - 1) * 12 + house * 10;
} else {
cargo.charge = 18;
}
} else if (partsChoice >= 29 && partsChoice <= 30) {
if (weight > 1) {
cargo.charge = 30 + (weight - 1) * 25 + house * 10;
} else {
cargo.charge = 30;
}
}
}
public void showMessage() {
super.showMessage();
}
public void showCharge() {
super.showCharge();
}
}