简体   繁体   中英

Java program for package installer

May you please help in writing a java program which should accept an array of strings defining dependencies. Each string contains the name of a package followed by a colon and space, then any dependencies required by that package. For simplicity we'll assume a package can have at most one dependency. The program should output a comma separated list of package names in the order of install, such that a package's dependency will always precede that package. The program should reject as invalid a dependency specification that contains cycles.

Example of valid input KittenService: Leetmeme: Cyberportal Cyberportal: Ice CamelCaser: KittenService Fraudstream: Leetmeme Ice:

A valid output for the above would be:

KittenService, Ice, Cyberportal, Leetmeme, CamelCaser, Fraudstream

Example of input that should be rejected (contains cycles) KittenService: Leetmeme: Cyberportal Cyberportal: Ice CamelCaser: KittenService Fraudstream: Ice: Leetmeme

First of all: SO is not a coding-service. Show us what you've got and where the problem lies and you'll get help with it. Now for the solution - I won't code anything and I hope noone else does. Since the dependencies don't contain any cycles, the structure is a tree. Just traverse the tree postorder and import each package in this order.

Get Package List from input String Array:

public static List<String> getPackages(String[] intputStrings,
        String inputDelimiter, char wordEndChar, String rejectWord,
        String outputDelimiter) {

    List<String> al = new ArrayList<String>();

    for (String inputString : intputStrings) {

        if (!(inputString.indexOf(rejectWord) > 0)) {

            for (String string : inputString.split(inputDelimiter)) {
                int idx = string.indexOf(wordEndChar);
                if (idx > 1)
                    al.add(string.substring(0, idx));
            }
        }
    }

    return al;
}

Invocation Part (Input and output) :

public static void main(String[] args) {

String intputString[] = {
        "KittenService: Leetmeme: Cyberportal Cyberportal: Ice CamelCaser: KittenService Fraudstream: Ice: Leetmeme",
        "(contains cycles) KittenService: Leetmeme: Cyberportal Cyberportal: Ice CamelCaser: KittenService Fraudstream: Ice: Leetmeme",
        "One: Two: x Three: Ice CamelCaser: KittenService Fraudstream: Five: Leetmeme" };

List<String> s = getPackages(intputString, " ", ':', " cycles", ", ");

for (String string : s)
    System.out.print(string + " ,");        
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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