//父类
package com.dadazuche;
public abstract class GongJu {
//载货量
public int zaihuo;
//载人数
public int zairen;
//租金
public int zujin;
//车名
public String name;
//车序号
public int id;
//封装
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public abstract void chexing(); public int getZaihuo() {
return zaihuo;
}
public void setZaihuo(int zaihuo) {
this.zaihuo = zaihuo;
}
public int getZairen() {
return zairen;
}
public void setZairen(int zairen) {
this.zairen = zairen;
}
public int getZujin() {
return zujin;
}
public void setZujin(int zujin) {
this.zujin = zujin;
}
}
//子类奥迪a4
public class Aodi extends GongJu {
//给每个属性赋值
public int zairen=4;
public int zujin=500;
public String name="奥迪A4";
public int id=1;
@Override
//封装,下面子类都一样
public void chexing() {
System.out.println(id+"\t"+name+"\t"+zujin+"\t"+"载人:"+zairen);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getZairen() {
return zairen;
}
public void setZairen(int zairen) {
this.zairen = zairen;
}
public int getZujin() {
return zujin;
}
public void setZujin(int zujin) {
this.zujin = zujin;
}
}
//子类马自达6
public class Mazida extends GongJu {
public int zairen=4;
public int zujin=400;
public String name="马自达6";
public int id=2;
public void chexing() {
System.out.println(id+"\t"+name+"\t"+zujin+"\t"+"载人:"+zairen);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) { t
his.id = id;
}
public int getZairen() {
return zairen;
}
public void setZairen(int zairen) {
this.zairen = zairen;
}
public int getZujin() {
return zujin;
}
public void setZujin(int zujin) {
this.zujin = zujin;
}
}
//子类皮卡雪6
public class Pikaxue extends GongJu {
public int zaihuo=2;
public int zairen=4;
public int zujin=450;
public String name="皮卡雪6";
public int id=3;
public void chexing() {
System.out.println(id+"\t"+name+"\t"+zujin+"\t"+"载人:"+zairen+" 载货:"+zaihuo);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getZaihuo() {
return zaihuo;
}
public void setZaihuo(int zaihuo) {
this.zaihuo = zaihuo;
}
public int getZairen() {
return zairen;
}
public void setZairen(int zairen) {
this.zairen = zairen;
}
public int getZujin() {
return zujin;
}
public void setZujin(int zujin) {
this.zujin = zujin;
}
}
//子类金龙
public class Jinlong extends GongJu {
public int zairen=20;
public int zujin=800;
public String name="金龙";
public int id=4;
public void chexing() {
System.out.println(id+"\t"+name+"\t"+zujin+"\t"+"载人:"+zairen);
}
public int getZairen() {
return zairen;
}
public void setZairen(int zairen) {
this.zairen = zairen;
}
public int getZujin() {
return zujin;
}
public void setZujin(int zujin) {
this.zujin = zujin;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
//子类松花江
public class Songhuajiang extends GongJu {
public int zaihuo=4;
public String name ="松花江";
public int zujin=400;
public int id=5;
public void chexing() {
System.out.println(id+"\t"+name+"\t"+zujin+"\t"+"载货:"+zaihuo);
}
public int getZaihuo() {
return zaihuo;
}
public void setZaihuo(int zaihuo) {
this.zaihuo = zaihuo;
}
public int getZujin() {
return zujin;
}
public void setZujin(int zujin) {
this.zujin = zujin;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
//子类依维柯
public class Yiweike extends GongJu {
public int zaihuo=20;
public int zujin=1000;
public String name="依维柯";
public int id=6; @Override
public void chexing() { // TODO 自动生成的方法存根
System.out.println(id+"\t"+name+"\t"+zujin+"\t"+"载货:"+zaihuo);
}
public int getZaihuo() {
return zaihuo;
}
public void setZaihuo(int zaihuo) {
this.zaihuo = zaihuo;
}
public int getZujin() {
return zujin;
}
public void setZujin(int zujin) {
this.zujin = zujin;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
//测试类
package com.dadazuche;
import java.util.Scanner;
public class Test extends GongJu {
public static void main(String[] args) {
GongJu p1=new Aodi();
GongJu p2=new Mazida();
GongJu p3=new Pikaxue();
GongJu p4=new Jinlong();
GongJu p5=new Songhuajiang();
GongJu p6=new Yiweike();
String[]car= {"奥迪A4","马自达6","皮卡雪6","金龙"};
String[]bus= {"松花江","依维柯"};
Scanner sc=new Scanner(System.in);
System.out.println("欢迎使用嗒嗒租车系统:");
System.out.println("您是否要租车:1是 0否");
int a=sc.nextInt();
if(a==1) {
System.out.println("您可租车的类型及其价目表:");
System.out.println("***序号"+"\t"+"汽车名称"+"\t"+"租金"+"\t"+"容量***");
//获取子类数据
p1.chexing();
p2.chexing();
p3.chexing();
p4.chexing();
p5.chexing();
p6.chexing();
//获取每辆车的数量
String x = " ";
String x1 = " ";
String x2 = " ";
String x3 = " ";
String x4 = " ";
String x5 = " ";
System.out.println("请输入租车天数:");
int b=sc.nextInt();
System.out.println("请输入您要租汽车的数量:");
int c=sc.nextInt();
//获取每个车的数量
System.out.println("请输入第1辆车的数量");
int h=sc.nextInt();
//如果为0则为空
if(h==0) {
System.out.print(" ");
//如果不为零则输出车名。下同
}else {
x=car[0];
}
//第二辆车
System.out.println("请输入第2辆车的数量");
int i=sc.nextInt();
if(i==0) {
System.out.print(" ");
}else {
x1=car[1];
}
//第三辆车
System.out.println("请输入第3辆车的数量");
int j=sc.nextInt();
if(j==0) {
System.out.print(" ");
}else {
x2=car[2];
}
//第四辆车
System.out.println("请输入第4辆车的数量");
int k=sc.nextInt();
if(k==0) {
System.out.print(" ");
}else {
x3=car[3];
}
//第五辆车
System.out.println("请输入第5辆车的数量");
int l=sc.nextInt();
if(l==0) {
System.out.print(" ");
}else {
x4=bus[0];
}
//第六辆车
System.out.println("请输入第6辆车的数量");
int m=sc.nextInt();
if(m==0) {
System.out.print(" ");
}else {
x5=bus[1];
}
//计算总座位
int zuowei=p1.getZairen()*h+p2.getZairen()*i+p3.getZairen()*j+p4.getZairen()*k;
//计算吨位
int dunwei=p3.getZaihuo()*j+p5.getZaihuo()*l+p6.getZaihuo()*m;
//计算租金b为天数
int zujin=(p1.getZujin()*h+p2.getZujin()*i+p3.getZujin()*j+p4.getZujin()*k+p5.getZujin()*l+p6.getZujin()*m)*b;
System.out.println("您的账单:");
System.out.println("***可载人的车有:");
System.out.println(x+"\t"+x1+"\t"+x2+"\t"+x3+"\t"+"共载人:"+zuowei+"人");
System.out.println("***可载货的车有:");
System.out.println(x2+"\t"+x4+"\t"+x5+"\t"+"共载货:"+dunwei+"吨");
System.out.println("***租车总价格:"+zujin+"元");
}else {
System.out.println("谢谢光临"); }
}
public void chexing() {
}
}