VL-Programmieren/VL12/Aufgabe01/Aufgabe01.java

107 lines
4.1 KiB
Java
Raw Normal View History

2024-05-01 21:38:01 +00:00
package VL12.Aufgabe01;
2024-05-06 11:13:58 +00:00
import java.io.File;
2024-05-01 21:38:01 +00:00
import java.io.FileWriter;
import java.util.Random;
/**
2024-05-08 10:37:30 +00:00
* Vorlesung 12 / Aufgabe 1
2024-05-06 20:19:27 +00:00
*
2024-05-01 21:38:01 +00:00
* @author Sebastian Brosch
*/
public class Aufgabe01 {
public static void main(String[] args) {
final String COLUMN_SEPARATOR = "#";
2024-05-06 09:24:00 +00:00
final int START_KUNDENNUMMER = 100000;
2024-05-06 20:19:27 +00:00
int numberOfDatasets = 0;
2024-05-01 21:38:01 +00:00
2024-05-06 09:24:00 +00:00
do {
try {
System.out.print("Anzahl der Datensätze: ");
2024-05-06 20:19:27 +00:00
numberOfDatasets = Integer.parseInt(System.console().readLine());
2024-05-01 21:38:01 +00:00
2024-05-06 09:24:00 +00:00
// only positive numbers are valid.
2024-05-06 20:19:27 +00:00
if (numberOfDatasets <= 0) {
2024-05-06 09:24:00 +00:00
System.out.println("Bitte geben Sie eine gültige Anzahl ein (min. 1)!");
}
2024-05-06 20:19:27 +00:00
} catch (NumberFormatException e) {
2024-05-06 09:24:00 +00:00
System.out.println("Es wurde keine gültige Anzahl Datensätze eingegeben!");
}
2024-05-06 20:19:27 +00:00
} while (numberOfDatasets <= 0);
2024-05-01 21:38:01 +00:00
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);
2024-05-06 11:13:58 +00:00
// 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();
2024-05-06 20:19:27 +00:00
if (null != parentFolder) {
2024-05-06 11:13:58 +00:00
parentFolder.mkdirs();
}
2024-05-01 21:38:01 +00:00
try {
2024-05-06 11:13:58 +00:00
FileWriter outputFixedWidth = new FileWriter(fileOutputFixedWidth);
FileWriter outputSeparator = new FileWriter(fileOutputSeparator);
2024-05-01 21:38:01 +00:00
2024-05-06 20:19:27 +00:00
for (int i = 0; i < numberOfDatasets; i++) {
2024-05-06 09:24:00 +00:00
int kundennummer = START_KUNDENNUMMER + i;
2024-05-01 21:38:01 +00:00
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();
2024-05-06 20:19:27 +00:00
} catch (Throwable e) {
;
}
2024-05-06 09:24:00 +00:00
2024-05-06 20:19:27 +00:00
System.out.println("Die Testdaten (" + numberOfDatasets + " Zeilen) wurden erzeugt.");
2024-05-01 21:38:01 +00:00
}
/**
* Method to determine a random number from a certain range.
2024-05-06 20:19:27 +00:00
*
2024-05-01 21:38:01 +00:00
* @param start The first number of the range.
2024-05-06 20:19:27 +00:00
* @param end The last number of the range.
2024-05-01 21:38:01 +00:00
* @return A random number from a certain range.
*/
private static int getRandomNumber(int start, int end) {
2024-05-06 20:19:27 +00:00
return (new Random()).nextInt(end - start) + start;
2024-05-01 21:38:01 +00:00
}
/**
* Method to get the relative path based on the package name.
2024-05-06 20:19:27 +00:00
*
2024-05-01 21:38:01 +00:00
* @return The relative path of the package based on the package name.
*/
private static String getPathFromPackage() {
return Aufgabe01.class.getPackageName().replace(".", "/");
}
}