style guide

This commit is contained in:
Sebastian Brosch 2024-05-06 22:19:27 +02:00
parent 6a87bb9fd3
commit 5447569e18
3 changed files with 40 additions and 27 deletions

View File

@ -6,28 +6,28 @@ import java.util.Random;
/** /**
* Vorlesung 12 - Ein- und Ausgabe - Aufgabe 1 * Vorlesung 12 - Ein- und Ausgabe - Aufgabe 1
*
* @author Sebastian Brosch * @author Sebastian Brosch
* @see https://gitea.hb.dhbw-stuttgart.de/sebastianbrosch/VL-Programmieren/src/branch/main/VL12/Aufgabe01
*/ */
public class Aufgabe01 { public class Aufgabe01 {
public static void main(String[] args) { public static void main(String[] args) {
final String COLUMN_SEPARATOR = "#"; final String COLUMN_SEPARATOR = "#";
final int START_KUNDENNUMMER = 100000; final int START_KUNDENNUMMER = 100000;
int cntDataSets = 0; int numberOfDatasets = 0;
do { do {
try { try {
System.out.print("Anzahl der Datensätze: "); System.out.print("Anzahl der Datensätze: ");
cntDataSets = Integer.parseInt(System.console().readLine()); numberOfDatasets = Integer.parseInt(System.console().readLine());
// only positive numbers are valid. // only positive numbers are valid.
if(cntDataSets <= 0) { if (numberOfDatasets <= 0) {
System.out.println("Bitte geben Sie eine gültige Anzahl ein (min. 1)!"); System.out.println("Bitte geben Sie eine gültige Anzahl ein (min. 1)!");
} }
} catch(NumberFormatException e) { } catch (NumberFormatException e) {
System.out.println("Es wurde keine gültige Anzahl Datensätze eingegeben!"); System.out.println("Es wurde keine gültige Anzahl Datensätze eingegeben!");
} }
} while(cntDataSets <= 0); } while (numberOfDatasets <= 0);
StringFeld namen = new StringFeld(getPathFromPackage() + "/input/Nachnamen.TXT"); StringFeld namen = new StringFeld(getPathFromPackage() + "/input/Nachnamen.TXT");
StringFeld strassen = new StringFeld(getPathFromPackage() + "/input/Strassen.TXT"); StringFeld strassen = new StringFeld(getPathFromPackage() + "/input/Strassen.TXT");
@ -41,7 +41,7 @@ public class Aufgabe01 {
final File fileOutputSeparator = new File(folderOutput + "output-separator.txt"); final File fileOutputSeparator = new File(folderOutput + "output-separator.txt");
final File parentFolder = fileOutputFixedWidth.getParentFile(); final File parentFolder = fileOutputFixedWidth.getParentFile();
if(null != parentFolder) { if (null != parentFolder) {
parentFolder.mkdirs(); parentFolder.mkdirs();
} }
@ -49,7 +49,7 @@ public class Aufgabe01 {
FileWriter outputFixedWidth = new FileWriter(fileOutputFixedWidth); FileWriter outputFixedWidth = new FileWriter(fileOutputFixedWidth);
FileWriter outputSeparator = new FileWriter(fileOutputSeparator); FileWriter outputSeparator = new FileWriter(fileOutputSeparator);
for(int i = 0; i < cntDataSets; i++) { for (int i = 0; i < numberOfDatasets; i++) {
int kundennummer = START_KUNDENNUMMER + i; int kundennummer = START_KUNDENNUMMER + i;
int hausnummer = getRandomNumber(1, 99999); int hausnummer = getRandomNumber(1, 99999);
int umsatz = getRandomNumber(500, 2000); int umsatz = getRandomNumber(500, 2000);
@ -77,23 +77,27 @@ public class Aufgabe01 {
outputFixedWidth.close(); outputFixedWidth.close();
outputSeparator.close(); outputSeparator.close();
} catch(Throwable e) {;} } catch (Throwable e) {
;
}
System.out.println("Die Testdaten (" + cntDataSets + " Zeilen) wurden erzeugt."); System.out.println("Die Testdaten (" + numberOfDatasets + " Zeilen) wurden erzeugt.");
} }
/** /**
* Method to determine a random number from a certain range. * Method to determine a random number from a certain range.
*
* @param start The first number of the range. * @param start The first number of the range.
* @param end The last number of the range. * @param end The last number of the range.
* @return A random number from a certain range. * @return A random number from a certain range.
*/ */
private static int getRandomNumber(int start, int end) { private static int getRandomNumber(int start, int end) {
return (new Random()).nextInt(end-start) + start; return (new Random()).nextInt(end - start) + start;
} }
/** /**
* Method to get the relative path based on the package name. * Method to get the relative path based on the package name.
*
* @return The relative path of the package based on the package name. * @return The relative path of the package based on the package name.
*/ */
private static String getPathFromPackage() { private static String getPathFromPackage() {

View File

@ -8,6 +8,7 @@ import java.util.List;
/** /**
* A field to store some information. * A field to store some information.
*
* @author Sebastian Brosch * @author Sebastian Brosch
*/ */
class StringFeld { class StringFeld {
@ -23,13 +24,14 @@ class StringFeld {
List<String> lines = getFileLines(filePath); List<String> lines = getFileLines(filePath);
this.values = new String[lines.size()]; this.values = new String[lines.size()];
for(int i = 0; i < lines.size(); i++) { for (int i = 0; i < lines.size(); i++) {
this.values[i] = lines.get(i).split(separator)[index]; this.values[i] = lines.get(i).split(separator)[index];
} }
} }
/** /**
* Method to get a value. * Method to get a value.
*
* @param index The index of the value. * @param index The index of the value.
* @return The value of the given index. * @return The value of the given index.
*/ */
@ -40,14 +42,15 @@ class StringFeld {
/** /**
* Method to get a value. * Method to get a value.
* @param index The index of the value. *
* @param index The index of the value.
* @param maxLength The max length of the value. * @param maxLength The max length of the value.
* @return The value of the given index. * @return The value of the given index.
*/ */
public String get(int index, int maxLength) { public String get(int index, int maxLength) {
index = index % this.values.length; index = index % this.values.length;
if(this.values[index].length() > maxLength) { if (this.values[index].length() > maxLength) {
return this.values[index].substring(0, maxLength); return this.values[index].substring(0, maxLength);
} else { } else {
return this.values[index]; return this.values[index];
@ -56,6 +59,7 @@ class StringFeld {
/** /**
* Method to get all the lines of a file. * Method to get all the lines of a file.
*
* @param filePath The path to the file to get the lines from. * @param filePath The path to the file to get the lines from.
* @return A list with all lines of the file. * @return A list with all lines of the file.
*/ */

View File

@ -8,41 +8,45 @@ import java.nio.file.Paths;
/** /**
* Vorlesung 12 - Ein- und Ausgabe - Aufgabe 2 * Vorlesung 12 - Ein- und Ausgabe - Aufgabe 2
*
* @author Sebastian Brosch * @author Sebastian Brosch
* @see https://gitea.hb.dhbw-stuttgart.de/sebastianbrosch/VL-Programmieren/src/branch/main/VL12/Aufgabe02
*/ */
public class Aufgabe02 { public class Aufgabe02 {
public static void main(String[] args) { public static void main(String[] args) {
try { try {
int words = 0; int numberOfWords = 0;
int numbers = 0; int numberOfNumbers = 0;
StringReader stringReader = new StringReader(getInputString(getPathFromPackage() + "/input/input.txt")); StringReader stringReader = new StringReader(getInputString(getPathFromPackage() + "/input/input.txt"));
StreamTokenizer streamTokenizer = new StreamTokenizer(stringReader); StreamTokenizer streamTokenizer = new StreamTokenizer(stringReader);
while(streamTokenizer.nextToken() != StreamTokenizer.TT_EOF) { while (streamTokenizer.nextToken() != StreamTokenizer.TT_EOF) {
switch(streamTokenizer.ttype) { switch (streamTokenizer.ttype) {
case StreamTokenizer.TT_WORD: case StreamTokenizer.TT_WORD:
words++; numberOfWords++;
System.out.printf("ZEICHENKETTE:\t%s\n", streamTokenizer.sval); System.out.printf("ZEICHENKETTE:\t%s\n", streamTokenizer.sval);
break; break;
case StreamTokenizer.TT_NUMBER: case StreamTokenizer.TT_NUMBER:
numbers++; numberOfNumbers++;
System.out.printf("ZAHL:\t\t%.0f\n", streamTokenizer.nval); System.out.printf("ZAHL:\t\t%.0f\n", streamTokenizer.nval);
break; break;
} }
} }
System.out.printf("Gelesene Zahlen: %d\n", numbers); System.out.printf("Gelesene Zahlen: %d\n", numberOfNumbers);
System.out.printf("Gelesene Zeichenketten: %d\n", words); System.out.printf("Gelesene Zeichenketten: %d\n", numberOfWords);
System.out.printf("Insgesamt gelesene Token: %d\n", (numbers + words)); System.out.printf("Insgesamt gelesene Token: %d\n", (numberOfNumbers + numberOfWords));
} catch (Throwable e) {;} } catch (Throwable e) {
;
}
} }
/** /**
* Method to get the content of a file as a string. * Method to get the content of a file as a string.
*
* @param inputPath The path of the file. * @param inputPath The path of the file.
* @return The content of the file or an empty string if the file is not available. * @return The content of the file or an empty string if the file is not
* available.
*/ */
private static String getInputString(String inputPath) { private static String getInputString(String inputPath) {
try { try {
@ -55,6 +59,7 @@ public class Aufgabe02 {
/** /**
* Method to get the relative path based on the package name. * Method to get the relative path based on the package name.
*
* @return The relative path of the package based on the package name. * @return The relative path of the package based on the package name.
*/ */
private static String getPathFromPackage() { private static String getPathFromPackage() {