Traversierung Binärbaum

This commit is contained in:
2025-01-08 22:22:34 +01:00
parent 14abc4faae
commit ae55752587
22 changed files with 111 additions and 0 deletions

37
Klassen/Aktienhandel/Aktie.java Executable file
View File

@ -0,0 +1,37 @@
public class Aktie{
private String name;
private int count;
private double value;
public Aktie(String name, int count, double value) {
this.name = name;
this.count = count;
this.value = value;
}
public double getAktienWert() {
return count * value;
}
public void anzahlErhoehen(int amt) {
this.count += amt;
}
public void anzahlVerringern(int amt) {
this.count -= (this.count < amt) ? 0 : amt;
}
public String getName() {
return this.name;
}
public int getCount() {
return this.count;
}
public double getValue() {
return this.value;
}
public void setName(String name) {
this.name = name;
}
public void setCount(int count) {
this.count = count;
}
public void setValue(int value) {
this.value = value;
}
}

View File

@ -0,0 +1,16 @@
public class Broker extends Person {
protected double provision;
protected int clientCount;
public Broker(String name, int age, double money, double provision, int clientCount) {
super(name, age, money);
this.provision = provision;
this.clientCount = clientCount;
}
public void tradeAbwickeln(Trader trader, String aktieName, double price, int amt) {
trader.aktieKaufen(aktieName, price, amt);
this.money += this.provision;
}
public void kundenAnzeigen() {
System.out.println("Kunden: " + this.clientCount);
}
}

View File

@ -0,0 +1,15 @@
public class DayTrader extends Trader {
protected int tradesPerDay;
protected boolean hebelTrade;
public DayTrader(String name, int age, double money, int tradesPerDay, boolean hebelTrade) {
super(name, age, money);
this.tradesPerDay = tradesPerDay;
this.hebelTrade = hebelTrade;
}
public void schnellerTrade(double profit) {
this.money += profit;
}
public void risikoEingrenzen() {
//do stuff I guess
}
}

View File

@ -0,0 +1,15 @@
public class LangfristInvestor extends Trader {
protected double dividends;
protected double investDuration;
public LangfristInvestor(String name, int age, double money, double dividends) {
super(name, age, money);
this.dividends = dividends;
}
public void dividendeErhalten(double amt) {
this.dividends += amt;
this.money += amt;
}
public void portfolioUmschichten() {
//stuff
}
}

29
Klassen/Aktienhandel/Main.java Executable file
View File

@ -0,0 +1,29 @@
public class Main {
public static void main(String[] args) {
// Erstellen eines LangfristInvestors
LangfristInvestor investor = new LangfristInvestor("Lisa", 45, 20000, 10);
investor.aktieKaufen("Apple", 150, 10);
investor.dividendeErhalten(500);
investor.portfolioUmschichten();
investor.anzeigen();
// Erstellen eines Brokers
Broker broker = new Broker("Paul", 50, 50000, 100, 5);
broker.tradeAbwickeln(investor, "Apple", 150, 5);
broker.kundenAnzeigen();
// Erstellen eines OnlineBrokers
OnlineBroker onlineBroker = new OnlineBroker("OnlineTrade", 35, 100000, 50, 1000, 10, "TradeZone");
onlineBroker.gebuehrErheben(investor);
onlineBroker.neueKundenAkquirieren(50);
// Erstellen eines DayTraders
DayTrader dayTrader = new DayTrader("Tom", 30, 30000, 5, true);
dayTrader.aktieKaufen("Tesla", 700, 3);
dayTrader.schnellerTrade(300); // Profit von 300 EUR durch schnellen Trade
dayTrader.aktieVerkaufen("Tesla", 720, 2); // Verkauf von 2 Tesla-Aktien
dayTrader.anzeigen();
}
}

View File

@ -0,0 +1,16 @@
public class OnlineBroker extends Broker {
protected double platformFee;
protected String platformName;
public OnlineBroker(String name, int age, double money, double provision, int clientCount, double platformFee, String platformName) {
super(name, age, money, provision, clientCount);
this.platformFee = platformFee;
this.platformName = platformName;
}
public void gebuehrErheben(Trader trader) {
trader.geldAuszahlen(this.platformFee);
this.money += this.platformFee;
}
public void neueKundenAkquirieren(int amt) {
this.clientCount += amt;
}
}

View File

@ -0,0 +1,19 @@
public class Person {
protected String name;
protected int age;
protected double money;
public Person(String name, int age, double money) {
this.name = name;
this.age = age;
this.money = money;
}
public void geldEinzahlen(double amt) {
this.money += amt;
}
public void geldAuszahlen(double amt) {
this.money -= (this.money < amt) ? 0 : amt;
}
public void show() {
System.out.println("Name: " + name + ", Age: " + age + ", Money: " + money);
}
}

View File

@ -0,0 +1,37 @@
import java.util.ArrayList;
public class Trader extends Person {
protected ArrayList<Aktie> aktien;
public Trader(String name, int age, double money) {
super(name, age, money);
this.aktien = new ArrayList<Aktie>();
}
public void aktieKaufen(String name, double price, int amt) {
Aktie aktie = new Aktie(name, amt, price);
this.aktien.add(aktie);
this.geldAuszahlen(aktie.getAktienWert());
}
public void aktieVerkaufen(String name, double price, int amt) {
for (Aktie aktie : this.aktien) {
if (aktie.getName() == name) {
this.geldEinzahlen(price * amt);
aktie.anzahlVerringern(amt);
break;
}
}
}
public double getPortfolioWert() {
double out = 0;
for (Aktie aktie : this.aktien) {
out += aktie.getAktienWert();
}
return out;
}
public void anzeigen() {
String out = "";
for (Aktie aktie : aktien) {
out += "\nAktie: " + aktie.getName() + ", Count: " + aktie.getCount() + ", Value: " + aktie.getValue();
}
System.out.println("Name: " + name + ", Age: " + age + ", Money: " + money + out + "\nGesamtwert: " + this.getPortfolioWert());
}
}

View File

@ -0,0 +1,28 @@
import java.util.ArrayList;
public class Bibliothek {
private ArrayList<Buch> books = new ArrayList<>();
public void buchHinzufuegen(Buch buch) {
books.add(buch);
}
public Buch buchSuchen(String isbn) {
for (Buch buch : books) {
if (buch.ISBN.toLowerCase().equals(isbn.toLowerCase())) {
return buch;
}
}
return null;
}
public void alleBuecherAnzeigen() {
for (Buch buch : books) {
buch.buchInfo();
System.out.println("-".repeat(10));
}
}
}

View File

@ -0,0 +1,128 @@
import java.util.Scanner;
public class BibliotheksApp {
private static Bibliothek bibliothek = new Bibliothek();
private static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("-".repeat(20));
System.out.println("\t Bibliotheksverwaltung\t");
boolean stop = false;
while (!stop) {
printAktionen();
System.out.print("Aktion: ");
String aktion = sc.nextLine();
switch (aktion.toUpperCase()) {
case "N":
newBuch();
break;
case "A":
ausleihen();
break;
case "Z":
zurueckgeben();
break;
case "I":
suchen();
break;
case "L":
alleAuflisten();
break;
case "E":
stop = true;
break;
default:
System.out.println("Falsche Eingabe.");
break;
}
}
}
private static void printAktionen() {
System.out.println("-".repeat(20));
System.out.println("Mögliche Aktionen:");
System.out.println("\t (N) - Neues Buch anlegen");
System.out.println("\t (A) - Buch ausleihen");
System.out.println("\t (Z) - Buch zurückgeben");
System.out.println("\t (I) - Buchinfo ausgeben");
System.out.println("\t (L) - Alle Bücher auflisten");
System.out.println("\t (E) - Beenden");
System.out.println("-".repeat(20));
}
private static void newBuch() {
System.out.println("-".repeat(20));
System.out.print("Titel: ");
String titel = sc.nextLine();
System.out.print("Autor: ");
String autor = sc.nextLine();
System.out.print("ISBN: ");
String isbn = sc.nextLine();
System.out.print("Verfügbar: ");
int count = sc.nextInt();
sc.nextLine();
Buch buch = new Buch(titel, autor, isbn, count);
bibliothek.buchHinzufuegen(buch);
System.out.println("Buch erfolgreich angelegt.");
System.out.println("-".repeat(20));
}
private static void ausleihen() {
System.out.println("-".repeat(20));
System.out.print("ISBN: ");
String isbn = sc.nextLine();
Buch buch = bibliothek.buchSuchen(isbn);
if (buch == null) {
System.out.println("Das Buch existiert nicht");
return;
}
boolean erfolg = buch.ausleihen();
if (erfolg) {
System.out.println("Buch erfolgreich ausgeliehen.");
} else {
System.out.println("Das Buch ist derzeit nicht verfügbar.");
}
System.out.println("-".repeat(20));
}
private static void zurueckgeben() {
System.out.println("-".repeat(20));
System.out.print("ISBN: ");
String isbn = sc.nextLine();
Buch buch = bibliothek.buchSuchen(isbn);
if (buch == null) {
System.out.println("Das Buch existiert nicht");
return;
}
buch.ausleihen();
System.out.println("Buch erfolgreich zurückgegeben.");
System.out.println("-".repeat(20));
}
private static void suchen() {
System.out.println("-".repeat(20));
System.out.print("ISBN: ");
String isbn = sc.nextLine();
Buch buch = bibliothek.buchSuchen(isbn);
if (buch == null) {
System.out.println("Das Buch existiert nicht");
return;
}
buch.buchInfo();
System.out.println("-".repeat(20));
}
private static void alleAuflisten() {
System.out.println("-".repeat(20));
bibliothek.alleBuecherAnzeigen();
System.out.println("-".repeat(20));
}
}

View File

@ -0,0 +1,33 @@
public class Buch {
private String title;
private String author;
public String ISBN;
private int count;
Buch(String newTitle, String newAuthor, String newISBN, int newCount) {
title = newTitle;
author = newAuthor;
ISBN = newISBN;
count = newCount;
}
public boolean ausleihen() {
if (count > 0) {
count--;
return true;
} else {
return false;
}
}
public void zurueckgeben() {
count++;
}
public void buchInfo() {
System.out.printf("Titel: %s\n", title);
System.out.printf("Autor: %s\n", author);
System.out.printf("ISBN: %s\n", ISBN);
System.out.printf("Verfügbar: %s\n", count);
}
}

View File

@ -0,0 +1,37 @@
import java.util.ArrayList;
public class Client {
private static int clientCounter;
private int clientID;
private String clientName;
private String clientEmail;
private ArrayList<Order> clientOrders;
public Client(String clientName, String clientEmail) {
clientCounter++;
this.clientID = clientCounter;
this.clientName = clientName;
this.clientEmail = clientEmail;
this.clientOrders = new ArrayList<Order>();
}
public int getID() {
return this.clientID;
}
public String getName() {
return this.clientName;
}
public String getEmail() {
return this.clientEmail;
}
public ArrayList<Order> getOrders() {
return this.clientOrders;
}
public void addOrder(Order order) {
this.clientOrders.add(order);
}
public boolean hasOrder() {
return !this.clientOrders.isEmpty();
}
public String toString() {
return ("ID: " + this.clientID + ", Name: " + this.clientName + ", Email: " + this.clientEmail);
}
}

42
Klassen/Technikladen/Order.java Executable file
View File

@ -0,0 +1,42 @@
import java.util.ArrayList;
public class Order {
private int orderClientID;
private ArrayList<OrderEntry> orderEntries;
private String orderDate;
public Order(int clientID, String date) {
this.orderClientID = clientID;
this.orderEntries = new ArrayList<OrderEntry>();
this.orderDate = date;
}
public int getClientID() {
return this.orderClientID;
}
public String getDate() {
return this.orderDate;
}
public ArrayList<OrderEntry> getEntries() {
return this.orderEntries;
}
public void addEntry(OrderEntry entry) {
this.orderEntries.add(entry);
}
public int calcPrice() {
int price = 0;
for (OrderEntry entry : this.orderEntries) {
price += entry.calcPrice();
}
return price;
}
public String createReceipt() {
String output = "\n";
for (OrderEntry entry : this.orderEntries) {
output += entry.toString() + ", Price: " + entry.calcPrice() + "\n";
}
output += "=====" + this.calcPrice() + "=====";
return output;
}
public String toString() {
return ("Client: " + this.orderClientID + ", Date: " + this.orderDate);
}
}

View File

@ -0,0 +1,14 @@
public class OrderEntry {
private Product orderProduct;
private int entryAmount;
public OrderEntry(Product product, int amt) {
this.orderProduct = product;
this.entryAmount = amt;
}
public int calcPrice() {
return this.entryAmount * this.orderProduct.getPrice();
}
public String toString() {
return ("Product: " + this.orderProduct.getName() + ", Amount: " + this.entryAmount);
}
}

View File

@ -0,0 +1,55 @@
import java.util.ArrayList;
public class OrderSystem {
private StorageManager systemManager;
private ArrayList<Client> systemClients;
public OrderSystem(StorageManager manager) {
this.systemManager = manager;
this.systemClients = new ArrayList<Client>();
}
public void addProduct(Product product) {
this.systemManager.addProduct(product);
}
public void addClient(Client client) {
this.systemClients.add(client);
}
public Client getClientByID(int id) {
for (Client client : this.systemClients) {
if (client.getID() == id) {
return client;
}
}
return new Client("", "");
}
public void createOrder(int id, Order order) {
this.getClientByID(id).addOrder(order);
}
public ArrayList<Client> getClients() {
return this.systemClients;
}
public Product getProductByID(int id) {
for (Product product : this.systemManager.getStorage()) {
if (product.getID() == id) {
return product;
}
}
return new Product(-1, "", 0, 0);
}
public StorageManager getManager() {
return this.systemManager;
}
public void showClients() {
for (Client client : this.systemClients) {
System.out.println(client.toString());
}
}
public void showProducts() {
if (this.systemManager.getStorage().isEmpty()) {
System.out.println("No products exist");
return;
}
for (Product product : this.systemManager.getStorage()) {
System.out.println("ID: " + product.getID() + ", Name: " + product.getName() + ", Price: " + product.getPrice() + ", Amount: " + product.getAmount());
}
}
}

View File

@ -0,0 +1,34 @@
public class Product {
private int productID;
private String productName;
private int productPrice;
private int productAmount;
public Product(int id, String name, int price, int amt) {
this.productID = id;
this.productName = name;
this.productPrice = price;
this.productAmount = amt;
}
public int getID() {
return this.productID;
}
public String getName() {
return this.productName;
}
public int getPrice() {
return this.productPrice;
}
public int getAmount() {
return this.productAmount;
}
public void reduceAmount(int amt) {
if (this.productAmount < amt) {
System.out.println("Not enough of product " + this.productID + "(" + this.productName + ") available!");
return;
}
this.productAmount -= amt;
}
public void increaseAmount(int amt) {
this.productAmount += amt;
}
}

View File

@ -0,0 +1,12 @@
public class Receipt {
private Order receiptOrder;
private Client receiptClient;
public Receipt(Order order, Client client) {
this.receiptOrder = order;
this.receiptClient = client;
}
public void printReceipt() {
System.out.println("Client:\n" + this.receiptClient.toString());
System.out.println(this.receiptOrder.createReceipt());
}
}

View File

@ -0,0 +1,25 @@
import java.util.ArrayList;
public class StorageManager {
private ArrayList<Product> storageProducts;
public StorageManager() {
this.storageProducts = new ArrayList<Product>();
}
public void addProduct(Product product) {
this.storageProducts.add(product);
}
public ArrayList<Product> getStorage() {
return this.storageProducts;
}
public void increaseStorage(int id, int amt) {
this.getProductByID(id).increaseAmount(amt);
}
public Product getProductByID(int id) {
for (Product product : this.storageProducts) {
if (product.getID() == id) {
return product;
}
}
return new Product(-1, "", 0, 0);
}
}

View File

@ -0,0 +1,113 @@
import java.util.ArrayList;
import java.util.Scanner;
public class TechStore {
public static void p(String input) {
System.out.print(input);
}
public static void main(String[] args) {
StorageManager manager = new StorageManager();
OrderSystem system = new OrderSystem(manager);
Scanner scnr = new Scanner(System.in);
while (true) {
p("\nInput mode: ");
String input = scnr.nextLine();
if (input.equals("8")) {
break;
}
switch (Integer.parseInt(input)) {
case 1: {
p("Name: ");
String name = scnr.nextLine();
p("E-Mail: ");
String email = scnr.nextLine();
system.addClient(new Client(name, email));
break;
}
case 2: {
ArrayList<Client> clients = system.getClients();
if (clients.isEmpty()) {
System.out.println("No clients found");
break;
}
for (Client client : clients) {
System.out.println(client.toString());
}
break;
}
case 3: {
p("ID: ");
int id = Integer.parseInt(scnr.nextLine());
p("Name: ");
String name = scnr.nextLine();
p("Price: ");
int price = Integer.parseInt(scnr.nextLine());
p("Amount: ");
int amt = Integer.parseInt(scnr.nextLine());
system.addProduct(new Product(id, name, price, amt));
break;
}
case 4: {
system.showProducts();
break;
}
case 5: {
system.showProducts();
p("ID: ");
int id = Integer.parseInt(scnr.nextLine());
p("Amount: ");
int amt = Integer.parseInt(scnr.nextLine());
manager.increaseStorage(id, amt);
break;
}
case 6: {
system.showClients();
p("ID: ");
int clientID = Integer.parseInt(scnr.nextLine());
p("Date: ");
String date = scnr.nextLine();
Order order = new Order(clientID, date);
system.showProducts();
while (true) {
p("ID: ");
String id_str = scnr.nextLine();
if (id_str.equals("exit")) {
break;
}
int id = Integer.parseInt(id_str);
p("Amount: ");
int amt = Integer.parseInt(scnr.nextLine());
if (manager.getProductByID(id).getAmount() < amt) {
System.out.println("Amount chosen too high");
continue;
}
order.addEntry(new OrderEntry(system.getProductByID(id), amt));
}
system.createOrder(clientID, order);
break;
}
case 7: {
system.showClients();
p("ID: ");
int id = Integer.parseInt(scnr.nextLine());
int n = 0;
for (Order order : system.getClientByID(id).getOrders()) {
System.out.println((n++) + order.toString());
}
p("ID: ");
int order_id = Integer.parseInt(scnr.nextLine());
System.out.println(system.getClientByID(id).getOrders().get(order_id).createReceipt());
break;
}
case 8: {
return;
}
}
}
scnr.close();
}
}