diff --git a/src/nogard/schritt5b/BefehlFactory.java b/src/nogard/schritt5b/BefehlFactory.java index a8a4886..b5c4f9e 100644 --- a/src/nogard/schritt5b/BefehlFactory.java +++ b/src/nogard/schritt5b/BefehlFactory.java @@ -1,7 +1,5 @@ package nogard.schritt5b; -import java.util.Arrays; - public class BefehlFactory { public static Befehl createBefehl(String input) throws BefehlUnbekanntException { diff --git a/src/nogard/schritt6/Bereich.java b/src/nogard/schritt6/Bereich.java deleted file mode 100644 index 08caddd..0000000 --- a/src/nogard/schritt6/Bereich.java +++ /dev/null @@ -1,102 +0,0 @@ -package nogard.schritt6; - -import nogard.schritt6.Gegenstand; -import nogard.schritt6.GegenstandNichtVorhandenException; -import nogard.schritt6.Richtungen; -import nogard.schritt6.exceptions.GegenstandNichtVorhandenException; - -import java.util.HashMap; -import java.util.LinkedList; -import java.util.Map; - -/** - * Diese Klasse modelliert Bereiche.Ein Bereich kann ein Gebiet, ein Haus, ein Raum etc. sein. - * - * Jeder Bereich ist mit anderen Bereichen �ber Ausg�nge verbunden. M�gliche Ausg�nge liegen im Norden, Osten, S�den und Westen. - * - * F�r jeden Ausgang h�lt ein Bereich eine Referenz auf den benachbarten Bereich parat. - */ -public class Bereich { - - private final String beschreibung; - private final Map nachbarn; - private final LinkedList gegenstaende; - /** - * Konstruktor. - * @param beschreibung Die Beschreibung des Areals. - */ - public Bereich(String beschreibung) { - this.beschreibung = beschreibung; - nachbarn = new HashMap<>(); - gegenstaende = new LinkedList<>(); - - for (Richtungen r: Richtungen.values()) { - nachbarn.put(r, null); - } - } - - /** - * Liefert die Beschreibung des Bereichs. - * @return Die Beschreibung des Bereichs. - */ - public String getBeschreibung() { - return beschreibung; - } - - /** - * Setzt den Nachbarn des Bereiches. - * @param r Die Richtung / Ort des Nachbarbereichs. - * @param n Der Nachbar welcher Relativ zu diesem Objekt liegt. - */ - public void setNachbar(Richtungen r, Bereich n) { - nachbarn.put(r, n); - } - - /** - * - * @param richtung Die Richtung in welcher ein Nachbar liegen könnte. - * @return Den Nachbarn in der angegebenen Richtung, oder NULL wenn in der Richtung kein Bereich liegt. - */ - public Bereich getNachbar(Richtungen richtung) { - return nachbarn.get(richtung); - } - - public void platziereGegenstand(Gegenstand g) { - gegenstaende.add(g); - } - - public void entferneGegenstand(Gegenstand g) throws GegenstandNichtVorhandenException { - boolean success = gegenstaende.remove(g); - if (!success) { - throw new GegenstandNichtVorhandenException(g); - } - } - - public Gegenstand sucheGegenstand(String name) throws GegenstandNichtVorhandenException { - for (Gegenstand g: gegenstaende) { - if (g.getName().equalsIgnoreCase(name)) { - return g; - } - } - - throw new GegenstandNichtVorhandenException(name); - } - - public String getInfo() { - StringBuilder sb = new StringBuilder(); - sb.append("\n\tDu bist im Bereich "); - sb.append(getBeschreibung()); - sb.append("\n\tDeine Nachbarn sind:"); - for (Map.Entry paar: nachbarn.entrySet()) { - if (paar.getValue() != null) { - sb.append("\n\t").append(paar.getKey().name()); - } - } - sb.append("\nGegenstaende:"); - for (Gegenstand g: gegenstaende) { - sb.append("\n\t").append(g.getInfo()); - } - - return sb.toString(); - } -} diff --git a/src/nogard/schritt6/Gegenstand.java b/src/nogard/schritt6/Gegenstand.java deleted file mode 100644 index ae38873..0000000 --- a/src/nogard/schritt6/Gegenstand.java +++ /dev/null @@ -1,38 +0,0 @@ -package nogard.schritt6; - -public class Gegenstand { - private final String name; - private final String description; - private final double weight_kg; - - public Gegenstand(String name, String description, double weight_kg) { - this.name = name; - this.description = description; - this.weight_kg = weight_kg; - } - - public Gegenstand(String name, String description) { - this.name = name; - this.description = description; - this.weight_kg = 0; - } - - public String getName() { - return name; - } - - public String getDescription() { - return description; - } - - public double getWeight_kg() { - return weight_kg; - } - - public String getInfo() { - return "Name: " + name + - "\nBeschreibung: " + description + - "\nGewicht" + weight_kg + - "\n"; - } -} diff --git a/src/nogard/schritt6/Nahrung.java b/src/nogard/schritt6/Nahrung.java deleted file mode 100644 index e6ecfae..0000000 --- a/src/nogard/schritt6/Nahrung.java +++ /dev/null @@ -1,21 +0,0 @@ -package nogard.schritt6; - -import nogard.schritt6.Gegenstand; - -public class Nahrung extends Gegenstand { - private final int nutrients; - - public Nahrung(String name, String description, int nutrients) { - super(name, description); - this.nutrients = nutrients; - } - - public int getNutrients() { - return nutrients; - } - - public String getInfo() { - return super.getInfo() + - getNutrients(); - } -} diff --git a/src/nogard/schritt6/Program.java b/src/nogard/schritt6/Program.java deleted file mode 100644 index c7d950c..0000000 --- a/src/nogard/schritt6/Program.java +++ /dev/null @@ -1,11 +0,0 @@ -package nogard.schritt6; - -import nogard.schritt6.Spiel; - -public class Program { - - public static void main(String[] args) { - new Spiel().spielen(); - } - -} diff --git a/src/nogard/schritt6/Richtungen.java b/src/nogard/schritt6/Richtungen.java deleted file mode 100644 index e8bfad2..0000000 --- a/src/nogard/schritt6/Richtungen.java +++ /dev/null @@ -1,5 +0,0 @@ -package nogard.schritt6; - -public enum Richtungen { - NORTH, EAST, SOUTH, WEST, UP, DOWN -} diff --git a/src/nogard/schritt6/Spiel.java b/src/nogard/schritt6/Spiel.java deleted file mode 100644 index bf1bb65..0000000 --- a/src/nogard/schritt6/Spiel.java +++ /dev/null @@ -1,109 +0,0 @@ -package nogard.schritt6; - -import nogard.schritt6b.*; -import nogard.schritt6b.commands.CommandFactory; -import nogard.schritt6b.commands.CommandQuit; -import nogard.schritt6b.commands.ICommand; -import nogard.schritt6b.exceptions.*; - -import java.util.Scanner; - -/** - * - * Dies ist die Hauptklasse der Anwendung "Die Welt von Nogard". - * - * "Die Welt von Nogard" ist ein sehr einfaches, textbasiertes Adventure-Game, in dem sich ein Spieler durch Nogard bewegen kann. - * - * Das Spiel sollte auf jeden Fall ausgebaut werden, damit es interessanter wird! - * - * Zum Spielen muss an einer Instanz dieser Klasse die Methode "spielen()" aufgerufen werden. - * - * Diese Klasse erzeugt und initialisiert alle Objekte der Anwendung: - * - Sie legt alle Bereiche an. Anschlie�end startet das Spiel. - * - Sie wertet die Befehle aus und sorgt f�r ihre Ausf�hrung. - */ -public class Spiel { - private Spieler spieler; - - public Spiel() { - erzeugeDorf(); - } - /** - * Die Hauptmethode zum Spielen. - * L�uft bis zum Ende des Spiels in einer Schleife. - */ - public void spielen() { - ausgebenStartText(); - - // Befehle einlesen und auswerten. - Scanner scannerZeile = new Scanner(System.in); - - boolean end = false; - while (! end) { - // Eingabeaufforderung anzeigen. - System.out.print("> "); - String input = scannerZeile.nextLine(); - try { - ICommand command = CommandFactory.createCommand(spieler, input); - end = command instanceof CommandQuit; - command.execute(); - } catch (BefehlUnbekanntException e) { - System.out.println(e.getMessage()); - } - } - - // Endbildschirm ausgeben. - ausgebenEndText(); - } - - private void ausgebenEndText() { - System.out.println("Danke für dieses Spiel. Auf Wiedersehen."); - } - - private void ausgebenStartText() { - // Begr��ungsbildschirm ausgeben. - System.out.println("Willkommen in Nogard!"); - System.out.println("Entdecke die Welt von Nogard. Doch Vorsicht, überall lauern Gefahren!"); - System.out.println("Wenn du Hilfe benötigst, tippe 'help'."); - System.out.println(); - System.out.println(spieler.getInfo()); - System.out.println(spieler.getBereich().getInfo()); - } - - private void erzeugeDorf() { - // Die Bereiche erzeugen. - Bereich friedhof = new Bereich("auf einem Friedhof, umgeben von dunklen Tannen"); - Bereich wald = new Bereich("im dunklen Stadtwald von Nogard"); - Bereich taverne = new Bereich("in der Taverne, mit zwielichtigen Gestalten an der Theke"); - Bereich hexenhaus = new Bereich("im finsteren Hexenhaus"); - Bereich rathaus = new Bereich("im Rathaus von Nogard"); - Bereich weinkeller = new Bereich("Weinkeller der Taverne"); - - // Die Nachbarschaften festlegen. - taverne.setNachbar(Richtungen.NORTH, rathaus); - taverne.setNachbar(Richtungen.WEST, wald); - taverne.setNachbar(Richtungen.DOWN, weinkeller); - weinkeller.setNachbar(Richtungen.UP, taverne); - wald.setNachbar(Richtungen.NORTH, friedhof); - friedhof.setNachbar(Richtungen.SOUTH, wald); - friedhof.setNachbar(Richtungen.EAST, hexenhaus); - hexenhaus.setNachbar(Richtungen.WEST, friedhof); - - // Gegenstände Platzieren - hexenhaus.platziereGegenstand(new Gegenstand("Krug", "mit fauligem Krötenwasser", 5.0)); - hexenhaus.platziereGegenstand(new Nahrung("Froschschenkel", "mit ranziger Knoblauchbutter", 700)); - - // Erzeugen des Spielers - Scanner scannerZeile = new Scanner(System.in); - System.out.println("Spieler Erstellung\n"); - System.out.println("Name > "); - String name = scannerZeile.nextLine(); - System.out.println("Traglast > "); - float traglast = scannerZeile.nextFloat(); - System.out.println("Fitness > "); - int fitness = scannerZeile.nextInt(); - - spieler = new Spieler(name, traglast, fitness, hexenhaus); - } - -} diff --git a/src/nogard/schritt6/Spieler.java b/src/nogard/schritt6/Spieler.java deleted file mode 100644 index 6bba1c4..0000000 --- a/src/nogard/schritt6/Spieler.java +++ /dev/null @@ -1,136 +0,0 @@ -package nogard.schritt6; - -import nogard.schritt6.*; -import nogard.schritt6.Bereich; -import nogard.schritt6.Nahrung; -import nogard.schritt6.exceptions.GegenstandNichtVorhandenException; -import nogard.schritt6.exceptions.GegenstandZuSchwerException; -import nogard.schritt6.exceptions.SpielerSattException; -import nogard.schritt6.exceptions.SpielerZuSchwachException; - -import java.util.LinkedList; - -public class Spieler { - private static final int FITNESS_max = 5000; - private static final int VERBRAUCH_GEHEN = 500; - - private final String name; - private final float max_traglast; - private int fitness; - private final LinkedList items; - private nogard.schritt6.Bereich bereich; - - public Spieler(String name, float max_traglast, int fitness, nogard.schritt6.Bereich bereich) { - this.name = name; - this.max_traglast = max_traglast; - this.fitness = fitness; - items = new LinkedList<>(); - this.bereich = bereich; - } - - /** - * Berechnet das Gesamtgewicht aller items des Spielers. - * @return Die Auslastung des Spielers. - */ - public float getTraglast() { - float total_weight_kg = 0; - for (Gegenstand g: items) { - total_weight_kg += g.getWeight_kg(); - } - return total_weight_kg; - } - - /**! - * Sucht nach einem Gegenstand in dem derzeitigen Bereich. - * @param name Der Name des aufzunehmenden Gegenstands. - * @throws GegenstandZuSchwerException wenn das Aufnehmen des Gegenstands die Traglast überschreitet. - * @throws GegenstandNichtVorhandenException wenn der Gegenstand nicht in dem Bereich vorhanden ist - */ - public void aufnehmenGegenstand(String name) throws GegenstandZuSchwerException, GegenstandNichtVorhandenException { - Gegenstand g = bereich.sucheGegenstand(name); - - if (getTraglast() + g.getWeight_kg() > max_traglast) { - throw new GegenstandZuSchwerException(g); - } - - bereich.entferneGegenstand(g); - items.add(g); - } - - /** - * Entfernt den Gegenstand mittels des namens aus dem Inventar des spielers. - * @param name Der name des Gegenstands. - * @throws GegenstandNichtVorhandenException der Gegenstand befindet sich nicht in dem Inventar des Spielers - */ - public void ablegenGegenstand(String name) throws GegenstandNichtVorhandenException { - Gegenstand l_g = null; - for (Gegenstand g: items) { - if (g.getName().equals(name)) { - l_g = g; - break; - } - } - - if (l_g == null) { - throw new GegenstandNichtVorhandenException(name); - } - bereich.platziereGegenstand(l_g); - items.remove(l_g); - } - - /** - * Sucht nach Nahrung mithilfe des Namens in dem Bereich & isst es, wenn sie vorhanden ist - * @param name Der Name der Nahrung - * @throws GegenstandNichtVorhandenException die Nahrung ist nicht in dem Bereich vorhanden. - * @throws SpielerSattException der Spieler kann nicht mehr essen. - */ - public void essen(String name) throws GegenstandNichtVorhandenException, SpielerSattException { - Gegenstand g = bereich.sucheGegenstand(name); - - boolean is_nahrung = g instanceof nogard.schritt6b.Nahrung; - if (is_nahrung) { - nogard.schritt6b.Nahrung n = (Nahrung) g; - if (n.getNutrients() + fitness > FITNESS_max) { - throw new SpielerSattException("Spieler ist Satt"); - } - - fitness += n.getNutrients(); - } else { - throw new GegenstandNichtVorhandenException(name); - } - } - - /** - * Wechselt den Bereich - * @param b der neue Bereich. - * @throws SpielerZuSchwachException der Spieler hat nicht genug Ausdauer für den wechsel. - */ - public void geheInBereich(nogard.schritt6b.Bereich b) throws SpielerZuSchwachException { - if (fitness - VERBRAUCH_GEHEN < 0) { - throw new SpielerZuSchwachException(); - } - - fitness -= VERBRAUCH_GEHEN; - bereich = b; - } - - public Bereich getBereich() { - return bereich; - } - - public String getInfo() { - StringBuilder sb = new StringBuilder(); - sb.append("Spieler:"); - sb.append("\n\tName: ").append(name); - sb.append("\n\tFitness: ").append(fitness); - sb.append("\n\tTraglast: ").append(getTraglast()).append("/").append(max_traglast); - sb.append("\n\tItems: ["); - for (Gegenstand g: items) { - sb.append(g.getName()).append(", "); - } - sb.append("]"); - sb.append("\n\tBereich: ").append(bereich.getBeschreibung()); - - return sb.toString(); - } -} diff --git a/src/nogard/schritt6/commands/CommandEat.java b/src/nogard/schritt6/commands/CommandEat.java deleted file mode 100644 index 66dfb68..0000000 --- a/src/nogard/schritt6/commands/CommandEat.java +++ /dev/null @@ -1,23 +0,0 @@ -package nogard.schritt6.commands; -import nogard.schritt6b.Spieler; -import nogard.schritt6b.exceptions.GegenstandNichtVorhandenException; -import nogard.schritt6b.exceptions.SpielerSattException; - -public class CommandEat implements ICommand { - private Spieler spieler; - private String nahrung; - - public CommandEat(Spieler spieler, String nahrung) { - this.spieler = spieler; - this.nahrung = nahrung; - } - - @Override - public void execute() { - try { - spieler.essen(nahrung); - } catch (GegenstandNichtVorhandenException | SpielerSattException e) { - System.out.println(e.getMessage()); - } - } -} diff --git a/src/nogard/schritt6/commands/CommandFactory.java b/src/nogard/schritt6/commands/CommandFactory.java deleted file mode 100644 index ad55590..0000000 --- a/src/nogard/schritt6/commands/CommandFactory.java +++ /dev/null @@ -1,21 +0,0 @@ -package nogard.schritt6.commands; - -import nogard.schritt6b.Richtungen; -import nogard.schritt6b.Spieler; -import nogard.schritt6b.exceptions.BefehlUnbekanntException; - -public class CommandFactory { - public static ICommand createCommand(Spieler spieler, String input) throws BefehlUnbekanntException { - String[] cmd_data = input.split(" "); - - return switch (cmd_data[0]) { - case "go" -> new CommandGo(spieler, Richtungen.valueOf(cmd_data[1].toUpperCase())); - case "take" -> new CommandTake(spieler, cmd_data[1]); - case "put" -> new CommandPut(spieler, cmd_data[1]); - case "info" -> new CommandInfo(spieler); - case "quit" -> new CommandQuit(); - case "eat" -> new CommandEat(spieler, cmd_data[1]); - default -> throw new BefehlUnbekanntException(input); - }; - } -} diff --git a/src/nogard/schritt6/commands/CommandGo.java b/src/nogard/schritt6/commands/CommandGo.java deleted file mode 100644 index 821bde8..0000000 --- a/src/nogard/schritt6/commands/CommandGo.java +++ /dev/null @@ -1,24 +0,0 @@ -package nogard.schritt6.commands; - -import nogard.schritt6b.Richtungen; -import nogard.schritt6b.Spieler; -import nogard.schritt6b.exceptions.SpielerZuSchwachException; - -public class CommandGo implements ICommand { - private Spieler spieler; - private Richtungen richtung; - - public CommandGo(Spieler spieler, Richtungen richtung) { - this.spieler = spieler; - this.richtung = richtung; - } - - @Override - public void execute() { - try { - spieler.geheInBereich(spieler.getBereich().getNachbar(richtung)); - } catch (SpielerZuSchwachException e) { - System.out.println(e.getMessage()); - } - } -} diff --git a/src/nogard/schritt6/commands/CommandHelp.java b/src/nogard/schritt6/commands/CommandHelp.java deleted file mode 100644 index 732f8c7..0000000 --- a/src/nogard/schritt6/commands/CommandHelp.java +++ /dev/null @@ -1,8 +0,0 @@ -package nogard.schritt6.commands; - -public class CommandHelp implements ICommand { - @Override - public void execute() { - System.out.println("AAAAAAAAAAAAAa"); - } -} diff --git a/src/nogard/schritt6/commands/CommandInfo.java b/src/nogard/schritt6/commands/CommandInfo.java deleted file mode 100644 index 25f50dc..0000000 --- a/src/nogard/schritt6/commands/CommandInfo.java +++ /dev/null @@ -1,16 +0,0 @@ -package nogard.schritt6.commands; - -import nogard.schritt6b.Spieler; - -public class CommandInfo implements ICommand { - private final Spieler spieler; - - public CommandInfo(Spieler spieler) { - this.spieler = spieler; - } - - @Override - public void execute() { - System.out.println(spieler.getInfo()); - } -} diff --git a/src/nogard/schritt6/commands/CommandPut.java b/src/nogard/schritt6/commands/CommandPut.java deleted file mode 100644 index 948ee8a..0000000 --- a/src/nogard/schritt6/commands/CommandPut.java +++ /dev/null @@ -1,23 +0,0 @@ -package nogard.schritt6.commands; - -import nogard.schritt6b.Spieler; -import nogard.schritt6b.exceptions.GegenstandNichtVorhandenException; - -public class CommandPut implements ICommand { - private Spieler spieler; - private String gegenstand; - - public CommandPut(Spieler spieler, String gegenstand) { - this.spieler = spieler; - this.gegenstand = gegenstand; - } - - @Override - public void execute() { - try { - spieler.ablegenGegenstand(gegenstand); - } catch (GegenstandNichtVorhandenException e) { - System.out.println(e.getMessage()); - } - } -} diff --git a/src/nogard/schritt6/commands/CommandQuit.java b/src/nogard/schritt6/commands/CommandQuit.java deleted file mode 100644 index 92e682a..0000000 --- a/src/nogard/schritt6/commands/CommandQuit.java +++ /dev/null @@ -1,8 +0,0 @@ -package nogard.schritt6.commands; - -public class CommandQuit implements ICommand { - @Override - public void execute() { - - } -} diff --git a/src/nogard/schritt6/commands/CommandTake.java b/src/nogard/schritt6/commands/CommandTake.java deleted file mode 100644 index 969ebb3..0000000 --- a/src/nogard/schritt6/commands/CommandTake.java +++ /dev/null @@ -1,24 +0,0 @@ -package nogard.schritt6.commands; - -import nogard.schritt6b.Spieler; -import nogard.schritt6b.exceptions.GegenstandNichtVorhandenException; -import nogard.schritt6b.exceptions.GegenstandZuSchwerException; - -public class CommandTake implements ICommand { - private Spieler spieler; - private String gegenstand; - - public CommandTake(Spieler spieler, String gegenstand) { - this.spieler = spieler; - this.gegenstand = gegenstand; - } - - @Override - public void execute() { - try { - spieler.aufnehmenGegenstand(gegenstand); - } catch (GegenstandNichtVorhandenException | GegenstandZuSchwerException e) { - System.out.println(e.getMessage()); - } - } -} diff --git a/src/nogard/schritt6/commands/ICommand.java b/src/nogard/schritt6/commands/ICommand.java deleted file mode 100644 index 7c93d38..0000000 --- a/src/nogard/schritt6/commands/ICommand.java +++ /dev/null @@ -1,5 +0,0 @@ -package nogard.schritt6.commands; - -public interface ICommand { - void execute(); -} diff --git a/src/nogard/schritt6/exceptions/BefehlUnbekanntException.java b/src/nogard/schritt6/exceptions/BefehlUnbekanntException.java deleted file mode 100644 index 3145406..0000000 --- a/src/nogard/schritt6/exceptions/BefehlUnbekanntException.java +++ /dev/null @@ -1,14 +0,0 @@ -package nogard.schritt6.exceptions; - -public class BefehlUnbekanntException extends Exception { - private final String befehl; - - public BefehlUnbekanntException(String befehl) { - this.befehl = befehl; - } - - @Override - public String getMessage() { - return befehl; - } -} diff --git a/src/nogard/schritt6/exceptions/GegenstandNichtVorhandenException.java b/src/nogard/schritt6/exceptions/GegenstandNichtVorhandenException.java deleted file mode 100644 index c1ee866..0000000 --- a/src/nogard/schritt6/exceptions/GegenstandNichtVorhandenException.java +++ /dev/null @@ -1,22 +0,0 @@ -package nogard.schritt6.exceptions; - -import nogard.schritt6b.Gegenstand; - -public class GegenstandNichtVorhandenException extends Exception { - private final String text; - public GegenstandNichtVorhandenException(Gegenstand g) { - StringBuilder sb = new StringBuilder(); - sb.append(g.getInfo()); - sb.append("Ist nicht in dem Bereich vorhanden."); - text = sb.toString(); - } - - public GegenstandNichtVorhandenException(String text) { - this.text = text; - } - - @Override - public String getMessage() { - return text; - } -} diff --git a/src/nogard/schritt6/exceptions/GegenstandZuSchwerException.java b/src/nogard/schritt6/exceptions/GegenstandZuSchwerException.java deleted file mode 100644 index fe29c19..0000000 --- a/src/nogard/schritt6/exceptions/GegenstandZuSchwerException.java +++ /dev/null @@ -1,20 +0,0 @@ -package nogard.schritt6.exceptions; - -import nogard.schritt6b.Gegenstand; - -public class GegenstandZuSchwerException extends Exception { - private final String text; - - public GegenstandZuSchwerException(Gegenstand g) { - text = "Ist zu schwer: " + g.getDescription(); - } - - public GegenstandZuSchwerException(String text) { - this.text = text; - } - - @Override - public String getMessage() { - return text; - } -} diff --git a/src/nogard/schritt6/exceptions/SpielerSattException.java b/src/nogard/schritt6/exceptions/SpielerSattException.java deleted file mode 100644 index 1bdd3e9..0000000 --- a/src/nogard/schritt6/exceptions/SpielerSattException.java +++ /dev/null @@ -1,14 +0,0 @@ -package nogard.schritt6.exceptions; - -public class SpielerSattException extends Exception { - private final String text; - - public SpielerSattException(String text) { - this.text = text; - } - - @Override - public String getMessage() { - return text; - } -} diff --git a/src/nogard/schritt6/exceptions/SpielerZuSchwachException.java b/src/nogard/schritt6/exceptions/SpielerZuSchwachException.java deleted file mode 100644 index 969432e..0000000 --- a/src/nogard/schritt6/exceptions/SpielerZuSchwachException.java +++ /dev/null @@ -1,18 +0,0 @@ -package nogard.schritt6.exceptions; - -public class SpielerZuSchwachException extends Exception { - private String text; - - public SpielerZuSchwachException() { - text = "Der Spieler ist zu schwach"; - } - - public SpielerZuSchwachException(String text) { - this.text = text; - } - - @Override - public String getMessage() { - return text; - } -} diff --git a/src/strategyPattern/Bubblesort.java b/src/strategyPattern/Bubblesort.java new file mode 100644 index 0000000..5456c56 --- /dev/null +++ b/src/strategyPattern/Bubblesort.java @@ -0,0 +1,30 @@ +package strategyPattern; + +public class Bubblesort implements ISortieralgorithmus { + @Override + public double[] sortiereAbsteigend(double[] array) { + for (int i = 0; i < array.length; i++) { + for (int j = 0; j < array.length - 1; j++) { + if (array[j] < array[j + 1]) { + double temp = array[j]; + array[j] = array[j + 1]; + array[j + 1] = temp; + } + } + } + return array; + } + @Override + public double[] sortiereAufsteigend(double[] array) { + for (int i = 0; i < array.length; i++) { + for (int j = 0; j < array.length - 1; j++) { + if (array[j] > array[j + 1]) { + double temp = array[j]; + array[j] = array[j + 1]; + array[j + 1] = temp; + } + } + } + return array; + } +} diff --git a/src/strategyPattern/ISortieralgorithmus.java b/src/strategyPattern/ISortieralgorithmus.java new file mode 100644 index 0000000..f4f7122 --- /dev/null +++ b/src/strategyPattern/ISortieralgorithmus.java @@ -0,0 +1,6 @@ +package strategyPattern; + +public interface ISortieralgorithmus { + public double[] sortiereAbsteigend(double[] array); + public double[] sortiereAufsteigend(double[] array); +} \ No newline at end of file diff --git a/src/strategyPattern/Programm.java b/src/strategyPattern/Programm.java new file mode 100644 index 0000000..3de0e21 --- /dev/null +++ b/src/strategyPattern/Programm.java @@ -0,0 +1,23 @@ +package strategyPattern; + +public class Programm { + public static void main(String[] args) { + Programm programm = new Programm(); + programm.doWork(); + } + + public void doWork(){ + System.out.println("Doing work"); + ISortieralgorithmus sortieralgorithmus = new Bubblesort(); + long start = System.nanoTime(); + double[] result = sortieralgorithmus.sortiereAbsteigend(new double[]{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}); + + long end = System.nanoTime(); + long duration = (end - start); + System.out.println("Duration: " + duration / 1000 + " microseconds"); + + for (double d : result) { + System.out.println(d); + } + } +}