Results 1 to 10 of 10
  1. #1
    Lorelei7 is offline Member
    Join Date
    Nov 2013
    Posts
    14
    Rep Power
    0

    Default demo side not pulling "style" from main class program ?

    Hello - I have a project here with 2 classes and a demo side program to test the classes. The "genre" variable is first cast from a string to an int to use in a while validation statement. This works. However, It is not returning the genre name from the BookChoice Class switch statement as a genre name - it is either blank or uses the numerical value. I have placed all three programs here. I don't know what to do.
    LIBRARY RESERVE CLASS CODE - NO PROBLEMS WITH IT:
    Java Code:
     
    public class LibraryReserve {
    
    	private String name, stAddress, email, cityStZip, phone, cardNumber;
    	
    	public LibraryReserve()
    		{
    		name = "";
    		stAddress = "";
    		cityStZip = "";
    		email = "";
    		phone = "";
    		cardNumber = "";
    		}
    	public LibraryReserve(String theName, String theStAddress, String theCityStZip, String theEmail, String theCardNumber, String thePhone)
    	{
    		name = theName;
    		stAddress = theStAddress;
    		cityStZip = theCityStZip;
    		email = theEmail;
    		phone = thePhone;
    		cardNumber = theCardNumber;
    	}
    	public void setName(String theName)
    	{
    		name = theName;
    	}
    	public void setStAddress(String theStAddress)
    	{
    		stAddress = theStAddress;
    	}
    	public void setCityStZip(String theCityStZip)
    	{
    		cityStZip = theCityStZip;
    	}
    	public void setEmail(String theEmail)
    	{
    		email = theEmail;
    	}
    	public void setPhone(String thePhone)
    	{
    		phone = thePhone;
    	}
    	public void setCardNumber(String theCardNumber)
    	{
    		cardNumber = theCardNumber;
    	}
    	public String getName()
    	{
    		return name;
    	}
    	public String getStAddress()
    	{
    		return stAddress;
    	}
    	public String getCityStZip()
    	{
    		return cityStZip;
    	}
    	public String getEmail()
    	{
    		return email;
    	}
    	public String getPhone()
    	{
    		return phone;
    	}
    	public String getCardNumber()
    	{
    		return cardNumber;
    	}
    	
    }
    BOOK CHOICE CLASS THAT HAS THE SWITCH THAT ISN'T WORKING AND TURNING NUMBER ENTERED TO THE NAME:
    Java Code:
    public class BookChoice {
    
    	//declare instance fields/attributes
    		private String author, title, genre;
    		private int pubYear;
    		
    	//constructor method (goes here, has the same name as the class)	
    		public BookChoice(String theAuthor, String theGenre, String theTitle, int thePubYear)
    		{//regular constructor, sets all fields to default
    			genre = theGenre;
    			author = theAuthor;
    			pubYear= thePubYear;
    			title = theTitle;
    		}
    	//the compiler automatically creates a 'no-arg constructor' that assigns 0 or "" to each field depending on its type		
    		public BookChoice()
    		{//no-arg constructor
    			author = "";
    			title = "";
    			genre = "";
    			pubYear = 0;
    		}
    	//setters/mutators
    		public void setAuthor(String theAuthor)
    		{
    			author = theAuthor;
    		}
    		public void setPubDate(int thePubYear)
    		{
    			pubYear = thePubYear;
    		}
    		public void setTitle(String theTitle)
    		{
    			title = theTitle;
    		}
    		public void setGenre(String theGenre)
    		{
    			switch(genre)
    			{
    			case "1":  genre = "Mystery"; break;
    			case "2":  genre = "Science Fiction"; break;
    			case "3":  genre = "Fantasy"; break;
    			case "4":  genre = "Children"; break;
    			case "5":  genre = "How-To"; break;
    				
    			default:  break;
    			}
    		}
    	//getters/accessors
    		public String getAuthor()
    		{
    			return author;
    		}
    		public String getGenre()
    		{
    			return genre;
    		}
    		public String getTitle()
    		{
    			return title;
    		}
    		public int getPubYear()
    		{
    			return pubYear;
    		}
    }
    DEMO PROGRAM TESTING BOTH THE NO ARG AND REGULAR CONSTRUCTOR - INPUT IS NOT OUTPUTTING WITH STYLE NAME - EITHER BLANK OR THE NUMBER:
    Java Code:
    import javax.swing.JOptionPane;
    
    public class BookReserve {
    	
    		public static void main(String[] args)
    			{
    			//variables
    				String author, title, strPubYear, strGenre;
    				String name, stAddress, email, cityStZip, cardNumber, phone;
    				int genre, pubYear;
    					
    			//instantiate first order objects to test the no-arg constructor
    				BookChoice bookChoice1 = new BookChoice();
    				LibraryReserve customer1 = new LibraryReserve();
    			//get input for bookChoice1
    					
    				name = JOptionPane.showInputDialog("Please enter your name:  ");
    				stAddress = JOptionPane.showInputDialog("Please enter your street address:  "); 
    				cityStZip = JOptionPane.showInputDialog("Please enter your city,state and zip code in this format:  'City, ST #####'");
    				email = JOptionPane.showInputDialog("Please enter your email address: ");
    				phone = JOptionPane.showInputDialog("Please enter your phone number with no dashes or spacing:  ");
    				cardNumber = JOptionPane.showInputDialog("Please enter your library card number:  ");
    				strGenre = JOptionPane.showInputDialog	("Please enter the genre:\n" +
    															"1 - Mystery\n" +
    															"2 - Science Fiction\n" +
    															"3 - Fantasy\n" +
    															"4 - Children\n" +
    															"5 - How-to\n");
    				genre = Integer.parseInt(strGenre);
    			//check to see if the genre is invalid
    				while (genre < 1 || genre > 5) //loops when TRUE
    					{
    					JOptionPane.showMessageDialog(null, "The number you chose is not a valid input.");
    			//get input again
    					strGenre = JOptionPane.showInputDialog("Please enter the genre:\n" +
    																"1 - Mystery\n" +
    																"2 - Science Fiction\n" +
    																"3 - Fantasy\n" +
    																"4 - Children\n" +
    																"5 - How-to\n");
    					genre = Integer.parseInt(strGenre);
    					}
    				author = JOptionPane.showInputDialog("Please enter the author");
    				title = JOptionPane.showInputDialog	("Please enter the title");
    				strPubYear = JOptionPane.showInputDialog("Please enter the year published");
    
    			//cast data
    				
    				pubYear = Integer.parseInt(strPubYear);
    			
    			// instance methods (used for no-arg constructor)
    				
    				customer1.setName(name);
    				customer1.setStAddress(stAddress);
    				customer1.setCityStZip(cityStZip);
    				customer1.setEmail(email);
    				customer1.setPhone(phone);
    				customer1.setCardNumber(cardNumber);
    				
    				bookChoice1.setGenre	(strGenre);
    				bookChoice1.setAuthor	(author);
    				bookChoice1.setTitle	(title);
    				bookChoice1.setPubDate	(pubYear);
    					
    			// info for regular constructor
    				
    				name = JOptionPane.showInputDialog("Please enter your name:  ");
    				stAddress = JOptionPane.showInputDialog("Please enter your street address:  "); 
    				cityStZip = JOptionPane.showInputDialog("Please enter your city,state and zip code in this format:  'City, ST #####'");
    				email = JOptionPane.showInputDialog("Please enter your email address: ");
    				phone = JOptionPane.showInputDialog("Please enter your phone number with no dashes or spacing:  ");
    				cardNumber = JOptionPane.showInputDialog("Please enter your library card number:  ");	
    				strGenre = JOptionPane.showInputDialog("Please enter the genre:\n" +
    							"1 - Mystery\n" +
    							"2 - Science Fiction\n" +
    							"3 - Fantasy\n" +
    							"4 - Children\n" +
    							"5 - How-to\n");
    						genre = Integer.parseInt(strGenre);
    			//check to see if the genre is invalid
    				while (genre < 1 || genre > 5) //loops when TRUE
    					{
    						JOptionPane.showMessageDialog(null, "That is not a valid input.");
    			//get input again
    						strGenre = JOptionPane.showInputDialog("Please enter the genre:\n" +
    								"1 - Mystery\n" +
    								"2 - Science Fiction\n" +
    								"3 - Fantasy\n" +
    								"4 - Children\n" +
    								"5 - How-to\n");
    						genre = Integer.parseInt(strGenre);
    					}
    				author = JOptionPane.showInputDialog("Please enter the author");
    				title = JOptionPane.showInputDialog	("Please enter the title");
    				strPubYear = JOptionPane.showInputDialog("Please enter the year published");
    
    			//cast data
    
    				pubYear = Integer.parseInt(strPubYear);
    				
    			//instantiate second book objects
    				
    				BookChoice bookChoice2 = new BookChoice(author, strGenre, title, pubYear);
    				LibraryReserve customer2 = new LibraryReserve(name, stAddress, cityStZip, email, cardNumber, phone);
    					
    					
    			//output for bookChoice1
    					JOptionPane.showMessageDialog(null, "BOOK RESERVATION #1:\n\n" +
    							"CUSTOMER NAME:  " + customer1.getName() + "\n" +
    							"ADDRESS:  " + customer1.getStAddress() + "\n" +
    							"PHONE:  " + customer1.getPhone() + "\n" +
    							"EMAIL:  " + customer1.getEmail() + "\n" +
    							"LIBRARY CARD #: " + customer1.getCardNumber() + "\n" + 
    							"TITLE:  " + bookChoice1.getTitle() + "\n" +
    							"AUTHOR:  " + bookChoice1.getAuthor() + "\n" +
    							"YEAR PUBLISHED: " + bookChoice1.getPubYear() + "\n" +
    							"GENRE:  " + bookChoice1.getGenre() + "\n" +
    							"This book has been reserved for you.  Please stop by the library within \n" +
    							"the next 72 hours to retrieve your reservation.  If this reservation is \n" +
    							"not picked up within that 72 hours, it will be returned to circulation. \n" +
    							"Thank you for using the Java Library." );
    					
    			//output for bookChoice2
    					JOptionPane.showMessageDialog(null, "BOOK RESERVATION #2:\n\n" +
    							"CUSTOMER NAME:  " + customer2.getName() + "\n" +
    							"ADDRESS:  " + customer2.getStAddress() + "\n" +
    							"PHONE:  " + customer2.getPhone() + "\n" +
    							"EMAIL:  " + customer2.getEmail() + "\n" +
    							"LIBRARY CARD #:  " + customer2.getCardNumber() + "\n" + 
    							"TITLE:  " + bookChoice2.getTitle() + "\n" +
    							"AUTHOR:  " + bookChoice2.getAuthor() + "\n" +
    							"YEAR PUBLISHED:  " + bookChoice2.getPubYear() + "\n" +
    							"GENRE:  " + bookChoice2.getGenre() + "\n" +
    							"This book has been reserved for you.  Please stop by the library within \n" +
    							"the next 72 hours to retrieve your reservation.  If this reservation is \n" +
    							"not picked up within that 72 hours, it will be returned to circulation. \n" +
    							"Thank you for using the Java Library." );
    					
    	}
    }
    Last edited by Lorelei7; 11-24-2013 at 10:35 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: demo side not pulling "style" from main class program ?

    The "style" variable is first cast from a string to an int
    That is not possible. Use the Integer class's parse method to convert a String to an int.
    Where is the style variable? I do not see it.

    It is not returning the style name from the BookChoice Class switch statment
    switch statements do NOT return a value.

    You should always have the default: case of a switch statement print out an error message to show unexpected value.
    Don't ignore it.
    Last edited by Norm; 11-24-2013 at 09:53 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Lorelei7 is offline Member
    Join Date
    Nov 2013
    Posts
    14
    Rep Power
    0

    Default Re: demo side not pulling "style" from main class program ?

    Norm, I am SO SO Sorry, I will have to go and edit my post, I meant to say (got another program confused with this one) that the GENRE is cast to an integer to use in the while validation statement. then i enter the query input for the genre as 1-5 and the output doesn't return it as the NAME of the genre from the switch statement. It returns nothing in the message output or the number I enter, instead of the name of the genre.

  4. #4
    Lorelei7 is offline Member
    Join Date
    Nov 2013
    Posts
    14
    Rep Power
    0

    Default Re: demo side not pulling "style" from main class program ?

    Also I did cast it using the integer class parse right below the genre input both places. (IDK why the format isn't showing the number lines... )
    The switch statement is supposed to SWITCH the value entered from the 1-5 to the name of the genre.. and it is coded as such, but not working. The while validation loop is used to print the error message for entering something outside of the 1-5. That is why the "error" message isn't on the default. Even with it on the default, it isn't showing the switch when using an appropriate number (1-5).
    Last edited by Lorelei7; 11-24-2013 at 10:40 PM.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: demo side not pulling "style" from main class program ?

    That is why the "error" message isn't on the default.
    Until the code is fully debugged, don't assume it is working correctly. A print statement in the default: can show unexpected values.

    If you added a print statement, did it print the unexpected value?
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    Lorelei7 is offline Member
    Join Date
    Nov 2013
    Posts
    14
    Rep Power
    0

    Default Re: demo side not pulling "style" from main class program ?

    the output message dialog boxes show no value, number or genre name on the first round and on the second it shows a numerical value, not the switch genre name.

    Norm, I did a println statement in multiple places and all I am getting is a number, for the choice that entered. Seems the switch is not working. The printout for the first set doesn't even show the number in the message box under Genre. It is blank.
    Last edited by Lorelei7; 11-24-2013 at 11:36 PM.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: demo side not pulling "style" from main class program ?

    Can you copy what the program's debug println statements print out and paste it here?
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    Lorelei7 is offline Member
    Join Date
    Nov 2013
    Posts
    14
    Rep Power
    0

    Default Re: demo side not pulling "style" from main class program ?

    Here is the output:

    2 2
    2 2 ****

    Here are the println statements with the line numbers of placement:
    Java Code:
    			
    System.out.println(strGenre + " " + genre);   //line 33
    
    System.out.println(strGenre + " " + genre + " **" + bookChoice1.getGenre() + "**" );  //line 68

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: demo side not pulling "style" from main class program ?

    Does what is printed look ok?

    I've lost track of what the problem is. Is the print out related to the problem?
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    Lorelei7 is offline Member
    Join Date
    Nov 2013
    Posts
    14
    Rep Power
    0

    Default Re: demo side not pulling "style" from main class program ?

    Norm, thank you for your help. We finally got it sorted out. Silly errors. Many apologies...

Similar Threads

  1. Replies: 1
    Last Post: 11-02-2012, 11:38 PM
  2. Replies: 2
    Last Post: 06-20-2012, 12:18 AM
  3. Replies: 11
    Last Post: 08-18-2011, 10:54 PM
  4. Replies: 5
    Last Post: 06-22-2010, 03:51 PM
  5. Replies: 6
    Last Post: 05-18-2010, 05:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •