Pickup类、Truck类、Manned类均继承Car类;
Car类
属性:name(车的类型),price(每天的租金),cargo(载货量),manned(载客量)
方法:
1)初始化Car类中各个属性的值,包括:setName(); setPrice(); setCargo(); setManned().
2)获得Car类中各个属性的值,包括:getName(); getPrice(); getCargo; getManned()
3)待实现的方法——输出所选车型的相关信息。abstract getlist()方法
抽象类——Car类
public abstract class Car {
private String name;//车类型
private int price;//每天的租金
private int manned;//车的载人量
private int cargo;//车的载货量
//此抽象方法留给子类继承实现
public abstract void getlist();
//获得Car类封装的属性
public String getName(){
return name;
}
public int getPrice(){
return price;
}
public int getManned(){
return manned;
}
public int getCargo(){
return cargo;
}
//初始化Car类封装的属性
public void setName(String name){
this.name=name;
}
public void setPrice(int price){
this.price=price;
}
public void setManned(int manned){
this.manned=manned;
}
public void setCargo(int cargo){
this.cargo=cargo;
}
}
皮卡车:Pickup类继承Car类
public class Pickup extends Car{
public Pickup(String carType,int carCapacity,int carTourists,int day, int price){
this.setName(carType);
this.setManned(carTourists);
this.setCargo(carCapacity);
this.setPrice(price);
}
@Override
public void getlist() {
System.out.println(getName()+"\t"+getPrice()+"元/天"+"\t"+"载货"+getCargo()+"T"+"\t"+"载人"+getManned()+"人");
}
}
卡车:Truck类继承Car类
public class Truck extends Car{
public Truck(String carType,int carCapacity,int day, int price){
this.setName(carType);
this.setCargo(carCapacity);
this.setPrice(price);
}
@Override
public void getlist() {
System.out.println(getName()+"\t"+getPrice()+"元/天"+"\t"+"载货"+getCargo()+"T");
}
}
客车:Manned类继承Car类
public class Manned extends Car{
public Manned(String carType,int carTourists,int day, int price){
this.setName(carType);
this.setManned(carTourists);
this.setPrice(price);
}
@Override
public void getlist() {
System.out.println(getName()+"\t"+getPrice()+"元/天"+"\t"+"载人"+getManned()+"人");
}
}
入口类
import java.util.Scanner;
public class CarRental {
public static void main(String[] args) {
Car[] cars={
new Truck("松花江 ",4,3,400),
new Truck("依维柯 ",20,3,1000),
new Manned("马自达6",10,3,400),
new Manned("奥迪A4",5,3,400),
new Manned("金龙 ",9,3,400),
new Pickup("皮卡雪6 ",2,4,3,400),
};
System.out.println("欢迎使用答答租车系统\n您是否要租车:1是 0否");
Scanner input=new Scanner(System.in);
if(input.nextInt()==1){
//显示车列表
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号"+"\t"+"汽车名称"+"\t"+"租金"+"\t"+"容量");
for(int i=0;i
System.out.print(i+"\t");
cars[i].getlist();
}
}
System.out.println("请选择车的数量");
int val=input.nextInt();//输入租车数量
Car[] cars1=new Car[val];
int[] days=new int[val];
for(int i=0;i
System.out.println("请选择第"+(i+1)+"辆车的序号:");
int inputValue=input.nextInt();
System.out.println(inputValue);
cars1[i]=cars[inputValue];//租车的类型
System.out.println(cars1[i].getName());
System.out.println("请选择借出天数");
days[i]=input.nextInt();
}
System.out.println("清单:");
int[] totalPrice=new int[val];
int sum=0;
for(int i=0;i
totalPrice[i]=days[i]*cars[i].getPrice();
System.out.println(i+"\t"+cars1[i].getName()+"\t"+"借出天数:"+days[i]+"\t"+"总计:"+totalPrice[i]);
}
for(int i=0;i
sum+=totalPrice[i];
System.out.println("总价格:"+sum);
}
}
打开App,阅读手记