Permalink
Browse files

unfinished homework

  • Loading branch information...
jkalitseva committed Jun 12, 2011
1 parent 23badde commit 0396656fceec0ab11e1c4583c960d0830b30ec82
@@ -0,0 +1,79 @@
package net.azib.java.students.t100224.homework;
import net.azib.java.students.t100224.homework.io.ResultsLoader;
import net.azib.java.students.t100224.homework.io.ResultsPrinter;
import net.azib.java.students.t100224.homework.model.Result;
import org.apache.log4j.xml.DOMConfigurator;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import static net.azib.java.students.t100224.homework.model.Decathlon.countPoints;
public class DecathlonCalculator {
List<Result> results;
ResultsLoader loader;
ResultsPrinter printer;
String inputParam;
String outputParam;
public DecathlonCalculator(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
for (int i = 0; i < args.length; i++) {
if (args[i].startsWith("-")) {
if (i == 0)
loader = (ResultsLoader) Class.forName(getClassPath(args[i]) + "Loader").newInstance();
else
printer = (ResultsPrinter) Class.forName(getClassPath(args[i]) + "Printer").newInstance();
} else {
if (i == 1) inputParam = args[i];
else
outputParam = args[i];
}
}
}
private String getClassPath(String arg) {
arg = arg.substring(1);
arg = arg.substring(0, 1).toUpperCase() + arg.substring(1).toLowerCase();
return DecathlonCalculator.class.getPackage().getName() + ".io." + arg;
}
private void process() {
results = loader.loadResults(inputParam);
countTotalScores();
sort();
printer.printResults(results, outputParam);
}
private void countTotalScores() {
for (Result result : results) {
int totalScore = 0;
for (Map.Entry<String, Float> map : result.getPerformances().entrySet()) {
totalScore += countPoints(map.getKey(), map.getValue());
}
result.setTotalScore(totalScore);
}
}
private void sort() {
Collections.sort(results);
}
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
if (args.length < 2) {
System.err
.println("Usage: <program> -<input-method> [input-parameters] -<output-method> [output-parameters]");
} else {
DOMConfigurator.configure(DecathlonCalculator.class.getResource("log4j.xml"));
DecathlonCalculator c = new DecathlonCalculator(args);
c.process();
}
}
}

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.
@@ -0,0 +1,40 @@
package net.azib.java.students.t100224.homework.io;
import net.azib.java.students.t100224.homework.model.Result;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import static net.azib.java.students.t100224.homework.io.CsvLoader.readLine;
public class ConsoleLoader implements ResultsLoader, ResultsPrinter {
private PrintStream out = System.out;
@Override
public List<Result> loadResults(String p) {
List<Result> results = new ArrayList<Result>();
out.println("Enter a line of text (type 'stop' to exit): ");
Scanner scanner = new Scanner(System.in);
String line;
while (scanner.hasNextLine()) {
line = scanner.nextLine();
if (!line.equalsIgnoreCase("stop"))
results.add(readLine(line));
else break;
}
scanner.close();
return results;
}
@Override
public void printResults(List<Result> results, String p) {
out.println("Here are the results: ");
for (Result result : results) {
//TODO
}
}
}
@@ -1,8 +1,7 @@
package net.azib.java.students.t100224.homework.io;
import net.azib.java.students.t100224.homework.interfaces.IResultsLoader;
import net.azib.java.students.t100224.homework.interfaces.IResultsPrinter;
import net.azib.java.students.t100224.homework.model.Athlete;
import net.azib.java.students.t100224.homework.model.Decathlon;
import net.azib.java.students.t100224.homework.model.Result;
import org.apache.log4j.Logger;
@@ -13,19 +12,13 @@
import java.util.*;
public class CsvIO implements IResultsLoader, IResultsPrinter {
public class CsvLoader implements ResultsLoader, ResultsPrinter {
private static final Logger LOG = Logger.getLogger("IO");
private static final DateFormat DATE_FORMAT = new SimpleDateFormat("dd.MM.yyyy");
private String filename;
public CsvIO(String filename) {
this.filename = filename;
}
@Override
public List<Result> loadResults() {
public List<Result> loadResults(String filename) {
List<Result> results = new ArrayList<Result>();
try {
Scanner scanner = new Scanner(new File(filename))
@@ -41,30 +34,15 @@ public CsvIO(String filename) {
}
@Override
public void printResults(List<Result> results) {
public void printResults(List<Result> results, String filename) {
Writer fileWriter;
PrintWriter out;
try {
fileWriter = new FileWriter(filename);
out = new PrintWriter(fileWriter);
for (Result result : results) {
out.printf("%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s \n",
results.indexOf(result) + 1,
result.getTotalScore(),
result.getAthlete().getName(),
result.getAthlete().getDateOfBirth(),
result.getAthlete().getCountry(),
result.getSprint100(),
result.getLongJump(),
result.getShotPut(),
result.getHighJump(),
result.getSprint400(),
result.getHurdles110(),
result.getDiscusThrow(),
result.getPoleVault(),
result.getJavelinThrow(),
result.getRace1500());
//TODO
}
out.close();
} catch (IOException e) {
@@ -75,38 +53,23 @@ public void printResults(List<Result> results) {
public static Result readLine(String line) {
Result result = new Result();
Athlete athlete = new Athlete();
Map<String, Float> performances = new HashMap<String, Float>();
// String TIME_FORMAT = "([0-9]{1,2}\\:)*[0-5][0-9]\\.[0-9][0-9]";
String time;
Scanner lineScanner = new Scanner(line).useDelimiter(", *");
lineScanner.useLocale(Locale.US); //used the "Locale.US" to make sure that the PERIOD is used as decimal separator.
athlete.setName(lineScanner.next());
try {
athlete.setName(lineScanner.next());
athlete.setDateOfBirth(DATE_FORMAT.parse(lineScanner.next()));
athlete.setCountry(lineScanner.next());
for (int i = 0; i < Decathlon.Event.values().length; i++) {
performances.put(Decathlon.Event.values()[i].name(), lineScanner.nextFloat());
}
} catch (ParseException e) {
LOG.error("Invalid date format");
}
athlete.setCountry(lineScanner.next());
try {
result.setSprint100(lineScanner.nextFloat());
result.setLongJump(lineScanner.nextFloat());
result.setShotPut(lineScanner.nextFloat());
result.setHighJump(lineScanner.nextFloat());
time = lineScanner.next();
result.setSprint400(convertToSeconds(time));
result.setHurdles110(lineScanner.nextFloat());
result.setDiscusThrow(lineScanner.nextFloat());
result.setPoleVault(lineScanner.nextFloat());
result.setJavelinThrow(lineScanner.nextFloat());
time = lineScanner.next();
result.setRace1500(convertToSeconds(time));
} catch (InputMismatchException e) {
e.printStackTrace();
}
result.setAthlete(athlete);
return result;
}
@@ -0,0 +1,50 @@
package net.azib.java.students.t100224.homework.io;
import net.azib.java.students.t100224.homework.DecathlonCalculator;
import org.apache.log4j.Logger;
import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class DBConnection {
private final Logger LOG = Logger.getLogger(getClass().getName());
private Connection connection;
public DBConnection() {
URL resUrl = DecathlonCalculator.class.getResource("db.properties");
Properties properties = new Properties();
try {
properties.load(resUrl.openStream());
} catch (IOException e) {
LOG.error(e.getMessage());
}
try {
String url = properties.getProperty("url");
String user = properties.getProperty("user");
String password = properties.getProperty("password");
connection = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
LOG.error(e.getMessage());
}
}
public Connection getConnection() {
return connection;
}
public void close() {
try {
connection.close();
} catch (SQLException e) {
LOG.error(e.getMessage());
}
}
}
Oops, something went wrong.

0 comments on commit 0396656

Please sign in to comment.