为了账号安全,请及时绑定邮箱和手机立即绑定

嗒嗒租车系统 个人原创代码 仅供参考相互学习

标签:
Java
/**奥迪A4
 * @author Administrator
 *
 */
public class A4 extends Vehicle {
    public A4() {//构造函数初始化属性
        this.setRental(500);
        this.setPassengers(4);
        this.setCarName("奥迪A4");
    }

    @Override
    public String toString() {//重写toString()方法
        String info=this.getCarName()+"\t"+this.getRental()+"元/天\t载人:"+this.getPassengers()+"人";
        return info;
    }

}

/**金龙

  • @author Administrator
  • */
    public class JinLong extends Vehicle {
    public JinLong(){//构造函数初始化属性
    this.setCarName("金龙");
    this.setPassengers(20);
    this.setRental(800);
    }

    @Override
    public String toString() {
    String info=this.getCarName()+"\t\t"+this.getRental()+"元/天\t载人:"+this.getPassengers()+"人";
    return info;
    }
    }

/** 马自达6

  • @author Administrator
  • */
    public class M6 extends Vehicle {
    public M6(){//构造函数初始化属性
    this.setCarName("马自达6");
    this.setPassengers(4);
    this.setRental(400);
    }

    @Override
    public String toString() {
    String info=this.getCarName()+"\t"+this.getRental()+"元/天\t载人:"+this.getPassengers()+"人";
    return info;
    }
    }

/**皮卡
 * @author Administrator
 *
 */
public class Pickup extends Vehicle {
    public Pickup(){
        this.setCarName("皮卡雪6");
        this.setPassengers(4);
        this.setWeight(2);
        this.setRental(450);
    }

    @Override
    public String toString() {
        String info = this.getCarName() + "\t" + this.getRental() + "元/天\t载人:" + this.getPassengers()+"人 载货:"+this.getWeight()+"吨";
        return info;
    }

}
/**松花江
 * @author Administrator
 *
 */
public class SongHuaJiang extends Vehicle{
    public SongHuaJiang(){
        this.setCarName("松花江");
        this.setRental(400);
        this.setWeight(4);
    }

    @Override
    public String toString() {
        String info=this.getCarName()+"\t"+this.getRental()+"元/天\t载货:"+this.getWeight()+"吨";
        return info;
    }

}
/**依维柯
 * @author Administrator
 *
 */
public class YiWeiKe extends Vehicle {
    public YiWeiKe(){
        this.setCarName("依维柯");
        this.setRental(1000);;
        this.setWeight(20);
    }

    @Override
    public String toString() {
        String info=this.getCarName()+"\t"+this.getRental()+"元/天\t载货:"+this.getWeight()+"吨";
        return info;
    }
}
/**交通工具类(父类)
 * @author Administrator
 *
 */
public  class Vehicle {
    private String CarName;// 车型
    private int passengers;// 载客人数
    private double rental;// 每天的租金
    private double weight;// 载货数

    public String getCarName() {
        return CarName;
    }

    public void setCarName(String carName) {
        CarName = carName;
    }

    public Vehicle() {
        super();
    }

    public int getPassengers() {
        return passengers;
    }

    public void setPassengers(int passengers) {
        this.passengers = passengers;
    }

    public double getRental() {
        return rental;
    }

    public void setRental(double rental) {
        this.rental = rental;
    }

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }

}
import java.util.Scanner;

/**租车操作类
 * @author Administrator
 *
 */
public class RentalCar {
    private Vehicle[] vehicles = null;//保存租车系统所有类型的车型

    public RentalCar() {//初始化
        vehicles = new Vehicle[6];
        vehicles[0] = new A4();
        vehicles[1] = new M6();
        vehicles[2] = new Pickup();
        vehicles[3] = new JinLong();
        vehicles[4] = new SongHuaJiang();
        vehicles[5] = new YiWeiKe();
    }

    /**
     * 租车清单
     * 
     * @param count
     *            租车的数量
     */
    public void rentalAccount(int count) {
        Scanner input = new Scanner(System.in);
        int index = 0;// 接收用户输入的车型
        Vehicle[] rencalCars = new Vehicle[count];// 保存用户租车的型号
        for (int i = 0; i < count; i++) {
            System.out.println("请输入第" + (i + 1) + "辆车的序号:");
            index = input.nextInt();
            switch (index) {
            case 1:
                rencalCars[i] = new A4();
                break;
            case 2:
                rencalCars[i] = new M6();
                break;
            case 3:
                rencalCars[i] = new Pickup();
                break;
            case 4:
                rencalCars[i] = new JinLong();
                break;
            case 5:
                rencalCars[i] = new SongHuaJiang();
                break;
            case 6:
                rencalCars[i] = new YiWeiKe();
                break;
            default:
                System.out.println("输入的序号有误!重新输入");
                i--;// 重新输入
            }
        }
        System.out.println("请输入租车的天数:");
        int day = input.nextInt();
        ;// 租车天数
        double money = 0;// 租车总费用
        int passengerSum = 0;// 统计乘车总人数
        double weightSum = 0;// 统计总载货量
        StringBuffer s1 = new StringBuffer();
        StringBuffer s2 = new StringBuffer();
        // 遍历用户租车的所有车型
        for (Vehicle v : rencalCars) {
            if (v.getPassengers() != 0) {
                if(s1.indexOf(v.getCarName())<0){//防止相同车型名称重复添加
                    s1.append(v.getCarName() + " ");
                }
                passengerSum += v.getPassengers();
            }
            if (v.getWeight() != 0) {
                weightSum += v.getWeight();
                if(s2.indexOf(v.getCarName())<0){//防止相同车型名称重复添加
                    s2.append(v.getCarName() + " ");
                }

            }
            money += v.getRental() * day;
        }
        System.out.println("您的账单:");
        if (!s1.toString().equals("")) {
            System.out.println("***可载人的车有:\n" + s1.toString() + " 共载人:" + passengerSum + "人");
        }
        if (!s2.toString().equals("")) {
            System.out.println("**载货的车有:\n" + s2 + " 共载货:" + weightSum + "吨");
        }
        System.out.println("***租车总价格:" + money + "元");
        input.close();
    }

    /**
     * 打印租车价格表
     */
    public void printInfo() {
        System.out.println("您可租车的类型及其价目表:");
        System.out.println("序号\t汽车名称\t租金\t容量");
        int i = 1;
        for (Vehicle v : this.vehicles) {
            System.out.println(i + ".\t" + v.toString());
            i++;
        }

    }
}
import java.util.Scanner;

/**测试类
 * @author Administrator
 *
 */
public class Test {

    public static void main(String[] args) {
        System.out.println("欢迎使用嗒嗒租车系统:\n您是否要租车:1是 0否");
        Scanner input = new Scanner(System.in);
        int choice = input.nextInt();//接收用户的选择
        if (choice == 0) {
            System.out.println("再见!欢迎下次使用嗒嗒租车。");
            System.exit(0);
        } else if (choice == 1) {
            RentalCar rc=new RentalCar();//创建操作类对象
            rc.printInfo();// 输出租车价格表
            System.out.println("请输入您要租汽车的数量:");
            int count = input.nextInt();
            rc.rentalAccount(count);//打印租车清单
        } else {
            System.out.println("输入错误,程序结束");
        }
        input.close();
    }

}
点击查看更多内容
6人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消