首先创建一个父类 Car
//Car 类
public abstract class Car {
private int price;//车的租金
private String name;//车的名称
private int passengerLoad; //载客人数
private double goodsLoad;//载货数量
public int getPrice() {
return price;
}
public void setPrice(int rent) {
this.price = rent;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPassengerLoad() {
return passengerLoad;
}
public void setPassengerLoad(int passengerLoad) {
this.passengerLoad = passengerLoad;
}
public double getGoodsLoad() {
return goodsLoad;
}
public void setGoodsLoad(double goodsLoad) {
this.goodsLoad = goodsLoad;
}
}
然后创建 三种子类(只载人 只载货 既载人又载货)
//只载人的 KeCar类
public class KeCar extends Car {
public KeCar(String name ,int price,int passengerLoad){
this.setPrice(price);
this.setName(name);
this.setPassengerLoad(passengerLoad);
}
public String toString() {
return this.getName()+"----"+this.getPrice()+"元/天---- 载客"+this.getPassengerLoad()+"人"; }
}
//只载客的 Truck类
public class Truck extends Car {
public Truck( String name,int price ,int goodsLoad){
this.setPrice(price);
this.setName(name);
this.setGoodsLoad(goodsLoad);
}
public String toString() {
return this.getName()+"----"+this.getPrice()+"元/天---- 载货"+this.getGoodsLoad()+"吨"; }
}
//既载客又载人的 BothCarry类
public class BothCarry extends Car {
public BothCarry(String name,int price,int passengerLoad,double goodsLoad){
this.setName(name);
this.setPrice(price);
this.setPassengerLoad(passengerLoad);
this.setGoodsLoad(goodsLoad);
}
public String toString() {
return this.getName()+"----"+this.getPrice()+"元/天---- 载客"+this.getPassengerLoad()+"人/载货"+this.getGoodsLoad()+"吨"; }
}
最后就是测试类 Test
import java.util.Arrays;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("***************欢迎使用滴滴租车系统***************");
System.out.println();
System.out.println("*******租车请按 (1) 退出请按(0)*******");
int num=sc.nextInt();
Car[] cars={new KeCar("奥迪A4",300,4),
new KeCar("雪佛兰5",250,4),
new KeCar("宝骏巴士",800,45),
new Truck("东风Rl",1000,10),
new Truck("依维柯S",1500,25),
new BothCarry("皮卡雪Z",600,2,5)};
if(num!=1){
System.out.println("****欢迎下次光临****");
System.exit(0);
}else{
for(int i=1;i<=cars.length;i++){
System.out.println("序号"+i+"——————————-"+cars[i-1]);
}
}
System.out.println("请输入你要预定的数量");
int a=sc.nextInt();
int priceSum=0;//价格总量
double goodsLoadSum=0;//载货总量
int passengerLoadSum=0;//载客总量
for(int i=1;i<=a;i++){
System.out.println("请输入第"+i+"辆要预定车辆的序号");
int on=sc.nextInt();
passengerLoadSum=passengerLoadSum+cars[i-1].getPassengerLoad();
goodsLoadSum=goodsLoadSum+cars[i-1].getGoodsLoad();
priceSum=priceSum+cars[i-1].getPrice();
}
System.out.println("一共需要"+priceSum+"元/天"+" "+"共载货量:"+goodsLoadSum+"吨 "+"共载客量:"+ passengerLoadSum+"人");
System.out.println("************************谢谢惠顾!");
}
}
热门评论
int on=sc.nextInt(); on在哪使用了?
我感觉 这个abstract关键字 没必要用啊
为什么我写的是错的呢?总量是不对的,无语了