Java Case 14: Simulating Logistics Express System (Online Shopping)

Idea:

/*
Simulated logistics express system (online shopping)

When people place an order on a shopping website, the goods in the order will go through a series of processes and then be delivered to the customer.
During delivery, logistics managers can view the logistics information of all items in the system
Simulate the process of cargo processing by the logistics express backend system

The concepts and uses of object-oriented encapsulation, inheritance and polymorphism
Use of abstract classes and interfaces


1. Transporting goods requires a means of transportation. Define a means of transportation class.
Since there are many means of transportation, the means of transportation can be defined as an abstract class
The class contains attributes such as the number, model, and person in charge of the transportation of the vehicle.
You also need to define an abstract transportation method

2. After the transportation is completed, the vehicle needs to be maintained
A maintenance interface needs to be defined to provide maintenance functions for transportation vehicles.

3. There are many types of transportation vehicles. Define a special transport vehicle class.
This class needs to inherit the vehicle class and implement the maintenance interface

4. Once you have the transportation means, you can start transporting the goods.
Goods need to be inspected and recorded before, during and after transportation.
And each express delivery has a delivery tracking number.
At this time, you can define a courier task class, including the attributes of the courier number and cargo weight, as well as the methods before, on the way, and after delivery.

5. During transportation, the transport vehicle is positioned so that the location information of the goods can be tracked at any time.
The positioning function uses GPS,
Considering that there are many types of devices that implement positioning functions (mobile phones, locators, etc.),
Define an interface containing the positioning function, and the instrument class that implements the interface (such as phone, etc.)

6. Write test classes.

 */

Code:

Code structure:

 

Test class:

package base.base014;

/*
模拟物流快递系统(网购)

当人们在购物网站下单后,订单中的货物会经过一系列的流程后送到客户手上
在送货期间,物流管理人员可以在系统中查看所有物品的物流信息
模拟物流快递后台系统处理货物的过程

面向对象封装、继承和多态的概念和使用
抽象类和接口的使用


1.运输货物需要有交通工具,定义一个交通工具类
由于交通工具有很多,所以可以将该交通工具定义成一个抽象类
类中包含该交通工具的编号、型号、运货负责人等属性
还需要定义一个抽象的运输方法

2.运输完成后,需要对交通工具进行保养
需要定义保养接口,具备交通工具的保养功能

3.交通工具有很多种,定义一个专用运输车类,
该类需要继承交通工具类,并实现保养接口

4.有了运输的交通工具后,就可以开始运送货物了
货物在运输前,运输时、运输后,都需要检查和记录,
并且每个快递都有快递单号
这时可以定义一个快递任务类,包含快递单号和货物重量的属性,以及送前、途中、送后的方法

5.运输过程中,对运输车定位,以便随时跟踪货物位置信息。
定位功能使用GPS,
考虑到实现定位功能的设备有很多种(手机、定位仪等),
定义一个包含定位功能的接口,以及实现了该接口的仪器类(如phone等)

6.编写测试类。

 */

public class Test14 {
    public static void main(String[] args) {

        SendTask s1 = new SendTask("G0001",20);
        s1.sendBefore();
        zTransportation t1 = new zTransportation("Z001","宝马","夏习清 ");
        Phone p1 = new Phone();
        s1.send(t1,p1);
        s1.sendAfter(t1);
        t1.upKeep();

        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        SendTask s2 = new SendTask("G0002",25);
        zTransportation t2 = new zTransportation("Z002","奔驰","周自珩");
        Phone p2 = new Phone();
        s2.sendBefore();
        s2.send(t2,p2);
        s2.sendAfter(t2);
        t2.upKeep();





    }
}
Transportation category:
package base.base014;

/*
1.运输货物需要有交通工具,定义一个交通工具类
由于交通工具有很多,所以可以将该交通工具定义成一个抽象类
类中包含该交通工具的编号、型号、运货负责人等属性
还需要定义一个抽象的运输方法
 */

public abstract class Transportation {
    private String number;//编号
    private String model;//型号
    private String admin;//货运负责人

    public Transportation(String number, String model, String admin) {
        this.number = number;
        this.model = model;
        this.admin = admin;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public String getAdmin() {
        return admin;
    }

    public void setAdmin(String admin) {
        this.admin = admin;
    }

    public abstract void trantport();
}
Careable interface:
package base.base014;

/*
2.运输完成后,需要对交通工具进行保养
需要定义保养接口,具备交通工具的保养功能
 */

public interface Careable {
    public abstract void upKeep();
}
GPS interface:
package base.base014;

/*
5.运输过程中,对运输车定位,以便随时跟踪货物位置信息。
定位功能使用GPS,
考虑到实现定位功能的设备有很多种(手机、定位仪等),
定义一个包含定位功能的接口,以及实现了该接口的仪器类(如phone等)
 */

public interface GPS {
    public abstract String showGPS();

}
Phone class:
package base.base014;

public class Phone implements GPS {
    @Override
    public String showGPS() {
        return "135,680";
    }
}
SendTask class:
package base.base014;

/*
4.有了运输的交通工具后,就可以开始运送货物了
货物在运输前,运输时、运输后,都需要检查和记录,
并且每个快递都有快递单号
这时可以定义一个快递任务类,包含快递单号和货物重量的属性,以及送前、途中、送后的方法
 */

public class SendTask {
    private String number;
    private double goodsWeight;

    public SendTask(String number, double goodsWeight) {
        this.number = number;
        this.goodsWeight = goodsWeight;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public double getGoodsWeight() {
        return goodsWeight;
    }

    public void setGoodsWeight(double goodsWeight) {
        this.goodsWeight = goodsWeight;
    }

    //发送前
    public void sendBefore(){
        System.out.println("订单开始处理,仓库正在验货...");
        System.out.println("货物重量"+this.getGoodsWeight()+"kg");
        System.out.println("验货完毕,正在揽收");
        System.out.println("货运人接单,货物即将发出...");
        System.out.println("货物已发出");
        System.out.println("快递单号:"+this.getNumber());
    }

    //发送中
    public void send(Transportation t,GPS tool){
        System.out.println("运货人:"+t.getAdmin()+"正在驾驶编号为:"+t.getNumber()+"的"+t.getModel()+"发送货物");
        t.trantport();
        System.out.println("货物当前坐标为:"+tool.showGPS());

    }

    //发送后
    public void sendAfter(Transportation t){
        System.out.println("货物已到达目的地");
        System.out.println("运货车辆:"+t.getNumber()+"已归还");
    }
}
zTransportation class:
package base.base014;

public class zTransportation extends Transportation implements Careable{

    public zTransportation(String number, String model, String admin) {
        super(number, model, admin);
    }

    @Override
    public void trantport() {

        System.out.println("货物正在运输");
    }

    @Override
    public void upKeep() {

        System.out.println("运输完成,即将进行车辆保养...");
    }
}

Guess you like

Origin blog.csdn.net/weixin_54446335/article/details/131802887