父类
package com.imooc;
public abstract class Car {
int xuhao;
String name;
int price;
int zairen;
int zaihuo;
public abstract void chengren();
public abstract void chenghuo();
}
子类
package com.imooc;
public class xiaoqiche extends Car {
@Override
public void chenghuo() {
// TODO Auto-generated method stub
}
@Override
public void chengren() {
// TODO Auto-generated method stub
System.out.println(xuhao + ".\t" + name + "\t" +price + "元/天\t" + zairen + "人");
}
}
package com.imooc;
public class bus extends Car {
@Override
public void chenghuo() {
// TODO Auto-generated method stub
System.out.println(xuhao + ".\t" + name + "\t" +price + "元/天\t" + zaihuo + "吨");
}
@Override
public void chengren() {
// TODO Auto-generated method stub
}
}
package com.imooc;
public class pica extends Car {
@Override
public void chenghuo() {
// TODO Auto-generated method stub
}
@Override
public void chengren() {
// TODO Auto-generated method stub
System.out.println(xuhao + ".\t" + name + "\t" +price + "元/天\t" + zairen + "人" + zaihuo + "吨");
}
}
测试类
package com.imooc;
import java.util.Scanner;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Car a1 = new xiaoqiche();
Car a2 = new xiaoqiche();
Car a3 = new pica();
Car a4 = new xiaoqiche();
Car a5 = new bus();
Car a6 = new bus();
a1.xuhao = 1;
a1.name = "奥迪A4";
a1.price = 500;
a1.zairen = 4;
a2.xuhao = 2;
a2.name = "马自达6";
a2.price = 400;
a2.zairen = 4;
a3.xuhao = 3;
a3.name = "皮卡雪6";
a3.price = 450;
a3.zairen = 4;
a3.zaihuo = 2;
a4.xuhao = 4;
a4.name = "金龙";
a4.price = 800;
a4.zairen = 20;
a5.xuhao = 5;
a5.name = "松花江";
a5.price = 400;
a5.zaihuo = 4;
a6.xuhao = 6;
a6.name = "依维柯";
a6.price = 1000;
a6.zaihuo = 20;
System.out.println("欢迎使用答答租车系统:");
System.out.println("您是否要租车:1 是 0否");
Scanner scanner = new Scanner(System.in);
int flag = scanner.nextInt();
if(flag == 1){
System.out.println("您可租车的类型及价目表:");
}else {
System.out.println("欢迎您再来!");
}
if(flag == 1){
System.out.println("序号"+"\t"+"汽车名称"+"\t"+"租金"+"\t"+"容量"+"\t");
a1.chengren();
a2.chengren();
a3.chengren();
a4.chengren();
a5.chenghuo();
a6.chenghuo();
System.out.println("请输入您要租汽车的数量:");
Scanner scanner2 = new Scanner(System.in);
int flag2 = scanner2.nextInt();
int[] a = new int[flag2];
for(int i=1;i<=flag2;i++){
System.out.println("请输入第"+i+"辆车的序号:");
a[i-1] = scanner2.nextInt();
}
System.out.println("请输入租车天数:");
int b = scanner2.nextInt();
System.out.println(b);
System.out.println("您的账单:");
int x = 0;
double y = 0;
double z = 0;
int zr = 0;
int zh = 0;
for(int i = 1;i<=flag2;i++){
switch(a[i-1]){
case 1:zr+=1;x+=a1.zairen;z+=a1.price;break;
case 2:zr+=1;x+=a2.zairen;z+=a2.price;break;
case 3:zr+=1;zh+=1;x+=a3.zairen;y+=a3.zaihuo;z+=a3.price;break;
case 4:zr+=1;x+=a4.zairen;z+=a4.price;break;
case 5:zh+=1;y+=a5.zaihuo;z+=a5.price;break;
case 6:zh+=1;y+=a6.zaihuo;z+=a6.price;break;
}
}
String[] ZR = new String[zr];
String[] ZH = new String[zh];
int q = 0;
int p = 0;
for(int i =1;i<=flag2;i++){
switch(a[i-1]){
case 1:q++;ZR[q-1]=a1.name; break;
case 2:q++;ZR[q-1]=a2.name;break;
case 3:q++;ZR[q-1]=a3.name;p++;ZH[p-1]=a3.name;break;
case 4:q++;ZR[q-1]=a4.name;break;
case 5:p++;ZH[p-1]=a5.name;break;
case 6:p++;ZH[p-1]=a6.name;break;
default:break;
}
}
System.out.println("***可载人的车有:");
for(int i=1;i<=zr;i++){
System.out.print(ZR[i-1]+"\t");
}
System.out.println(" 共载人:"+x+"人");
System.out.println("***可载货的车有:");
for(int i=1;i<=zh;i++){
System.out.print(ZH[i-1]+"\t");
}
System.out.println(" 共载货:"+y+"吨");
System.out.println("***租车总价格:"+z*b+"元");
}
}
}
输入结果:
欢迎使用答答租车系统:
您是否要租车:1 是 0否
1
您可租车的类型及价目表:
序号 汽车名称 租金 容量
- 奥迪A4 500元/天 4人
- 马自达6 400元/天 4人
- 皮卡雪6 450元/天 4人2吨
- 金龙 800元/天 20人
- 松花江 400元/天 4吨
- 依维柯 1000元/天 20吨
请输入您要租汽车的数量:
4
请输入第1辆车的序号:
1
请输入第2辆车的序号:
2
请输入第3辆车的序号:
3
请输入第4辆车的序号:
4
请输入租车天数:
3
您的账单:
可载人的车有:
奥迪A4 马自达6 皮卡雪6 金龙 共载人:32人
可载货的车有:
皮卡雪6 共载货:2.0吨
***租车总价格:6450.0元