Vorlesung 12 / Aufgabe 1

This commit is contained in:
Sebastian Brosch 2024-05-01 23:38:01 +02:00
parent cea993fcd1
commit 7f8b2953fb
8 changed files with 233 additions and 0 deletions

View File

@ -0,0 +1,95 @@
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(".", "/");
}
}

View File

@ -0,0 +1,70 @@
package VL12.Aufgabe01;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
/**
* A field to store some information.
* @author Sebastian Brosch
*/
class StringFeld {
private String[] values;
StringFeld(String filePath) {
List<String> lines = getFileLines(filePath);
this.values = new String[lines.size()];
lines.toArray(this.values);
}
StringFeld(String filePath, String separator, int index) {
List<String> lines = getFileLines(filePath);
this.values = new String[lines.size()];
for(int i = 0; i < lines.size(); i++) {
this.values[i] = lines.get(i).split(separator)[index];
}
}
/**
* Method to get a value.
* @param index The index of the value.
* @return The value of the given index.
*/
public String get(int index) {
index = index % this.values.length;
return this.values[index];
}
/**
* Method to get a value.
* @param index The index of the value.
* @param maxLength The max length of the value.
* @return The value of the given index.
*/
public String get(int index, int maxLength) {
index = index % this.values.length;
if(this.values[index].length() > maxLength) {
return this.values[index].substring(0, maxLength);
} else {
return this.values[index];
}
}
/**
* Method to get all the lines of a file.
* @param filePath The path to the file to get the lines from.
* @return A list with all lines of the file.
*/
private List<String> getFileLines(String filePath) {
try {
return Files.readAllLines(Paths.get(filePath), StandardCharsets.ISO_8859_1);
} catch (IOException e) {
e.printStackTrace();
return List.of();
}
}
}

View File

@ -0,0 +1,12 @@
Meier
Kaiser
Hellmann
Fuchs
Hampel
Hinz
Schmidt
Andrack
Zerlett
Novincack
Ullmann
Mayer

View File

@ -0,0 +1,25 @@
72160 Horb
70179 Stuttgart
70191 Stuttgart
70192 Stuttgart
70193 Stuttgart
70195 Stuttgart
70197 Stuttgart
70199 Stuttgart
70327 Stuttgart
72070 Tübingen
72072 Tübingen
72074 Tübingen
72076 Tübingen
72181 Starzach
72182 Eutingen
72184 Eutingen
72185 Empfingen
72186 Empfingen
72186 Weiherhof
72187 Vöhringen
72189 Vöhringen
72202 Nagold
72210 Altensteig
72213 Altensteig
72218 Wildberg

View File

@ -0,0 +1,11 @@
Hauptstr.
Dorfstr.
Rosenweg
Nelkenweg
Stuttgarter Str.
Ulmer Str.
Freiburger Str.
Münchner Str.
Goetheweg
Beethovenallee
Schillerstr.

View File

@ -0,0 +1,10 @@
Frank
Steffi
Uschi
Carola
Susanne
Marion
Ingo
Stefan
Jochen
Holger

View File

@ -0,0 +1,5 @@
100005Meier Frank Hauptstr. 9374272160Horb 728
100006Kaiser Steffi Dorfstr. 1743970179Stuttgart 1914
100007Hellmann Uschi Rosenweg 1318270191Stuttgart 593
100008Fuchs Carola Nelkenweg 3436270192Stuttgart 891
100009Hampel Susanne Stuttgarter Str. 2115270193Stuttgart 926

View File

@ -0,0 +1,5 @@
100005#Meier#Frank#Hauptstr.#93742#72160#Horb#728
100006#Kaiser#Steffi#Dorfstr.#17439#70179#Stuttgart#1914
100007#Hellmann#Uschi#Rosenweg#13182#70191#Stuttgart#593
100008#Fuchs#Carola#Nelkenweg#34362#70192#Stuttgart#891
100009#Hampel#Susanne#Stuttgarter Str.#21152#70193#Stuttgart#926