VL-Programmieren/VL12/Aufgabe01/Aufgabe01.java

103 lines
4.2 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;
/**
* Vorlesung 12 - Ein- und Ausgabe - Aufgabe 1
* @author Sebastian Brosch
2024-05-06 11:13:58 +00:00
* @see https://gitea.hb.dhbw-stuttgart.de/sebastianbrosch/VL-Programmieren/src/branch/main/VL12/Aufgabe01
2024-05-01 21:38:01 +00:00
*/
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-01 21:38:01 +00:00
int cntDataSets = 0;
2024-05-06 09:24:00 +00:00
do {
try {
System.out.print("Anzahl der Datensätze: ");
cntDataSets = 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.
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);
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();
if(null != parentFolder) {
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
for(int i = 0; i < cntDataSets; 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();
} catch(Throwable e) {;}
2024-05-06 09:24:00 +00:00
2024-05-06 11:13:58 +00:00
System.out.println("Die Testdaten (" + cntDataSets + " Zeilen) wurden erzeugt.");
2024-05-01 21:38:01 +00:00
}
/**
* 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(".", "/");
}
}