mirror of
https://github.com/DerTyp7/lf05-java.git
synced 2025-10-29 12:32:12 +01:00
Sortieralgoritmus
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
package nogard.schritt5b;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class BefehlFactory {
|
||||
|
||||
public static Befehl createBefehl(String input) throws BefehlUnbekanntException {
|
||||
|
||||
@@ -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 <20>ber Ausg<73>nge verbunden. M<>gliche Ausg<73>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<Richtungen, Bereich> nachbarn;
|
||||
private final LinkedList<Gegenstand> 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<Richtungen, Bereich> 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();
|
||||
}
|
||||
}
|
||||
@@ -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";
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package nogard.schritt6;
|
||||
|
||||
import nogard.schritt6.Spiel;
|
||||
|
||||
public class Program {
|
||||
|
||||
public static void main(String[] args) {
|
||||
new Spiel().spielen();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package nogard.schritt6;
|
||||
|
||||
public enum Richtungen {
|
||||
NORTH, EAST, SOUTH, WEST, UP, DOWN
|
||||
}
|
||||
@@ -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<69>end startet das Spiel.
|
||||
* - Sie wertet die Befehle aus und sorgt f<>r ihre Ausf<73>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<67><72>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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<Gegenstand> 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();
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
package nogard.schritt6.commands;
|
||||
|
||||
public class CommandHelp implements ICommand {
|
||||
@Override
|
||||
public void execute() {
|
||||
System.out.println("AAAAAAAAAAAAAa");
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
package nogard.schritt6.commands;
|
||||
|
||||
public class CommandQuit implements ICommand {
|
||||
@Override
|
||||
public void execute() {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package nogard.schritt6.commands;
|
||||
|
||||
public interface ICommand {
|
||||
void execute();
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
30
src/strategyPattern/Bubblesort.java
Normal file
30
src/strategyPattern/Bubblesort.java
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
6
src/strategyPattern/ISortieralgorithmus.java
Normal file
6
src/strategyPattern/ISortieralgorithmus.java
Normal file
@@ -0,0 +1,6 @@
|
||||
package strategyPattern;
|
||||
|
||||
public interface ISortieralgorithmus {
|
||||
public double[] sortiereAbsteigend(double[] array);
|
||||
public double[] sortiereAufsteigend(double[] array);
|
||||
}
|
||||
23
src/strategyPattern/Programm.java
Normal file
23
src/strategyPattern/Programm.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user