VL-Programmieren/VL12/Aufgabe01/Aufgabe01.java

103 lines
4.2 KiB
Java

package VL12.Aufgabe01;
import java.io.File;
import java.io.FileWriter;
import java.util.Random;
/**
* Vorlesung 12 - Ein- und Ausgabe - Aufgabe 1
* @author Sebastian Brosch
* @see https://gitea.hb.dhbw-stuttgart.de/sebastianbrosch/VL-Programmieren/src/branch/main/VL12/Aufgabe01
*/
public class Aufgabe01 {
public static void main(String[] args) {
final String COLUMN_SEPARATOR = "#";
final int START_KUNDENNUMMER = 100000;
int cntDataSets = 0;
do {
try {
System.out.print("Anzahl der Datensätze: ");
cntDataSets = Integer.parseInt(System.console().readLine());
// only positive numbers are valid.
if(cntDataSets <= 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(cntDataSets <= 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 < cntDataSets; 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 (" + cntDataSets + " 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(".", "/");
}
}