1.创建车类
package imooc;
public class Car {
public int id; //车型序号
public String name;//车名
public int price;//车价 元/天
public void setid(int id){ //设置id
this.id=id;
}
public int getid(){ //获取id
return id;
}
public void setname(String name){
this.name=name;
}
public String getname(){
return name;
}
public void setprice(int price){
this.price=price;
}
public int getprice(){
return price;
}
}
2.创建货物接口
package imooc;
public interface Goods {
public void setgoods(int goods);
public int getgoods();获取货物
}
3.创建人接口
package imooc;
public interface Persion {
public void setpersion(int persion);
public int getpersion();获取人数
}
4.创建汽车(载人)类
package imooc;
public class Bus extends Car implements Persion { //继承父类方法,人接口
private int persion;//人数
//构造方法
public Bus(int id,String name,int price,int persion){
setid(id); //设置相应属性值
setname(name);
setprice(price);
setpersion(persion);
}
//重载父类方法
@Override
public void setid(int id) {
// TODO Auto-generated method stub
this.id=id;
}
@Override
public int getid() {
// TODO Auto-generated method stub
return id;
}
@Override
public void setname(String name) {
// TODO Auto-generated method stub
this.name=name;
}
@Override
public String getname() {
// TODO Auto-generated method stub
return name;
}
@Override
public void setprice(int price) {
// TODO Auto-generated method stub
this.price=price;
}
@Override
public int getprice() {
// TODO Auto-generated method stub
return price;
}
//重载接口方法
@Override
public void setpersion(int persion) {
// TODO Auto-generated method stub
this.persion=persion;
}
@Override
public int getpersion() {
// TODO Auto-generated method stub
return persion;
}
@Override
public String toString() { //toString方法输出类的各项属性
return id+" "+name+" "+price+" "+persion;
}
}
5.创建卡车类(载物)
package imooc;
public class Truck extends Car implements Goods { //继承父类和货物接口
private int goods;
public Truck(int id,String name,int price,int goods){
setid(id);
setname(name);
setprice(price);
setgoods(goods);
}
@Override
public void setid(int id) {
// TODO Auto-generated method stub
this.id=id;
}
@Override
public int getid() {
// TODO Auto-generated method stub
return id;
}
@Override
public void setname(String name) {
// TODO Auto-generated method stub
this.name=name;
}
@Override
public String getname() {
// TODO Auto-generated method stub
return name;
}
@Override
public void setprice(int price) {
// TODO Auto-generated method stub
this.price=price;
}
@Override
public int getprice() {
// TODO Auto-generated method stub
return price;
}
@Override
public void setgoods(int goods) {
// TODO Auto-generated method stub
this.goods=goods;
}
@Override
public int getgoods() {
// TODO Auto-generated method stub
return goods;
}
@Override
public String toString() {
return id+" "+name+" "+price+" "+goods;
}
}
6.创建皮卡类(可载人载物)
package imooc;
public class Picar extends Car implements Goods, Persion {//继承父类,人和货物接口
private int goods;
private int persion;
public Picar(int id,String name,int price,int persion,int goods){
setid(id);
setname(name);
setprice(price);
setpersion(persion);
setgoods(goods);
}
@Override
public void setid(int id) {
// TODO Auto-generated method stub
this.id=id;
}
@Override
public int getid() {
// TODO Auto-generated method stub
return id;
}
@Override
public void setname(String name) {
// TODO Auto-generated method stub
this.name=name;
}
@Override
public String getname() {
// TODO Auto-generated method stub
return name;
}
@Override
public void setprice(int price) {
// TODO Auto-generated method stub
this.price=price;
}
@Override
public int getprice() {
// TODO Auto-generated method stub
return price;
}
@Override
public void setpersion(int persion) {
// TODO Auto-generated method stub
this.persion=persion;
}
@Override
public int getpersion() {
// TODO Auto-generated method stub
return persion;
}
@Override
public void setgoods(int goods) {
// TODO Auto-generated method stub
this.goods=goods;
}
@Override
public int getgoods() {
// TODO Auto-generated method stub
return goods;
}
@Override
public String toString() {
return id+" "+name+" "+price+" "+persion+" "+goods;
}
}
7.主测试类
package imooc;
import java.util.Scanner;
public class Test {
public static String percar=" "; //载人的车
public static String gocar=" "; //载物的车
public static int allprice; //总价钱
public static int allgoods; //总载物
public static int allpersion; //总载人
int num[];
public static void main(String[] args) {
// TODO Auto-generated method stub
Car[] cars={new Bus(1,"奥迪A4",500,4), //创建车类数组,通过使用对象输入车的属性值
new Bus(2,"马自达6",400,4),
new Picar(3,"皮卡雪6",450,4,2),
new Bus(4,"金龙 ",800,20),
new Truck(5,"松花江 ",400,4),
new Truck(6,"依维柯 ",1000,20)};
System.out.println("欢迎使用答答租车系统"+"\n"+"您是否要租车? 1是 0否");
Scanner input=new Scanner(System.in);
int s=input.nextInt();
if(s!=1){
System.out.println("欢迎下次使用");
}else{
System.out.println("您可租车的类型及价格表如下:"+"\n"+"序号 车名 租金 载量(人/个 货/t)");
for(Car car:cars){ //遍历数组并输出对象属性
System.out.println(car);
}
System.out.println("请输入您的租车数量:");
int n=input.nextInt();
int num[]=new int[n];
for(int i=0;i<n;i++){
System.out.println("请输入第"+(i+1)+"辆车的序号:");
num[i]=input.nextInt();
}
System.out.println("请输入租车天数:");
int time=input.nextInt();
input.close(); //关闭输入
for(int carid:num){ //遍历车id
for(Car car:cars){ //遍历车类数组对象
if(carid==car.id){
allprice+=(car.price*time); //计算总价
if(car instanceof Bus){ //instanceof判断一个对象是否属于某个类
percar=percar+car.name+" "; //载人车名累加(String类)
allpersion+=((Bus) car).getpersion(); // 计算总载人数,强制类型转换,通过get方法获得人数
}else{
if(car instanceof Truck){
gocar+=car.name+" ";
allgoods+=((Truck) car).getgoods();
}else{
percar+=car.name+" ";
gocar+=car.name+" ";
allgoods+=((Picar)car).getgoods();
allpersion+=((Picar)car).getpersion();
}
}
}
}
}
System.out.println("您的账单:"+"\n"+"能载人的车有:"+"\n"+percar+"总载人数:"+allpersion+"\n"+"能载货的车有:"+
"\n"+gocar+"总载货量:"+allgoods+"\n"+"总金额:"+allprice); //打印账单
}
}
}
运行结果: