主类
package dadazuche;
import java.util.Scanner;
public class Initial {
/**
* @param args
*/
public static void main(String[] args) {
Initial initial = new Initial();
// TODO Auto-generated method stub
System.out.println("欢迎使用达达租车系统:");
System.out.println("您是否要租车:1是0否");
//选择是否租车
int choose = initial.choose();
if(choose==1){
//打印价格类目表
initial.carPrint();
//选择车子数量及序号
initial.chooseCar();
}
}
private void chooseCar() {
// TODO Auto-generated method stub
System.out.println("需要租用几辆车?");
Scanner input = new Scanner(System.in);
int n = input.nextInt();
//总租金变量
float sumPrice =0;
for(int i=0;i<n;i++){
System.out.println("第"+(i+1)+"辆车选择:");
//输入车辆型号
int flag= input.nextInt();
//打印车辆信息
sumPrice += this.choosePrint(flag);
};
System.out.println("请输入租车天数:");
int m = input.nextInt();
//打印总租金
System.out.println("总租金为:"+(sumPrice*m));
}
private float choosePrint(int flag) {
// TODO Auto-generated method stub
//初始化车实例
Che[] che = {
new KeChe("奥迪A4",600,4),
new KeChe("马自达6",500,3),
new PiKa("皮卡雪6",450,4,2),
new KeChe("金龙",800,20),
new HuoChe("松花江",400,4),
new HuoChe("依维柯",1000,20)
};
//计费变量
float sumPrice = 0;
//打印选车信息
if(che[flag-1] instanceof KeChe){
System.out.println(che[flag-1].getBrand()+" "+che[flag-1].getPrice()+" "+che[flag-1].getZaikeliang());
sumPrice +=che[flag-1].getPrice();
}else if(che[flag-1] instanceof HuoChe){
System.out.println(che[flag-1].getBrand()+" "+che[flag-1].getPrice()+" "+che[flag-1].getZaihuoliang());
sumPrice +=che[flag-1].getPrice();
}else{
System.out.println(che[flag-1].getBrand()+" "+che[flag-1].getPrice()+" "+che[flag-1].getZaikeliang()+" "+che[flag-1].getZaihuoliang());
sumPrice +=che[flag-1].getPrice();
}
return sumPrice;
}
private void carPrint() {
// TODO Auto-generated method stub
System.out.println("您可租车的类型及其价目表:" +"\n"
+ "序号 汽车名称 租金 容量");
System.out.println(
"1. 奥迪A4 600元/天 载人:4人"+"\n"
+ "2. 马自达6 500元/天 载人:4人"+"\n"
+ "3. 皮卡雪6 450元/天 载人:4 载货:2吨"+"\n"
+ "4. 金龙 800元/天 载人:20人" +"\n"
+ "5. 松花江 400元/天 载货:4吨" +"\n"
+ "6. 依维柯 1000元/天 载货:20吨" +"\n");
}
private int choose() {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
int choose = input.nextInt();
int i;
if(choose==0){
System.out.println("欢迎再来!");
i = 0;
}else{
System.out.println("欢迎使用租车系统!");
i = 1;
}
return i;
}
}
车父类
package dadazuche;
public abstract class Che {
private int zaihuoliang;
private int zaikeliang;
private float price;
private String brand;
public int getZaihuoliang() {
return zaihuoliang;
}
public void setZaihuoliang(int zaihuoliang) {
this.zaihuoliang = zaihuoliang;
}
public int getZaikeliang() {
return zaikeliang;
}
public void setZaikeliang(int zaikeliang) {
this.zaikeliang = zaikeliang;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
}
货车子类
package dadazuche;
public class HuoChe extends Che{
public HuoChe(String name,float price,int zaihuo) {
this.setBrand(name);
this.setPrice(price);
this.setZaihuoliang(zaihuo);
}
@Override
public int getZaihuoliang() {
// TODO Auto-generated method stub
return super.getZaihuoliang();
}
@Override
public void setZaihuoliang(int zaihuoliang) {
// TODO Auto-generated method stub
super.setZaihuoliang(zaihuoliang);
}
@Override
public float getPrice() {
// TODO Auto-generated method stub
return super.getPrice();
}
@Override
public void setPrice(float price) {
// TODO Auto-generated method stub
super.setPrice(price);
}
@Override
public String getBrand() {
// TODO Auto-generated method stub
return super.getBrand();
}
@Override
public void setBrand(String brand) {
// TODO Auto-generated method stub
super.setBrand(brand);
}
}
客车子类
package dadazuche;
public class KeChe extends Che {
public KeChe(String name,float price,int zaike){
this.setBrand(name);
this.setPrice(price);
this.setZaikeliang(zaike);
}
@Override
public int getZaikeliang() {
// TODO Auto-generated method stub
return super.getZaikeliang();
}
@Override
public void setZaikeliang(int zaikeliang) {
// TODO Auto-generated method stub
super.setZaikeliang(zaikeliang);
}
@Override
public float getPrice() {
// TODO Auto-generated method stub
return super.getPrice();
}
@Override
public void setPrice(float price) {
// TODO Auto-generated method stub
super.setPrice(price);
}
@Override
public String getBrand() {
// TODO Auto-generated method stub
return super.getBrand();
}
@Override
public void setBrand(String brand) {
// TODO Auto-generated method stub
super.setBrand(brand);
}
}
皮卡车子类
package dadazuche;
public class PiKa extends Che {
public PiKa(String name,float price,int zairen,int zaihuo){
this.setBrand(name);
this.setPrice(price);
this.setZaikeliang(zairen);
this.setZaihuoliang(zaihuo);
}
@Override
public int getZaihuoliang() {
// TODO Auto-generated method stub
return super.getZaihuoliang();
}
@Override
public void setZaihuoliang(int zaihuoliang) {
// TODO Auto-generated method stub
super.setZaihuoliang(zaihuoliang);
}
@Override
public int getZaikeliang() {
// TODO Auto-generated method stub
return super.getZaikeliang();
}
@Override
public void setZaikeliang(int zaikeliang) {
// TODO Auto-generated method stub
super.setZaikeliang(zaikeliang);
}
@Override
public float getPrice() {
// TODO Auto-generated method stub
return super.getPrice();
}
@Override
public void setPrice(float price) {
// TODO Auto-generated method stub
super.setPrice(price);
}
@Override
public String getBrand() {
// TODO Auto-generated method stub
return super.getBrand();
}
@Override
public void setBrand(String brand) {
// TODO Auto-generated method stub
super.setBrand(brand);
}
}