package VL12.Aufgabe01; import java.io.FileWriter; import java.util.Random; /** * Vorlesung 12 - Ein- und Ausgabe - Aufgabe 1 * @author Sebastian Brosch */ public class Aufgabe01 { public static void main(String[] args) { final String COLUMN_SEPARATOR = "#"; int cntDataSets = 0; int startKundennummer = 0; try { System.out.print("Anzahl der Datensätze: "); cntDataSets = Integer.parseInt(System.console().readLine()); } catch(NumberFormatException e) { System.out.println("Es wurde keine gültige Anzahl für Datensätze angegeben!"); return; } try { System.out.print("Startwert für Kundennummer: "); startKundennummer = Integer.parseInt(System.console().readLine()); } catch(NumberFormatException e) { System.out.println("Es wurde keine gültige Startnummer für die Kundennummer angegeben!"); return; } // the customer number have to be at least six digits. if(startKundennummer < 100000) { startKundennummer += 100000; } StringFeld namen = new StringFeld(getPathFromPackage() + "/input/Nachnamen.TXT"); StringFeld strassen = new StringFeld(getPathFromPackage() + "/input/Strassen.TXT"); StringFeld vornamen = new StringFeld(getPathFromPackage() + "/input/Vornamen.TXT"); StringFeld postleitzahlen = new StringFeld(getPathFromPackage() + "/input/PLZOrt.TXT", " ", 0); StringFeld orte = new StringFeld(getPathFromPackage() + "/input/PLZOrt.TXT", " ", 1); try { FileWriter outputFixedWidth = new FileWriter(getPathFromPackage() + "/output/output-fixed-width.txt"); FileWriter outputSeparator = new FileWriter(getPathFromPackage() + "/output/output-separator.txt"); for(int i = 0; i < cntDataSets; i++) { int kundennummer = startKundennummer + i; int hausnummer = getRandomNumber(1, 99999); int umsatz = getRandomNumber(500, 2000); outputFixedWidth.write(String.format("%-6d", kundennummer)); outputFixedWidth.write(String.format("%-20s", namen.get(i, 20))); outputFixedWidth.write(String.format("%-20s", vornamen.get(i, 20))); outputFixedWidth.write(String.format("%-20s", strassen.get(i, 20))); outputFixedWidth.write(String.format("%-5s", hausnummer)); outputFixedWidth.write(String.format("%-5s", postleitzahlen.get(i))); outputFixedWidth.write(String.format("%-20s", orte.get(i, 20))); outputFixedWidth.write(String.format("%-5s", umsatz)); outputFixedWidth.write("\n"); outputSeparator.write(String.format("%d" + COLUMN_SEPARATOR, kundennummer)); outputSeparator.write(String.format("%s" + COLUMN_SEPARATOR, namen.get(i))); outputSeparator.write(String.format("%s" + COLUMN_SEPARATOR, vornamen.get(i))); outputSeparator.write(String.format("%s" + COLUMN_SEPARATOR, strassen.get(i))); outputSeparator.write(String.format("%d" + COLUMN_SEPARATOR, hausnummer)); outputSeparator.write(String.format("%s" + COLUMN_SEPARATOR, postleitzahlen.get(i))); outputSeparator.write(String.format("%s" + COLUMN_SEPARATOR, orte.get(i))); outputSeparator.write(String.format("%d", umsatz)); outputSeparator.write("\n"); } outputFixedWidth.close(); outputSeparator.close(); } catch(Throwable e) {;} } /** * Method to determine a random number from a certain range. * @param start The first number of the range. * @param end The last number of the range. * @return A random number from a certain range. */ private static int getRandomNumber(int start, int end) { return (new Random()).nextInt(end-start) + start; } /** * Method to get the relative path based on the package name. * @return The relative path of the package based on the package name. */ private static String getPathFromPackage() { return Aufgabe01.class.getPackageName().replace(".", "/"); } }