Frage

Grundsätzlich soll das Programm einen "runden Tisch" von Führungskräften erstellen, mit einem Vorsitzenden, der nicht geändert werden kann. Ich weiß fast fast, was ich tue, und ich bin ungefähr auf halbem Weg durch meine Methoden, um Führungskräfte einzuführen und zu entfernen, aber ich habe nur versucht, meinen Code zu testen, um zu sehen, wie es lief, und es wird Fehler, sobald ich die Vorsitzenden eingeben kann Information. Außerdem bin ich mir überhaupt nicht sicher, wie ich die REMEBYCORPORATION -Methode im Executivelist vornehmen würde. Ich bin mir fast sicher, dass die Methode fast alles falsch ist und ich nicht, wie ich einen Knoten in einer kreisförmigen doppelt verknüpften Liste wie dieser entfernen kann.

*Keine Notwendigkeit, mir bei den Druckmethoden zu helfen, ich bin einfach noch nicht zu ihnen gekommen.

tl; dr: 1) Warum stürzt es sofort ab? 2) Ich bin mir ziemlich sicher, dass meine Methode zur Entfernung byCorporation völlig falsch ist. Wenn ja, irgendwelche Vorschläge oder Hilfe, wie man es behebt?

Hier sind die beiden Klassen, mit denen ich Probleme habe, wenn Sie sehen möchten, dass die anderen mich wissen und ich werde sie posten, aber sie sind 99% Getter und Setter.

ERSTE KLASSE

public class ExecutiveList {

private ExecutiveNode chair;
ExecutiveNode cursor;

public ExecutiveList() {

}

public ExecutiveList (Executive chairperson) {

    chair.setExecutive(chairperson);
    chair.left = chair;
    chair.right = chair;
}

public void insertLeftOfChair(Executive exec) {

    ExecutiveNode newExec = new ExecutiveNode();
    newExec.setExecutive(exec);
    chair.setLeft(newExec);

}

public boolean insertRightOfExec (Executive exec, String target) {
    cursor = chair;
    ExecutiveNode newExec = new ExecutiveNode();

    do { cursor = cursor.getLeft();
    if (cursor.getExecutive().equals(exec)) {
        newExec.setExecutive(exec);
        cursor.getRight().setLeft(newExec);
        newExec.setLeft(cursor);
        newExec.setRight(cursor.getRight());
        cursor.setRight(newExec);
        return true;
    }
    else {
        return false;
    }
    }       while (cursor.getExecutive().getExecutiveName() != target);


}

public boolean insertLeftOfExec (Executive exec, String target) {
    cursor = chair;
    ExecutiveNode newExec = new ExecutiveNode();
    do { cursor = cursor.getLeft();
        if (cursor.getExecutive().equals(exec)) {
            newExec.setExecutive(exec);
            cursor.getLeft().setRight(newExec);
            newExec.setRight(cursor);
            newExec.setLeft(cursor.getRight());
            cursor.setLeft(newExec);

            return true;
        }
        else {
            return false;
        }
    }       while (cursor.getExecutive().getExecutiveName() != target);
}

public boolean removeTargetExec(String name) {
    if (chair.equals(name)) {
        return false;
    }
    else {
        return false;
    }
}

public int removeByCorporation(String corporation) {
    int removed = 0;
    cursor = chair;
    do {
        if (cursor.getExecutive().getCompanyName().equals(corporation)) {
            cursor.setExecutive(null);
            cursor.getLeft();
            removed = removed + 1;
        }
    } while (removed > 0);

    return removed;
}

public void printByCorporation(String corporation) {

}

public void printAllClockwise() {

}

public void printAllCounterClockwise() {

}
    }

ZWEITE KLASSE

import java.util.Scanner;

    public class MeetingManager {
public static void main(String[] args) {

    // scanner to read the users input
    Scanner input = new Scanner(System.in);

    // strings to pass information about the chairperson
    String chairpersonName;
    String chairpersonCompany;

    // strings to pass information about executives other
    // than the chairperson
    String execName;
    String execCompany;
    String target;

    // holds information on whether on not an operation
    // was successful and how many executives were removed
    // for the remove by corporation command.
    boolean success;
    int numRemoved = 0;

    // prompts the user for information about the chairperson
    // and sets it to an executive object name chairperson
    System.out.println("Enter the name of the chairperson: ");
    chairpersonName = input.next();
    if (chairpersonName.length() < 1) {
        System.out.println("Please enter a full name");
    }
    System.out.println("Enter the company of the chairperson: ");
    chairpersonCompany = input.next();
    if (chairpersonCompany.length() < 1) {
        System.out.println("Please enter a full name");
    }
    Executive chairperson = new Executive(chairpersonName, chairpersonCompany);

    // creates a new ExecutiveList object and passes information
    // about the chairperson
    ExecutiveList list = new ExecutiveList(chairperson);

    // for loop to repeatedly print the menu and take instructions
    // from the user until they choose to exit.
    for (int i = 1; i > 0; i++) {
        ShowMenu();
        String option = input.next();

        // error message for improper input
        if (option.length() > 3) {
            System.out.println("You can only enter one option");
        }

        // insert left of chairperson
        else if (option.toUpperCase().equals("ILC")) {
            System.out.println("Enter the executives name: ");
            execName = input.next();
            System.out.println("Enter the executives company: ");
            execCompany = input.next();
            Executive newGuy = new Executive(execName, execCompany);
            list.insertLeftOfChair(newGuy);
            System.out.println("Insertion successful.");
        }

        // insert left of executive
        else if (option.toUpperCase().equals("ILE")) {
            System.out.println("Enter the executives name: ");
            execName = input.next();
            System.out.println("Enter the executives company: ");
            execCompany = input.next();
            Executive newGuy = new Executive(execName, execCompany);
            System.out.println("Enter the name of the target executive: ");
            target = input.next();

            success = list.insertLeftOfExec(newGuy, target);
            if (success == true) {
                System.out.println("Insertion successful.");
            }
            else {
                System.out.println("The executive could not be inserted.");
            }
        }


        // insert right of executive
        else if (option.toUpperCase().equals("IRE")) {
            System.out.println("Enter the executives name: ");
            execName = input.next();
            System.out.println("Enter the executives company: ");
            execCompany = input.next();
            Executive newGuy = new Executive(execName, execCompany);
            System.out.println("Enter the name of the target executive: ");
            target = input.next();

            success = list.insertRightOfExec(newGuy, target);
            if (success) {
                System.out.println("Insertion successful.");
            }
            else {
                System.out.println("The executive could not be inserted.");
            }
        }

        // remove target executive
        else if (option.toUpperCase().equals("RTE")) {
            System.out.println("Enter the name of the executive to remove: ");
            execName = input.next();
            success = list.removeTargetExec(execName);

            if (execName.equals(chairpersonCompany))
                list.removeTargetExec(execName);
            if (success) {
                System.out.println(execName + " has been removed from the meeting.");
            }
            else {
                System.out.println(execName + " could not be found.");
            }
        }

        // remove by corporation
        else if (option.toUpperCase().equals("RBC")) {
            System.out.println("Enter the name of the corporation to remove: ");
            execCompany = input.next();
            numRemoved = list.removeByCorporation(execCompany);
            if (execCompany.equals(chairperson.getCompanyName())) {
                System.out.println("Invalid command: cannot remove all employees from the chairperson's corporation");
            }
            else if (numRemoved < 1) {
                System.out.println("That corporation could not be found and no executives were removed.");
            }
            else {
                System.out.println(numRemoved + " executive(s) from " + execCompany + " have been removed from the meeting.");
            }
        }

        // prints by corporation
        else if (option.toUpperCase().equals("PBC")) {
            System.out.println("Enter the name of a corporation to display: ");
            execCompany = input.next();
            list.printByCorporation(execCompany);
        }

        // prints all counter-clockwise
        else if (option.toUpperCase().equals("PCC")) {

            list.printAllCounterClockwise();
        }

        // prints all clockwise
        else if (option.toUpperCase().equals("PCL")) {

            list.printAllClockwise();
        }

        else if (option.toUpperCase().equals("EXT")) {
            System.out.println("Terminating program...");
            break;
        }

        // Error message
        else {
            System.out.println("Please select a valid option.");
        }

    }

}



// displays menu and prompts user for input
public static void ShowMenu() {
    System.out.println("\nILC) Insert an executive to the left of the chairperson\nILE) Insert an executive to the left of a given executive\nIRE) Insert an executive to the right of a given executive\nRTE) Remove Target Executive");
    System.out.println("RBC) Remove By Corporation\nPBC) Print By Corporation\nPCC) Print all in counter-clockwise order\nPCL) Print all in clockwise order\nEXT) Exit the program\n\nSelect a menu option: ");
}

}

Vielen Dank an alle, die irgendeine Art von Vorschlägen oder Ratschlägen oder tatsächliche Hilfe in irgendeiner Form oder Form geben. Ich weiß, dass die Leute wütend werden, wenn sie aus irgendeinem Grund Fragen zu Hausaufgaben sehen, weil sie glauben, dass der Schüler sie bittet, "ihre Hausaufgaben für mich zu machen", aber das ist nicht das, was ich tue. Ich möchte einfach einen Rat oder Tipps, ich bitte Sie nicht, nur die Lücken für mich auszufüllen und alles zu reparieren (nicht, dass ich dagegen dagegen wäre: P). Vielen Dank.

War es hilfreich?

Lösung

Bei der EntfernungsbyCorporation -Methode setzen Sie nur die Führungskraft auf NULL ein, aber wenn Sie dies als doppelt verknüpfte Liste betrachten, müssen Sie nicht die Referenzen des vorherigen und nächsten Exekutivs festlegen, damit die doppelt verknüpfte Liste nicht brechen.

Andere Tipps

Der Trick besteht darin, sicherzustellen, dass Sie bei jeder Operation das geänderte Element aktualisieren, und die beiden anderen, die es verweisen (ziemlich praktisch in einer doppelt verknüpften Liste, die beiden, auf die sich auch auf die beiden IT -Referenzen beziehen).

Überprüfen Sie jede Ihrer Methoden und stellen Sie sicher, dass Sie in jedem einzelnen 4 Felder pro Änderung aktualisieren - zwei im Subjekt und jeweils eine in den beiden, die aus dem Subjekt verknüpft sind.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top