Skip to content

Commit

Permalink
start på oppg 4-8
Browse files Browse the repository at this point in the history
  • Loading branch information
martin committed Feb 26, 2026
1 parent 3190cb8 commit d299544
Show file tree
Hide file tree
Showing 9 changed files with 270 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/main/java/temppackage/Exchange.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package temppackage;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class Exchange {
private String name;
private Map<String, Stock> stocks;
private int weekNumber;
private Random random = new Random();

public Exchange(String name, List<Stock> stockList){
this.name = name;
this.stocks = new HashMap<>();
this.weekNumber = 1;
}

public void buy(Player player, Stock stock, BigDecimal quantity){
Share shareToBuy = new Share(stock, quantity, stock.getPrice());
Purchase purchase = new Purchase(shareToBuy, this.weekNumber);
purchase.commit(player);
}

public void sell(Player player, Share share){
Sale sale = new Sale(share, weekNumber);
sale.commit(player);
}

public void advance(){
this.weekNumber++;
}


public Map<String, Stock> getStocks(){
return this.stocks;
}

public Stock geStock(String symbol){
// filter med symbol eller company name
}




}
13 changes: 13 additions & 0 deletions src/main/java/temppackage/Player.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package temppackage;

import java.math.BigDecimal;

public class Player {
private String name;
private BigDecimal startingMoney;
private BigDecimal money;
private Portfolio portfolio;
private TransactionArchive transactionArchive;


}
17 changes: 17 additions & 0 deletions src/main/java/temppackage/Purchase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package temppackage;

public class Purchase extends Transaction{


public Purchase(Share share, int week){


}

@Override
public void commit(Player player) {
// TODO Auto-generated method stub
super.commit(player);
}

}
36 changes: 36 additions & 0 deletions src/main/java/temppackage/PurchaseCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package temppackage;

import java.math.BigDecimal;

public class PurchaseCalculator implements TransactionCalculator {
private BigDecimal purchasePrice;
private BigDecimal quantity;

public PurchaseCalculator(Share share) {
this.purchasePrice = share.getPurchasePrice();
this.quantity = share.getQuantity();
}



@Override
public BigDecimal calculateGross() {
return this.purchasePrice.multiply(this.quantity);
}

@Override
public BigDecimal calculateComission() {
BigDecimal gross = this.calculateGross();
return gross.multiply(BigDecimal.valueOf(0.05));
}

@Override
public BigDecimal calculateTax() {
return new BigDecimal("0");
}

@Override
public BigDecimal calculateTotal() {
return this.calculateGross().add(this.calculateTax().add(this.calculateComission()));
}
}
14 changes: 14 additions & 0 deletions src/main/java/temppackage/Sale.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package temppackage;

public class Sale extends Transaction{
public Sale(Share share, int week){

}

@Override
public void commit(Player player) {
// TODO Auto-generated method stub
super.commit(player);
}

}
43 changes: 43 additions & 0 deletions src/main/java/temppackage/SaleCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package temppackage;

import java.math.BigDecimal;

public class SaleCalculator implements TransactionCalculator {
private BigDecimal purchasePrice;
private BigDecimal salesPrice;
private BigDecimal quantity;

public SaleCalculator(Share share) {
this.purchasePrice = share.getPurchasePrice();
this.salesPrice = share.getStock().getPrice();
this.quantity = share.getQuantity();
}

@Override
public BigDecimal calculateGross() {
return this.salesPrice.multiply(this.quantity);
}

@Override
public BigDecimal calculateComission() {
BigDecimal gross = this.calculateGross();
return gross.multiply(BigDecimal.valueOf(0.05));
}

@Override
public BigDecimal calculateTax() {
BigDecimal gross = this.calculateGross();
BigDecimal comission = this.calculateComission();
BigDecimal buyPrice = this.purchasePrice.multiply(this.quantity);
BigDecimal earnings = gross.subtract(comission).subtract(comission).subtract(buyPrice);
return earnings.multiply(BigDecimal.valueOf(0.3));
}

@Override
public BigDecimal calculateTotal() {
BigDecimal gross = this.calculateGross();
BigDecimal comission = this.calculateComission();
BigDecimal tax = this.calculateTax();
return gross.subtract(comission).subtract(tax);
}
}
42 changes: 42 additions & 0 deletions src/main/java/temppackage/Transaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package temppackage;

import temppackage.calculators.TransactionCalculator;

public abstract class Transaction {
private Share share;
private int week;
private TransactionCalculator transactionCalculator;
private boolean commited;

protected Transaction(Share share, int week, TransactionCalculator transactionCalculator){
this.share = share;
this.week = week;
this.transactionCalculator = transactionCalculator;
this.commited = commited;
}

public Share getShare(){
return this.share;
}

public int getWeeek(){
return this.week;
}

public TransactionCalculator getCalculator(){
return this.transactionCalculator;
}

public boolean isCommitted(){
return this.commited;
}

public void commit(Player player){
//TODO
this.commited = true;
}




}
47 changes: 47 additions & 0 deletions src/main/java/temppackage/TransactionArchive.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package temppackage;

import java.util.ArrayList;
import java.util.List;

public class TransactionArchive {
List<Transaction> transactions;


public TransactionArchive() {
this.transactions = new ArrayList<>();
}

public boolean add(Transaction transaction){
if (transactions.contains(transaction)){
return false;
}
this.transactions.add(transaction);
return true;
}

public boolean isEmpty(){
return transactions.isEmpty();
}

public List<Transaction> getTransactions(int week){
//TODO
return null;
}

public List<Purchase> getPurchases(int week){
//TODO
return null;
}

public List<Sale> getSales(int week){
//TODO
return null;
}

public int countDistinctWeeks(){
//TODO
return 0;
}


}
10 changes: 10 additions & 0 deletions src/main/java/temppackage/TransactionCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package temppackage;

import java.math.BigDecimal;

public interface TransactionCalculator {
public BigDecimal calculateGross();
public BigDecimal calculateComission();
public BigDecimal calculateTax();
public BigDecimal calculateTotal();
}

0 comments on commit d299544

Please sign in to comment.