我根据老师的题目要求,结合了一些慕友们的代码,完成了这么一个租车系统,编辑环境是IDEA,不过和eclipse也能兼容,初次编写,很多不合理的地方多多指教!!
ILoadCar(载货接口)
package com.rentcar;
public interface ILoadCar {
public int LoadCar();
}
IPeopleCar(载人接口)
package com.rentcar;
public interface IPeopleCar {
public int PeopleCar();
}
Car(父类)
package com.rentcar;
public class Car {
int id;
String CarName;
double price;
}
SeatCar(载人车)
package com.rentcar;
public class SeatCar extends Car implements IPeopleCar{
public int seatPeople;
public SeatCar(int id,String CarName,double price,int seatPeople){
this.id = id;
this.CarName = CarName;
this.price = price;
this.seatPeople = seatPeople;
}
@Override
public String toString() {
return "SeatCar{" +
"seatPeople=" + seatPeople +
'}';
}
@Override
public int PeopleCar() {
return seatPeople;
}
}
LoadCar(载货车)
package com.rentcar;
public class LoadCar extends Car implements ILoadCar{
public int loadWeight;
public LoadCar(int id,String CarName,double price,int loadWeight){
this.id = id;
this.CarName = CarName;
this.loadWeight = loadWeight;
this.price = price;
}
@Override
public String toString() {
return "LoadCar{" +
"loadWeight=" + loadWeight +
'}';
}
@Override
public int LoadCar() {
return loadWeight;
}
}
PickCar(双载车)
package com.rentcar;
public class PickCar extends Car implements IPeopleCar,ILoadCar{
public int seatPeople;
public int loadWeight;
public PickCar(int id,String CarName,double price,int seatPeople,int loadWeight){
this.id = id;
this.CarName = CarName;
this.price = price;
this.seatPeople = seatPeople;
this.loadWeight = loadWeight;
}
@Override
public String toString() {
return "PickCar{" +
"seatPeople=" + seatPeople +
", loadWeight=" + loadWeight +
'}';
}
@Override
public int LoadCar() {
return loadWeight;
}
@Override
public int PeopleCar() {
return seatPeople;
}
}
main(测试类)
package com.rentcar;
import java.util.Scanner;
public class main {
public static void main(String[] args) {
Car[] cars = new Car[]{new SeatCar(1,"奥迪A4",500,4),
new SeatCar(2,"马自达6",400,4),
new PickCar(3,"皮卡雪6",450,4,5),
new SeatCar(4,"金龙",800,20),
new LoadCar(5,"松花江",400,4),
new LoadCar(6,"依维柯",1000,20)};
double rentPrice=0;
int seatNumber = 0;
int loadWeight = 0;
String peopleName = "";
String loadName = "";
System.out.println("*************欢迎使用答答租车系统!**************");
System.out.println("你是否要租车?1是 0否");
main isTrue = new main();
int index = isTrue.isRent();
System.out.println("请输入您要租汽车的数量:");
main cn = new main();
int nums = cn.CarsNumber();
for (int i = 1;i<= nums;i++){
System.out.println("请输入第"+i+"辆车的序号");
main selectCar = new main();
int bland = selectCar.selectCar();
if(bland == 1 || bland == 2 || bland == 4){
rentPrice += cars[bland-1].price;
seatNumber += ((SeatCar)cars[bland-1]).PeopleCar();
peopleName +=cars[bland-1].CarName+" ";
}else if(bland == 5 || bland == 6){
rentPrice += cars[bland-1].price;
loadWeight += ((LoadCar)cars[bland-1]).LoadCar();
loadName += cars[bland-1].CarName+" ";
}else{
rentPrice += cars[bland-1].price;
seatNumber += ((PickCar)cars[bland-1]).PeopleCar();
loadWeight += ((PickCar)cars[bland-1]).LoadCar();
peopleName +=cars[bland-1].CarName+" ";
loadName += cars[bland-1].CarName+" ";
}
}
System.out.println("请输入租车天数:");
Scanner input = new Scanner(System.in);
int day = input.nextInt();
if(day > 0){
rentPrice *= day;
System.out.println("*********您的账单:");
System.out.println("*********您租用的可载人车:"+peopleName);
System.out.println("*********共可载客:"+seatNumber+"人");
System.out.println("*********您租用的可载货车:"+loadName);
System.out.println("*********共可载货:"+loadWeight+"吨");
System.out.println("*********租车总价格:"+rentPrice+"元");
System.out.println();
System.out.println("*************感谢使用答答租车系统,祝您生活愉快!*************");
}
}
public int isRent(){
int index;
Scanner scanner = new Scanner(System.in);
index = scanner.nextInt();
if (index == 0){
System.out.println("您已经退出答答租车系统,欢迎下次使用,谢谢!");
System.exit(0);
}else if (index == 1){
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号 租车名称 租金 容量");
System.out.println("1. 奥迪A4 500元/天 载人:4人");
System.out.println("2. 马自达6 400元/天 载人:4人");
System.out.println("3. 皮卡雪6 450元/天 载人:4人 载货:5吨");
System.out.println("4. 金龙 800元/天 载人:20人");
System.out.println("5. 松花江 400元/天 载货:4吨");
System.out.println("6. 依维柯 1000元/天 载货:20吨");
}
return index;
}
public int CarsNumber(){
int nums;
Scanner scanner = new Scanner(System.in);
nums = scanner.nextInt();
return nums;
}
public int selectCar(){
int count;
Scanner scanner = new Scanner(System.in);
count = scanner.nextInt();
return count;
}
}