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

根据某位大神的代码以及思路改进的答答租车系统.

标签:
Java

首先创建一个父类,Carstore,
然后创建三个子类,PickUp,Coach,Van
接着创建两个接口,IVan,ICoach.

package com.heida;

public class Carstore {
    private int id;
    private String name;
    private int rent;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getRent() {
        return rent;
    }
    public void setRent(int rent) {
        this.rent = rent;
    }

}
package com.heida;

public class Coach extends Carstore implements ICoach{
    //使用private修饰符,使内部数据更加安全.
    private int peopleCapacity;

    //构造一个带参的方法,并且通过super关键字调用父类的方法.
    public  Coach(int id,String name,int rent,int peopleCapacity) {
        super.setId(id);
        super.setName(name);
        super.setRent(rent);
        this.setpeopleCapacity(peopleCapacity);
    }

    /*
     * 这一段是不使用接口的get/set方法.
     * public int getpeopleCapacity() {
        return peopleCapacity;
    }

    public void setPeopleCapacity(int peopleCapacity) {
        this.peoplecapacity = peopleCapacity;
    }*/
    //使用toString方法输出车辆信息.
    public String toString() {
        return super.getId()+"\t"+super.getName()+"\t"+super.getRent()+"元/天"+"\t"+"\t"+this.getpeopleCapacity()+"人";
    }

    @Override
    //继承接口的方法.
    public void setpeopleCapacity(int peopleCapacity) {
        // TODO Auto-generated method stub
        this.peopleCapacity =peopleCapacity;
    }

    @Override
    public int getpeopleCapacity() {
        // TODO Auto-generated method stub
        return peopleCapacity;
    }

}
package com.heida;

public class Van extends Carstore implements IVan{
    private int cargoCapacity;

    public Van(int id,String name,int rent,int cargocapacity) {
        super.setId(id);
        super.setName(name);
        super.setRent(rent);
        this.setcargoCapacity(cargocapacity);

    }

    /*public int getCargocapacity() {
        return cargocapacity;
    }

    public void setCargocapacity(int cargocapacity) {
        this.cargocapacity = cargocapacity;
    }*/
    public String toString() {
        return super.getId()+"\t"+super.getName()+"\t"+super.getRent()+"元/天"+"\t"+this.getcargoCapacity()+"吨";
    }

    @Override
    public void setcargoCapacity(int cargoCapacity) {
        // TODO Auto-generated method stub
        this.cargoCapacity = cargoCapacity;
    }

    @Override
    public int getcargoCapacity() {
        // TODO Auto-generated method stub
        return cargoCapacity;
    }

}
package com.heida;

public class PickUp extends Carstore implements ICoach,IVan{
    private int peopleCapacity;
    private int cargoCapacity;

    public PickUp(int id,String name,int rent,int peopleCapacity,int cargocapacity) {
        super.setId(id);
        super.setName(name);
        super.setRent(rent);
        this.setpeopleCapacity(peopleCapacity);
        this.setcargoCapacity(cargocapacity);
    }

    /*public int getPeopleCapacity() {
        return peopleCapacity;
    }
    public void setPeopleCapacity(int peopleCapacity) {
        this.peopleCapacity = peopleCapacity;
    }
    public int getCargocapacity() {
        return cargocapacity;
    }
    public void setCargocapacity(int cargocapacity) {
        this.cargocapacity = cargocapacity;
    }*/

    public String toString() {
        return super.getId()+"\t"+super.getName()+"\t"+super.getRent()+"元/天"+"\t"+this.getpeopleCapacity()+"人"+"\t"+this.getcargoCapacity()+"吨";
    }

    @Override
    public void setcargoCapacity(int cargoCapacity) {
        // TODO Auto-generated method stub
        this.cargoCapacity = cargoCapacity;
    }

    @Override
    public int getcargoCapacity() {
        // TODO Auto-generated method stub
        return cargoCapacity;
    }

    @Override
    public void setpeopleCapacity(int peopleCapacity) {
        // TODO Auto-generated method stub
        this.peopleCapacity = peopleCapacity;
    }

    @Override
    public int getpeopleCapacity() {
        // TODO Auto-generated method stub
        return peopleCapacity;
    }

}
package com.heida;

public interface ICoach {
    public void setpeopleCapacity(int peopleCapacity);
    public int getpeopleCapacity();

}
package com.heida;

public interface IVan {
    public void setcargoCapacity(int cargoCapacity);
    public int getcargoCapacity();
}
package com.heida;
import java.util.Scanner;
public class CarUseSystem {
    public void CarUse() {
    /*通过"数组+多态"的方法创建各种类型的车
     * 轿车参数,
     * 皮卡参数,
     * 火车参数,
     */
    Carstore[] car = {new Coach(1,"普拉多",999,6),
                      new Coach(2,"金牛座",800,4),
                      new Coach(3,"自由侠",700,4),
                      new Coach(4,"朗逸",109,5),
                      new Coach(5,"科鲁兹",109,5),
                      new Coach(6,"帕萨特",212,5),
                      new PickUp(7,"丰田坦途",1000,5,15),
                      new Van(8,"依维柯",1100,20),
                      new Van(9,"松花江",1000,15)};
    System.out.println("欢迎您使用格致租车系统"+"\n是否租车    1是    0否  ");
    Scanner input = new Scanner(System.in); //调用Scanner工具类接收输入的值.
    int i = input.nextInt();
    if(i==1) {
        System.out.println("下面是可租车辆价目表:"      +"\n序号"+"\t"+"车名"+"\t"+"租金"+"\t"+"\t"+"容量");
        //通过for循环将车辆信息将车辆信息依次输出.
        for(i=0;i<car.length;i++) {
            System.out.println(car[i]);
        }
        System.out.println("请输入您要租的数量:");
        int j = input.nextInt();
            if(i>9||i==0||i<0) {
                System.out.println("输入错误,系统无法验证!");
                System.exit(0);
            }
            String outname = "";     //车名
            int rentsum = 0;         //租金总和
            int peoplesum = 0;       //载客人数和
            int cargosum = 0;        //载货量和
            int count = 1;           
            int w ;
            while(count<=j) {
                //使用do..while循环,先执行后判断.
                do {
                    System.out.println("请输入第"+count+"辆车的序号:");
                    w=input.nextInt()-1;
                }while(w>=car.length||w<0);
                //使用if循环,判断输入的车辆序号为哪种类型,并且计算租金等信息.
                if(car[w].getClass()==Coach.class) {
                     outname=car[w].getName();
                     System.out.println(outname);
                     rentsum = car[w].getRent()+rentsum;
                     peoplesum = ((Coach)car[w]).getpeopleCapacity()+peoplesum;
                }else if(car[w].getClass()==PickUp.class){
                    outname = car[w].getName();
                    System.out.println(outname);
                    rentsum = car[w].getRent()+rentsum;
                    peoplesum = ((PickUp)car[w]).getpeopleCapacity()+peoplesum;
                    cargosum = ((PickUp)car[w]).getcargoCapacity()+cargosum;    
                }else if(car[w].getClass()==Van.class) {
                    outname = car[w].getName();
                    System.out.println(outname);
                    rentsum = car[w].getRent()+rentsum;
                    cargosum = ((Van)car[w]).getcargoCapacity()+cargosum;
                }
                count++;    
            }
            System.out.println("请输入您要租的天数:");
            int day = input.nextInt();
            System.out.println("您的账单如下:");
            System.out.println("载人数:"+peoplesum+"人"+"\n载货量:"+cargosum+"吨"+"\n总租金:"+rentsum*day+"元");
            System.out.println("感谢您光临格致租车,祝您生活愉快!");

    }

    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
          CarUseSystem cus = new CarUseSystem();
          cus.CarUse();
          System.exit(0);
    }

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

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

评论

作者其他优质文章

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

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消