继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

借鉴了其他同学的写了半天呢 还是不熟悉

慕粉1359211223
关注TA
已关注
手记 1
粉丝 1
获赞 1

父类
package car;

public abstract class Car{
protected int price;//车的租金
protected String name;//车的名字
protected int passengerLoad;//载人数量
protected double goodsLoad;//载货重量

public int getPrice(){
    return price;
}

public void setPrice(int price){//执行方法
    this.price=price;
}
public String getName(){
    return name;
}
public void setName(String name){
    this.name=name;
}
public int getPassengerLoad(){
    return passengerLoad;
}
public void setpassengerLoad(int passengerLoad){
    this.passengerLoad=passengerLoad;

}
public double getGoodsLoad(){
    return goodsLoad;
}
public void setgoodsLoad(double goodsLoad){
    this.goodsLoad=goodsLoad;
}

}
客车
package car;

public class keCar extends Car {
public keCar (String name,int price,double goodsLoad,int passengerLoad){
this.setName(name);
this.setPrice(price);
this.setpassengerLoad(passengerLoad);

}

public String toString(){
return this.getName()+"----"+this.getPrice()+"元/天----载客"+this.getPassengerLoad()+"人";
}
}
货车
public class truck extends Car{
public truck(String name,int price,double goodsLoad,int passengerLoad){
this.setName(name);
this.setPrice(price);
this.setgoodsLoad(goodsLoad);

}

public String toString(){
return this.getName()+"----"+this.getPrice()+"元/天----载客"+this.getGoodsLoad()+"货";

}
}
两用
public class bothCarry extends Car{
public bothCarry (String name,int price,double goodsLoad,int passengerLoad){
this.setName(name);
this.setPrice(price);
this.setpassengerLoad(passengerLoad);
this.setgoodsLoad(goodsLoad);
}
public String toString(){
return this.getName()+"----"+this.getPrice()+"元/天----载客"+this.getPassengerLoad()+"人"+this.getGoodsLoad()+"货";

}
}
test
public class text {

public static void main(String[] args) {
    // TODO Auto-generated method stub

Scanner sc=new Scanner(System.in);
System.out.println("欢迎使用答答租车系统");
System.out.println();
System.out.println("租车请按(1) 推出请按(0)");
int num=sc.nextInt();
Car[] cars={new keCar("奥迪A4",300,0,4),
new keCar("雪佛兰5",250,0,4),
new keCar("宝骏巴士",800,0,45),
new truck("东风R1",1000,10,0),
new truck("依维柯S",1500,25,0),
new bothCarry("皮卡雪Z",600,5,2)};
if(num!=1){
System.out.println("
欢迎下次光临**");
System.exit(0);
}else{

    System.out.println("请输入你要预订的数量") ;
  }

    int priceSum=0;
double goodsLoadSum=0;
    int passengerLoadSum=0;
    int i=sc.nextInt();
    for(int j=1;j<=i;j++){
        System.out.println("请输入第"+j+"辆要预订车辆的序号");

        int id=sc.nextInt();
        passengerLoadSum=passengerLoadSum+cars[id-1].getPassengerLoad();
        goodsLoadSum=goodsLoadSum+cars[id-1].getGoodsLoad();
        priceSum=priceSum+cars[id-1].getPrice();

    }
    System.out.println("请输入租车天数");
    int day=sc.nextInt();
    priceSum=priceSum*day;

    System.out.println("您的账单:");
    System.out.println("共租车"+i+"辆,"+"共租车"+day+"天");
    System.out.println("一共可载"+passengerLoadSum+"人,"+"共可载"+goodsLoadSum+"吨");
    System.out.println("总价格"+priceSum+"元"); 

    sc.close();

System.out.println("谢谢惠顾");
}

}

打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP