Write a complete Java application that will determine the correct location in which to shelve a library book. The application should request the call number of a book from the user, then display the correct location of the book, with an appropriate message. A book?s location is based on its call number and is determined using the following table:
Call Numbers Location
100 to 199 basement 200 to 500 and over 900 main floor 501 to 900 except 700 to 750 upper floor 700 to 750 archives
If the user enters a call number that is greater than 999 or less than 100, the application should display an error message.The application should continue asking the user to enter call numbers and displaying either locations or error messages until the user enters zero for the call number. At that point, the application should display a message thanking the user for using the program and a listing of the number of call numbers that were entered by the user that belong in the basement, the number that belong in the archives, the number that belong on the main floor, the number that belong on the upper floor, and the number that were incorrect. The program should then end. It should not display either a location or an error message when the zero is entered.
/* LibraryLocator.java
* May 10, 2003
*/
import java.io.*;
/** LibraryLocator.java determines the correct location
* in which to shelve a library book. The application
* continues to ask the user to enter call numbers and
* displays either locations or error messages until
* the user enters zero for the call number. At that
* point, the application displays a message thanking
* the user for using the program and a listing
* of the number of call numbers that were entered
* by the user that belong in the basement, the
* number that belong in the archives, the number
* that belong on the main floor, the number that
* belong on the upper floor, and the number that
* were incorrect. The program then ends. It does
* not display either a location or an error
* message when the zero is entered.
*/
publicclass LibraryLocator{
/** Constructs a LibraryLocator which handles the
* user interaction.
*/
public LibraryLocator(){
try{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String lineRead = "";
System.out.println("Enter a book call number or 0 to stop:");
int basement = 0;
int mainFloor = 0;
int archives = 0;
int upperFloor = 0;
int errors = 0;
while (!(lineRead = br.readLine()).equals("0")){
String bookLocation = getBookLocation(lineRead);
if ((bookLocation != null) && (bookLocation.length() > 0)){
System.out.println("The library location for book number "
+ lineRead + " is " + bookLocation + ".");
if (bookLocation.equals("Basement")){
basement++;
}elseif (bookLocation.equals("Main Floor")){
mainFloor++;
}elseif (bookLocation.equals("Archives")){
archives++;
}elseif (bookLocation.equals("Upper Floor")){
upperFloor++;
}
}else{
errors++;
}
System.out.println("Enter a book call number or 0 to stop:");
}
System.out.println("Thanks for using the libray book locator.");
System.out.println("There were " + String.valueOf(basement)
+ " calls for the basement.");
System.out.println("There were " + String.valueOf(archives)
+ " calls for the archives.");
System.out.println("There were " + String.valueOf(mainFloor)
+ " calls for the main floor.");
System.out.println("There were " + String.valueOf(upperFloor)
+ " calls for the upper floor.");
System.out.println("There were " + String.valueOf(errors)
+ " incorrect calls.");
}catch(IOException ioe){
System.err.println("IOException: " + ioe.getMessage());
}
}
/** Runs the application.
*/
publicstaticvoid main(String[] args){
new LibraryLocator();
}
/** Determines locations from call numbers:
* 100 to 199 basement
* 200 to 500 and over 900 main floor
* 501 to 900 except 700 to 750 upper floor
* 700 to 750 archives
* greater than 999 or less than 100 is an error
*/
public String getBookLocation(String numberString){
int bookNumber = toInt(numberString);
String location = "";
if ((bookNumber >= 100) && (bookNumber <= 999)){
if ((bookNumber >= 100) && (bookNumber <= 199)){
location = "Basement";
}elseif ((bookNumber >= 200) && (bookNumber <= 500)){
location = "Main Floor";
}elseif ((bookNumber >= 501) && (bookNumber <= 699)){
location = "Upper Floor";
}elseif ((bookNumber >= 700) && (bookNumber <= 750)){
location = "Archives";
}elseif ((bookNumber >= 751) && (bookNumber <= 900)){
location = "Upper Floor";
}elseif ((bookNumber >= 751) && (bookNumber <= 999)){
location = "Main Floor";
}
}else{
System.out.println("Error Try again.");
}
return location;
}
/** Converts a string object to an int primitive
* or returns -1 if an invalid integer string
* is entered.
*/
privateint toInt(String s){
int n = -1;
try{
n = Integer.parseInt(s);
}catch(NumberFormatException nfe){
System.err.println("That was not a valid book number");
}
return n;
}
}