Skip to content

Commit

Permalink
Merge pull request #131 from einaskoi/per/mailapp
Browse files Browse the repository at this point in the history
Per/mailapp
  • Loading branch information
peretr authored May 18, 2026
2 parents 2b1e43c + 2821247 commit db2b77a
Show file tree
Hide file tree
Showing 15 changed files with 282 additions and 161 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import edu.ntnu.idi.idatt2003.gruppe42.Model.Transaction.Transaction;
import edu.ntnu.idi.idatt2003.gruppe42.View.Apps.StockApp;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
Expand All @@ -25,6 +28,13 @@ public final class StockAppController implements AppController {
private final Player player;
private Stock currentStock;

private enum SortType {
ALPHA, GAINER, PRICE
}

private SortType currentSort = SortType.ALPHA;
private boolean isDescending = false;

/** True while the game is on Saturday or Sunday. */
private boolean isWeekend = false;

Expand All @@ -43,8 +53,7 @@ public StockAppController(
this.marketController = marketController;
this.player = player;

stockApp.getStockList().getItems()
.addAll(marketController.getExchange().getAllStocks());
updateStockList();

stockApp.getStockList().setCellFactory(lv -> createStockCell());

Expand All @@ -53,10 +62,39 @@ public StockAppController(
if (currentStock != null && !newValue.isEmpty()) {
navigateToSearch();
}
stockApp.getStockList().getItems()
.setAll(marketController.getMarket(newValue));
updateStockList();
});

stockApp.getAlphaButton().setOnAction(e -> {
if (currentSort == SortType.ALPHA) {
isDescending = !isDescending;
} else {
currentSort = SortType.ALPHA;
isDescending = false;
}
updateStockList();
});

stockApp.getGainerButton().setOnAction(e -> {
if (currentSort == SortType.GAINER) {
isDescending = !isDescending;
} else {
currentSort = SortType.GAINER;
isDescending = false;
}
updateStockList();
});

stockApp.getPriceButton().setOnAction(e -> {
if (currentSort == SortType.PRICE) {
isDescending = !isDescending;
} else {
currentSort = SortType.PRICE;
isDescending = true;
}
updateStockList();
});

stockApp.getQuantitySpinner().valueProperty().addListener(
(obs, old, newValue) -> {
if (currentStock != null) {
Expand Down Expand Up @@ -220,7 +258,7 @@ private void handleTransaction() {

BigDecimal quantity = new BigDecimal(Math.abs(value));
if (value > 0) {
try{
try {
handleBuy(currentStock, quantity);
} catch (Exception e) {
onInsufficientFunds.run();
Expand All @@ -237,6 +275,7 @@ private void handleBuy(final Stock stock, final BigDecimal quantity) throws Exce
if (transaction != null) {
transaction.commit(player);
player.getPortfolio().addShare(transaction.getShare());
player.updateStatus();
}
}

Expand All @@ -259,6 +298,7 @@ private void handleSell(final Stock stock, final BigDecimal quantity) {
System.out.println("Transaction failed");
}
player.getPortfolio().removeShare(transaction.getShare());
player.updateStatus();
}
});
}
Expand All @@ -276,6 +316,28 @@ private void updateReceipt() {
}
}

/** Updates the stock list with current sort and filter. */
private void updateStockList() {
List<Stock> stocks = new ArrayList<>(marketController.getMarket(stockApp.getSearchField().getText()));
Comparator<Stock> comparator;
switch (currentSort) {
case GAINER:
comparator = Comparator.comparing(Stock::getLatestPriceChange);
break;
case PRICE:
comparator = Comparator.comparing(Stock::getSalesPrice);
break;
default:
comparator = Comparator.comparing(Stock::getSymbol);
break;
}
if (isDescending) {
comparator = comparator.reversed();
}
stocks.sort(comparator);
stockApp.getStockList().getItems().setAll(stocks);
}

/** Processes next tick. */
@Override
public void nextTick() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package edu.ntnu.idi.idatt2003.gruppe42.Controller;

import edu.ntnu.idi.idatt2003.gruppe42.Controller.AppControllers.MailController;
import edu.ntnu.idi.idatt2003.gruppe42.Model.EventBus;
import edu.ntnu.idi.idatt2003.gruppe42.Model.Stock;

import edu.ntnu.idi.idatt2003.gruppe42.Model.StockEvent;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class StockController {

private static final int STEEP_THRESHOLD = 46;
private static final int STEEP_THRESHOLD = 45;
private static final int EVENT_MAGNITUDE = 90;
private static final double NOISE_RANGE = 8.0;
private static final int MIN_TREND_FRAMES = 10;
Expand All @@ -24,15 +21,13 @@ public class StockController {
private int trendFramesRemaining;
private int trend;
private int delay;
private int time;
private Random random;
private BigDecimal price;
private BigDecimal startPrice;
private BigDecimal pendingShock;

public StockController(final Stock stock) {
this.stock = stock;
this.time = 0;
this.delay = 0;
this.random = new Random();
this.startPrice = stock.getSalesPrice();
Expand Down Expand Up @@ -72,7 +67,6 @@ public BigDecimal updatePrice() {
}

trendFramesRemaining--;
time++;

if (trendFramesRemaining <= 0) {
nextTrend();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public DesktopViewController(
() -> {
News news = new News(event.company(), event.trend());
mailController.createMessage(
news.generateAuthor(), event.company(), news.generateNews());
news.generateAuthor(), "BREAKING NEWS", news.generateNews());
}));
}

Expand Down Expand Up @@ -193,8 +193,10 @@ private void setupDesktopUI() {
}

private void setupListeners() {
timeAndWeatherController.hourProperty().addListener((obs, ov, nv) ->
desktopView.updateClock(timeAndWeatherController.getTimeString()));
timeAndWeatherController.hourProperty().addListener((obs, ov, nv) -> {
desktopView.updateClock(timeAndWeatherController.getTimeString());
player.updateStatus();
});

timeAndWeatherController.dayIndexProperty().addListener((obs, ov, nv) -> {
String dayName = timeAndWeatherController.getDayOfWeekString();
Expand All @@ -213,6 +215,7 @@ private void setupListeners() {
player.getNameProperty().addListener((obs, ov, nv) ->
desktopView.updatePlayerName(nv));

player.updateStatus();
player.getStatusProperty().addListener((obs, ov, nv) ->
desktopView.updatePlayerStatus(nv.name()));

Expand Down Expand Up @@ -241,6 +244,7 @@ private void doAdvanceWeek() {
marketController.advanceWeek();
timeAndWeatherController.advanceWeek();
mailController.clearMessages();
player.updateStatus();
}

private void showMarketClosedWarning() {
Expand Down
43 changes: 41 additions & 2 deletions src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/News.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,55 @@ public News(String company, int trend){
public String generateAuthor(){
List<String> authors = new ArrayList<>();
authors.add("market@stocks.com");
authors.add("myfatherworksatmicrosoft@hotmail.com");
authors.add("definitely.obama@whitehouse.gov");
authors.add("bigfoot.financial@yeti.net");
authors.add("notaciaagent@cia.gov");
authors.add("iamyourfather@deathstar.empire");
authors.add("shakespeareswifi@globe.theatre");
authors.add("dinosaur.investor@jurassic.park");
authors.add("flat.earth.stonks@edgeoftheworld.com");
authors.add("timtraveler2089@yahoo.com");
authors.add("schroedingers.portfolio@maybe.com");
authors.add("elon@mymusk.biz");
authors.add("god@heaven.org");
authors.add("santaclaus@northpole.gifts");
authors.add("area51.leaks@ufo.gov");
authors.add("theguywhosneezedonthestockmarket@2008crash.com");
authors.add("nephew.of.warren.buffet@gmail.com");
authors.add("batman@notbruce.wayne");
authors.add("mycatinvestedmysavings@meow.finance");
authors.add("nsa.definitely.not.reading.this@nsa.gov");
return authors.get(random.nextInt(authors.size()));
}
public String generateNews(){
if (trend > 0) {
List<String> goodNews = new ArrayList<>();
goodNews.add("Turns out the product of " + company +" is good for the enviroment");
goodNews.add("Turns out the product of " + company + " is good for the environment.");
goodNews.add("Beaver population increased due to the efforts of " + company + ".");
goodNews.add("Eminem dropped new hit single, namedropping " + company + ".");
goodNews.add("Founder of " + company + " seen feeding the homeless.");
goodNews.add(company + " got frontpage in new forbes magazine, on top companies to invest in.");
goodNews.add("IShowSpeed seen using " + company + " products in the background of viral livestream.");
goodNews.add("New studies show that " + company + " took part in inventing the rainbow back in 4,542,997,974 BC");
goodNews.add(company + " has made breakthroughs in the field of artificial intelligence.");
goodNews.add("After the release of the brand new logo of " + company + ", the logo has been called the \"a glimpse of the future\".");
goodNews.add("EX-EMPLOYEES of " + company + " REVEAL that the work environment and safety regulations are not up to basic standards but far surpassing them.");
goodNews.add("Founder of " + company + " seen rudely pushing a homeless man out of the road, saving his life.");
return goodNews.get(random.nextInt(goodNews.size()));
}
List<String> badNews = new ArrayList<>();
badNews.add("Turns out the product of " + company +" is bad for the enviroment");
badNews.add("Turns out the product of " + company + " is bad for the environment.");
badNews.add("Founder of " + company + " seen rudely pushing a homeless man out of the way.");
badNews.add(company + " mentioned in epstein files.");
badNews.add(company + " has created many dams for beavers, causing them to lay around in the sun all day and die of laziness.");
badNews.add(company + " initiated a campaign to feed the homeless with burgers, using dog meat. This has decreased the number of starvation deaths among the homeless.");
badNews.add("Turns out " + company + " created the first covid-19 vaccines, which are not safe for humans.");
badNews.add("Extensive research of " + company + " shows financial holes in the company.");
badNews.add("Founder of " + company + " heiling at press conference discussing great ideas.");
badNews.add("EX-EMPLOYEES of " + company + " REVEAL that the work environment and safety regulations are not up to basic standards.");
badNews.add("US-Army found using " + company + " issued weapons that malfunctioned causing mass civilian casualties.");
badNews.add(company + " got frontpage in new forbes magazine, on top companies not to invest in.");
return badNews.get(random.nextInt(badNews.size()));
}
}
14 changes: 11 additions & 3 deletions src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public String getStatus() {
/** Adds money. */
public void addMoney(final BigDecimal amount) {
money = money.add(amount);
updateStatus();
}

/** Withdraws money. */
Expand All @@ -67,11 +68,13 @@ public void withdrawMoney(final BigDecimal amount) throws InsufficientFunds {
throw new InsufficientFunds("Not enough money to withdraw " + amount);
}
money = money.subtract(amount);
updateStatus();
}

/** Deducts rent. */
public void deductRent(final BigDecimal amount) {
money = money.subtract(amount);
updateStatus();
}

/** @return true if in debt. */
Expand Down Expand Up @@ -112,12 +115,12 @@ public TransactionArchive getTransactionArchive() {
}


/** @return player status. */
public ObjectProperty<Status> getStatusProperty() {
/** Updates the player's status. */
public void updateStatus() {
final int investorWeeks = 10;
final int speculatorWeeks = 20;
final double investorMult = 1.2;
final double speculatorMult = 2.0;
final double speculatorMult = 5.0;

int weeksActive = transactionArchive.countDistinctWeeks();
BigDecimal netWorth = getNetWorth();
Expand All @@ -137,6 +140,11 @@ public ObjectProperty<Status> getStatusProperty() {
} else {
status.set(Status.NOVICE);
}
}


/** @return player status property. */
public ObjectProperty<Status> getStatusProperty() {
return status;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,11 @@ public BigDecimal getLowestPrice() {

/** @return price change. */
public BigDecimal getLatestPriceChange() {
final int minSize = 2;
if (prices.size() < minSize) {
if (prices.isEmpty()) {
return BigDecimal.ZERO;
}
return prices.get(prices.size() - 1)
.subtract(prices.get(prices.size() - 2));
int index120Ago = Math.max(0, prices.size() - 121);
return getSalesPrice().subtract(prices.get(index120Ago));
}

/** Updates the price. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class BankApp extends Popup {

/** Constructs the Bank app. */
public BankApp() {
super(500, 550, 220, 100, App.BANK);
super(600, 550, 220, 100, App.BANK);

portfolioList = new ListView<>();
portfolioList.setSelectionModel(null);
Expand Down Expand Up @@ -73,7 +73,7 @@ private VBox buildSummaryCard() {
totalReturnLabel.setMinWidth(Region.USE_PREF_SIZE);

GridPane grid = new GridPane();
grid.setHgap(40);
grid.setHgap(20);
grid.setVgap(15);

ColumnConstraints c0 = new ColumnConstraints();
Expand Down
Loading

0 comments on commit db2b77a

Please sign in to comment.