Also bei mir funktioniert er einwandfrei, auch wenn dann natürlich wieder die Prüfung fehlt, was eigendlich ausgegeben werden soll.
Hier noch mal komplett meiner:
Hier noch mal komplett meiner:
Java:
package core;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
public class Main
{
private static final byte BOOKS_TO_SORT = 2;
private static final byte SORT_BY_TITLE = 1;
private static final byte SORT_BY_ARTIST = 2;
private static final byte SORT_BY_ISBN = 3;
private static final byte SORT_BY_PRICE = 4;
private static final boolean OUTPUT_WAS_NAMES = true;
private static final boolean OUTPUT_WAS_ARTIST = false;
private final static int SORT_ASC = 1;
private final static int SORT_DESC = 2;
private static List<Book> sortedList;
static
{
sortedList = new ArrayList<Book>();
}
public static void main(String[] args)
{
Book[] books = new Book[BOOKS_TO_SORT];
for(byte loopCounter = 1; loopCounter <= BOOKS_TO_SORT; loopCounter++)
{
String title = JOptionPane.showInputDialog("Bitte geben Sie den Titel des "+loopCounter+". Buches ein.");
String artist = JOptionPane.showInputDialog("Bitte geben Sie den Author des "+loopCounter+". Buches " +
"ein.");
String isbn = JOptionPane.showInputDialog("Bitte geben Sie die ISBN Nummer des "+loopCounter+". Buches" +
" ein.");
String price = JOptionPane.showInputDialog("Bitte geben Sie den Preis des "+loopCounter+". Buches ein.");
long filteredLongISBN = filterAndParseStringToLong(isbn, '-');
double floatingPointPrice = Double.parseDouble(price);
books[loopCounter - 1] = new Book(title, artist, filteredLongISBN, floatingPointPrice);
}
String sortTyp = JOptionPane.showInputDialog("Nach welcher Kathegorie sollen die Bücher sortiert werden?" +
System.getProperty("line.separator") +
"(Titel = 1, Autor = 2, ISBN = 3, Preis = 4)");
byte byteSortTyp = Byte.parseByte(sortTyp);
String sortDirectory = JOptionPane.showInputDialog("In welche Richtung sortieren?" +
System.getProperty("line.separator") +
"(Aufsteigend = 1, Absteigend = 2)");
byte byteSortDirectory = Byte.parseByte(sortDirectory);
for(Book book:books)
setBookInRow(book, byteSortTyp);
if(OUTPUT_WAS_NAMES ^ OUTPUT_WAS_ARTIST)
{
for(Book book:sortedList)
{
String output = createOutputString(byteSortDirectory);
Book.output(output);
}
}
else
throw new Error("Illegal Output-Mode selected. (Non Output or both Output selected, possible is only " +
"XOR Output Mode.");
}
private static void setBookInRow(Book book, byte sortTyp)
{
if(sortedList.size() == 0)
sortedList.add(book);
else
{
int positionToAdd = 0;
boolean breakLoop = false;
if(sortedList.size() == 0)
sortedList.add(book);
{
for(byte loopCounter = 0, size = (byte) sortedList.size(); (loopCounter < size) && (!breakLoop);
loopCounter++)
{
Book loopBook = sortedList.get(loopCounter);
if(sortTyp == SORT_BY_ARTIST ^ sortTyp == SORT_BY_TITLE)
{
String lexicographicalCompareString = null;
if(sortTyp == SORT_BY_TITLE)
{
lexicographicalCompareString = book.getTitle();
if(lexicographicalCompareString.compareTo(loopBook.getTitle()) <= 0)
{
positionToAdd = loopCounter;
breakLoop = true;
}
}
else if(sortTyp == SORT_BY_ARTIST)
{
lexicographicalCompareString = book.getArtist();
if(lexicographicalCompareString.compareTo(loopBook.getArtist()) <= 0)
{
positionToAdd = loopCounter;
breakLoop = true;
}
}
}
else if(sortTyp == SORT_BY_ISBN)
{
long isbn = book.getISBN();
if(isbn < loopBook.getISBN())
{
positionToAdd = loopCounter;
breakLoop = true;
}
}
else if(sortTyp == SORT_BY_PRICE)
{
double price = book.getPrice();
if(price < loopBook.getPrice())
{
positionToAdd = loopCounter;
breakLoop = true;
}
}
}
}
sortedList.add(positionToAdd, book);
}
}
private static String createOutputString(byte sortDirectory)
{
String message = null;
switch(sortDirectory)
{
case(SORT_ASC):
message = "Absteigend sortiert:" + System.getProperty("line.separator");
for(int i = 0; i < sortedList.size(); i++)
message+= sortedList.get(i).getTitle() + System.getProperty("line.separator");
break;
case(SORT_DESC):
message = "Aufsteigend sortiert:" + System.getProperty("line.separator");
for(int i = sortedList.size() - 1; i >= 0; i--)
message+= sortedList.get(i).getTitle() + System.getProperty("line.separator");
break;
}
return message;
}
private static long filterAndParseStringToLong(String stringToFilter, char charToRemove)
{
String result = null;
for(byte b = 0, strSize = (byte) stringToFilter.length(); b < strSize; b++)
{
char c = stringToFilter.charAt(b);
if(c != charToRemove)
{
String s = String.valueOf(c);
if(result != null)
result+= s;
else
result = s;
}
}
return(Long.parseLong(result));
}
private static class Book
{
private String title;
private String artist;
private long isbn;
private double price;
public Book(String title, String artist, long filteredLongISBN, double floatingPointPrice)
{
super();
this.title = title;
this.artist = artist;
this.isbn = filteredLongISBN;
this.price = floatingPointPrice;
}
private String getTitle()
{
return(title);
}
private String getArtist()
{
return(artist);
}
private long getISBN()
{
return(isbn);
}
private double getPrice()
{
return(price);
}
public static void output(String output)
{
System.out.println(output);
}
}
}