做了个差不多。。。 账单那简化了一点 不 其实都简化了点。。。
入口:
package com.mgh; import java.util.Scanner; public class Initial { public static void main(String[] args) { // TODO Auto-generated method stub Vehicle vcl[]=new Vehicle[6]; vcl[0]=new Car("奥迪A4",1,500,4); vcl[1]=new Car("马自达6",2,400,4); vcl[2]=new Pickup("皮卡学6",3,450,4,2); vcl[3]=new Car("金龙",4,800,20); vcl[4]=new Truck("松花江",5,400,4); vcl[5]=new Truck("依维柯",6,1000,20); System.out.println("欢迎使用答答租车系统:"); System.out.println("您是否要租车: 1.是 0.否"); Scanner scan=new Scanner(System.in); String isRent=scan.next(); if(isRent.equals("1")){ System.out.println("您可租车的类型及其价目表:"); for(int i=0;i<5;i++){ vcl[i].infoShow(); } System.out.println("请输入您要租汽车的数量:"); int rentNum=scan.nextInt(); StringBuffer selectVclList=new StringBuffer(); int costPerDay=0; int selectVclID[]=new int[rentNum]; for(int i=0;i<rentNum;i++){ System.out.println("请输入第"+(i+1)+"辆车的序号:"); selectVclID[i]=scan.nextInt()-1; selectVclList.append(vcl[selectVclID[i]].name+" "); costPerDay+=vcl[selectVclID[i]].rentPrice; } System.out.println("请输入租车天数:"); int rentDays=scan.nextInt(); int allCost=costPerDay*rentDays; String bill=new String(); System.out.println("您的账单:"); bill=selectVclList.toString()+" "+ rentDays +"天 总价"+allCost+"元"; System.out.println(bill); scan.close(); } else{ System.out.println("感谢使用答答租车系统,欢迎再来!"); } } }
父类:
package com.mgh; public abstract class Vehicle { String name=new String(); int rentPrice; int vehicleNum; public abstract void infoShow(); }
Car子类,客车也在其中了 属性一样没必要另开一类:
package com.mgh; public class Car extends Vehicle { int menLoad; public Car(String vName,int vNum,int rPrice,int vmLoad){ this.name=vName; this.vehicleNum=vNum; this.rentPrice=rPrice; this.menLoad=vmLoad; } @Override public void infoShow(){ // TODO Auto-generated method stub System.out.println(this.vehicleNum+". "+this.name+" "+this.rentPrice+"元/天 载客:"+this.menLoad+"人"); } }
Truck子类:
package com.mgh; public class Truck extends Vehicle { private int cargoLoad; public Truck(String vName,int vNum,int rPrice,int vcLoad){ this.name=vName; this.vehicleNum=vNum; this.rentPrice=rPrice; this.cargoLoad=vcLoad; } @Override public void infoShow() { // TODO Auto-generated method stub System.out.println(this.vehicleNum+". "+this.name+" "+this.rentPrice+"元/天 载重:"+this.cargoLoad+"吨"); } }
Pickup子类 皮卡:
package com.mgh; public class Pickup extends Vehicle { private int menLoad; private int cargoLoad; public Pickup(String vName,int vNum,int rPrice,int vmLoad,int vcLoad){ this.name=vName; this.vehicleNum=vNum; this.rentPrice=rPrice; this.cargoLoad=vcLoad; this.menLoad=vmLoad; } @Override public void infoShow() { // TODO Auto-generated method stub System.out.println(this.vehicleNum+". "+this.name+" "+this.rentPrice+"元/天 载客:"+this.menLoad+"人,载重:"+this.cargoLoad+"吨"); } }