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);
}
}