Vehicle.java
package com.imooc;
public class Vehicle {
public int id;
public String name;
public int price;
public int getPrice() {
return price;
}
}
Car.java
package com.imooc;
public class Car extends Vehicle implements IPeople {
public int Num;
public Car(int id,String name,int price,int Num){
this.id=id;
this.name=name;
this.price=price;
this.Num=Num;
}
public String toString(){
return id+"\t"+name+"\t"+price+"元/天\t"+Num+"人";
}
@Override
public int getPeople() {
// TODO Auto-generated method stub
return Num;
}
}
Truck.java
package com.imooc;
public class Truck extends Vehicle implements ICargo {
public int Weight;
public Truck(int id,String name,int price,int Weight){
this.id=id;
this.name=name;
this.price=price;
this.Weight=Weight;
}
public String toString(){
return id+"\t"+name+"\t"+price+"元/天\t"+Weight+"吨";
}
@Override
public int getCargo() {
// TODO Auto-generated method stub
return Weight;
}
}
Pickup.java
package com.imooc;
public class Pickup extends Vehicle implements IPeople, ICargo {
public int Num;
public int Weight;
public Pickup(int id,String name,int price,int Num,int Weight){
this.id=id;
this.name=name;
this.price=price;
this.Num=Num;
this.Weight=Weight;
}
public String toString(){
return id+"\t"+name+"\t"+price+"元/天\t"+Num+"人,"+Weight+"吨";
}
@Override
public int getCargo() {
// TODO Auto-generated method stub
return Weight;
}
@Override
public int getPeople() {
// TODO Auto-generated method stub
return Num;
}
}
ICargo.java
package com.imooc;
public interface ICargo {
public int getCargo();
}
IPeople.java
package com.imooc;
public interface IPeople {
public int getPeople();
}
RentSystem.java
package com.imooc;
import java.util.Scanner;
public class RentSystem {
int rentSum=0;
String PeopleVehicle="",cargoVehicle="";
public void method(){
Vehicle[] Vehicles={new Car(1,"奥迪A4",500,4),
new Car(2,"马自达6",400,4),
new Pickup(3,"皮卡雪6",450,4,2),
new Car(4,"金龙",800,20),
new Truck(5,"松花江",400,4),
new Truck(6,"依维柯",1000,20)};
System.out.println("welcome to RentSystem");
System.out.println("Which car do you like to pick:1.car;2.truck;3.pickup;4.no need.");
Scanner input1=new Scanner(System.in);
switch (input1.nextInt()){
case 4:{
System.out.println("quit!");
System.exit(0);
break;
}
case 1:{
for(int i=0;i<Vehicles.length;i++){
if(Vehicles[i].getClass()==Car.class){
System.out.println(Vehicles[i]);
}
}
break;
}
case 2:{
for(int i=0;i<Vehicles.length;i++){
if(Vehicles[i].getClass()==Truck.class){
System.out.println(Vehicles[i]);
}
}
break;
}
case 3:{
for(int i=0;i<Vehicles.length;i++){
if(Vehicles[i].getClass()==Pickup.class){
System.out.println(Vehicles[i]);
}
}
break;
}
default:
System.out.println("Wrong Input!");
System.exit(0);
}
System.out.println("Which one:");
Scanner input2=new Scanner(System.in);
int idNum=input2.nextInt();
System.out.println("How long:");
Scanner input3=new Scanner(System.in);
int day=input3.nextInt();
rentSum=day*Vehicles[idNum-1].getPrice();
System.out.println("Totally:"+rentSum+"元");
}
public static void main(String[] args) {
RentSystem service = new RentSystem();
int quit=0;
do{
service.method();
System.out.println("Do you like to pick another one?1.yes,2.no");
Scanner input0=new Scanner(System.in);
quit=input0.nextInt();
}while(quit==1);
System.out.println("quit!1");
}
}
参考了前面人的代码,做了简单的实现,两个接口获得载货和载客量没有用上,主程序控制部分还有优化空间
热门评论
谢谢!你的代码非常具有可读心,我非常的喜欢!