- 定义2个接口
载货接口
package com.xiaomo;
//定义载货的接口
public interface Izaihuo {
public int getZai();
public void setZai(int zaishu);
}
载客接口
package com.xiaomo;
//载客接口
public interface Izairen {
public int getZairen();
public void setZairen(int zairen);
}
2.定义汽车抽象类,并继承接口
package com.xiaomo;
//定义汽车抽象类属性和方法,并继承接口
public abstract class Care implements Izairen,Izaihuo{
/ 定义汽车类别(父类)的序号、名字和价格; */
public int intNum;
public String careName;
public int careZujin;
//封装属性
public int getIntNum() {
return intNum;
}
public void setIntNum(int intNum) {
this.intNum = intNum;
}
public String getCareName() {
return careName;
}
public void setCareName(String careName) {
this.careName = careName;
}
public int getCareZujin() {
return careZujin;
}
public void setCareZujin(int careZujin) {
this.careZujin = careZujin;
}
//定义抽象打印类
public abstract void printList();
}
3 . 定义客车、货车、皮卡具体实现类
客车实现类
package com.xiaomo;
public class KeCare extends Care {
//构造卡车汽车
private int zairen=0;
public KeCare(int intNum,String careName,int careZujin,int zaiRen) {
this.setIntNum(intNum);
this.setCareName(careName);
this.setCareZujin(careZujin);
this.setZairen(zaiRen);
}
@Override
public int getZairen() {
// TODO Auto-generated method stub
return zairen;
}
@Override
public void setZairen(int zairen) {
// TODO Auto-generated method stub
this.zairen=zairen;
}
@Override
public void printList() {
// TODO Auto-generated method stub
System.out.println( getIntNum()+".\t"+getCareName()+"\t"+getCareZujin()+"元/天"+"\t"+
"载人:"+getZairen()+"人");
}
@Override
public int getZai() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void setZai(int zaishu) {
// TODO Auto-generated method stub
}
}
货车实现类
package com.xiaomo;
public class HuoCare extends Care{
//构造货车汽车
private int zaishu=0;
public HuoCare(int intNum,String careName,int careZujin,int zaiHuo) {
this.setIntNum(intNum);
this.setCareName(careName);
this.setCareZujin(careZujin);
this.setZai(zaiHuo);
}
@Override
public int getZai() {
// TODO Auto-generated method stub
return zaishu;
}
@Override
public void setZai(int zaishu) {
// TODO Auto-generated method stub
this.zaishu=zaishu;
}
@Override
public void printList() {
// TODO Auto-generated method stub
System.out.println( getIntNum()+".\t"+getCareName()+"\t"+getCareZujin()+"元/天"+"\t"+"载货:"+getZai()+"吨");
}
@Override
public int getZairen() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void setZairen(int zairen) {
// TODO Auto-generated method stub
}
}
皮卡实现类
package com.xiaomo;
public class PiCare extends Care{
//构造皮卡汽车
int zairen=0;
int zaishu=0;
public PiCare(int intNum,String careName,int careZujin,int zaiRen,int zaiHuo) {
this.setIntNum(intNum);
this.setCareName(careName);
this.setCareZujin(careZujin);
this.setZai(zaiHuo);
this.setZairen(zaiRen);
}
@Override
public int getZairen() {
// TODO Auto-generated method stub
return zairen;
}
@Override
public void setZairen(int zairen) {
// TODO Auto-generated method stub
this.zairen=zairen;
}
@Override
public int getZai() {
// TODO Auto-generated method stub
return zaishu;
}
@Override
public void setZai(int zaishu) {
// TODO Auto-generated method stub
this.zaishu=zaishu;
}
//实现care抽象方法
public void printList() {
// TODO Auto-generated method stub
System.out.println( getIntNum()+".\t"+getCareName()+"\t"+getCareZujin()+"元/天"+"\t"+
"载人:"+getZairen()+"人"+"载货:"+getZai()+"吨");
}
}
4..测试类
package com.xiaomo;
import java.util.Scanner;
public class ceshi {
private static Scanner inpit;
public static void main(String[] args) {
// TODO Auto-generated method stub
//实例化汽车类型
Care[] cares={
new KeCare(1,"奥迪A4",500,4),
new KeCare(2,"马自达6",400,4),
new PiCare(3,"皮卡雪6",450,4,2),
new KeCare(4,"金龙",800,20),
new HuoCare(5,"松花江",400,4),
new HuoCare(6,"依维柯",1000,20),
};
inpit = new Scanner(System.in);
System.out.println("******欢迎使用答答租车系统!!*******");
System.out.println("\n您是否要租车:1是 0否");
int num=inpit.nextInt();
if(num==1){
System.out.println("您可租车的类型及其价目表:\n序号"+"\t"+"汽车名称"+"\t"+"租金"+"\t"+"容量");
for(int i = 0;i<cares.length;i++){
cares[i].printList();
}
}else{
System.out.println("感谢你使用达达租车系统!!");
}
//获取需要租入的车辆数量、序号以及天数;
System.out.println("请输入您要租的汽车数量");
num=inpit.nextInt();
int[] selectNum=new int[num];
for(int i=0;i<num;i++){
int j=i+1;
System.out.println("请输入第"+j+"辆车的序号:");
selectNum[i]=inpit.nextInt();
}
System.out.println("请输入你租车时间:");
int day=inpit.nextInt();
/*
* 输出可载人车辆的清单及可载人数
* 作者: Candy07
链接:http://www.imooc.com/article/13438
来源:慕课网;
*/
System.out.println("您的账单:\n***可载人的车有:");
int pessengerSum=0;
for(int i=0;i<num;i++){
int j=selectNum[i]-1;
int pessengerNum=cares[j].getZairen();
if(pessengerNum>0){
System.out.print(cares[j].getCareName()+"\t");
}
pessengerSum+=pessengerNum;
}
if(pessengerSum>0){
System.out.println("共载人:"+pessengerSum+"人");
}else{
System.out.println("抱歉!您选中的车辆中没有可载人的车辆!");
}
/*
* 输出可载货车辆的清单及可载吨数
*/
System.out.println("***可载货的车有:");
double cargoSum=0;
for(int i=0;i<num;i++){
int j=selectNum[i]-1;
int cargoNum=cares[j].getZai();
if(cargoNum>0){
System.out.print(cares[j].getCareName()+"\t");
}
cargoSum+=cargoNum;
}
if(cargoSum>0){
System.out.println("共载货:"+cargoSum+"吨");
}else{
System.out.println("抱歉!您选中的车辆中没有可载货的车辆!");
}
/*
* 输出总价款
*/
double priceSum=0;
for(int i=0;i<num;i++){
int j=selectNum[i]-1;
int priceNum=cares[j].getCareZujin();
priceSum=priceSum+priceNum*day;
}
System.out.println("***租车总价格:"+priceSum+"元");
}
}