程序要求
多态父类car
package dadaren;
public abstract class car {
int ID;
String carName;
int rent;
int zairen;
int zaihuo;
public abstract void zai();
}
子类aodi
package dadaren;
public class aodi extends car {
int ID=1;
String carName="奥迪A4";
int rent=500;
int zairen=4;
int zaihuo=0;
String zais;
@Override
public void zai() {
if(zairen>0 && zaihuo>0) {
zais="载人:"+zairen+"人;"+"载货:"+zaihuo+"吨";
}else if(zairen>0){
zais="载人:"+zairen+"人";
}else {
zais="载货:"+zaihuo+"吨";
}
// TODO Auto-generated method stub
System.out.println(ID+"\t"+carName+"\t"+rent+"元/天\t"+zais);
}
}
子类pika
package dadaren;
public class pika extends car {
int ID=2;
String carName="皮卡雪6";
int rent=450;
int zairen=4;
int zaihuo=2;
String zais;
@Override
public void zai() {
if(zairen>0 && zaihuo>0) {
zais="载人:"+zairen+"人;"+"载货:"+zaihuo+"吨";
}else if(zairen>0){
zais="载人:"+zairen+"人";
}else {
zais="载货:"+zaihuo+"吨";
}
// TODO Auto-generated method stub
System.out.println(ID+"\t"+carName+"\t"+rent+"元/天\t"+zais);
}
}
子类jinlong
package dadaren;
public class jlong extends car {
int ID=3;
String carName="金龙";
int rent=800;
int zairen=20;
int zaihuo=0;
String zais;
@Override
public void zai() {
if(zairen>0 && zaihuo>0) {
zais="载人:"+zairen+"人;"+"载货:"+zaihuo+"吨";
}else if(zairen>0){
zais="载人:"+zairen+"人";
}else {
zais="载货:"+zaihuo+"吨";
}
// TODO Auto-generated method stub
System.out.println(ID+"\t"+carName+"\t"+rent+"元/天\t"+zais);
}
}
子类shjiang
package dadaren;
public class shjiang extends car {
int ID=4;
String carName="松花江";
int rent=400;
int zairen=0;
int zaihuo=4;
String zais;
@Override
public void zai() {
if(zairen>0 && zaihuo>0) {
zais="载人:"+zairen+"人;"+"载货:"+zaihuo+"吨";
}else if(zairen>0){
zais="载人:"+zairen+"人";
}else {
zais="载货:"+zaihuo+"吨";
}
// TODO Auto-generated method stub
System.out.println(ID+"\t"+carName+"\t"+rent+"元/天\t"+zais);
}
}
子类mzda
package dadaren;
public class mzda extends car {
int ID=5;
String carName="马自达6";
int rent=400;
int zairen=4;
int zaihuo=0;
String zais;
@Override
public void zai() {
if(zairen>0 && zaihuo>0) {
zais="载人:"+zairen+"人;"+"载货:"+zaihuo+"吨";
}else if(zairen>0){
zais="载人:"+zairen+"人";
}else {
zais="载货:"+zaihuo+"吨";
}
// TODO Auto-generated method stub
System.out.println(ID+"\t"+carName+"\t"+rent+"元/天\t"+zais);
}
}
子类ywke
package dadaren;
public class ywke extends car {
int ID=6;
String carName="依维柯";
int rent=1000;
int zairen=0;
int zaihuo=20;
String zais;
@Override
public void zai() {
if(zairen>0 && zaihuo>0) {
zais="载人:"+zairen+"人;"+"载货:"+zaihuo+"吨";
}else if(zairen>0){
zais="载人:"+zairen+"人";
}else {
zais="载货:"+zaihuo+"吨";
}
// TODO Auto-generated method stub
System.out.println(ID+"\t"+carName+"\t"+rent+"元/天\t"+zais);
}
}
主程序test
package dadaren;
import java.util.Scanner;
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
aodi c1=new aodi();
pika c2=new pika();
jlong c3=new jlong();
shjiang c4=new shjiang();
mzda c5=new mzda();
ywke c6=new ywke();
Scanner input=new Scanner(System.in);//创建Scanner对象
System.out.println("欢迎使用达达租车系统:"+"\n你是否要租车:1是 0否");
int isRen=input.nextInt();
if(isRen==1) {
System.out.println("你可租车的类型及其价目表:");
System.out.println("序号"+"\t汽车类型"+"\t日租"+"\t1容量");
c1.zai();
c2.zai();
c3.zai();
c4.zai();
c5.zai();
c6.zai();
System.out.println("输入租车数量:");
int carNum=input.nextInt();
int cID[]=new int[carNum];
int adNum=0;
int pkNum=0;
int jlNum=0;
int shjNum=0;
int mzdNum=0;
int ywkNum=0;
//翻牌
for(int i=0;i<carNum;i++) {
System.out.println("请输入第"+(i+1)+"辆车的序号");
cID[i]=input.nextInt();
}
//取货
for(int j=0;j<carNum;j++) {
switch(cID[j]) {
case 1:
adNum++;
break;
case 2:
pkNum++;
break;
case 3:
jlNum++;
break;
case 4:
shjNum++;
break;
case 5:
mzdNum++;
break;
case 6:
ywkNum++;
break;
}
}
//租金*车的数量;载人数量*车的数量;载货数量*车的数量
c1.rent*=adNum;
c1.zairen*=adNum;
c1.zaihuo*=adNum;
c2.rent*=pkNum;
c2.zairen*=pkNum;
c2.zaihuo*=pkNum;
c3.rent*=jlNum;
c3.zairen*=jlNum;
c3.zaihuo*=jlNum;
c4.rent*=shjNum;
c4.zairen*=shjNum;
c4.zaihuo*=shjNum;
c5.rent*=mzdNum;
c5.zairen*=mzdNum;
c5.zaihuo*=mzdNum;
c6.rent*=ywkNum;
c6.zairen*=ywkNum;
c6.zaihuo*=ywkNum;
int zr=c1.zairen+c2.zairen+c3.zairen+c4.zairen+c5.zairen+c6.zairen;
int zh=c1.zaihuo+c2.zaihuo+c3.zaihuo+c4.zaihuo+c5.zaihuo+c6.zaihuo;
//租的天数
System.out.println("输入你要租的天数:");
int rentDay=input.nextInt();
int zRent=c1.rent*rentDay+c2.rent*rentDay+c3.rent*rentDay+c4.rent*rentDay+c5.rent*rentDay+c6.rent*rentDay;
//账单
System.out.println("你的订单:");
//载人车
if(adNum>0 && c1.zairen>0) {System.out.println("***可载人的车有:"+c1.carName);};
if(pkNum>0 && c2.zairen>0) {System.out.println("***可载人的车有:"+c2.carName);};
if(jlNum>0 && c3.zairen>0) {System.out.println("***可载人的车有:"+c3.carName);};
if(shjNum>0 && c4.zairen>0) {System.out.println("***可载人的车有:"+c4.carName);};
if(mzdNum>0 && c5.zairen>0) {System.out.println("***可载人的车有:"+c5.carName);};
if(ywkNum>0 && c6.zairen>0) {System.out.println("***可载人的车有:"+c6.carName);};
System.out.println("可共载人:"+zr+"个.");
//载货车
if(adNum>0 && c1.zaihuo>0) {System.out.println("***可载人的车有:"+c1.carName);};
if(pkNum>0 && c2.zaihuo>0) {System.out.println("***可载人的车有:"+c2.carName);};
if(jlNum>0 && c3.zaihuo>0) {System.out.println("***可载人的车有:"+c3.carName);};
if(shjNum>0 && c4.zaihuo>0) {System.out.println("***可载人的车有:"+c4.carName);};
if(mzdNum>0 && c5.zaihuo>0) {System.out.println("***可载人的车有:"+c5.carName);};
if(ywkNum>0 && c6.zaihuo>0) {System.out.println("***可载人的车有:"+c6.carName);};
System.out.println("可共载货:"+zh+"吨。");
System.out.println("租车总价格:"+zRent+"元。");
System.out.println("感谢使用!");
}
}
}
主程序中
aodi c1=new aodi(); pika c2=new pika();
jlong c3=new jlong(); shjiang c4=new shjiang();
mzda c5=new mzda(); ywke c6=new ywke();
本打算用
car c1=new aodi(); .....car c6=new ywke();
后面1.
c1.rent的值 该值为car.rent的值 无法真正取到子类aodi.rent值
不知道这里怎么改进
2.无法用for遍历c1,c2,c3,c4,c5,c6循环获取方法