Optimized Exception handling for De-/Serialization in PrivateBank.java
This commit is contained in:
@@ -367,7 +367,7 @@ public class PrivateBank implements Bank, JsonSerializer<Transaction>, JsonDeser
|
|||||||
*
|
*
|
||||||
* @throws IOException if an error occurs while deserializing
|
* @throws IOException if an error occurs while deserializing
|
||||||
*/
|
*/
|
||||||
public void readAccounts() throws IOException, java.io.IOException, AccountAlreadyExistsException, TransactionAlreadyExistException, TransactionAttributeException {
|
public void readAccounts() throws IOException, AccountAlreadyExistsException, TransactionAlreadyExistException, TransactionAttributeException {
|
||||||
/*
|
/*
|
||||||
1. JSONs einlesen
|
1. JSONs einlesen
|
||||||
2. for each Transaction
|
2. for each Transaction
|
||||||
@@ -375,23 +375,30 @@ public class PrivateBank implements Bank, JsonSerializer<Transaction>, JsonDeser
|
|||||||
2. Objekte in einer Liste zusammenführen
|
2. Objekte in einer Liste zusammenführen
|
||||||
3. Konto mit einer Liste von Transaktionen erzeugen
|
3. Konto mit einer Liste von Transaktionen erzeugen
|
||||||
*/
|
*/
|
||||||
List<Transaction> transactions = new ArrayList<>();
|
|
||||||
|
|
||||||
File directory = new File(directoryName);
|
File directory = new File(directoryName);
|
||||||
File[] files = directory.listFiles();
|
File[] files = directory.listFiles();
|
||||||
|
|
||||||
if (files != null) {
|
if (files != null) {
|
||||||
// jedes file ist ein Konto
|
// jedes file ist ein Konto
|
||||||
for (File file : files) {
|
for (File file : files) {
|
||||||
|
List<Transaction> transactions;
|
||||||
String accountName = file.getName().substring(0, file.getName().lastIndexOf("."));
|
String accountName = file.getName().substring(0, file.getName().lastIndexOf("."));
|
||||||
|
String jsonContent;
|
||||||
|
|
||||||
|
try {
|
||||||
|
jsonContent = Files.readString(Paths.get(directoryName + "/" + file.getName()));
|
||||||
|
} catch (java.io.IOException e) {
|
||||||
|
System.out.println("Zum Account: " + accountName + " konnte die Datei nicht geöffnet werden");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
String jsonContent = Files.readString(Paths.get(directoryName + "/" + file.getName()));
|
|
||||||
JsonElement accountData = JsonParser.parseString(jsonContent);
|
JsonElement accountData = JsonParser.parseString(jsonContent);
|
||||||
|
|
||||||
for (JsonElement account : accountData.getAsJsonArray()) {
|
try{
|
||||||
JsonObject accountObject = account.getAsJsonObject();
|
transactions = readAccountHelper(accountData, accountName);
|
||||||
Transaction t = deserialize(accountObject, null, null);
|
} catch (IOException e){
|
||||||
transactions.add(t);
|
System.out.println(e);
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
createAccount(accountName, transactions);
|
createAccount(accountName, transactions);
|
||||||
@@ -399,6 +406,21 @@ public class PrivateBank implements Bank, JsonSerializer<Transaction>, JsonDeser
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private List<Transaction> readAccountHelper(JsonElement accountData, String accountName) throws IOException {
|
||||||
|
List<Transaction> transactions = new ArrayList<>();
|
||||||
|
for (JsonElement account : accountData.getAsJsonArray()) {
|
||||||
|
JsonObject accountObject = account.getAsJsonObject();
|
||||||
|
Transaction t;
|
||||||
|
try{
|
||||||
|
t = deserialize(accountObject, null, null);
|
||||||
|
} catch (JsonParseException e){
|
||||||
|
throw new IOException("Fehler beim Laden des Accounts: " + accountName);
|
||||||
|
}
|
||||||
|
transactions.add(t);
|
||||||
|
}
|
||||||
|
return transactions;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Export Account to JSON
|
* Export Account to JSON
|
||||||
*
|
*
|
||||||
@@ -415,7 +437,11 @@ public class PrivateBank implements Bank, JsonSerializer<Transaction>, JsonDeser
|
|||||||
List<Transaction> transactions = this.accountsToTransactions.get(account);
|
List<Transaction> transactions = this.accountsToTransactions.get(account);
|
||||||
|
|
||||||
for (Transaction transaction : transactions)
|
for (Transaction transaction : transactions)
|
||||||
accountData.add(serialize(transaction, null, null));
|
try{
|
||||||
|
accountData.add(serialize(transaction, null, null));
|
||||||
|
} catch (JsonParseException e){
|
||||||
|
throw new IOException("Fehler beim Speichern des Accounts: " + account);
|
||||||
|
}
|
||||||
|
|
||||||
Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||||
String filename = directoryName + "/" + account + ".json";
|
String filename = directoryName + "/" + account + ".json";
|
||||||
@@ -458,8 +484,9 @@ public class PrivateBank implements Bank, JsonSerializer<Transaction>, JsonDeser
|
|||||||
case "Payment":
|
case "Payment":
|
||||||
return new Payment(date, amount, description, transactionInstance.get("incomingInterest").getAsDouble(), transactionInstance.get("outgoingInterest").getAsDouble());
|
return new Payment(date, amount, description, transactionInstance.get("incomingInterest").getAsDouble(), transactionInstance.get("outgoingInterest").getAsDouble());
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw new JsonParseException("Unknown transaction type: " + transactionType);
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -482,7 +509,8 @@ public class PrivateBank implements Bank, JsonSerializer<Transaction>, JsonDeser
|
|||||||
|
|
||||||
else if (transaction instanceof Payment)
|
else if (transaction instanceof Payment)
|
||||||
rootObject.addProperty("CLASSNAME", "Payment");
|
rootObject.addProperty("CLASSNAME", "Payment");
|
||||||
|
else
|
||||||
|
throw new JsonParseException("Unknown transaction type: " + transaction.getClass().getName());
|
||||||
|
|
||||||
JsonObject jsonObject = new JsonObject();
|
JsonObject jsonObject = new JsonObject();
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package bank.exceptions;
|
package bank.exceptions;
|
||||||
|
|
||||||
public class IOException extends Exception {
|
public class IOException extends Exception {
|
||||||
public IOException() {
|
public IOException(String message) {
|
||||||
super("Fehler in der De-/ Serialisierung der Daten");
|
super("Fehler in der De-/ Serialisierung der Daten. " + message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user