0

I have an inventory management system that reads in .txt files of Items and Stores and a bridge entity called Stocks and outputs a .txt file which displays the info based on those three files.

Here it is at the bottom.

public class Ims {

    private static Logger LOG = Logger.getLogger(Ims.class);

    public static void main(String[] args) throws Exception {
        PropertyConfigurator.configure("log.properties");

        LOG.debug("main()");

        File itemsFile = new File("items.txt");
        File storesFile = new File("stores.txt");
        File stockFile = new File("stocks.txt");

        if (!itemsFile.exists()) {
            LOG.error("Required 'items.txt' is missing");
        } else if (!storesFile.exists()) {
            LOG.error("Required 'stores.txt' is missing");
        }

        new Ims(itemsFile, storesFile, stockFile);
    }

    public Ims(File itemsFile, File storesFile, File stockFile) {
        LOG.debug("Ims()");
        HashMap<String, Item> items = null;
        HashMap<String, Store> stores = null;
        List<Stock> stocks = null;

        try {
            items = InventoryReader.read(itemsFile);
            stores = StoresReader.read(storesFile);
            stocks = StockReader.read(stockFile);
        } catch (ApplicationException e) {
            LOG.error(e.getMessage());
            return;
        }

        // Collections.sort(items, new CompareByPrice()); <-- this should sort it. How do I do this as a command line argument?

        File inventory = new File("inventory.txt");
        PrintStream out = null;
        try {
            out = new PrintStream(new FileOutputStream(inventory));
            InventoryReport.write(items, stores, stocks, out);
        } catch (FileNotFoundException e) {
            LOG.error(e.getMessage());
        }
    }
}

I want to be able to sort the read in arguments a number of ways using command line arguments.

For example:

java –jar Ims.jar by_value desc total

How would I do this?

lorenzoid
  • 1,812
  • 10
  • 33
  • 51

2 Answers2

5

The command line parameters that you put in the java invocation appear in the args parameter of the Main method.

So, you would have

 args[0] = "by_value"
 args[1] = "desc"
 args[2] = "total"

UPDATE: If your command line is complicated (flags, parameters in any order/missing), there is an Apache CLI (command line interface) library that helps to deal with it.

SJuan76
  • 24,532
  • 6
  • 47
  • 87
5

What are you having trouble with? Reading the actual command line arguments? You can do that using the args[] array, and then just put a switch in for all the different command line arguments you allow that do whatever manipulation you want to do to the sorting.

The args[] array is built into a number of languages (including java) and allows you to easily access the arguments that are passed in when you call something via the command line. For instance, I believe in your example, you could read in 'by_value' by saying args[0], desc by args[1], and total by args[2], etc.

So to clarify what I was saying in the comments below, youll eventually want to have something like this:

if (args.length > 0)
{
  for (int i=0; i<args.length;i++)
  {
     switch(args[i])
     {
        case <whatever your keyword is>: code for this keyword here
                                      break;
        case <next keyword>: code for next keyword
                             break;
     }
  }
}

etc.

Sorry for any oddities in formatting and stuff, I havent used Java in a while, but this should get you going.

One note, if this is your first time using a switch, remember that you always have to have a default. This will typically be an 'invalid input' message of some sort, like you can see in that example in the javadocs.

Josh Bibb
  • 477
  • 4
  • 14
  • Not really reading the command line args, it's more how to get those args to do stuff. Like by_value should sort it by value, descending, etc... How would I map those strings to functions that do what they're supposed to do? – lorenzoid Nov 05 '12 at 19:00
  • make a switch, and do something like "if args[].length > 0" first to make sure there are arguments, then loop through them and have your switch set up to check the value, then go and find which case that arg is and do what you want to do for that value. So in your case do a switch on whatever the argument you pass in is (by reading it from the array slot) Here are the javadocs on switch statements: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html – Josh Bibb Nov 05 '12 at 19:06
  • This was totally awesome. Thank you kind sir. – lorenzoid Nov 05 '12 at 19:23
  • You may be interested in using [JCommander](http://jcommander.org). This API makes short and easy work of processing command line arguments. You could have a `TreeSet` property that would sort the elements you are after for you. It's also neat because it handles the command line help text automatically. – Brett Ryan Nov 15 '12 at 15:42