0

To keep things as simple as I can, I am trying to pass an ArrayList holding values used for a method in a separate class in my program. The issue is that whatever I tweak, the values held in retailerID and prodCat never seem to make it to the subscribe method in the other class as previously mentioned. Here is the snippet I am trying to overcome at the moment. Please ingore the commented out portions as those were previous attempts I've yet to get rid of.

public class SupplyDemand {

Producer nProducer = new Producer();
Retailer nRetailer = new Retailer();
Broker nBroker = new Broker();


ArrayList<String> output = new ArrayList<String>();
ArrayList<String> inputCommand = new ArrayList<String>();

/**
 * Class constructor - you may set up any needed objects and data here. Specification of constructor is optional - it is acceptable if you leave it blank.
 */
public SupplyDemand() {



}

/**
 * This method accepts a single command and carry out the instruction given. You do not need to (and probably shouldn't) do everything in this method - delegate responsibilities to other classes.
 */
public void processInput(String command) {
    //command.toLowerCase();
    //String str[] = command.split(",");
    inputCommand.add(command.toLowerCase());
    //inputCommand = Arrays.asList(str);

    if(inputCommand.contains("subscribe")){
        String retailerID = inputCommand.get(1);
        String prodCat = inputCommand.get(2);

        nRetailer.subscribe(retailerID, prodCat);
    }
    else if(inputCommand.contains("unsubscribe")){
        String retailerID = inputCommand.get(1);
        String prodCat = inputCommand.get(2);

        nRetailer.unsubscribe(retailerID, prodCat);
    }
    else if(inputCommand.contains("publish")){
        String producerID = inputCommand.get(1);
        String prodCat = inputCommand.get(2);
        String brand = inputCommand.get(3);

        nProducer.publish(brand, prodCat, producerID);
        nBroker.checkMatch();

    }
    else{

    }

    //nBroker.checkMatch();


}

/**
 * After each round of execution, this method would be called to fetch all output lines, if there are any. The lines must be ordered by the time they are received.
 */
public ArrayList<String> getAggregatedOutput() {
    output = nRetailer.subscribeList;

    return output;

}

/**
 * Finally, this method would be called to clear all saved information in the system, so that information from previous round would not be carried to next round. After calling this method the system should be effectively starting anew.
 */
public void reset() {
    nRetailer.subscribeList.clear();
    nProducer.producerList.clear();
    nBroker.buildMatch.clear();

}

}

Here is the Retailer class with the subscribe method and how I need to use the needed strings if its any help.

public class Retailer implements ISubscriber {

public String retailerID = " ";

public ArrayList<String> subscribeList = new ArrayList<String>();

/*/**
 * @see SupplyDemand.ISubscriber#subscribe(String)
 */
public void subscribe(String retailerID, String prodCat) {
    subscribeList.add("TEST1");
    if(retailerID.equals(" ")){
        subscribeList.add(retailerID);
        subscribeList.add(prodCat);

    }
    else{
        subscribeList.add("TEST2");
    }
}


/*/**
 * @see SupplyDemand.ISubscriber#unsubscribe(String)
 */
public void unsubscribe(String retailerID, String prodCat) {
    int removeRetailer = subscribeList.indexOf(retailerID);
    int removeProdCat = subscribeList.indexOf(removeRetailer + 1);
    subscribeList.remove(removeProdCat);
    subscribeList.remove(removeRetailer);
}

}

sadboi
  • 1
  • 1
  • What is the defintion of ```nRetailer``` ? – Olivier Depriester Apr 29 '20 at 20:03
  • Sorry, nRetailer is a constructor for my Retailer class containing the subscribe method. – sadboi Apr 29 '20 at 20:04
  • Actually the problem must be in the use of ```command.toLowerCase();```. It returns a lowered case string but does not change ```command```. So ```inputCommand``` does likely not contain ```"subscribe"``` – Olivier Depriester Apr 29 '20 at 20:10
  • Olivier is correct. Use `inputCommand.add(command.toLowerCase());` – Modus Tollens Apr 29 '20 at 20:13
  • I tried that in a previous attempt with the same result unfortunately. I have tested to see if inputCommand was receiving the right strings, and it actually is - a string beginning with "subscribe" followed by the other information I need to pull from the string – sadboi Apr 29 '20 at 20:17
  • @sadboi It's best to post a complete example in this case. Otherwise, all we can do now is guess. Please [edit] your question and provide a small example including input values. – Modus Tollens Apr 29 '20 at 20:20
  • I just updated my post with addition context. However, input is unknown to me as am running it against automated test cases. When printing out inputCommand instead of subscribeList though, the string appears as follows: [subscribe, dole, banana] - the last two of which I am unable to pass into my subscribe method – sadboi Apr 29 '20 at 20:22
  • From your new code: never compare Strings without using equals! `retailerID != " "` see https://stackoverflow.com/q/513832/1288408 – Modus Tollens Apr 29 '20 at 20:24
  • Again, please edit the question, put the full example code including the classes. It's very hard to guess here. – Modus Tollens Apr 29 '20 at 20:27
  • My apologies, everything should be viewable now – sadboi Apr 29 '20 at 20:31
  • Now you replaced retailerid!=" " from the previous code snippet with equals... It 's not the same, should be not equals. Sorry, out of time now, have to leave... – Modus Tollens Apr 29 '20 at 20:34

0 Answers0