Car类
public class Car {
private String CarName;
private int Price;
public Car(String CarName,int Price){
this.CarName = CarName;
this.Price = Price;
}
public String getCarName() {
return CarName;
}
public void setCarName(String carName) {
CarName = carName;
}
public int getPrice() {
return Price;
}
public void setPrice(int price) {
Price = price;
}
}
MannerCar类-------载客类型
public class MannerCar extends Car
implements ImannedCar{
private int mannednum;
public MannerCar(String CarName,int Pirce,int mannedNum){
super(CarName,Pirce);
this.mannednum = mannedNum;
}
@Override
public void setMannedNum(int num) {
// TODO Auto-generated method stub
mannednum = num;
}
@Override
public int getMannedNum() {
// TODO Auto-generated method stub
return mannednum;
}
}
ImannedCar----载客类型接口
public interface ImannedCar {
public void setMannedNum(int num);
public int getMannedNum();
}
CargoCar-----载货类型
public class CargoCar extends Car
implements ICargoCar{
private int carryingNum;
public CargoCar(String CarName,int Price,int carryingNum) {
// TODO Auto-generated constructor stub
super(CarName, Price);
this.carryingNum = carryingNum;
}
@Override
public void setCarrying(int num) {
// TODO Auto-generated method stub
carryingNum = num;
}
@Override
public int getCarrying() {
// TODO Auto-generated method stub
return carryingNum;
}
}
ICargoCar------载货类型接口
public interface ICargoCar {
public void setCarrying(int num);
public int getCarrying();
}
MoreCar------既能载客又能载货类型
public class MoreCar extends Car
implements ImannedCar,ICargoCar{
private int mannedNum;
private int carryingNum;
public MoreCar(String CarName, int Price, int mannedNum,int carryingNum) {
super(CarName, Price);
this.carryingNum = carryingNum;
this.mannedNum = mannedNum;
}
@Override
public void setCarrying(int num) {
// TODO Auto-generated method stub
carryingNum = num;
}
@Override
public int getCarrying() {
// TODO Auto-generated method stub
return carryingNum;
}
@Override
public void setMannedNum(int num) {
// TODO Auto-generated method stub
mannedNum = num;
}
@Override
public int getMannedNum() {
// TODO Auto-generated method stub
return mannedNum;
}
}
Test-----测试类
public class Test {
Car[] NewCar;
public static void main(String[] args) {
// TODO Auto-generated method stub
//设置初始化测试对象
Test test = new Test();
test.Setting();
//num:租车数量
//TotalNum:车的总数量
//Serial:客户租车的序号
//用户租车的天数
int num = 0,TotalNum = 0;
int[] Serial = null;
int TotalDay = 0,TotalPirce = 0;
Scanner sc = new Scanner(System.in);
System.out.println("欢迎使用答答租车系统");
System.out.println("您是否要租车:1是 0否");
int InOrOu = sc.nextInt();
if(InOrOu != 1) return;
System.out.println("您可租车的类型及价目表:");
System.out.println("序号 汽车名称 租金 容量");
int j = 1;
int length = test.NewCar.length;
for(Car car:test.NewCar){
if(car instanceof MannerCar)
System.out.println((j++) + " " + car.getCarName() +
" " + car.getPrice() + "元/天" +
" " + "载人:" + ((MannerCar) car).getMannedNum() + "人");
if(car instanceof CargoCar)
System.out.println((j++) + " " + car.getCarName() +
" " + car.getPrice() + "元/天" +
" " + "载人:" + ((CargoCar) car).getCarrying() + "吨");
if(car instanceof MoreCar)
System.out.println((j++) + " " + car.getCarName() +
" " + car.getPrice() + "元/天" +
" " + "载人:" + ((MoreCar) car).getMannedNum() + "人" +
" 载货:" + ((MoreCar) car).getCarrying() + "吨");
}
System.out.println("请输入您要租汽车的数量");
num = sc.nextInt();
while(num > length){
System.out.println("您所需要的数量超出了总数量,请重新输入");
num = sc.nextInt();
}
Serial = new int[num];
for(int i=0;i<num;i++){
System.out.println("请输入第" + (i+1) + "辆汽车的序号");
Serial[i] = sc.nextInt();
while(Serial[i] > length){
System.out.println("您所需要的数量超出了总数量,请重新输入");
Serial[i] = sc.nextInt();
}
}
System.out.println("请输入租车的天数");
TotalDay = sc.nextInt();
System.out.println("您的账单:");
System.out.println("***可载人的车有:");
int TotalPeople = 0;
for(int i=0;i<num;i++){
if(test.NewCar[Serial[i]-1] instanceof MannerCar){
System.out.print(" " + ((MannerCar) test.NewCar[Serial[i]-1]).getCarName());
TotalPeople += ((MannerCar) test.NewCar[Serial[i]-1]).getMannedNum();
TotalPirce += ((MannerCar) test.NewCar[Serial[i]-1]).getPrice() * TotalDay;
}
if(test.NewCar[Serial[i]-1] instanceof MoreCar){
System.out.print(" " + ((MoreCar) test.NewCar[Serial[i]-1]).getCarName());
TotalPeople += ((MoreCar) test.NewCar[Serial[i]-1]).getMannedNum();
}
}
System.out.println(" 共载人:" + TotalPeople + "人");
System.out.println("***载货的车有:");
int TotalCarrying = 0;
for(int i=0;i<num;i++){
if(test.NewCar[Serial[i]-1] instanceof CargoCar){
System.out.print(" " + ((CargoCar) test.NewCar[Serial[i]-1]).getCarName());
TotalCarrying += ((CargoCar) test.NewCar[Serial[i]-1]).getCarrying();
TotalPirce += ((CargoCar) test.NewCar[Serial[i]-1]).getPrice() * TotalDay;
}
if(test.NewCar[Serial[i]-1] instanceof MoreCar){
System.out.print(" " + ((MoreCar) test.NewCar[Serial[i]-1]).getCarName());
TotalCarrying += ((MoreCar) test.NewCar[Serial[i]-1]).getCarrying();
TotalPirce += ((MoreCar) test.NewCar[Serial[i]-1]).getPrice() * TotalDay;
}
}
System.out.println(" 共载货:" + TotalCarrying + "吨");
System.out.println("***租车总价格:" + TotalPirce + "元");
}
private void Setting(){
NewCar = new Car[]{
new MannerCar("奥迪A4", 500, 4),
new MannerCar("马自达6", 400, 4),
new MannerCar("奥迪A4", 500, 4),
new CargoCar("松花江", 400, 4),
new CargoCar("依维柯", 1000, 20),
new MoreCar("皮卡雪6", 450, 4, 2)
};
}
}
- 列表项