0

The problem I have encountered is as follows: I have created two array representing docking spaces for ships. The first array ship object be saved in the array and if there is no space then it will be added to a waiting list array. But when I remove an object from the first array the object from the waiting list array is not removed and added.

The dock can accommodate three sizes of ship; cargo, container and super-container. The rows are made up of 5 small and 3 medium and 2 large. A cargo ship (small) can berth in any available space. A container ship (medium) can berth in the medium space and large, but not in small spaces. A super-container can only fit in large space.

So if I enter shipName3 and Super-Container for example and there is already two Super-Container's it adds to the waiting list but when I remove a Super-Container from the dock it does not remove a ship from the waiting list and add it to the dock Can you help? Here's my dock method:

import java.util.*;

public class Main {

static Scanner scan = new Scanner(System.in);
private static Ship[] dock1 = new Ship[10];
private static Ship[] waitingList = new Ship[10];

public static void main(String[] args) {
    menu();
}

public static void menu() {


    Scanner scan = new Scanner(System.in);


    while (true) {

        System.out.println("Choose an option: 1-3");
        System.out.println("1. Dock");
        System.out.println("2. Undock");
        System.out.println("3. Status");

        int menu = scan.nextInt();
        switch (menu) {
            case 1:
                System.out.println("1. Dock");
                dock();
                break;
            case 2:
                System.out.println("2. Undock");
                undock();
                break;
            case 3:
                System.out.println("3. Status");
                printDock();
                printWaitingList();
                break;
            case 4:
                System.out.println("4. Exit");
                System.exit(0);
            default:
                System.out.println("No such option");
                break;
        }
    }
}


public static void dock() {

    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:");
    //Check if the dock number is valid
    int i = Integer.valueOf(scan.nextLine());
    if (i >= 0 && i < 10 && dock1[i] == null) {
        int c = 0;
        int co = 0;
        int sco = 0;
        for (int j = 0; j < dock1.length; j++) {
            if (dock1[j] != null && dock1[j].getShipSize().equals("Cargo")) {
                c++;
            }
            if (dock1[j] != null && dock1[j].getShipSize().equals("Container")) {
                co++;
            }
            if (dock1[j] != null && dock1[j].getShipSize().equals("Super-Container")) {
                sco++;
            }
        }

        if (c < 10 && co < 5 && sco < 2) {
            //Add ship to the dock
            dock1[i] = new Ship(name, size);
            System.out.println("Enough space you can dock");
            System.out.println("Ship has been docked");
        } else {
            System.out.println("You cannot dock");
            waitingList(name,size);
        }

    } else {
        System.out.println("Couldn't dock");
        waitingList(name, size);
    }

}


public static void undock() { 
    System.out.println("Status of ships: ");
    printDock();
    System.out.println("Enter ship's name to undock: ");
    String name = scan.nextLine();
    System.out.println("Enter ship's size to undock: ");
    String size = scan.nextLine();
    for (int i = 0; i < dock1.length; i++) {
        if (dock1[i] != null && dock1[i].getShipName().equals(name)) {
            dock1[i] = null;
            System.out.println("Ship removed");
           //break;
            ///HERE CHECK IF SHIP IN DOCK
            for (int j = 0; j < dock1.length; j++) {
                if (dock1[j] == null) {
                    //Add ship to the dock
                    dock1[j] = new Ship(name, size);
                    System.out.println("Move ship from waiting list to dock 1");
                    break;
                } else {
                    System.out.println("No space in dock1");
                    return;
                }
            }
        } else {
            System.out.println("Ship not docked here");
            break;
        }

    }

}

public static void waitingList(String name, String size){
    System.out.println("Dock 1 is full, ship will try to be added to Waiting List");
    for (int i = 0; i < dock1.length; i++) { //waitingList?
        if (waitingList[i] == null) {
            //Add ship to the dock
            waitingList[i] = new Ship(name, size);
            System.out.println("Enough space added to waiting list");
            break;
        } else {
            System.out.println("No space on waiting list, ship turned away");
            return;
        }
    }

}

public static void printDock() {

    System.out.println("Docks:");

    for (int i = 0; i < dock1.length; i++) {
        if (dock1[i] == null) {
            System.out.println("Dock " + i + " is empty");
        } else {
            System.out.println("Dock " + i + ": " + dock1[i].getShipName() + " " + dock1[i].getShipSize());
        }
    }
}

private static void printWaitingList() {

    System.out.println("Waiting List:");

    for (int i = 0; i < waitingList.length; i++) {
        if (waitingList[i] == null) {
            System.out.println("Dock " + i + " is empty");
        } else {
            System.out.println("Dock " + i + ": " + waitingList[i].getShipName() + " " + waitingList[i].getShipSize());
        }
    }
}
}

Ship class

public class Ship {

private String shipName;
private  String shipSize;

public String getShipName() {
    return shipName;
}

public void setShipName(String shipName) {
    this.shipName = shipName;
}

public String getShipSize() {
    return shipSize;
}

public void setShipSize(String shipSize) {
    this.shipSize = shipSize;
}

public Ship(String shipName, String shipSize) {
    this.shipName = shipName;
    this.shipSize = shipSize;
}
}

I put my attempt in the undock method.

9
  • where is the error method name ? Commented Apr 2, 2018 at 13:44
  • The problem is in the undock class but there is no error message it just does not move the object Commented Apr 2, 2018 at 13:47
  • 1
    @S.smith94 when you will remove object from an array then you have to add elements from waiting list to array if objects are present in waiting list and if there is space in array? correct me if i am wrong Commented Apr 2, 2018 at 13:51
  • @S.smith94 at the same time you have to remove object from waiting list Commented Apr 2, 2018 at 13:52
  • yes I need to remove it from the waiting list array and add to dock1 array Commented Apr 2, 2018 at 13:57

2 Answers 2

1

Please check your undock method in that you are removing from dock1 array and again you are adding to same object to dock1 array but there is no code to remove object from waitingList array simply update your loop in undock method as mentioned below

for (int i = 0; i < dock1.length; i++) {
        if (dock1[i] != null && dock1[i].getShipName().equals(name)) {
            dock1[i] = null;
            System.out.println("Ship removed");
            // break;
            /// HERE CHECK IF SHIP IN DOCK
            for (int j = 0; j < waitingList.length; j++) {
                if (dock1[i] == null) {
                    // Add ship to the dock
                    dock1[i] = new Ship(waitingList[j].getShipName(), waitingList[j].getShipSize());
                    System.out.println("Move ship from waiting list to dock 1");
                    waitingList[j]=null;
                    break;
                } else {
                    System.out.println("No space in dock1");
                    return;
                }
            }
        } else {
            System.out.println("Ship not docked here");
            break;
        }

    }

I have changed inner loop iteration so it will loop to the size of waitingList array because we need to remove object from waitingList aray and add to dock1 array.Also i am runtime getting the shipname and shipsize from waitingList array so that it will add object from waitingList to dock1 array. I have tested code to my machine working here hope it will help you.

Sign up to request clarification or add additional context in comments.

1 Comment

@S.smith94 check elements in array
0

In your undock method, you remove the ship at the i'th position in dock1 array by setting it to null. This is ok but then you have a for loop that runs through all of the ships in dock1 looking for one with a null valued one. You aready know the one you just removed is null because you just set it to that value. Then you set it equal to a new ship that has the exact name and size of the one you removed (essentially putting the ship back). Instead, you want the for loop to traverse your list of waiting ships to find one that matches the space of the ship you just removed.

Replace:

for (int j = 0; j < dock1.length; j++) {
    if (dock1[j] == null) {
        //Add ship to the dock
        dock1[j] = new Ship(name, size);
        System.out.println("Move ship from waiting list to dock 1");
        break;
     } else {
         System.out.println("No space in dock1");
         return;
     }
 }

with

for (int j = 0; j < waitingList.length; j++) {
    if (waitingList[j].getShipSize() <= size) {
        //Add ship to the dock
        dock1[i] = waitingList[j];
        System.out.println("Move ship from waiting list to dock 1");
        return;
    }
}
System.out.println("No space in dock1");
return;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.