VL-Programmieren/VL12/Aufgabe01/Aufgabe01.java

107 lines
4.1 KiB
Java

package VL12.Aufgabe01;
import java.io.File;
import java.io.FileWriter;
import java.util.Random;
/**
* Vorlesung 12 / Aufgabe 1
*
* @author Sebastian Brosch
*/
public class Aufgabe01 {
public static void main(String[] args) {
final String COLUMN_SEPARATOR = "#";
final int START_KUNDENNUMMER = 100000;
int numberOfDatasets = 0;
do {
try {
System.out.print("Anzahl der Datensätze: ");
numberOfDatasets = Integer.parseInt(System.console().readLine());
// only positive numbers are valid.
if (numberOfDatasets <= 0) {
System.out.println("Bitte geben Sie eine gültige Anzahl ein (min. 1)!");
}
} catch (NumberFormatException e) {
System.out.println("Es wurde keine gültige Anzahl Datensätze eingegeben!");
}
} while (numberOfDatasets <= 0);
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);
// create missing output folder.
final String folderOutput = getPathFromPackage() + "/output/";
final File fileOutputFixedWidth = new File(folderOutput + "output-fixed-width.txt");
final File fileOutputSeparator = new File(folderOutput + "output-separator.txt");
final File parentFolder = fileOutputFixedWidth.getParentFile();
if (null != parentFolder) {
parentFolder.mkdirs();
}
try {
FileWriter outputFixedWidth = new FileWriter(fileOutputFixedWidth);
FileWriter outputSeparator = new FileWriter(fileOutputSeparator);
for (int i = 0; i < numberOfDatasets; i++) {
int kundennummer = START_KUNDENNUMMER + 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) {
;
}
System.out.println("Die Testdaten (" + numberOfDatasets + " Zeilen) wurden erzeugt.");
}
/**
* 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(".", "/");
}
}