很粗糙的成品,很多细节都没有处理,懒得弄了。正确操作的前提没什么问题,少了一些输入不合理信息时的报错提示。强行使用了接口,弄得有点晕哈哈。
父类抽象类:
package dada;
public abstract class Car {
private int id;
private String name;
private int price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
两个接口:
package dada;
public interface ICapacity {
void setCapacity(int cap);
int getCapacity();
}
package dada;
public interface ICargoVolume {
void setCargoVolume(int vol);
int getCargoVolume();
}
分别三个子类:
package dada;
public class PassengerCar extends Car implements ICapacity {
private int cap;
public PassengerCar(int id,String name,int price,int cap){
this.setId(id);
this.setName(name);
this.setPrice(price);
this.setCapacity(cap);
//客车类的属性
}
@Override
public void setCapacity(int cap) {
// TODO Auto-generated method stub
this.cap=cap;
}
@Override
public int getCapacity() {
// TODO Auto-generated method stub
return cap;
}
}
package dada;
public class Pickup extends Car implements ICapacity,ICargoVolume {
private int cap;
private int vol;
public Pickup(int id,String name,int price,int cap,int vol){
this.setId(id);
this.setName(name);
this.setPrice(price);
this.cap=cap;
this.vol=vol;
//皮卡类的属性
}
public int getCapacity() {
return cap;
}
public void setCapacity(int cap) {
this.cap = cap;
}
public int getCargoVolume() {
return vol;
}
public void setCargoVolume(int vol) {
this.vol = vol;
}
}
package dada;
public class Truck extends Car implements ICargoVolume {
private int vol;//载货量
public Truck(int id,String name,int price,int vol){
this.setId(id);
this.setName(name);
this.setPrice(price);
this.vol=vol;
}
public int getCargoVolume() {
return vol;
}
public void setCargoVolume(int vol) {
this.vol = vol;
}
}
运行:
package dada;
import java.util.Scanner;
public class Test {
public static final Car[] cars={
new PassengerCar(1,"奥迪A4",500,4),
new PassengerCar(2,"马自达6",400,4),
new Pickup(3,"皮卡雪6",450,4,2),
new PassengerCar(4,"金龙",800,20),
new Truck(5,"松花江",400,4),
new Truck(6,"依维柯",1000,20)
};
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("欢迎使用dada租车系统!"+"\n"+"您是否要租车:1.是"+"\t"+"0.退出");
Scanner s = new Scanner(System.in);
int input = s.nextInt();
if(input ==1){
System.out.println("可供租用的车辆及其价目表:");
System.out.println("序号"+"\t"+"车型"+"\t"+"租金"+"\t"+"容量");
for(Car car:cars){
if(car.getClass().equals(PassengerCar.class)){
//判断该车是否是客车的实例
//使用getCapacity的时候需要将car强制转换成PassengerCar类型!
System.out.println(car.getId()+"\t"+car.getName()+"\t"+car.getPrice()+"/天"+"\t"+((PassengerCar)car).getCapacity()+"人");
}else if(car.getClass().equals(Truck.class)){
System.out.println(car.getId()+"\t"+car.getName()+"\t"+car.getPrice()+"/天"+"\t"+((Truck)car).getCargoVolume()+"吨");
}else{
System.out.println(car.getId()+"\t"+car.getName()+"\t"+car.getPrice()+"/天"+"\t"+((Pickup)car).getCapacity()+"人"+((Pickup)car).getCargoVolume()+"吨");
}
}
}else{
System.out.println("感谢使用dada租车系统!");
}
System.out.println("请输入您要租车的数量:");
int count = s.nextInt();
Car rent[] = new Car[count];
for(int i=0; i<count;i++){
System.out.println("请输入第"+(i+1)+"辆车的序号");
int num = s.nextInt();
rent[i]=cars[num-1];
}
System.out.println("请输入租车的天数:");
int days = s.nextInt();
int sum = 0;
for(Car car: rent){
System.out.println(car.getName());
}
System.out.println("以下是您本次的租车信息及账单:");
System.out.println("您本次共租用"+count+"辆车:");
for(int i= 0;i<rent.length;i++){
if(rent[i]!=null){
System.out.println(rent[i].getName());
}else{
}
sum += rent[i].getPrice()*days;
}
System.out.println("您本次需支付:"+sum+"元");
s.close();
}
}
运行结果:
点击查看更多内容
8人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦