Java Cartrader程序。留下1个Junit错误,但我没有´;我不明白
Eclipse在我的“Vehicle class”中显示错误“Vehicle的基本类型字节没有字段PASSENGER\u CAR”。这是什么意思?这是我们必须在课堂上使用的JUnit测试中剩下的唯一错误。当我写“7”或“5”这两个类型的常量时,它似乎可以工作,但我不允许这样做。以下是完整的程序:Java Cartrader程序。留下1个Junit错误,但我没有´;我不明白,java,Java,Eclipse在我的“Vehicle class”中显示错误“Vehicle的基本类型字节没有字段PASSENGER\u CAR”。这是什么意思?这是我们必须在课堂上使用的JUnit测试中剩下的唯一错误。当我写“7”或“5”这两个类型的常量时,它似乎可以工作,但我不允许这样做。以下是完整的程序: public class Vehicle { public static final float TANK_SIZE_PASSENGER_CAR = 40.f; public static final
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;
}
}
}
}
}
公共车辆[]出售车辆(字节车辆类型,整数金额){
车辆[]整车=新车[金额];
浮动电流金额=40*金额;
浮动电流金额2=80*金额;
开关(车辆类型){
案例0://VehicleType.TRUCK:
如果(此.dieselstockslolls>=当前数量2和此.truckstock>=数量){
卡车库存=卡车库存-金额;
柴油储备升=柴油储备升-(当前数量2);
对于(int i=0;i=currentAmount&&this.passengercarstock>=金额){
passengerCarsStock=passengerCarsStock-金额;
gasStockLiters=gasStockLiters-(当前量);
对于(int i=0;i您有一个名为VehicleType
的类,一个名为VehicleType
的浮点数,并将名为VehicleType
的字节传递到函数中。将Vehicle
初始化器更改为public Vehicle(byte 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;
}
}
@popdemic本节第4行有错误(assertEquals(40.f…)
这里是第二行测试SoldCarAttributes(VehicleType.PASSENGER\u CAR
public void testSoldCarAttributes() {
testSoldCarAttributes(VehicleType.PASSENGER_CAR, trader.sellVehicle(VehicleType.PASSENGER_CAR));
testSoldCarAttributes(VehicleType.TRUCK, trader.sellVehicle(VehicleType.TRUCK));
您应该有一个编译错误否?它的堆栈跟踪是什么?Eclipse通常会向您显示错误的确切位置。请创建一个(empahsis处于最小值)。在您的情况下,问题源于车辆的构造函数公共车辆(字节车辆类型)
:变量VehicleType
的类型为byte
和byte
(作为原语)没有字段乘用车
。您使用参数byte VehicleType
@SMA隐藏类VehicleType
。\n您确定这是问题吗?从这个问题来看,编译器似乎在车辆
的构造函数中对乘用车
进行了尖锐的抱怨(我自己还没有检查过)。呜呜!实际上OP使用了这个公共车辆(byte VehicleType){this();if(VehicleType==VehicleType.PASSENGER_CAR){
他还有一个类叫做VehicleType!就我个人而言,我只想说字节类型
,并将字段小写name@cricket_007to-ma-toe,to-mah-toe。谢谢。到目前为止,这是有效的。不幸的是,JUnit测试中仍然存在与此相关的错误。请编辑您的问题以包含其他错误,而不是answer
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);
}
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;
}
}
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());
}
public void testSoldCarAttributes() {
testSoldCarAttributes(VehicleType.PASSENGER_CAR, trader.sellVehicle(VehicleType.PASSENGER_CAR));
testSoldCarAttributes(VehicleType.TRUCK, trader.sellVehicle(VehicleType.TRUCK));