• 04-01-2011, 19:27:05
    #1
    Javadan basit bir ödev aldım fakat bir türlü işin içinden çıkamadım. Ödevin konusu basit bir kütüphane otomasyonu hocanın benden istediği database kullanmadan array kullanarak kitap ekleme eklenen kitabı arrayde tutmak ama beceremedim yardımcı olursanız sevinirim.Yazabildiğim kod aşağıda;

    
    package termproject;
    import java.util.Scanner;
    import java.util.ArrayList;
    
    public class BookList {
    
        private ArrayList<Book> books = new ArrayList<Book>();
    
    
        public void AddBook() {
            Scanner input = new Scanner(System.in);
            Book book = new Book();
            System.out.println("Enter book name:");
            book.setBookName(input.next());
            System.out.println("Enter ISBN NO:");
            book.setISBN(input.nextInt());
            System.out.println("Enter subject:");
            book.setSubject(input.next());
          
            
    
            books.add(book);
        }
    
    }
  • 06-01-2011, 16:38:52
    #2
    Eposta Aktivasyonu Gerekmekte
    Book sınıfınızı paylaşır mısınız?
  • 07-01-2011, 09:56:15
    #3
    Üyeliği durduruldu
    yaptiginiz islemle arrayde tutmussunuz gorunuyor zaten. hata mesaji aliyorsaniz ne hata mesaji aliyorsunuz?
  • 11-01-2011, 03:03:08
    #4
    Book.java

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package kitap;
    
    /**
     *
     * @author PsiCat
     */
    public class Book
    {
    
        private final String _isbn;
        private final String _name;
        private final String _subject;
    
        public Book(String isbn, String name, String subject)
        {
            _name = name;
            _isbn = isbn;
            _subject = subject;
        }
    
        public String getName()
        {
            return _name;
        }
    
        public String getISBN()
        {
            return _isbn;
        }
    
        public String getSubject()
        {
            return _subject;
        }
    
        @Override
        public String toString()
        {
            return "Book ISBN:" + _isbn + " Name:" + _name + " Subject:" + _subject;
        }
    }
    **************************

    Main.java

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package kitap;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.HashMap;
    
    /**
     *
     * @author PsiCat
     */
    public class Main
    {
    
        private enum Read
        {
    
            ISBN,
            NAME,
            SUBJECT;
        }
    
        private enum Command
        {
    
            ADD("-add"),
            GET_LIST("-getList"),
            REMOVE("-remove"),
            QUIT("-quit");
            private String _value;
    
            Command(String value)
            {
                _value = value;
            }
    
            public String getValue()
            {
                return _value;
            }
        }
        private static Main _instance;
        private HashMap<String, Book> _books = new HashMap<String, Book>();
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) throws IOException
        {
            _instance = new Main();
        }
    
        public Main() throws IOException
        {
            String commandLine = "";
    
            System.out.println(Command.ADD.getValue() + ": to register a book.");
            System.out.println(Command.GET_LIST.getValue() + ": to get a list of registered books.");
            System.out.println(Command.REMOVE.getValue() + " <id>: to remove a registered book.");
            System.out.println(Command.QUIT.getValue() + ": to quit.");
    
            InputStreamReader converter = new InputStreamReader(System.in);
            BufferedReader in = new BufferedReader(converter);
            while (true)
            {
                commandLine = in.readLine();
    
                if (commandLine.equals(Command.ADD.getValue()))
                    readBook();
                else if (commandLine.equals(Command.GET_LIST.getValue()))
                    printList();
                else if (commandLine.equals(Command.REMOVE.getValue()))
                    removeBook();
                else if (commandLine.equals(Command.QUIT.getValue()))
                    kill();
                else
                    System.out.println("Invalid command.");
            }
        }
    
        private void readBook() throws IOException
        {
            Book book;
            String commandLine = "";
    
            InputStreamReader converter = new InputStreamReader(System.in);
            BufferedReader in = new BufferedReader(converter);
    
            Read read = Read.ISBN;
            String isbn = null;
            String name = null;
            String subject = null;
            System.out.print("ISBN: ");
            while (true)
            {
                commandLine = in.readLine();
    
                if (commandLine != null && read == Read.ISBN)
                {
                    isbn = commandLine;
                    System.out.print("Name: ");
                    read = Read.NAME;
                } else if (commandLine != null && read == Read.NAME)
                {
                    name = commandLine;
                    System.out.print("Subject: ");
                    read = Read.SUBJECT;
                } else if (commandLine != null && read == Read.SUBJECT)
                {
                    subject = commandLine;
                    break;
                }
            }
    
            book = new Book(isbn, name, subject);
            registerBook(book);
        }
    
        private void registerBook(Book book)
        {
            if (!_books.containsKey(book.getISBN()))
            {
                _books.put(book.getISBN(), book);
                System.out.println("Book has successfully registered.");
            } else
            {
                System.out.println("Book is already registered.");
            }
            System.out.println("\n");
        }
    
        private void printList()
        {
            if (!_books.isEmpty())
            {
                System.out.println("List of registered books:");
                System.out.println(getList());
            } else
            {
                System.out.println("No books registered yet.");
            }
        }
    
        private void removeBook() throws IOException
        {
            String commandLine = "";
    
            InputStreamReader converter = new InputStreamReader(System.in);
            BufferedReader in = new BufferedReader(converter);
    
            String isbn = null;
            System.out.print("ISBN: ");
            while (true)
            {
                commandLine = in.readLine();
    
                if (commandLine != null)
                {
                    isbn = commandLine;
                    break;
                }
            }
    
            removeBook(isbn);
        }
    
        private void removeBook(String isbn)
        {
            if (_books.containsKey(isbn))
            {
                _books.remove(isbn);
                System.out.println("Book removed.");
            } else
            {
                System.out.println("No such book exist.");
            }
        }
    
        private Book getBook(String isbn)
        {
            Book book = null;
    
            if (_books.containsKey(isbn))
                book = _books.get(isbn);
    
            return book;
        }
    
        private String getList()
        {
            String list = new String();
    
            for (Book book : _books.values())
            {
                list += "ISBN:" + book.getISBN() + " ";
                list += "Name:" + book.getName() + " ";
                list += "Subject:" + book.getSubject() + "\n";
            }
    
    
            return list;
        }
    
        private void kill()
        {
            System.out.println("Terminating");
            System.exit(0);
        }
    }