1 思想
1、
创建两个接口(载客接口IPeopleCar),(载货接口ICargoCar)、一个抽象父类
Car(属性为:车型,名字,价格,载人量,载货量);
其中两个接口本次没有具体方法,为以后扩展使用。`
2、
实现-货车类 extends 父类Car implement 载货接口:ICargoCar;
实现-客车类 extends 父类Car implement 载客接口:IPeopleCar;
实现-皮卡类 extends 父类Car implement 载客和货接口:ICargoCar和ICargoCar;
3、
租车系统类RentCar:一维数组 实例化 卡车对象、客车对象 和 皮卡对象;
客户选车、分类汇总车辆后,输出租车明细与总价。
2 代码实现
//Car.java
package com.wzq.rentcar;
public abstract class Car {
public String name;
public double price;
public float peopleValue;
public float cargoValue;
public int flag;//标记车型,0:people,1:cargo,2:pick-up
public Car(){}
}
//IPeopleCar.java
package com.wzq.rentcar;
public interface IPeopleCar {
}
//ICargoCar.java
package com.wzq.rentcar;
public interface ICargoCar {
}
//CargoCar.java
package com.wzq.rentcar;
public class CargoCar extends Car implements ICargoCar {
public CargoCar(int flag,String name,double price,float cargoValue) {
// TODO Auto-generated constructor stub
this.flag = 1;
this.name = name;
this.price = price;
this.cargoValue = cargoValue;
}
}
//PickUp .java
package com.wzq.rentcar;
public class PickUp extends Car implements ICargoCar, IPeopleCar {
public PickUp(int flag,String name,double price,float peopleValue,float cargoValue) {
// TODO Auto-generated constructor stub
this.flag = 2;
this.name = name;
this.price = price;
this.peopleValue = peopleValue;
this.cargoValue = cargoValue;
}
}
//PeopleCar .java
package com.wzq.rentcar;
public class PeopleCar extends Car implements IPeopleCar {
public PeopleCar(int flag,String name,double price,float peopleValue) {
// TODO Auto-generated constructor stub
this.flag = 0;
this.name = name;
this.price = price;
this.peopleValue = peopleValue;
}
}
//RentCar .java
package com.wzq.rentcar;
import java.util.*;
public class RentCar {
public static void main(String[] args) {
// TODO Auto-generated method stub
Car car[] = new Car[6];
car[0] = new PeopleCar(0, "奥迪A4", 500, 4);
car[1] = new PeopleCar(0, "马自达6", 400, 4);
car[2] = new PickUp(2, "皮卡雪6", 450, 4, 2);
car[3] = new PeopleCar(0, "金龙", 800, 20);
car[4] = new CargoCar(1, "松花江", 400, 4);
car[5] = new CargoCar(1, "依维柯", 1000, 20);
System.out.println("欢迎使用嗒嗒租车系统");
System.out.println("您是否要租车:1是 0否");
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
int in = sc.nextInt();
if (1 == in) {
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号\t汽车名称\t租金\t\t容量");
int number;
for (int i = 0; i < 6; i++) {
number = i + 1;
if (car[i].flag == 0) {
System.out.println(
number + "\t" + car[i].name + "\t" + car[i].price + "元/天\t载人:" + car[i].peopleValue + "人");
} else if (car[i].flag == 1) {
System.out.println(
number + "\t" + car[i].name + "\t" + car[i].price + "元/天\t载货:" + car[i].cargoValue + "吨");
} else {
System.out.println(number + "\t" + car[i].name + "\t" + car[i].price + "元/天\t载人:"
+ car[i].peopleValue + "人,载货:" + car[i].cargoValue + "吨");
}
}
System.out.print("请输入本次租车的数量:");
int cartotal = sc.nextInt();
int[] carList = new int[6]; // 保存车子的序号
for (int i = 1; i <= cartotal; i++) {
System.out.print("请输入第" + i + "辆车的序号:");
int carNumer = sc.nextInt();
carList[i - 1] = carNumer;
}
System.out.print("请输入租车天数:");
int dayNumber = sc.nextInt();
System.out.println("您的账单:");
System.out.print("***可载人的车有:");
int totalPeople = 0;// 标记总人数
int totalCargo = 0;// 标记总货物量
double money = 0;// 标记总钱数
for (int i = 0; i < cartotal; i++) {
if (car[carList[i] - 1].flag == 0 car[carList[i] - 1].flag == 2) {
totalPeople += car[carList[i] - 1].peopleValue;
System.out.print(car[carList[i] - 1].name + " ");
}
}
System.out.println("\n\t\t共载人:" + totalPeople + "人");
System.out.print("***可载货的车有:");
for (int i = 0; i < cartotal; i++) {
if (car[carList[i] - 1].flag == 1 car[carList[i] - 1].flag == 2) {
totalCargo += car[carList[i] - 1].cargoValue;
System.out.print(car[carList[i] - 1].name + " ");
}
money += car[carList[i] - 1].price;
}
System.out.println("\n\t\t共载货:" + totalCargo + "吨");
System.out.print("***租车总金额为:" + money * dayNumber);
} else {
System.out.println("欢迎您的到来,下次再会!");
}
}
}
## 3 运行截图 ##
![图片描述][1]
![图片描述][2]
[1]: http://img.mukewang.com/579ce4930001b4ff07800241.png
[2]: http://img.mukewang.com/579ce4a90001063c07850592.png
打开App,阅读手记
热门评论
为何不是私有属性,这样安全吗
carList[i - 1] = carNumer;
这是什么意思啊
implements ICargoCar ICargoCar 这里报错了