vendredi 30 mars 2018

No more than a number of the same string name in the same array?

The problem I have encountered is as follows: I have created 10 row outputs representing docking spaces for ships.

But the dock can accommodate two sizes of ship; cargo and container. The rows are made up of 5 small and 5 medium. A cargo ship (small) can berth in any available space. A container ship (medium) can berth in the medium space, but not in small spaces.

So if I enter shipName and Container for example it searches the array making sure there is less than 5 Container's so it can dock i.e. save in the array. Can you help? Here's my dock method:

public static void dock() {

    int dockCapacity = 0;

    System.out.println("Enter ship's name: ");
    String name = scan.nextLine();

    System.out.println("Enter ship's size: ");
    String size = scan.nextLine();

    System.out.println("Enter the ships dock:");
    //search for 5 small 3 med 2 large
    for(int i = 1; i < dock1.length; i++) {
        if (dock1[i].getShipSize().contains("Cargo").) {
        }
        else {
            System.out.println("Couldn't dock");
        }
    }
    //Check if the dock number is valid
    int i = Integer.valueOf(scan.nextLine());
    if (i >= 0 && i < 10 && dock1[i] == null){
        //Add ship to the dock
        dock1[i] = new Ship(name, size);
        System.out.println("Ship has been docked");
    }
    else{
        System.out.println("Couldn't dock");
    }

}

Aucun commentaire:

Enregistrer un commentaire