-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from danieskj/transactions-view
Transactions view
- Loading branch information
Showing
22 changed files
with
567 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/main/java/edu/ntnu/idi/idatt/storage/util/TransactionAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package edu.ntnu.idi.idatt.storage.util; | ||
|
|
||
| import java.lang.reflect.Type; | ||
|
|
||
| import com.google.gson.JsonDeserializationContext; | ||
| import com.google.gson.JsonDeserializer; | ||
| import com.google.gson.JsonElement; | ||
| import com.google.gson.JsonObject; | ||
| import com.google.gson.JsonParseException; | ||
| import com.google.gson.JsonSerializationContext; | ||
| import com.google.gson.JsonSerializer; | ||
|
|
||
| import edu.ntnu.idi.idatt.model.transaction.Purchase; | ||
| import edu.ntnu.idi.idatt.model.transaction.Sale; | ||
| import edu.ntnu.idi.idatt.model.transaction.Transaction; | ||
|
|
||
| public class TransactionAdapter implements JsonSerializer<Transaction>, JsonDeserializer<Transaction> { | ||
|
|
||
| @Override | ||
| public JsonElement serialize(Transaction t, Type typeOfT, JsonSerializationContext context) { | ||
|
|
||
| JsonObject obj = context.serialize(t).getAsJsonObject(); | ||
|
|
||
| if (t instanceof Purchase) { | ||
| obj.addProperty("type", "purchase"); | ||
| } else if (t instanceof Sale) { | ||
| obj.addProperty("type", "sale"); | ||
| } | ||
|
|
||
| return obj; | ||
| } | ||
|
|
||
| @Override | ||
| public Transaction deserialize(JsonElement json, | ||
| Type typeOfT, | ||
| JsonDeserializationContext context) | ||
| throws JsonParseException { | ||
|
|
||
| JsonObject obj = json.getAsJsonObject(); | ||
|
|
||
| String type = obj.get("type").getAsString(); | ||
|
|
||
| switch (type) { | ||
| case "purchase": | ||
| return context.deserialize(obj, Purchase.class); | ||
|
|
||
| case "sale": | ||
| return context.deserialize(obj, Sale.class); | ||
|
|
||
| default: | ||
| throw new UnsupportedOperationException("Unknown type " + type); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } |
57 changes: 57 additions & 0 deletions
57
src/main/java/edu/ntnu/idi/idatt/storage/util/TransactionCalculatorAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package edu.ntnu.idi.idatt.storage.util; | ||
|
|
||
| import java.lang.reflect.Type; | ||
|
|
||
| import com.google.gson.JsonDeserializationContext; | ||
| import com.google.gson.JsonDeserializer; | ||
| import com.google.gson.JsonElement; | ||
| import com.google.gson.JsonObject; | ||
| import com.google.gson.JsonParseException; | ||
| import com.google.gson.JsonSerializationContext; | ||
| import com.google.gson.JsonSerializer; | ||
|
|
||
| import edu.ntnu.idi.idatt.service.transaction.PurchaseCalculator; | ||
| import edu.ntnu.idi.idatt.service.transaction.SaleCalculator; | ||
| import edu.ntnu.idi.idatt.service.transaction.TransactionCalculator; | ||
|
|
||
| public class TransactionCalculatorAdapter | ||
| implements JsonSerializer<TransactionCalculator>, JsonDeserializer<TransactionCalculator> { | ||
|
|
||
| @Override | ||
| public JsonElement serialize(TransactionCalculator calc, Type typeOfT, JsonSerializationContext context) { | ||
|
|
||
| JsonObject obj = context.serialize(calc).getAsJsonObject(); | ||
|
|
||
| if (calc instanceof PurchaseCalculator) { | ||
| obj.addProperty("type", "purchase"); | ||
| } else if (calc instanceof SaleCalculator) { | ||
| obj.addProperty("type", "sale"); | ||
| } | ||
|
|
||
| return obj; | ||
| } | ||
|
|
||
| @Override | ||
| public TransactionCalculator deserialize(JsonElement json, | ||
| Type typeOfT, | ||
| JsonDeserializationContext context) | ||
| throws JsonParseException { | ||
|
|
||
| JsonObject obj = json.getAsJsonObject(); | ||
|
|
||
| String type = obj.get("type").getAsString(); | ||
|
|
||
| switch (type) { | ||
| case "purchase": | ||
| return context.deserialize(obj, PurchaseCalculator.class); | ||
|
|
||
| case "sale": | ||
| return context.deserialize(obj, SaleCalculator.class); | ||
|
|
||
| default: | ||
| throw new UnsupportedOperationException("Unknown type " + type); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
src/main/java/edu/ntnu/idi/idatt/view/components/elements/TransactionComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| package edu.ntnu.idi.idatt.view.components.elements; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import edu.ntnu.idi.idatt.model.market.Stock; | ||
| import edu.ntnu.idi.idatt.model.transaction.Purchase; | ||
| import edu.ntnu.idi.idatt.model.transaction.Sale; | ||
| import edu.ntnu.idi.idatt.model.transaction.Transaction; | ||
| import edu.ntnu.idi.idatt.service.transaction.PurchaseCalculator; | ||
| import edu.ntnu.idi.idatt.service.transaction.SaleCalculator; | ||
| import edu.ntnu.idi.idatt.view.components.ui.UICompositor; | ||
| import edu.ntnu.idi.idatt.view.util.CssUtils; | ||
| import javafx.geometry.Insets; | ||
| import javafx.geometry.Pos; | ||
| import javafx.scene.control.Label; | ||
| import javafx.scene.layout.VBox; | ||
|
|
||
| public class TransactionComponent extends VBox { | ||
|
|
||
| public TransactionComponent(Transaction transaction) { | ||
|
|
||
| Stock stock = transaction.getShare().getStock(); | ||
|
|
||
| Label title; | ||
| Label name = new Label(stock.getCompany() + " (" + stock.getSymbol() + ")"); | ||
| Label totalValue; | ||
| Label taxComissionValue; | ||
| Label grossValue; | ||
| Label amountOfShares; | ||
| Label saleProfit; | ||
|
|
||
| if (transaction instanceof Purchase) { | ||
| title = new Label("PURCHASE"); | ||
| CssUtils.apply(title, CssUtils.GREEN); | ||
|
|
||
| PurchaseCalculator calculator = (PurchaseCalculator) transaction.getCalculator(); | ||
| totalValue = new Label("Total: " + calculator.calculateTotal() + " USD"); | ||
| taxComissionValue = new Label( | ||
| "Tax & Comission: " + calculator.calculateTax().add(calculator.calculateCommision()) + " USD"); | ||
| grossValue = new Label("Gross: " + calculator.calculateGross() + " USD"); | ||
| amountOfShares = new Label("Shares: " + transaction.getShare().getQuantity() + " [" + stock.getSymbol() + "]"); | ||
| saleProfit = new Label(); | ||
|
|
||
| } else if (transaction instanceof Sale) { | ||
| title = new Label("SALE"); | ||
| CssUtils.apply(title, CssUtils.RED); | ||
|
|
||
| SaleCalculator calculator = (SaleCalculator) transaction.getCalculator(); | ||
| totalValue = new Label("Total: " + calculator.calculateTotal() + " USD"); | ||
| taxComissionValue = new Label( | ||
| "Tax & Comission: " + calculator.calculateTax().add(calculator.calculateCommision()) + " USD"); | ||
| grossValue = new Label("Gross: " + calculator.calculateGross() + " USD"); | ||
| amountOfShares = new Label("Shares: " + transaction.getShare().getQuantity() + " [" + stock.getSymbol() + "]"); | ||
| saleProfit = new Label("Profit: " + calculator.calculateProfit() + " USD"); | ||
|
|
||
| CssUtils.apply(saleProfit, CssUtils.generateValueColors(calculator.calculateProfit().doubleValue())); | ||
| } | ||
|
|
||
| else { | ||
| System.out.println("Failed to initialize transactionComponent!"); | ||
| return; | ||
| } | ||
|
|
||
| this.setPrefSize(400, Double.MAX_VALUE); | ||
| this.setMinWidth(400); | ||
| this.setPadding(new Insets(40)); | ||
| this.setAlignment(Pos.TOP_CENTER); | ||
|
|
||
| // TODO: CHANGE AND IN STOCKComponent | ||
| this.setStyle("-fx-background-color: #404950;"); | ||
|
|
||
| CssUtils.apply(title, CssUtils.BIG_TEXT_32); | ||
| CssUtils.apply(name, CssUtils.BIG_TEXT_32); | ||
| List<Label> labels = List.of(totalValue, taxComissionValue, grossValue, amountOfShares, saleProfit); | ||
| labels.forEach(l -> CssUtils.apply(l, CssUtils.MED_TEXT_16)); | ||
|
|
||
| UICompositor transactionComponent = new UICompositor.Builder() | ||
| .parent(new VBox()) | ||
| .growWithAlignment(Pos.CENTER) | ||
| .addContent(title) | ||
| .addContent(name) | ||
| .filler() | ||
| .addContent(totalValue) | ||
| .addContent(taxComissionValue) | ||
| .addContent(grossValue) | ||
| .addContent(amountOfShares) | ||
| .addContent(saleProfit) | ||
| .build(); | ||
|
|
||
| this.getChildren().add(transactionComponent.makeUI()); | ||
|
|
||
| } | ||
|
|
||
| } |
Oops, something went wrong.