Java 汽车租赁系统详细实现代码

汽车租赁:

分为客车和轿车两种:

客车小于20座:500一天,大于20座:900一天。

轿车分为豪华和普通:豪华600一天,普通200一天。

效果图:

代码如下:

机动车类:

package busTest;

/*
机动车类
 */
public abstract  class MotoVehicle {
    private String carNumber; //车牌号
    private String carBrand;  // 车品牌

    //构造方法
    public MotoVehicle(){}
    public MotoVehicle(String carNumber, String carBrand) {
        this.carNumber = carNumber;
        this.carBrand = carBrand;
    }

    // get/set
    public String getCarNumber(){
        return carNumber;
    }
    public void setCarNumber(String carNumber){
        this.carNumber = carNumber;
    }

    public String getCarBrand(){
        return carBrand;
    }
    public void setCarBrand(String carBrand){
        this.carNumber = carNumber;
    }

    /*
    计算租赁的方法
     */
    public abstract int calRent(int days);

}

客车类:

package busTest;

public class Bus extends MotoVehicle {

    private int setCount;  //座位数

    //通过构造方法初始化对象
    public Bus(String carNUmber, String brand, int setCount) {
        super(carNUmber, brand);
        this.setCount = setCount;
    }

    @Override
    public int calRent(int days) {
        //根据座位数量来判断租赁的金额
        if (this.setCount < 20) {
            return days * 500;
        } else {
            return days * 900;
        }
    }

    public void showBusInfo(int days) {
        System.out.println("*");
        System.out.println("\t车牌号:" + super.getCarNumber());
        System.out.println("\t车品牌:" + super.getCarBrand());
        System.out.println("\t座位数:" + this.setCount);
        System.out.println("\t租赁天数:" + days);
        System.out.println("\t金额:" + calRent(days));

    }
}

轿车类:

package busTest;

public class Car extends MotoVehicle {

    private String type;  //汽车类型  普通/豪华

    //通过构造方法初始化对象
    public Car(String carNUmber, String brand, String type) {
        super(carNUmber, brand);
        this.type = type;
    }

    @Override
    public int calRent(int days) {
        //根据类型来决定价格
        if ("豪车".equals(type)) {
            return days * 600;
        } else {
            return days * 200;
        }
    }

    public void showCarInfo(int days) {
        System.out.println("*");
        System.out.println("\t车牌号:" + super.getCarNumber());
        System.out.println("\t车品牌:" + super.getCarBrand());
        System.out.println("\t车类型:" + this.type);
        System.out.println("\t租赁天数:" + days);
        System.out.println("\t金额:" + calRent(days));

    }
}

租车顾客类:

package busTest;

/*
顾客类
 */

import java.util.Scanner;

public class Customer {

    private String name;
    private int sum = 0;

    //当不确定我的购物车内具体是轿车还是客车,那就以父亲类类型创建对象数组
    MotoVehicle[] motos = new MotoVehicle[10];

    Scanner input = new Scanner(System.in);

    public void showMenu() {
        //定义一个父类机动车的对象,在下面可以接收
        MotoVehicle moto = null;

        System.out.println("******汽车租赁系统*******");
        String answer;
        do {
            System.out.println("1、租赁客车  2、租赁轿车");
            System.out.print("请输入编号:");
            int num = input.nextInt();
            if (num == 1) {
                //创建租赁的客车对象
                moto = rentBus();
            } else if (num == 2) {
                //创建租赁的轿车对象
                moto = rentCar();
            }
            for (int i = 0; i < motos.length; i++) {
                if (motos[i] == null) {
                    motos[i] = moto;
                    break;
                }
            }
            System.out.print("是否继续租赁?:y/n");
            answer = input.next();
        } while (!"n".equals(answer));
        System.out.print("请输入你的姓名:");
        this.name = input.next();
        System.out.print("租赁的天数:");
        int days = input.nextInt();

        //根据天数来统计租赁金额
        calTotalRent(days);

        //显示租赁的信息
        showInfo(days);

    }

    private void showInfo(int days) {

        System.out.println("---------------------租赁汽车信息---------------------");
        for (int i = 0; i < motos.length; i++) {
            MotoVehicle moto = this.motos[i];
            if (moto != null) {
                if (moto instanceof Bus) {
                    Bus bus = (Bus) moto;
                    bus.showBusInfo(days);
                } else if (moto instanceof Car) {
                    Car car = (Car) moto;
                    car.showCarInfo(days);
                }

            }
        }

        System.out.println("\t顾客:" + this.name + "\t\t总金额:" + sum);
        System.out.println("----------------------------------------------------");
    }

    private void calTotalRent(int days) {

        int total = 0;
        for (MotoVehicle moto : motos) {
            if (moto != null) {
                int rent = moto.calRent(days);
                total += rent;  //累加总金额
            }
            this.sum = total;// 把总金额复制给全局变量 sum

        }
    }

    //轿车
    private MotoVehicle rentCar() {
        System.out.print("请输入轿车品牌:");
        String brand = input.next();
        System.out.print("请输入轿车车牌号:");
        String carNumber = input.next();
        System.out.print("请选择轿车类型[1、豪车 2、普通车:]");
        int choise = input.nextInt();
        String type;
        if (choise == 1) {
            type = "豪车";
        } else {
            type = "普通型";
        }
        return new Car(carNumber, brand, type);
    }

    //客车
    private MotoVehicle rentBus() {
        System.out.print("请输入客车品牌:");
        String brand = input.next();
        System.out.print("请输入客车车牌号:");
        String carNumber = input.next();
        System.out.print("请输入客车座位数:");
        int seatCount = input.nextInt();

        return new Bus(carNumber, brand, seatCount);
    }
}

测试类:

package busTest;

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

        Customer customer = new Customer();
        customer.showMenu();
    }
}

到此这篇关于Java 汽车租赁系统详细实现代码的文章就介绍到这了,更多相关Java 汽车租赁系统内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • Java面向对象实现汽车租赁系统

    本文实例为大家分享了Java实现汽车租赁系统的具体代码,供大家参考,具体内容如下 父类Vehicle public abstract class Vehicle {     private String num;     private String brand;     private  double rent;     //重写equals方法     public abstract boolean equals(Vehicle v);     //计算费用     public abstr

  • java实现汽车租赁系统

    本文实例为大家分享了java实现汽车租赁系统的具体代码,供大家参考,具体内容如下 //车类 public abstract class Vehicle { //车牌号 品牌 日租金 private String id; private String brand; private int perRent; public Vehicle(){} //Vehicle的带参构造方法 public Vehicle(String id, String brand, int perRent) { this.i

  • java编写汽车租赁系统

    本文实例为大家分享了java编写汽车租赁系统的具体代码,供大家参考,具体内容如下 题目要求: 1,汽车租赁信息表如下: 2,类和属性: 3,运行效果: 效果实现: 代码实现: 1,车类: package homework.exam; public abstract  class Vehicle {     private String num;     private String brand;     private double rent;     public String getNum(

  • Java实现简单汽车租赁系统

    本文实例为大家分享了Java实现简单汽车租赁系统的具体代码,供大家参考,具体内容如下 需求如下:  问题分析: 首先应当构建一个MotoVehicle的抽象(abstract)类,类里面包含一个brand属性,表示汽车品牌:还包含一个no属性,表示汽车牌号: package cn.jbit.car; public abstract class MotoVehicle { private String no; private String brand; /** * 无参构造方法 */ public

  • java实现简单的汽车租赁系统

    本文实例为大家分享了java实现简单的汽车租赁系统的具体代码,供大家参考,具体内容如下 欢迎进入xx汽车租赁公司请输入用户名请输入密码(用户名默认是名字缩写,密码是123,将登陆模块封装到方法中去调用方法)请输入您的操作1)查看现在车库中的所有车辆信息2)租赁汽车3)往车库中添加汽车4)修改汽车租赁价格信息 用switch去判断操作 类分析 代码: package com.youjiuye.bms; public class CRMS {     public static void main(

  • Java 汽车租赁系统详细实现代码

    汽车租赁: 分为客车和轿车两种: 客车小于20座:500一天,大于20座:900一天. 轿车分为豪华和普通:豪华600一天,普通200一天. 效果图: 代码如下: 机动车类: package busTest; /* 机动车类 */ public abstract class MotoVehicle { private String carNumber; //车牌号 private String carBrand; // 车品牌 //构造方法 public MotoVehicle(){} publ

  • C#实现汽车租赁系统项目

    本文实例为大家分享了C#实现汽车租赁系统的具体代码,供大家参考,具体内容如下 汽车和卡车的父类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; //父类变量和方法 namespace 汽车租赁系统 { public class Inheritance { public Inheritance() { } publi

  • Java从零编写汽车租赁系统全程分析

    目录 覆盖知识 项目需求 设计步骤 开发思路 类的属性和方法 代码展示 效果展示 覆盖知识 程序基本概念.数据类型.流程控制.顺序.选择 .循环.跳转语句.变量.类.方法.继承.多态. 掌握数据库.JDBC.三层架构等相关知识. 掌握Druid连接池.Apache的DBUtils使用 . 项目需求 某汽车租赁公司出租多种轿车和客车,出租费用以日为单位计算. 出租车型及信息如下表所示: 车型 具体信息 日租金 折扣 轿车 宝马X6(京NY28588) 800 days>7天9折 days>30天

  • C#实现简单的汽车租赁系统

    最近学习了继承,多态,集合,设计模式,有一个汽车租凭系统,给大家分享一下: 我们首先来看看我们这个系统的效果 1.做一个项目,我们首先对项目进行分析 根据我们最近学的知识,我们可以看出继承,多态,集合,设计模式,我们都能用到 我们把所需要的类和简单模式中的"简单工厂"的工厂准备好 类图: 01.车辆类(父类) using System; using System.Collections.Generic; using System.Linq; using System.Text; usi

  • Java实现文件检索系统的示例代码

    示例代码 package Demo; import java.io.File; import java.io.FilenameFilter; import java.util.Scanner; import java.lang.Exception; import java.io.IOException; public class Demo8_1 { public static void main(String[] args) { while(true) { System.out.println(

随机推荐