JAVA编程题-交通工具信息查询系统

news2024/10/5 0:51:48

题目:

请编写一个交通工具信息查询系统,其中包含一个抽象父类:交通工具(Transports)类,四个具体子类飞机(Plane)类,轮船(Ship)类,火车(Train)类和巴士(Bus)类,一个单独的城市(City)类以及一个主方法类。其中每个类的属性及方法会再单独文件中给出。要求用户选择所在城市以及目标城市,系统创建相对应的航线对象,将所有对象保存在一个集合中并输出给用户。若目标城市不存在,则用户可以根据给出条件自行添加。

结构:

1抽象父类transports

4子类飞机,轮船,火车,巴士

1城市类

1主方法类

Transports(抽象):

属性:所属公司,型号

方法:计算所需时间,输出信息

飞机类:

属性:飞行速度,座位数,最大飞行高度,是否跨国

轮船类:

属性:最大载客量,航速,是否跨国

火车类:

属性:车厢数,每节车厢载客量、速度

巴士类:

属性:最大载客量,速度

城市类:

属性:城市名称,X坐标,Y坐标,是否沿海,是否为海岛,所属国家

城市:

A,100,50,是,否,X

B,200,300,是,否,X

C,500,350,是,是,X

D,1500,640,否,否,Y

E,800,500,是,否,Y

F,1100,500,是,否,Y

航线要求:

飞机:城市距离>300km则开设航线

轮船:城市沿海或为海岛则开设航线

火车:两座城市属同一国家且不为海岛则开设

汽车:两座城市属同一国家,不为海岛且城市距离<500km则开设

所属公司:

X国之内为X公司

Y国之内为Y公司

跨国为XY跨国公司

型号:

飞机:跨国为CC(Cross-Country),不跨国为LC(Local)

CC:90km/h,50人,3500m

LC:70km/h,30人,3000m

轮船:S型,100人,20km/h

火车:T型,5节,15人,30km/h

巴士:B型,30人,10km/h

最后的输出效果图

输出的结果样式,可跟自己的需求而改变

上代码

交通工具抽象类

/**
 * 交通工具抽象类
 */
public abstract class Transports {
    /**
     * 公司
     */
    protected String company;
    /**
     * 型号
     */
    protected String model;

    public Transports(String company, String model) {
        this.company = company;
        this.model = model;
    }

    /**
     * 计算时间
     * 
     * 距离 除以 速度
     * 
     * @param distance 距离
     */
    public abstract void calculateTime(double distance);

    /**
     * 输出交通工具的信息
     */
    public abstract void printInfo();
}

飞机类

/**
 * 飞机类
 */
public class Plane extends Transports {
    /**
     * 速度
     */
    private int speed;
    /**
     *  座位数
     */
    private int seats;
    /**
     *  最大高度
     */
    private int maxHeight;
    /**
     *  是否跨国
     */
    private boolean crossCountry;

    private City startCity;
    private City endCity;

    public Plane(String company, String model, int speed, int seats, int maxHeight, boolean crossCountry) {
        super(company, model);
        this.speed = speed;
        this.seats = seats;
        this.maxHeight = maxHeight;
        this.crossCountry = crossCountry;
    }

    @Override
    public void calculateTime(double distance) {
        // 计算所需时间的逻辑
        System.out.println("所需时间:" + Math.round(distance / speed * 100.0) / 100.0 + "小时");
        System.out.println("-------------------------------------");
    }

    @Override
    public void printInfo() {
        // 输出飞机信息的逻辑
        System.out.println("飞机公司:" + company);
        System.out.println("飞机型号:" + model);
        System.out.println("飞机速度:" + speed + "km/h");
        System.out.println("飞机座位数:" + seats + "人");
        System.out.println("飞机最大飞行高度:" + maxHeight + "m");
        System.out.println("飞机可否出国:" + (crossCountry ? "是" : "否"));
    }

}

轮船类

/**
 * 轮船类
 */
public class Ship extends Transports {
    /**
     *  容量
     */
    private int capacity;
    /**
     *  速度
     */
    private int speed;
    /**
     *  是否跨国
     */
    private boolean crossCountry;

    public Ship(String company, String model, int capacity, int speed, boolean crossCountry) {
        super(company, model);
        this.capacity = capacity;
        this.speed = speed;
        this.crossCountry = crossCountry;
    }

    @Override
    public void calculateTime(double distance) {
        // 计算所需时间的逻辑
        System.out.println("所需时间:" + Math.round(distance / speed * 100.0) / 100.0 + "小时");
        System.out.println("-------------------------------------");
    }

    @Override
    public void printInfo() {
        // 输出轮船信息的逻辑
        System.out.println("轮船公司:" + company);
        System.out.println("轮船型号:"+ model);
        System.out.println("轮船容量:"+ capacity + "人");
        System.out.println("轮船速度:"+ speed + "km/h");
        System.out.println("是否可跨国:"+ (crossCountry ? "是" : "否"));
    }
}

火车类

/**
 * 火车类
 */
public class Train extends Transports {
    /**
     *  车厢数
     */
    private int carriages;
    /**
     *  每节车厢容量
     */
    private int capacityPerCarriage;
    /**
     * 速度
     */
    private int speed;

    public Train(String company, String model, int carriages, int capacityPerCarriage, int speed) {
        super(company, model);
        this.carriages = carriages;
        this.capacityPerCarriage = capacityPerCarriage;
        this.speed = speed;
    }

    @Override
    public void calculateTime(double distance) {
        // 计算所需时间的逻辑
        System.out.println("所需时间:" + Math.round(distance / speed * 100.0) / 100.0 + "小时");
        System.out.println("-------------------------------------");
    }

    @Override
    public void printInfo() {
        // 输出火车信息的逻辑
        System.out.println("火车公司:" + company);
        System.out.println("火车型号:" + model);
        System.out.println("车厢数量:" + carriages + "节");
        System.out.println("每节车厢载客量:" + capacityPerCarriage + "人");
        System.out.println("行驶速度:" + speed + "km/h");
    }
}

巴士类

/**
 * 巴士类
 */
public class Bus extends Transports {
    /**
     * 容量
     */
    private int capacity;
    /**
     * 速度
     */
    private int speed;

    public Bus(String company, String model, int capacity, int speed) {
        super(company, model);
        this.capacity = capacity;
        this.speed = speed;
    }

    @Override
    public void calculateTime(double distance) {
        // 计算所需时间的逻辑
        System.out.println("所需时间:" + Math.round(distance / speed * 100.0) / 100.0 + "小时");
        System.out.println("-------------------------------------");
    }

    @Override
    public void printInfo() {
        // 输出巴士信息的逻辑
        System.out.println("巴士公司:" + company);
        System.out.println("巴士型号:" + model);
        System.out.println("巴士容量:" + capacity + "人");
        System.out.println("巴士速度:" + speed + "km/h");
    }
}

城市类

/**
 * 城市类
 */
public class City {
    /**
     * 城市名称
     */
    private String name;
    /**
     * X坐标
     */
    private int xCoordinate;
    /**
     * Y坐标
     */
    private int yCoordinate;
    /**
     * 是否为沿海城市
     */
    private boolean coastal;
    /**
     * 是否为岛屿
     */
    private boolean island;
    /**
     * 所属国家
     */
    private String country;

    public City(String name, int xCoordinate, int yCoordinate, boolean coastal, boolean island, String country) {
        this.name = name;
        this.xCoordinate = xCoordinate;
        this.yCoordinate = yCoordinate;
        this.coastal = coastal;
        this.island = island;
        this.country = country;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getxCoordinate() {
        return xCoordinate;
    }

    public void setxCoordinate(int xCoordinate) {
        this.xCoordinate = xCoordinate;
    }

    public int getyCoordinate() {
        return yCoordinate;
    }

    public void setyCoordinate(int yCoordinate) {
        this.yCoordinate = yCoordinate;
    }

    public boolean isCoastal() {
        return coastal;
    }

    public void setCoastal(boolean coastal) {
        this.coastal = coastal;
    }

    public boolean isIsland() {
        return island;
    }

    public void setIsland(boolean island) {
        this.island = island;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    @Override
    public String toString() {
        return  "城市名称:" + name +  "\n" +
                "所属国家:" + country + "\n" +
                "是否沿海:" + (coastal ? "是" : "否") + "\n" +
                "是否为海岛:" + (island ? "是" : "否") + "\n" +
                "X坐标:" + xCoordinate + "\n" +
                "Y坐标:" + yCoordinate + "\n" +
                "-------------------------------------";
    }
}

主要运行类

public class MainClass {
    public static void main(String[] args) {
        // 创建城市对象
        City cityA = new City("A", 100, 50, true, false, "X");
        City cityB = new City("B", 200, 300, true, false, "X");
        City cityC = new City("C", 500, 350, true, true, "X");
        City cityD = new City("D", 1500, 640, false, false, "Y");
        City cityE = new City("E", 800, 500, true, false, "Y");
        City cityF = new City("F", 1100, 500, true, false, "Y");

        // 将城市对象添加到城市列表中
        List<City> cities = new ArrayList<>();
        cities.add(cityA);
        cities.add(cityB);
        cities.add(cityC);
        cities.add(cityD);
        cities.add(cityE);
        cities.add(cityF);

        System.out.println("以下是已有的城市列表:");
        for (City city : cities) {
            System.out.println(city);
        }

        // 用户输入所在城市和目标城市
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入所在城市名称:");
        String fromCityName = scanner.nextLine();
        System.out.println("请输入目标城市名称:");
        String toCityName = scanner.nextLine();

        // 查找所在城市和目标城市对象
        City fromCity = cities.stream()
                .filter(city -> city.getName().equals(fromCityName))
                .findFirst()
                .orElse(null);
        City toCity = cities.stream()
                .filter(city -> city.getName().equals(toCityName))
                .findFirst()
                .orElse(null);

        // 如果目标城市不存在,允许用户自行添加
        if (toCity == null) {
            System.out.println("目标城市不存在,请自行添加:");
            System.out.println("请输入城市名称:");
            String newCityName = scanner.nextLine();
            System.out.println("请输入X坐标:");
            int xCoordinate = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符
            System.out.println("请输入Y坐标:");
            int yCoordinate = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符
            System.out.println("是否沿海(是/否):");
            boolean isCoastal = scanner.nextLine().equalsIgnoreCase("是");
            System.out.println("是否为海岛(是/否):");
            boolean isIsland = scanner.nextLine().equalsIgnoreCase("是");
            System.out.println("所属国家:");
            String country = scanner.nextLine();

            // 创建新城市对象并添加到城市列表中
            City newCity = new City(newCityName, xCoordinate, yCoordinate, isCoastal, isIsland, country);
            cities.add(newCity);
            toCity = newCity; // 更新目标城市对象
        }

        // 创建交通工具对象并添加到集合中
        Set<Transports> transports = new HashSet<>();
        if (fromCity != null && toCity != null) {
            createTransports(transports, fromCity, toCity);
        } else {
            System.out.println("无法找到有效的起始城市和目标城市。");
        }
    }

    private static void createTransports(Set<Transports> transports, City fromCity, City toCity) {
        // 计算起始城市和目标城市之间的距离
        double distance = Math.sqrt(Math.pow(toCity.getxCoordinate() - fromCity.getxCoordinate(), 2) + Math.pow(toCity.getyCoordinate() - fromCity.getyCoordinate(), 2));

        // 是否跨国 true 表示跨国
        boolean crossCountry = !fromCity.getCountry().equals(toCity.getCountry());
        // 所属公司
        String countryName = "X公司";
        if (crossCountry) {
            countryName = "XY跨国公司";
        } else if (!crossCountry && fromCity.getCountry().equals("Y公司")) {
            countryName = "Y公司";
        }
        // 判断是否满足开设航线的条件
        if (distance > 300) {
            // 创建飞机对象
            Plane plane = new Plane( countryName,
                    crossCountry ? "CC" : "LC",
                    crossCountry ? 90 : 70,
                    50,
                    3500,
                    crossCountry);
            transports.add(plane);
        }

        if (fromCity.isCoastal() || toCity.isCoastal() || fromCity.isIsland() || toCity.isIsland()) {
            // 创建轮船对象
            Ship ship = new Ship(
                    countryName,
                    "S型",
                    100,
                    20,
                    crossCountry
            );
            transports.add(ship);
        }

        if (crossCountry && !fromCity.isIsland() && !toCity.isIsland()) {
            // 创建火车对象
            Train train = new Train(
                    countryName,
                    "T型",
                    5,
                    15,
                    30
            );
            transports.add(train);
        }

        if (crossCountry && !fromCity.isIsland() && !toCity.isIsland() && distance < 500) {
            // 创建巴士对象
            Bus bus = new Bus(
                    countryName,
                    "B型",
                    30,
                    30
            );
            transports.add(bus);
        }

        // 显示交通工具信息
        System.out.println("\n可用交通工具信息:\n");
        for (Transports transport : transports) {
            transport.printInfo();
            transport.calculateTime(distance);
        }
    }
}

最后

代码还有很多的优化空间,例如:

  1. 代码复用:避免重复的代码片段。例如,countryName 的确定逻辑在创建不同的交通工具时被重复了。这可以提取到一个单独的方法中。
  2. 可读性:一些魔法数字和字符串(如 "X公司", "Y公司", 300, 500 等)最好定义为常量,以提高代码的可读性和可维护性。
  3. 条件判断:尽量简化嵌套的条件判断,以减少代码的复杂性。
  4. 异常处理:对于可能出错的地方(如计算距离、获取城市坐标等),应该加入适当的异常处理。
  5. 注释:虽然代码已经有注释,但可以进一步精炼和明确,以便更好地描述每个部分的功能和意图。
  6. OOP 设计原则:可以考虑使用面向对象的设计原则,如单一职责原则(SRP)和开闭原则(OCP),来进一步优化代码结构。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1322014.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

结构体基础例题

这里写目录标题 例题一例题解析答案 例题二例题解析答案 例题三例题解析答案 例题四例题解析答案 例题五例题解析及答案 例题六例题解析及答案 感谢各位大佬对我的支持,如果我的文章对你有用,欢迎点击以下链接 &#x1f412;&#x1f412;&#x1f412; 个人主页 &#x1f978…

手机数码品牌网站建设的作用是什么

手机数码产品几乎已经成为成年人必备的&#xff0c;包括手机、电脑、摄像机、键盘配件等&#xff0c;同时市场中相关企业也非常多&#xff0c;消费者可供选择的商品类型也很多样&#xff0c;而对企业来讲&#xff0c;只有不断提升品牌形象、获客拉新等才能不断提升企业地位&…

亚信安慧AntDB数据库成功助力通信业务核心转型

账务数据库扮演着通信运营商业务支撑系统的核心角色&#xff0c;负责处理亿万用户资料同步、充值缴费和账务记录等重要任务。在5G建设逐渐普及的趋势下&#xff0c;5G业务规模也逐步扩大。面对5G业务的新特点&#xff0c;账务系统对数据库的高并发和高可用性提出了更高的要求。…

Ubuntu系统使用Nginx搭建RTMP服务器

环境&#xff1a; 推流端 rockpi s 主控rk3308 运行ubuntu系统 服务端 ubuntu 播放器 VLC播放器 服务端安装依赖&#xff1a; apt-get install build-essential libpcre3 libpcre3-dev libssl-dev创建nginx编译目录&#xff1a; mkdir my_nginx_rtmp cd my_nginx_rtmp/下载 …

亚马逊,速卖通,shein卖家如何准确有效的测评补单

一、合理规划测评时间和数量 卖家需要合理规划测评的时间和数量。如果卖家过于频繁地进行测评&#xff0c;或者在短时间内进行大量的测评&#xff0c;这可能会被视为恶意行为&#xff0c;从而触犯风控机制。因此&#xff0c;卖家需要根据自己的销售情况和市场需求&#xff0c;…

【机器学习】卷积神经网络(CNN)的特征数计算

文章目录 基本步骤示例图解过程 基本步骤 在卷积神经网络&#xff08;CNN&#xff09;中&#xff0c;计算最后的特征数通常涉及到以下步骤&#xff1a; 确定输入尺寸&#xff1a; 首先&#xff0c;你需要知道输入数据的尺寸。对于图像数据&#xff0c;这通常是 (batch_size, c…

ST股票预测模型(机器学习_人工智能)

知己知彼&#xff0c;百战不殆&#xff1b;不知彼而知己&#xff0c;一胜一负&#xff1b;不知彼&#xff0c;不知己&#xff0c;每战必贻。--《孙子兵法》谋攻篇 ST股票 ST股票是指因连续两年净利润为负而被暂停上市的股票&#xff0c;其风险较高&#xff0c;投资者需要谨慎…

域架构下的功能安全思考

来源&#xff1a;联合电子 随着整车电子电气架构的发展&#xff0c;功能域控架构向整车集中式区域控制演进。新的区域控制架构下&#xff0c;车身控制模块(BCM)&#xff0c;整车控制单元&#xff08;VCU&#xff09;&#xff0c;热管理系统&#xff08;TMS&#xff09;和动力底…

JDK各个版本特性讲解-JDK14特性

JDK各个版本特性讲解-JDK14特性 一、Java14概述二、语法层面的变化1. instanceof2. switch表达式3. 文本块的改进4. Records记录类型 二、关于GC1.G1的NUMA内存分配优化2. 弃用SerialCMS,ParNewSerial Old3.删除CMS4.ZGC on macOS and Windows 三、其他变化1.友好的空指针异常提…

利用python在abaqus中画Voronoi多面体简单示例

利用python在abaqus中画Voronoi多面体简单示例 利用scipy.spatial库得到Voronoi多面体顶点坐标abaqus中绘制多面体CAE操作得到相应rpy文件0、 将vertices.csv和ridge_vertices.csv导入abaqus1、 新建一个part2、创建点3、画线4、画面 完整代码 利用scipy.spatial库得到Voronoi多…

【03】GeoScene创建海图或者电子航道图数据

1 配置Nautical属性 1.1 管理长名称 长名称&#xff08;LNAM&#xff09;是一个必要的对象标识符&#xff0c;是生产机构&#xff08;AGEN&#xff09;、要素识别号码&#xff08;FIDN&#xff09;和要素识别子项&#xff08;FIDS&#xff09;组件的串联。这三个子组件用于数…

azkaban编译时报错的解决方案

大数据单机学习环境搭建(11)Azkaban单机部署&#xff0c;关于Azkaban和gradle下载&#xff0c;本文编译不限于单机solo模式。 一.大多数报错处理 1.1首先操作 1)安装 git yum install git -y 2)替换 azkaban 目录下的 build.gradle 文件的 2处 repositories 信息。改为 阿里…

回归预测 | MATLAB实现GA-LSSVM基于遗传算法优化最小二乘向量机的多输入单输出数据回归预测模型 (多指标,多图)

回归预测 | MATLAB实现GA-LSSVM基于遗传算法优化最小二乘向量机的多输入单输出数据回归预测模型 &#xff08;多指标&#xff0c;多图&#xff09; 目录 回归预测 | MATLAB实现GA-LSSVM基于遗传算法优化最小二乘向量机的多输入单输出数据回归预测模型 &#xff08;多指标&#…

【HCIP学习记录】OSPF之DD报文

1.OSPF报文格式 24字节 字段长度含义Version1字节版本&#xff0c;OSPF的版本号。对于OSPFv2来说&#xff0c;其值为2。Type1字节类型&#xff0c;OSPF报文的类型&#xff0c;有下面几种类型&#xff1a; 1&#xff1a;Hello报文&#xff1b;● 2&#xff1a;DD报文&#xff1…

使用Kaptcha实现的验证码功能

目录 一.需求 二.验证码功能实现步骤 验证码 引入kaptcha依赖 完成application.yml配置文件 浏览器显示验证码 前端页面 登录页面 验证成功页面 后端 此验证码功能是以SpringBoot框架下基于kaptcha插件来实现的。 一.需求 1.页面生成验证码 2.输入验证码&#xff…

vue中echarts柱状图点击x轴数据复制

参考自&#xff1a;Vue 3 使用 vue-echarts 的柱状图 barItem 和 x, y 轴点击事件实现_echarts x轴点击事件-CSDN博客 例如柱状图如下&#xff1a; 步骤&#xff1a; 一、数据处理的时候需要在 xAxis 对象中添加&#xff1a;triggerEvent: true 这个键值对&#xff0c;以增加…

ES索引误删的名场面

慌了3秒&#xff0c;果断发个邮件&#xff1b; 01 最近&#xff0c;在版本发布时&#xff1b; ES线上未备份的索引&#xff0c;被当场「误删」了&#xff1b; 对于新手来说&#xff0c;妥妥的社死名场面&#xff1b; 对于老手来说&#xff0c;慌它3秒表示一下态度&#xff1…

Python3,100行代码,写一段新年祝福视频,为新年喝彩。

新年祝福 1、引言2、代码示例2.1 思路2.2 介绍2.2.1 画布2.2.2 用法 2.3 实例 3、总结 1、引言 小屌丝&#xff1a;鱼哥&#xff0c; 这2023年马上就结束了&#xff0c; 是不是要表示表示。 小鱼&#xff1a;我也在思考这个事情。 小屌丝&#xff1a;这还需要思考&#xff1f;…

kubernetesr安全篇之云原生安全概述

云原生 4C 安全模型 云原生 4C 安全模型&#xff0c;是指在四个层面上考虑云原生的安全&#xff1a; Cloud&#xff08;云或基础设施层&#xff09;Cluster&#xff08;Kubernetes 集群层&#xff09;Container&#xff08;容器层&#xff09;Code&#xff08;代码层&#xf…

modelsim使用技巧

Modelsim关闭Add items to the Project后&#xff0c;该如何添加existing file&#xff1a; 在project页面下&#xff0c;右键选择add to project-add existing file 设置modelsim的仿真波形时间单位&#xff1a; 打开Modelsim后&#xff0c;在Wave-Wave Preferences后&#…