简体   繁体   English

在 Java 中的类之间传递参数

[英]Passing arguments between classes in Java

I'm very stuck trying to solve an excercise consisting of a java pack being tested with some tests I find impossible to pass.我非常难以解决一个由 java 包组成的练习,其中一些测试我发现无法通过。

There are two classes in the pack, namely Car and Parking.包中有两个类,即汽车和停车。 The relationship between them is an aggregation - Parking is the compound and Car is the component:它们之间的关系是一种聚合——Parking 是复合,Car 是组件:

Parking -parking (0...1)<>------- -cars(*) Car停车 -parking (0...1)<>-------- -cars(*) 汽车

-parking and -cars are the attributes which with the classes Parking and Car are respectively related. -parking 和 -cars 是分别与类 Parking 和 Car 相关的属性。 The attribute -parking can have two values, 0 or 1, and -cars is an array of undefined dimension.属性-parking 可以有两个值,0 或1,-cars 是一个未定义维度的数组。

A car can be assigned to one or none parkings.一辆车可以分配到一个或没有停车位。 Likewise, a parking is compound of a variable number of parking lots.同样,停车场是由可变数量的停车场组成的。

The code is as follows:代码如下:

package package;

public class Parking {

    private String name; // Parking's name

    private String address; // Parking's address

    private int capacity; // Parking's capacity (number of cars can be parked in)

    private Car[] cars; // Array of cars that can be parked

    public Parking() { // Default constructor
        name = "Default parking";
        address = "59th Street";
        capacity = 10;
        cars = new Car[capacity];
    }

    public Car[] getCars() { // Getter of the array 'cars'
        return cars;
    }

    public int getFirstFreeParkingLot() { // Returns first free empty slot of the
                                         // array 'cars'. A car can be parked there.
                                         // Otherwise, returns -1
        int i;
        boolean b = false;
        for (i = 0; i < cars.length; i++) {
            if (cars[i] == null) {
                b = true;
                break;
            }
        }
        if (!b) return -1;
        else return i;
    }

    public void addCar (Car car) throws Exception { // Adds a car to a parking lot
        if (car == null) throw new Exception("[ERROR] The car cannot be null");
        else if (getParkingLotNumberByCar(car) != -1) throw new Exception("[ERROR] This car is already in this parking");
        else if (isFull()) throw new Exception("[ERROR] This parking is full");
        else if (getFirstFreeParkingLot() != -1) {
            cars[getFirstFreeParkingLot()] = car;
            car.setParking(car.getParking());
            }
    }

    public void removeCar (Car car) throws Exception { // remove a car from a parking 
                                                       // lot
        if(getParkingLotNumberByCar(car) != -1) {
            cars[getParkingLotNumberByCar(car)] = null;
            car.setParking(null);
            }
        else throw new Exception("[ERROR] This car does not exist in this parking");
    }

    public boolean isFull() { // Checks if the parking is full. 
        int i;
        boolean b = false;
        for (i = 0; i < cars.length; i++) {
            if (cars[i] == null) {
                b = true;
                break;
            }
        }
        return !b;
    }

    public boolean isFree() { // Checks if there's at least one empty parking lot
        int i;
        boolean b = false;
        for (i = 0; i < cars.length; i++) {
            if (cars[i] == null) {
                b = true;
                break;
            }
        }
        return b;
    }

    public boolean isEmpty() { // Checks if the entire parking lot is empty
        int i;
        boolean b = false;
        for (i = 0; i < cars.length; i++) {
            if (cars[i] != null) {
                b = true;
                break;
            }
        }
        return !b;
    }

    public int getParkingLotNumberByCar (Car car) { // Return the index of the array 
                                                    // 'cars' where the car given as
                                                    // argument is present in the 
                                                    // parking lot. Otherwise, returns -1
        int i;
        boolean b = false;
        for (i = 0; i < cars.length; i++) {
            if (cars[i] == car) {
                b = true;
                break;
            }
        }
        if (!b) return -1;
        else return i;
    }

    public int getNumFreeParkingLots() { // Return the number of free parking lots in a parking
        int i;
        int n = 0;
        for (i = 0; i < cars.length; i++) {
            if (cars[i] == null) n++;
        }
        return n;
    }

}
package package;

import javax.management.ObjectName;
import java.time.LocalDate;
import java.util.Objects;
import java.util.UUID;

public class Car {

    private Parking parking;

    public Car() {
        parking = null;
    }

    public Parking getParking() {
        return parking;
    }

    public void setParking(Parking parking) throws Exception {
        if (parking == null)
            this.parking = null;
        else {
            parking.addCar(parking.getCars()[parking.getParkingLotNumberByCar(new Car())]);
            if (this.parking != parking) this.parking.removeCar(parking.getCars()[parking.getParkingLotNumberByCar(new Car())]);
        }
    }
}

And here's one test which I don't pass as an example:这是一个我没有通过的测试作为例子:

package package;

import org.junit.jupiter.api.*;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.TestInstance.Lifecycle;

import static org.junit.jupiter.api.Assertions.*;

@TestInstance(Lifecycle.PER_CLASS)
@TestMethodOrder(OrderAnnotation.class)
class IntegrationTest {
    Parking parking1;
    Car car1;
    
    @BeforeAll
    void init(){
        try {
            parking1 = new Parking();
            car1 = new Car();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Init failed");
        }
    }

    @Test
    @Order(1)
    void testIntegration1() {
        try {
            parking1.addCar(car1);
            assertEquals(0, parking1.getParkingLotNumberByCar(car1));
            assertEquals(9, parking1.getNumFreeParkingLots());
            assertEquals(1, parking1.getFirstFreeParkingLot());
            assertEquals(car1, parking1.getCars()[parking1.getParkingLotNumberByCar(car1)]);
            assertEquals(parking1, car1.getParking());
        } catch (Exception e) {
            e.printStackTrace();
            fail("Integration1 failed");
        }
    }

The critical part comes when utilizing the setParking (Parking parking) method in the Car class and the addCar (Car car) and removeCar (Car car) methods in the Parking class.当使用 Car 类中的 setParking(停车泊车)方法以及 Parking 类中的 addCar(Car car)和 removeCar(Car car)方法时,关键部分就出现了。 The setParking method in the Car class establishes the attribute parking and also calls the methods addCar and remove Car of the Parking class, which in turn add to and remove a car from a parking, and finally call the mentioned setParking method. Car 类中的 setParking 方法建立了属性 parking 并调用了 Parking 类的 addCar 和 remove Car 方法,依次在停车场中添加和移除汽车,最后调用上面提到的 setParking 方法。

I get to add a Car to a parking, but I fail in adding the parking's information to that car.我可以将汽车添加到停车场,但我未能将停车场的信息添加到该汽车。

To refer to a Car object in the Car class, I use 'new Car', and when it comes to the Parking class, I use 'new Parking'.为了在 Car 类中引用 Car 对象,我使用“new Car”,当涉及到 Parking 类时,我使用“new Parking”。 Am I proceeding correctly?我是否正确进行?

All the tests I'm failing at are related with the unsuccessful intertwining of these above-mentioned methods - parking.addCar, parking.removeCar, car.setParking.我失败的所有测试都与上述这些方法的不成功交织有关——parking.addCar、parking.removeCar、car.setParking。

Could somebody help me?有人可以帮我吗?

Thank you a lot in advance非常感谢你提前

I finally found a solution.我终于找到了解决方案。


public class Parking {

    private String name; // Parking's name

    private String address; // Parking's address

    private int capacity; // Parking's capacity (number of cars can be parked in)

    private Car[] cars; // Array of cars that can be parked

    public Parking() { // Default constructor
        name = "Default parking";
        address = "59th Street";
        capacity = 10;
        cars = new Car[capacity];
    }

    public Car[] getCars() { // Getter of the array 'cars'
        return cars;
    }

    public int getFirstFreeParkingLot() { // Returns first free empty slot of the
                                         // array 'cars'. A car can be parked there.
                                         // Otherwise, returns -1
        int i;
        boolean b = false;
        for (i = 0; i < cars.length; i++) {
            if (cars[i] == null) {
                b = true;
                break;
            }
        }
        if (!b) return -1;
        else return i;
    }

    public void addCar (Car car) throws Exception {
        if (car == null) throw new Exception("[ERROR] The car cannot be null");
        else if ((getParkingLotNumberByCar(car) != -1)) throw new Exception("[ERROR] This car is already in this parking");
        else if (isFull()) throw new Exception("[ERROR] This parking is full");
        else if (isFree()) {
            cars[getFirstFreeParkingLot()] = car;
            car.setParking(this);
            }
    }

    public void removeCar (Car car) throws Exception {
        if(getParkingLotNumberByCar(car) != -1) {
            cars[getParkingLotNumberByCar(car)] = null;
            car.setParking(null);
            }
        else throw new Exception("[ERROR] This car does not exist in this parking");
    }

    public boolean isFull() { // Checks if the parking is full. 
        int i;
        boolean b = false;
        for (i = 0; i < cars.length; i++) {
            if (cars[i] == null) {
                b = true;
                break;
            }
        }
        return !b;
    }

    public boolean isFree() { // Checks if there's at least one empty parking lot
        int i;
        boolean b = false;
        for (i = 0; i < cars.length; i++) {
            if (cars[i] == null) {
                b = true;
                break;
            }
        }
        return b;
    }

    public boolean isEmpty() { // Checks if the entire parking lot is empty
        int i;
        boolean b = false;
        for (i = 0; i < cars.length; i++) {
            if (cars[i] != null) {
                b = true;
                break;
            }
        }
        return !b;
    }

    public int getParkingLotNumberByCar (Car car) { // Return the index of the array 
                                                    // 'cars' where the car given as
                                                    // argument is present in the 
                                                    // parking lot. Otherwise, returns -1
        int i;
        boolean b = false;
        for (i = 0; i < cars.length; i++) {
            if (cars[i] == car) {
                b = true;
                break;
            }
        }
        if (!b) return -1;
        else return i;
    }

    public int getNumFreeParkingLots() { // Return the number of free parking lots in a parking
        int i;
        int n = 0;
        for (i = 0; i < cars.length; i++) {
            if (cars[i] == null) n++;
        }
        return n;
    }

}
public class Car {

    private Parking parking;

    public Car() {
        parking = null;
    }

    public Parking getParking() {
        return parking;
    }

    public void setParking(Parking parking) throws Exception {
        if (parking != null) {
            if (parking.getParkingLotNumberByCar(this) == -1) {
                    parking.addCar(this);
            }
            if (this.parking != null && this.parking != parking) {
                        this.parking.removeCar(this);
                    }
            this.parking = parking;
                }
        else {
            if (this.parking != null) {
                if (this.parking.getParkingLotNumberByCar(this) != -1)
                    this.parking.removeCar(this);
            }
            this.parking = null;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM