大虾们,java java
题目:计算各种交通工具的运输费用,定义一个接口:Vehicle:包括,yuan_kilometer,distance,roadtoll,ridership(乘客数),还有一个计费方法。现有两种交通工具:Car:计费方式:yuan_kilometer*distance+roadtoll+ridership*1
Bus:计费方式:yuan_kilometer*distance+roadtoll+ridership*0.2
我的代码如下:
package traspotecharging;
public interface Vehicle {
float yuan_kilometer=0f;
float distance=0f;
float roadtoll=0f;
int ridership=0;
public double charge();
}
package traspotecharging;
public class Bus implements Vehicle{
float yuan_kilometer;
float distance;
float roadtoll;
int ridership;
Bus(){}
Bus(float a,float b,float c,int d){
this.yuan_kilometer=a;
this.distance=b;
this.roadtoll=c;
this.ridership=d;
}
public double charge(){
return this.distance*this.yuan_kilometer+this.roadtoll+this.ridership*0.2;
}
}
package traspotecharging;
public class Car implements Vehicle{
float yuan_kilometer;
float distance;
float roadtoll;
int ridership;
Car(){}
Car(float a,float b,float c,int d){
this.yuan_kilometer=a;
this.distance=b;
this.roadtoll=c;
this.ridership=d;
}
public double charge(){
return this.distance*this.yuan_kilometer+this.roadtoll+this.ridership;
}
}
package traspotecharging;
import java.util.Scanner;
public class Charge {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
float a=sc.nextFloat();
int d=sc.nextInt();
float b=sc.nextFloat();
float c=sc.nextFloat();
System.out.println("请输入交通工具:");
char op=sc.next().charAt(0);
Vehicle charge=null;
switch(op){
case 'b': charge=new Bus(a,b,c,d);
break;
case 'c': charge=new Car(a,b,c,d);
break;
}
System.out.println(charge.charge());
}
}
现在问题是要求增加其他交通工具时,不必改动以前的任何程序,只需要编写新的交通工具。(具体应该是用那什么哈希表之类的存在文件中,通过文件读入,加入新的交通工具,只需在文件中增加这项就可)
求助各位大虾们!!! 万分感谢!!!