Also ich hab nen Programm geschreiben, aber mir fehlen die Ausgaben Preis und ISBN

Johnny8519

Mitglied
Ich hab hier ein Programm aber es gibt mir nicht den Preis oder isbn nummer aus sondern den Namen des autors?
Java:
package grundlagenuebung;
 
import java.util.ArrayList;
import java.util.List;
 
import javax.swing.JOptionPane;
 
public class Booksorter
{
    private static final byte BOOKS_TO_SORT = 3;
   
    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 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);
       
       
                String output = createOutputString(byteSortDirectory);
                Book.output(output);
            }
       
    
    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);
        }
    }
}
 
Hi,

ich kann dir zwar nicht Helfen, aber du solltest 1. den Titel ändern (hört sich nicht wirklich Deutsch an) und 2. für deinen Quelltext die Code-Tags benutzen.

Dies kannst du tuen indem du um die Quelltexte [CODE#]Dein formatierter Quelltext[/CODE#] schreibst(das #-Zeichen löschen!). Zwischen diese Code-Tags kannst du dann den Quelltext aus deiner Entwicklungsumgebung, wo der Quelltext sicher lesbar eingerückt ist, einfügen.

Gruß
RudolfG
 
Testest Du wirklich den Code, den du hier gepostet hast?

Bei mir wird schön der Titel angezeigt, genau so wie du das auch definiert hast. Wenn du mehr Felder sehen willst, musst du deine Ausgabe entsprechend ergänzen - in createOutputString().

Kleiner Tipp: Trenne die Einleselogik von deiner Verarbeitung. Zum Testen ist es wesentlich einfacher, wenn du nicht immer von vorne die Werte von Hand eingeben musst.
 
Zurück