慕粉3848213
2016-08-29 13:38:47浏览 3799
定义 2 个接口
public interface IMannedCar {
int mannedNumber();
}
public interface ICarryFreight {
int carryFreightNumber();
}
定义抽线父类 Car.java,并定义两个抽象方法 name(), rental().
public abstract class Car {
public abstract String name();
public abstract int rental();
}
定义三个抽象类,继承 Car.java,并根据不同的需求实现不同的接口
public abstract class PersonCar extends Car implements IMannedCar {
}
public abstract class PersonCar extends Car implements IMannedCar {
}
public abstract class PickupTruck extends Car implements IMannedCar,ICarryFreight {
}
具体不同的汽车,实现不同的类
public class Audi extends PersonCar {
@Override
public int mannedNumber() {
return 4;
}
@Override
public String name() {
return "奥迪 A4";
}
@Override
public int rental() {
return 500;
}
@Override
public String toString() {
return name() + " " + rental() + "元/天 载人:" + mannedNumber();
}
}
- Mazada6.java,马自达6,载人汽车实现类
public class Mazada6 extends PersonCar {
@Override
public int mannedNumber() {
return 4;
}
@Override
public String name() {
return "马自达6";
}
@Override
public int rental() {
return 400;
}
@Override
public String toString() {
return name() + " " + rental() + "元/天 载人:" + mannedNumber();
}
}
public class Xue6 extends PickupTruck {
@Override
public int mannedNumber() {
return 4;
}
@Override
public int carryFreightNumber() {
return 2;
}
@Override
public String name() {
return "皮卡雪6";
}
@Override
public int rental() {
return 450;
}
@Override
public String toString() {
return name() + " " + rental() + "元/天 载人:" + mannedNumber() + " 载货:" + carryFreightNumber();
}
}
public class YiWeiKe extends CarryCar {
@Override
public int carryFreightNumber() {
return 20;
}
@Override
public String name() {
return "依维柯";
}
@Override
public int rental() {
return 1000;
}
@Override
public String toString() {
return name() + " " + rental() + "元/天 载货:" + carryFreightNumber();
}
}
- SongHuaJiang.java,松花江,载货汽车实现类
public class SongHuaJiang extends CarryCar {
@Override
public int carryFreightNumber() {
return 4;
}
@Override
public String name() {
return "松花江";
}
@Override
public int rental() {
return 400;
}
@Override
public String toString() {
return name() + " " + rental() + "元/天 载货:" + carryFreightNumber();
}
}
public class JinLong extends PersonCar {
@Override
public int mannedNumber() {
return 20;
}
@Override
public String name() {
return "金龙";
}
@Override
public int rental() {
return 800;
}
@Override
public String toString() {
return name() + " " + rental() + "元/天 载人:" + mannedNumber();
}
}
public class PiCar extends PickupTruck {
@Override
public int mannedNumber() {
return 4;
}
@Override
public int carryFreightNumber() {
return 5;
}
@Override
public String name() {
return "皮卡";
}
@Override
public int rental() {
return 600;
}
@Override
public String toString() {
return name() + " " + rental() + "元/天 载人:" + mannedNumber()+" 载货:" + carryFreightNumber();
}
}
租车系统 RentCar
public class RentCar {
private List<Car> cars;
private List<Car> rentCars;
public void rent() {
rentCars = new ArrayList<>();
System.out.println("您可租车的类型及其价目表:");
showRentCarList();
}
private void showRentCarList() {
cars = new ArrayList<>();
Car audi = new Audi();
cars.add(audi);
Car yiweike = new YiWeiKe();
Car piCar = new PiCar();
Car songhuajiang = new SongHuaJiang();
Car mazada6 = new Mazada6();
Car xue6 = new Xue6();
Car jinlong = new JinLong();
cars.add(piCar);
cars.add(yiweike);
cars.add(songhuajiang);
cars.add(mazada6);
cars.add(xue6);
cars.add(jinlong);
if (cars != null && cars.size() > 0) {
System.out.println("序号 汽车名称 租金 容量");
for (int i = 0; i < cars.size(); i++) {
System.out.println(i + 1 + " " + cars.get(i).toString());
}
int carsNumber = 0;
boolean isCardNumber = false;
System.out.println("请输入您要租赁车辆的数量:");
while (!isCardNumber) {
try {
carsNumber = inNumber();
if (carsNumber <= 0) {
isCardNumber = false;
continue;
}
isCardNumber = true;
} catch (ParseErrorException e) {
isCardNumber = false;
System.out.println("请输入您要租赁车辆的数量:");
}
}
for (int j = 0; j < carsNumber; j++) {
System.out.println("请输入第" + (j + 1) + "辆车的序号:");
boolean isSerial = false;
while (!isSerial) {
int serial = 0;
try {
serial = inNumber();
} catch (ParseErrorException e) {
isSerial = false;
System.out.println("请正确的输入车辆需要");
continue;
}
if (serial <= 0 serial > cars.size()) {
System.out.println("请正确的输入车辆需要");
isSerial = false;
continue;
}
rentCars.add(cars.get(serial - 1));
isSerial = true;
}
}
statisticsCars();
} else {
System.out.println("暂时没有可供租赁的车辆!");
return;
}
}
public int inNumber() throws ParseErrorException {
Scanner scanner1 = new Scanner(System.in);
try {
String trim = scanner1.next().trim();
return Integer.parseInt(trim);
} catch (Exception e) {
throw new ParseErrorException(e.getMessage());
}
}
private void statisticsCars() {
List<Car> carryName = new ArrayList<>();
List<Car> personName = new ArrayList<>();
int manned = 0;
int carryFreight = 0;
int rently = 0;
if (rentCars != null && rentCars.size() > 0) {
for (int i = 0; i < rentCars.size(); i++) {
Car car = rentCars.get(i);
rently += car.rental();
if (car instanceof PersonCar) {
manned += ((IMannedCar) car).mannedNumber();
personName.add(car);
} else if (car instanceof CarryCar) {
carryFreight += ((ICarryFreight) car).carryFreightNumber();
carryName.add(car);
} else {
manned += ((IMannedCar) car).mannedNumber();
carryFreight += ((ICarryFreight) car).carryFreightNumber();
carryName.add(car);
personName.add(car);
}
}
System.out.println("您租赁了:" + rentCars.size() + "辆车,总金额:" + rently);
if (carryFreight != 0) {
System.out.println("总载货量:" + carryFreight);
System.out.print("车辆类型为:");
for (Car car : carryName) {
System.out.print(car.name() + " ");
}
System.out.println();
}
if (manned != 0) {
System.out.println("总载人量:" + manned);
System.out.print("车辆类型为:");
for (Car car : personName) {
System.out.print(car.name() + " ");
}
System.out.println();
}
} else {
System.out.println("租赁信息有误");
}
}
}
答答租车系统入口:
public class DadaRentCar {
public static void main(String[] args) {
System.out.println("欢迎使用答答租车系统:");
System.out.println("您是否要租车:1-是,0-否");
boolean isRent = true;
while (isRent) {
Scanner scanner = new Scanner(System.in);
String trim = scanner.next().trim();
if (trim.equals("1")) {
RentCar rentCar = new RentCar();
rentCar.rent();
} else if (trim.equals("0")) {
isRent = false;
System.out.println("再见!");
} else {
System.out.println("请输入正确的指令");
}
}
}
}
热门评论
租车系统哪里的if----else 看不懂