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

达达租车测试类,不足之处请指点。

qq_红尘如梦_0
关注TA
已关注
手记 2
粉丝 2
获赞 70

车类

package com.rental;
public class ToolCar {
    private String Name;       //车类型
    private int    cargo;      //车的载货量
    private int    manned;     //车的载人量
    private float  price;      //价格

    public void getList(){     //此方法给子类用到

    }
     /*get和set方法封装属性*/
    public String getName() {
        return Name;
    }
    public void setName(String name) {
        Name = name;
    }
    public int getCargo() {
        return cargo;
    }
    public void setCargo(int cargo) {
        this.cargo = cargo;
    }
    public float getPrice() {
        return price;
    }
    public void setPrice(float price) {
        this.price = price;
    }
    public int getManned() {
        return manned;
    }
    public void setManned(int manned) {
        this.manned = manned;
    }
}

载货类继承—>车类

package com.rental;
public class Truck extends ToolCar {
    /*有参构造方法*/
    public Truck(String carType, int carCapacity, int day, int Price){
        this.setName(carType);
        this.setCargo(carCapacity);
        this.setPrice(Price);
    }

    public void getList(){//重写父类方法
    System.out.println(getName()+"\t"+getPrice()+"元/天"+"\t"+"载货:"+getCargo()+"T");
    }
}

载人类继承—>车类

package com.rental;
public class Manned extends ToolCar {
/*有参构造方法*/
public Manned(String carType, int carTourists, int day, int Price){
this.setName(carType);
this.setManned(carTourists);
this.setPrice(Price);
}
public void getList(){//重写父类方法
System.out.println(getName()+"\t"+getPrice()+"元/天"+"\t"+"载人:"+getManned()+"人");
}
}

载人载货的类继承—>车类

package com.rental;
public class cargo extends ToolCar {
/*有参构造方法*/
public cargo(String carType, int carCapacity, int carTourists, int day, int Price){
this.setName(carType);
this.setCargo(carCapacity);
this.setManned(carTourists);
this.setPrice(Price);
}
public void getList(){//重写父类方法
System.out.println(getName()+"\t"+getPrice()+"元/天"+"\t"+"载货:"+getCargo()+"T"+"\t"+"载人:"+getManned()+"人");
}
}

达达租车测试类

package com.rental;
import java.util.Scanner;
public class CarRental {
static Scanner inputs;
public static void main(String[] args) {    
ToolCar[] cars={new Truck ("松花江   " ,  4, 3 ,400),
new Truck ("依维柯   " , 20, 3 ,1000),
new Manned("马自达6" , 10, 3 ,400),
new Manned("奥迪A4 " ,  5, 3 ,400),
new Manned("金龙        " ,  9, 3 ,400), 
new cargo ("皮卡雪6" ,  2, 4 ,3,400),};
System.out.println("欢迎使用达达租车系统\n您是否需要租车:1是    0否");
inputs =new Scanner(System.in);//初始化Scanner对象
int num =inputs.nextInt();     //获取终端输入的值
if(num==1){                    //值为1,进入块
/*显示车列表*/
System.out.println("序号"+"\t"+"汽车名称"+"\t"+"
租金"+"\t"+"\t"+"容量");
for(int i=0;i<cars.length;i++){
System.out.print(i+1+"\t");
cars[i].getList();         //调用此方法打印每辆车的信息
}
System.err.println("请选择车的数量");
int value =inputs.nextInt();
ToolCar[] car1 =new ToolCar[value];
for(int i=0;i<value;i++){
System.out.print("请选择第"+(i+1)+"辆车的序号:");
int inputValue =inputs.nextInt();
System.out.println(inputValue);
car1[i] =cars[inputValue-1];
System.out.println(car1[i].getName());
}
System.out.println("请选择借出天数:");
int day =inputs.nextInt();
System.out.println("清单列表:");
String carName;
int    Price;
int    Total=0;

for(int i=0;i<car1.length;i++){
carName =car1[i].getName();
Price   =(int)car1[i].getPrice()*day;
Total  +=Price;
System.out.println((i+1)+"."+carName+"  单价:"+car1[i].getPrice()+"  借出:"+day+"天"+"  总计:"+Price);
}
System.out.println("总价格:"+Total);
}else {
System.out.println("欢迎下次光临");
}
}

}

>测试结果

欢迎使用达达租车系统
您是否需要租车:1是 0否
1
序号 汽车名称 租金 容量
1 松花江 400.0元/天 载货:4T
2 依维柯 1000.0元/天 载货:20T
3 马自达6 400.0元/天 载人:10人
4 奥迪A4 400.0元/天 载人:5人
5 金龙 400.0元/天 载人:9人
6 皮卡雪6 400.0元/天 载货:2T 载人:4人

请选择车的数量
5
请选择第1辆车的序号:5
5
金龙
请选择第2辆车的序号:2
2
依维柯
请选择第3辆车的序号:3
3
马自达6
请选择第4辆车的序号:6
6
皮卡雪6
请选择第5辆车的序号:1
1
松花江

请选择借出天数:
6
清单列表:
1.金龙 单价:400.0 借出:6天 总计:2400
2.依维柯 单价:1000.0 借出:6天 总计:6000
3.马自达6 单价:400.0 借出:6天 总计:2400
4.皮卡雪6 单价:400.0 借出:6天 总计:2400
5.松花江 单价:400.0 借出:6天 总计:2400
总价格:15600

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

热门评论

数组里的3是什么。。对应哪里的代码

查看全部评论