Eclipse在我的“Vehicle class”中显示错误“Vehicle的基本类型字节没有字段PASSENGER_CAR”。这是什么意思?这是我们必须在课堂上使用的JUnit测试中唯一的错误。当我写“7”或“5”这些类型的常量似乎工作,但我不允许这样做。这是完整的计划:
public class Vehicle {
public static final float TANK_SIZE_PASSENGER_CAR = 40.f;
public static final float TANK_SIZE_TRUCK = 80.f;
private float fuel;
private float VehicleType;
public Vehicle(){
}
public Vehicle(byte VehicleType){
this();
if (VehicleType == VehicleType.PASSENGER_CAR){
this.VehicleType= VehicleType.PASSENGER_CAR ;
this.fuel= Vehicle.TANK_SIZE_PASSENGER_CAR;
}
if(VehicleType == VehicleType.TRUCK){
this.VehicleType= VehicleType.TRUCK;
this.fuel= Vehicle.TANK_SIZE_TRUCK;
}
public float getFuel(){
return fuel;
}
public byte getVehicleType(){
return VehicleType;
}
}
}
public final class VehicleType {
public static final byte PASSENGER_CAR = 7;
public static final byte TRUCK = 5;
/**
* This class is just an organizer for the above constants; Not instantiable.
*/
private VehicleType(){
}
}
public class CarTrader {
public static final float CAPACITY_DIESEL_LITERS = 250.f;
public static final float CAPACITY_GAS_LITERS = 180.f;
public static final int CAPACITY_PASSENGER_CARS = 15;
public static final int CAPACITY_TRUCKS = 5;
float gasStockLiters=180.f;
float dieselStockLiters=250.f;
int passengerCarsStock=15;
int trucksStock=5;
void setGasStockLiters(float gasStockLiters){
this.gasStockLiters=gasStockLiters;
}
void setDieselStockLiters(float dieselStockLiters){
this.dieselStockLiters=dieselStockLiters;
}
void setPassengerCarsStock(int passengerCarsStock){
this.passengerCarsStock=passengerCarsStock;
}
void setTrucksStock(int trucksStock){
this.trucksStock=trucksStock;
}
int getPassengerCarsStock(){
return passengerCarsStock;
}
int getTrucksStock(){
return trucksStock;
}
float getGasStockLiters(){
return gasStockLiters;
}
float getDieselStockLiters(){
return dieselStockLiters;
}
public Vehicle sellVehicle(byte vehicleType){
Vehicle soldVehicle=null;
switch (vehicleType){
case 0: //VehicleType.TRUCK:
if(this.dieselStockLiters>=Vehicle.TANK_SIZE_TRUCK && this.trucksStock>=1){
trucksStock = trucksStock - 1;
dieselStockLiters = dieselStockLiters - Vehicle.TANK_SIZE_PASSENGER_CAR;
soldVehicle = new Vehicle(VehicleType.TRUCK);
System.out.println("LKW erfolgreich verkauft");
}
else{
soldVehicle=null;
}
break;
case 1://VehicleType.PASSENGER_CAR:
if(gasStockLiters>=Vehicle.TANK_SIZE_PASSENGER_CAR && this.passengerCarsStock>=1){
passengerCarsStock = passengerCarsStock - 1;
gasStockLiters = gasStockLiters - Vehicle.TANK_SIZE_PASSENGER_CAR;
soldVehicle = new Vehicle(VehicleType.PASSENGER_CAR);
System.out.println("PKW erfolgreich verkauft");
}
else{
soldVehicle=null;
}
break;
default:
break;
}
return soldVehicle;
}
public Vehicle[] sellVehicles(byte vehicleType, int amount){
Vehicle[] soldVehicles = new Vehicle[amount];
float currentAmount=40*amount;
float currentAmount2=80*amount;
switch (vehicleType){
case 0://VehicleType.TRUCK:
if(this.dieselStockLiters>=currentAmount2 && this.trucksStock>=amount){
trucksStock = trucksStock - amount;
dieselStockLiters = dieselStockLiters - (currentAmount2);
for (int i=0; i<amount; i++){
soldVehicles[i]=new Vehicle(VehicleType.TRUCK);
}
System.out.println("LKW erfolgreich verkauft");
}
else{
soldVehicles=null;
}
break;
case 1://VehicleType.PASSENGER_CAR:
if(this.gasStockLiters>=currentAmount && this.passengerCarsStock>=amount){
passengerCarsStock = passengerCarsStock - amount;
gasStockLiters = gasStockLiters - (currentAmount);
for (int i=0; i<amount; i++){
soldVehicles[i]=new Vehicle(VehicleType.PASSENGER_CAR);
}
System.out.println("PKW erfolgreich verkauft");
}
else{
soldVehicles=null;
}
break;
default:
soldVehicles=null;
break;
}
return soldVehicles;
}
public boolean fillGas(float gas){
boolean filled=false;
if((gasStockLiters + gas)<=CarTrader.CAPACITY_GAS_LITERS){
gasStockLiters = gasStockLiters + gas;
filled=true;
}
else{
System.err.println("Die Kapazität beträgt höchstens 180 Liter");
}
return filled;
}
public boolean fillDiesel(float diesel){
boolean filled2=false;
if((dieselStockLiters + diesel)<=CarTrader.CAPACITY_DIESEL_LITERS){
dieselStockLiters = dieselStockLiters + diesel;
filled2=true;
}
else{
System.err.println("Die Kapazität beträgt höchstens 250 Liter");
}
return filled2;
}
public boolean reorderVehicles(byte vehicle, int amountVehicles){
boolean full=false;
if (vehicle==1){
if ((trucksStock + amountVehicles)<=CarTrader.CAPACITY_TRUCKS){
trucksStock = trucksStock + amountVehicles;
full=true;
}
}
if (vehicle==0){
if ((passengerCarsStock + amountVehicles)<=CarTrader.CAPACITY_PASSENGER_CARS){
passengerCarsStock = passengerCarsStock + amountVehicles;
full=true;
}
}
else {
System.out.println("Bitte 5 oder 7 wählen");
}
return full;
}
void statusOutput()
{
System.out.println("AutoHändler - Lagerbestand:");
System.out.println(" PKW: " + passengerCarsStock);
System.out.println(" - Benzin: " + gasStockLiters);
System.out.println(" LKW: " + trucksStock);
System.out.println(" - Diesel: " + dieselStockLiters);
}
}
答案 0 :(得分:1)
你有一个名为VehicleType
的类,一个名为VehicleType
的浮点数,并将一个名为VehicleType
的字节传入函数。将您的Vehicle
初始化程序更改为public Vehicle(byte vehicleType)
,错误应该消失,停止命名所有内容VehicleType
。
public class Vehicle {
public static final float TANK_SIZE_PASSENGER_CAR = 40.f;
public static final float TANK_SIZE_TRUCK = 80.f;
private float fuel;
private float mVehicleType;
public Vehicle(){}
public Vehicle(byte vehicleType){
this();
if (vehicleType == VehicleType.PASSENGER_CAR){
this.mVehicleType= VehicleType.PASSENGER_CAR ;
this.fuel= Vehicle.TANK_SIZE_PASSENGER_CAR;
}
if(vehicleType == VehicleType.TRUCK){
this.mVehicleType= VehicleType.TRUCK;
this.fuel= Vehicle.TANK_SIZE_TRUCK;
}
}
public float getFuel(){
return this.fuel;
}
public byte getVehicleType(){
return this.mVehicleType;
}
}
答案 1 :(得分:-1)
@popdemic这是第4行中的错误(assertEquals(40.f ....)
private void testSoldCarAttributes(byte vehicleType, final Vehicle v) {
switch (vehicleType) {
case VehicleType.PASSENGER_CAR:
assertEquals(40.f, v.getFuel(), DELTA_FLOAT);
assertEquals(VehicleType.PASSENGER_CAR, v.getVehicleType());
break;
case VehicleType.TRUCK:
assertEquals(80.f, v.getFuel(), DELTA_FLOAT);
assertEquals(VehicleType.TRUCK, v.getVehicleType());
}
并在第二行testSoldCarAttributes(VehicleType.PASSENGER_CAR .....
public void testSoldCarAttributes() {
testSoldCarAttributes(VehicleType.PASSENGER_CAR, trader.sellVehicle(VehicleType.PASSENGER_CAR));
testSoldCarAttributes(VehicleType.TRUCK, trader.sellVehicle(VehicleType.TRUCK));