创建一个抽象的汽车基类,有名称、租金、租用天数3个属性,一个抽象方法获取汽车容量,需要子类实现,一个实现的方法得到总租金。
package com.car;
/**
* 汽车基类
*/
public abstract class Car {
/*
* 汽车名称
*/
private String name;
/*
* 租金
*/
private Float rent;
/*
* 租车天数
*/
private int days;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Float getRent() {
return rent;
}
public void setRent(Float rent) {
this.rent = rent;
}
public int getDays() {
return days;
}
public void setDays(int days) {
this.days = days;
}
/**
* 获取汽车容量
* @return 容量信息
*/
public abstract String getCapacity();
/**
* 获取总租金
* @return 租金*租车天数
*/
public Float getSumPrice() {
return this.rent * this.days;
}
}
客车类继承汽车基类,增加属性载客数
package com.car;
/**
* 客车
*/
public class Coach extends Car {
/*
* 载客数
*/
private int number;
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
@Override
public String getCapacity() {
return "载客:" + this.number + "人";
}
/**
* 实例化客车对象
* @param name 车子名称
* @param rent 租金
* @param number 载客数
*/
public Coach(String name, Float rent, int number) {
super();
this.setName(name);
this.setRent(rent);
this.setNumber(number);
}
}
货车类继承汽车基类,增加属性载货量
package com.car;
/**
* 货车
*/
public class Van extends Car {
/*
* 载货量,单位:吨
*/
private Float number;
public Float getNumber() {
return number;
}
public void setNumber(Float number) {
this.number = number;
}
@Override
public String getCapacity() {
return "载货:" + this.number + "吨";
}
/**
* 实例化货车对象
* @param name 车子名称
* @param rent 租金
* @param number 载货量,单位:吨
*/
public Van(String name, Float rent, Float number) {
super();
this.setName(name);
this.setRent(rent);
this.setNumber(number);
}
}
皮卡类继承汽车基类,增加属性载客数和载货量
package com.car;
/**
* 皮卡
*/
public class Pickup extends Car {
/*
* 载客数
*/
private int personNumber;
/*
* 载货量,单位:吨
*/
private Float goodsNumber;
public int getPersonNumber() {
return personNumber;
}
public void setPersonNumber(int personNumber) {
this.personNumber = personNumber;
}
public Float getGoodsNumber() {
return goodsNumber;
}
public void setGoodsNumber(Float goodsNumber) {
this.goodsNumber = goodsNumber;
}
@Override
public String getCapacity() {
return "载客:" + this.personNumber + "人,载货:" + this.goodsNumber + "吨";
}
/**
* 实例化皮卡车对象
* @param name 车子名称
* @param rent 租金
* @param personNumber 载客数
* @param goodsNumber 载货量,单位:吨
*/
public Pickup(String name, Float rent, int personNumber, Float goodsNumber) {
super();
this.setName(name);
this.setRent(rent);
this.setPersonNumber(personNumber);
this.setGoodsNumber(goodsNumber);
}
}
主类,完成显示和操作
package com.car;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Rental {
private List<Car> allCar;
private List<Car> rentedCar;
public static void main(String[] args) {
System.out.println("******欢迎进入呵呵哒租车系统******");
Rental _this = new Rental();
_this.initCars();
_this.displayAllCar();
_this.rentCar();
}
/**
* 初始化所有车子信息
*/
private void initCars() {
allCar = new ArrayList<Car>();
allCar.add(new Coach("昂克赛拉", 400f, 5));
allCar.add(new Coach("金龙客车", 2000f, 30));
allCar.add(new Van("解放轻卡", 1000f, 5f));
allCar.add(new Van("中国重汽", 1500f, 15f));
allCar.add(new Pickup("福特克罗", 800f, 5, 0.5f));
allCar.add(new Pickup("丰田皮卡", 600f, 2, 1f));
}
/**
* 显示车辆信息
*/
private void displayAllCar() {
System.out.println("车型及价目表:");
System.out.println("序号 汽车名称 租金 容量");
for(int i=0; i < allCar.size(); i++) {
Car car = allCar.get(i);
System.out.println(getStringWithSpace(i + 1 + "", 4)
+ car.getName() + " "
+ getStringWithSpace(car.getRent() + "元/天", 12)
+ getStringWithSpace(car.getCapacity(), 0));
}
System.out.println("0 退出系统");
System.out.println("");
}
/**
* 选择汽车租车
*/
private void rentCar() {
System.out.print("请输入您要租的汽车的序号:");
Integer num = getInputInt(0, allCar.size());
if (num == 0) {
System.exit(0);
}
Car car = allCar.get(num - 1);
System.out.println("请输入您要租的" + car.getName() + "的天数,最多可租10天,输入0则不租该车:");
Integer days = getInputInt(0, 10);
if (days == 0) {
System.out.println("您取消了" + car.getName() + "的租用!");
tip();
}
else {
car.setDays(days);
if(rentedCar == null) rentedCar = new ArrayList<Car>();
rentedCar.add(car);
System.out.println("租用成功:" + car.getName() + " * " + days + "天!");
tip();
}
}
/**
* 显示已租车列表
*/
private void displayRentedCar() {
if (rentedCar == null || rentedCar.size() == 0) {
System.out.println("您还没有租用任何汽车,请去选择租用车辆!");
System.out.println("");
displayAllCar();
rentCar();
}
else {
Float sumPrice = 0f;
System.out.println("您已租用的汽车:");
System.out.println("序号 汽车名称 租金 天数 容量");
for(int i=0; i < rentedCar.size(); i++) {
Car car = rentedCar.get(i);
sumPrice += car.getSumPrice();
System.out.println(getStringWithSpace(i + 1 + "", 4)
+ car.getName() + " "
+ getStringWithSpace(car.getRent() + "元/天", 12)
+ getStringWithSpace(car.getDays() + "", 4)
+ getStringWithSpace(car.getCapacity(), 0)
);
}
System.out.println(" 合计:" + sumPrice + "元");
tip();
}
}
/**
* 操作提示
*/
private void tip() {
System.out.println("");
System.out.println("您可以做如下操作:");
System.out.println("1. 查看租车列表并租车");
System.out.println("2. 查看已租车辆");
System.out.println("0. 退出系统");
int result = getInputInt(0, 2);
if(result == 1) {
displayAllCar();
rentCar();
}
else if (result == 2){
displayRentedCar();
}
else {
System.exit(0);
}
}
/**
* 获取输入的整数
* @param min 不能超过的最小数值,null则是不限制
* @param max 不能超过的最大数值,null则是不限制
* @return 输入的整数
*/
private Integer getInputInt(Integer min, Integer max) {
Scanner scanner = new Scanner(System.in);
try {
Integer result = scanner.nextInt();
if ((min == null || result >= min) && (max == null || result <= max)) {
return result;
}
}
catch (Exception ex) { }
System.out.print("您的输入有误,请重新输入:");
return getInputInt(min, max);
}
/**
* 字符串补空格
* @param str 目标字符串
* @param length 需要占用长度
* @return 字符串补空格后的值
*/
private String getStringWithSpace(String str, int length) {
if (str == null) {
str = "";
}
if (length <= 0) {
return str;
}
int count = 0;
try {
count = str.getBytes("gb2312").length;
} catch (UnsupportedEncodingException ex) {
return str;
}
if(count >= length) {
return str;
}
StringBuilder strb = new StringBuilder();
for(;count < length; count++) {
strb.append(" ");
}
return str + strb.toString();
}
}
热门评论
抱歉,是我搞错了
额,老铁,抽象类只有方法的声明,不应该有方法的实现