3 September 2013

After hours fun exercise 01: IntegerAdder for people fancy using args from main(String[] args) method.

HONEST WARNINGS:
  • I have only 3 years of experience as professional with many things to learn left,so keep in mind that this source code and program are NOT masterpiece of software engineering
  • Do not learn programming style from me. If you want good programming styles. Download source code for Apache Commons.
  • My solution as simple as possible,not as best as possible.
  • Comment can contains lots of sarcasm.
  • I do this for fun , learning or practice purposes.
ABOUT EXAMPLE
This is a very simple example of using args from main(String[] args) method.

MISSION OBJECTIVE
  1. I wanted to learn how to use GitHub ;)
  2. (I have many experience with using args,but i want to reuse one of my code)

WHAT THIS EXAMPLE DO ?
* It will take arguments and each argument is a path to file.
* Each file can have any number of lines from 1 to N.
* Each line can contain only one integer.
* All of the numbers from all of the files should be added to the final result.
* The result is just one number and it will be displayed usingS System.out.println() method


If you don't copy and paste code,then simply download from program from GitHub
https://github.com/pastorcmentarny/java.examples.integeradder

For simple  cases of  command line attributes.It is enough.
If planning to crazy things  and perfom lots of magic then I suggest to use libraries like:
http://commons.apache.org/proper/commons-cli/

:)

RESOURCES:
Java's tutorial
http://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html
http://commons.apache.org/proper/commons-cli/


CODE

IntegerAdder class

package dms.pastor.integeradder;

/**
*
* @author: Pastor cmentarny
* #WWW: http://pastor.ovh.org
* #Github: https://github.com/pastorcmentarny
* #Google Play: https://play.google.com/store/apps/developer?id=Dominik+Symonowicz
* #LinkedIn: uk.linkedin.com/pub/dominik-symonowicz/5a/706/981/
* #Email: email can be found on my website
*
* This is an example of using args from main(String[] args) method.
*
*
* About program:
* It will take arguments and each argument is a path to file.
* Each file can have any number of lines from 1 to N.
* Each line can contain only one integer.
* All of the numbers from all of the files should be added to the final result.
* The result is just one number and it will be displayed usingS System.out.println() method
*/

public class IntegerAdder {
/**
* @param args the command line arguments. Each argument should be a valid path to file.
*/
public static void main(String[] args) {
int sum = 0;
Adder adder = new Adder();
if(args != null){
for (String integer: args) {
sum += adder.add(integer);
}
}else{
Utils.error();
}
System.out.println(String.valueOf(sum));
}
}
Adder class
package dms.pastor.integeradder;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

/**
*
* @author: Pastor cmentarny
* #WWW: http://pastor.ovh.org
* #Github: https://github.com/pastorcmentarny
* #Google Play: https://play.google.com/store/apps/developer?id=Dominik+Symonowicz
* #LinkedIn: uk.linkedin.com/pub/dominik-symonowicz/5a/706/981/
* #Email: email can be found on my website
*
* Adder is response for sum of numbers from file.
*/
public class Adder {
public int add(String path){
File inputFile = readFile(path);
FileInputStream fis;
InputStreamReader isr;
BufferedReader br;
int sum = 0;
try {
fis = new FileInputStream(inputFile);
isr = new InputStreamReader(fis, "UTF-8");
br = new BufferedReader(isr);

String strLine;
while ((strLine = br.readLine()) != null) {
sum += Integer.valueOf(strLine);
}

} catch (Exception e) {
Utils.error();
}
return sum;
}
public static File readFile(String filePath) {
if (filePath == null || filePath.equals("")) {
Utils.error();
}
try {
File file = new File(filePath);
if (file.exists() && file.canRead()) {
return file;
} else {
Utils.error();
}
} catch (Exception e) {
Utils.error();
}
return null;//Yes, I know is crap,but i have done this way for simplicity reasons.
}
}


Utils class

package dms.pastor.integeradder;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
/**
*
* @author: Pastor cmentarny
* #WWW: http://pastor.ovh.org
* #Github: https://github.com/pastorcmentarny
* #Google Play: https://play.google.com/store/apps/developer?id=Dominik+Symonowicz
* #LinkedIn: uk.linkedin.com/pub/dominik-symonowicz/5a/706/981/
* #Email: email can be found on my website
*
* Adder is response for sum of numbers from file.
*/
public class Adder {
public int add(String path){
File inputFile = readFile(path);
FileInputStream fis;
InputStreamReader isr;
BufferedReader br;
int sum = 0;
try {
fis = new FileInputStream(inputFile);
isr = new InputStreamReader(fis, "UTF-8");
br = new BufferedReader(isr);
String strLine;
while ((strLine = br.readLine()) != null) {
sum += Integer.valueOf(strLine);
}
} catch (Exception e) {
Utils.error();
}
return sum;
}
public static File readFile(String filePath) {
if (filePath == null || filePath.equals("")) {
Utils.error();
}
try {
File file = new File(filePath);
if (file.exists() && file.canRead()) {
return file;
} else {
Utils.error();
}
} catch (Exception e) {
Utils.error();
}
return null;//Yes, I know is crap,but i have done this way for simplicity reasons.
}
}

No comments:

Post a Comment