The Artima Developer Community
Sponsored Link

Java Answers Forum
Try this one!! I need help.

4 replies on 1 page. Most recent reply: May 10, 2003 9:11 AM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
anthony hutchinson

Posts: 5
Nickname: hutchae1
Registered: Apr, 2003

Try this one!! I need help. Posted: May 6, 2003 5:53 AM
Reply to this message Reply
Advertisement
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.


James Patterson

Posts: 16
Nickname: jmep
Registered: Mar, 2003

Re: Try this one!! I need help. Posted: May 8, 2003 6:28 AM
Reply to this message Reply
What exactly is your question?

anthony hutchinson

Posts: 5
Nickname: hutchae1
Registered: Apr, 2003

Re: Try this one!! I need help. Posted: May 9, 2003 4:56 PM
Reply to this message Reply
The entire thing is the question. I am not sure how to do this.

Singh M.

Posts: 154
Nickname: ms
Registered: Mar, 2002

Re: Try this one!! I need help. Posted: May 10, 2003 2:10 AM
Reply to this message Reply
Bill, why not start a forum for Java Assignments?

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Try this one!! I need help. Posted: May 10, 2003 9:11 AM
Reply to this message Reply
/*   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.
 */
public class 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++;
                    }else if (bookLocation.equals("Main Floor")){
                        mainFloor++;
                    }else if (bookLocation.equals("Archives")){
                        archives++;
                    }else if (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.
     */
    public static void 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";
            }else if ((bookNumber >= 200) && (bookNumber <= 500)){
                location = "Main Floor";
            }else if ((bookNumber >= 501) && (bookNumber <= 699)){
                location = "Upper Floor";
            }else if ((bookNumber >= 700) && (bookNumber <= 750)){
                location = "Archives";
            }else if ((bookNumber >= 751) && (bookNumber <= 900)){
                location = "Upper Floor";
            }else if ((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.
     */
    private int 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;
    }
}

Flat View: This topic has 4 replies on 1 page
Topic: JTable Question? Previous Topic   Next Topic Topic: Stuck writing code for arrayCopyOfQueue method

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use