4 Replies - 6252 Views - Last Post: 11 May 2008 - 08:27 PM Rate Topic: -----

#1 xaosotter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-May 08

object arrays, user input w/ scanner, calling methods, Oh My!

Post icon  Posted 11 May 2008 - 07:40 PM

Hi, I need a little (read lot) of help with this program.
Basically it needs to have an array of objects representing a hotel and the objects are rooms. The catch: only 101-120, 201-220, on so on to 801-820 can actually be referenced.
The program is to print to screen a menu asking the user to make a choice.
If they choose checkin the program prints a list of all unoccupied rooms in that range, the user then selects an empty room and enters the room number, which calls the room constructor and takes the users input to build the room object and insert it into that index of the array ie; room 101 would go in Room[101]. It will then print to screen the string containg the variable values for that object and then print to screen the menu again.
if they choose check out it needs to ask for the room number and look to see if that room number is correct ie; actually occupied
if it is not then it will print to screen all occupied rooms with the string that contains the room values.
if it is the correct room then it needs to calculate the cost of the stay from the variables in the room, print to screen the total amount and then empty the room, and go back to the menu.
The other three choices are exit, print all full rooms, and print all empty rooms which I have already described in the first two.
I am not posting the RoomException class but I am posting the other two, the Room class and the Hotel class. I am not breaking it up because then I would have to explain what each method that I was calling does ect, over and over.
I asked my questions inside the code in the format of comments so. They are uppercase and red.
Any and all help is appreciated,
Thanks
import java.util.*;
import java.io.*;

public class Room{
//variable declaration
 private int iDays;
 private int iRoomNumber;
 private double dCostPerDay;
 private String sGuest;
 String s1 = " ";
 String s2 = " ";
 Object Room;

 /**
  *instance variables for Room object
  * @param iRoomNumber
  * @param sGuest
  * @param dCostPerDay
  * @param iDays
  * @throws java.lang.Exception
  */
 public Room(int iRoomNumber, String sGuest, double dCostPerDay, int iDays)
		  throws Exception{
	 this.setiRoomNumber (iRoomNumber);
	 this.setiDays (iDays);
	 this.setsGuest (sGuest);
	 this.setdCostPerDay (dCostPerDay);
 
//SHOULD THERE BE A RETURN STATEMENT HERE?????

 }//closes constructor


	Room(Object object) {
		throw new UnsupportedOperationException("Not yet implemented");

//NETBEANS SUGGESTED THAT I ADD THIS DO I NEED IT????	  

	}
   
  public int getiRoomNumber( ){
	 return this.iRoomNumber;
  }//closes getiRoomNumber 
 
/**
 * setter method for the room number,
 * it throws a InputMismatchException when the user
 * enters anything but an integer
 * it throws a RoomException if the integer entered is
 * not in one of the ranges provided  
 * @param newiRoomNumber
 * @throws java.lang.Exception
 */
 
//SHOULD I HAVA A TRY CATCH STATEMENT FOR THE
//InputMismatchException IN THE setiRoomNumber METHOD???

  public void setiRoomNumber(int newiRoomNumber)throws Exception{
	 if((iRoomNumber>=101)&&(iRoomNumber<=120)){
	   this.iRoomNumber= newiRoomNumber; 
	 }
	 if((iRoomNumber>=201)&&(iRoomNumber<=220)){
		this.iRoomNumber= newiRoomNumber;
	 }
	 if((iRoomNumber>=301)&&(iRoomNumber<=320)){
		this.iRoomNumber= newiRoomNumber;
	 }
	 if((iRoomNumber>=401)&&(iRoomNumber<=420)){
		this.iRoomNumber= newiRoomNumber;
	 }
	 if((iRoomNumber>=501)&&(iRoomNumber<=520)){
		this.iRoomNumber= newiRoomNumber;
	 }
	 if((iRoomNumber>=601)&&(iRoomNumber<=620)){
		this.iRoomNumber= newiRoomNumber;
	 }
	 if((iRoomNumber>=701)&&(iRoomNumber<=720)){
		this.iRoomNumber= newiRoomNumber;
	 }
	 if((iRoomNumber>=801)&&(iRoomNumber<=820)){
		this.iRoomNumber= newiRoomNumber;
	 }
	 else{
		RoomException re = new RoomException( );
		re.setMessage("Sorry this is not a valid room number, "
			+ " please try again");
		throw re;
	 }
  }//closes setiRoomNumber

  public String getsGuest(){
	 return this.sGuest;
  }//closes getsGuest method

/**
 * setter method for guest name,
 * the name cannot be less than two digits or it
 * throws a RoomExceptuon
 * @param newsGuest
 * @throws java.lang.Exception
 */

//SHOULD I HAVE A TRY CATCH FOR THE InputMismatchException
// IN THE setsGuest METHOD???

  public void setsGuest (String newsGuest)throws Exception{
	 if(newsGuest.length( )>=2){
		this.sGuest= newsGuest;
	 }
	 else{
		RoomException re = new RoomException( );
		re.setMessage("This is not a valid name, please try again.");
		throw re;		
	 }
  }//closes setsGuest method

  public double getdCostPerDay(){
	 return this.dCostPerDay;
  }//closes getdCostPerDay
 
/**
 * setter method for the cost per day. throw a RoomException if
 * the user enters a negative number.
 * @param newdCostPerDay
 * @throws java.lang.Exception
 */
 
//SHOULD I HAVE A TRY CATCH FOR THE InputMismatchException
// IN THE setdCostPerDay METHOD???

  public void setdCostPerDay (double newdCostPerDay)throws Exception{
	  if(dCostPerDay>=0){
		 this.dCostPerDay= newdCostPerDay;
	  }
	  else{
		RoomException re = new RoomException( );
		re.setMessage("Sorry the cost per day cannot be a negative amount, "
		   + " please try again.");
		throw re;		 
	  }
  }//closes setdCostPerDay method

  public int getiDays(){
	 return this.iDays;
  }//closes getiDays method
/**
 * setter method for setting the number of days the guest is
 * staying. Throws RoomException if user enters a 0 or a half day.
 * @param newiDays
 * @throws javaapplication3.RoomException
 */
 
//SHOULD I HAVE A TRY CATCH FOR THE InputMismatchException
// IN THE setiDays METHOD???

  public void setiDays(int newiDays)throws RoomException{
	 if(iDays>=1){
	   this.iDays= newiDays;
	 }
	 else{
	   RoomException re = new RoomException( );
	   re.setMessage("Sorry you cannot register for less than a day, "
		   + " please try again.");
	   throw re;	   
	 }
  }//closes setiDays method
 
   @Override
  
//CAN I HAVE THIS RETURN TO TWO DIFFERENT VARIBLES S1 AND S2?
//OR DO I NEED TO HAVE THEM BOTH RETURNING THE SAME, EXAMPLE S;
//AND THEN WHEN I CALL THEM IN THE MAIN METHOD INCLUDING IN THE PARAMETER
//THE STRING CONTENTS??? 
//EXAMPLE:  s toString(iRoomNumber, dCostPerDay);

  public String toString(){	
	 if(!(Room.equals (null))){
		 s1 = s1 + "Room Number: " + this.iRoomNumber;
		 s1 = s1 + "\nGuest Name: " + this.sGuest;
		 s1 = s1 + "\nCost Per Day: " + this.dCostPerDay;
		 s1 = s1 + "\nDays: " + this.iDays;
		 return s1;
	 }//closes if
	 else{
		s2 = s2 + "Room Number: " + this.iRoomNumber;
		s2 = s2 + "Cost Per Day: " + this.dCostPerDay;
		return s2;
	 }//closes else
  }//closes toString method
}



And here is the Hotel class

import java.io.*;
import java.util.*;

public class Hotel {

//WHY AM I ASKED TO DECLARE THE INSTANCE VARIABLES AGAIN?
//IS THI NECESSARY SINCE I ALREADY DECLARED THEM IN MY ROOM CLASS?

private static int iDays; private static int iNum;
private static int iRoomNumber;
public static int i;
private static Object Room[];
private static double dCostPerDay;
private static double dTotal;
private static String sGuest;
String sChoice;
static String s2 = " "; static String s1 = " ";

//ALL OF THE FOLLOWING PRIVATE STATIC VOID METHODS NETBEANS
//RECOMMENDED THAT I PUT HERE...DID NOT WANT TO RECOGNIZE METHODS
//FROM THE ROOM CLASS WITHOUT ME ADDING THESE.
//
//DO I NEED TO FILL THESE IN LIKE I DID THE ROOM CLASS?
//THOUGHT THAT THE WHOLE REASON BEHIND DOING THAT IS SO THAT I
//COULD CALL THEM FROM THERE WHEN COMPILED TOGETHER?
//SHOULD I GET RID OF THESE AND IF SO WHAT DO I NEED TO DO DIFFERENTLY
//TO BE ABLE TO CALL THEM?

   private static void Room() {
	  throw new UnsupportedOperationException("Not yet implemented");
   }

   private static void getiRoomNumber() {
	  throw new UnsupportedOperationException("Not yet implemented");
   } 

   private static void setdCostPerDay() {
	  throw new UnsupportedOperationException("Not yet implemented");
   }

   private static void setiDays() {
	  throw new UnsupportedOperationException("Not yet implemented");
   }

   private static void setiRoomNumber() {
	  throw new UnsupportedOperationException("Not yet implemented");
   }

   private static void setsGuest() {
	  throw new UnsupportedOperationException("Not yet implemented");
   }

   private static void toString(String s1) {
	  throw new UnsupportedOperationException("Not yet implemented");
   }
  
 
static Scanner sc = new Scanner(System.in);

//THIS IS MY MENU METHOD, WROTE IT SO THAT I COULD CALL IT IN THE MAIN METHOD

   public  static String printMenu(){
		 System.out.println("1. Check-in");
		 System.out.println("2. Check-out");
		 System.out.println("3. Print all occupied rooms");
		 System.out.println("4. Print empty rooms in a given floor");
		 System.out.println("0. Exit");
		 String printMenu = new String (" ");
		 return printMenu;
	  }//closes menu method

 /**
  * @param args
  * @throws java.lang.Exception
  */
 
  public static void main(String[] args)throws Exception {
	  printMenu();						//prints the menu
	  int iChoice = 0;					  //declares variable for users choice
	  System.out.print("\nPlease enter your selection from the menu:  ");
	  iChoice = sc.nextInt( );			 //gathers value from user for variable
									 //try catch block in case user inputs something other than an int.
	  try{
		 switch(iChoice){
			case 1:
			   System.out.println("\nYou have started the check-in process.");

//HERE WHEN I TRY AND CALL THIS METHOD IT BRINGS AN ERROR, I BELIEVE THAT THE
//PROBLEM IS IN THE METHOD ITSELF BUT EITHER WAY, I CAN DO IT THIS WAY, RIGHT???

			   checkIn();			   
			   break;
			case 2:
			   System.out.println("\nYou have started the check-out process.");
			   checkOut();   
			   break;
			case 3:
			   System.out.println("\nHere is a list of all occupied rooms.");
			   printFull();
			   break;
			case 4:
			   System.out.println("\nHere is a list of all un-occupied rooms.");
			   printEmpty();
			   break;
			case 0:
			   System.out.println("\nYou have chosen to exit, goodbye.");			  
			   break;
			default:
			   System.out.println("\nThat is not a valid choice, "
			   + "please try again.");
			   break;	
		 }//closes switch 
	  }//closes try
	  catch (InputMismatchException e){
		 System.out.println("\nYou did not enter a valid number,"
			 + "please try again.");  
	  }//closes catch   
   }//closes main
  
//THIS IS MY CHECK IN METHOD THAT I AM TRYING TO CALL FROM THE MAIN METHOD

   public static String checkIn() throws Exception{

//HERE I AM TRYING TO CALL THE printEmpty METHOD WITHIN THIS METHOD
//FIRST, I WANT IT TO PRINT A LIST OF ALL EMPTY ROOMS
//SHOULD I HAVE IT HERE, OR SHOULD I PUT IT BEFORE THE checkIn METHOD	
//IN THE CASE ABOVE?

	  printEmpty();
	  System.out.println("\nPlease enter the room number that "
			  + "you would like:  " + iRoomNumber);
	  iRoomNumber = sc.nextInt( );	 
	  setiRoomNumber();
	 
	  System.out.println("\nPlease enter the guests name:  " + sGuest);
	  sGuest = sc.nextLine( );
	  setsGuest();
	 
	  System.out.println("\nPlease enter the number of days staying:  "
			  + iDays);
	  iDays = sc.nextInt( );
	  setiDays();
	 
	  System.out.println("\nPlease enter the cost per day of the room:  "
			  + dCostPerDay);
	  dCostPerDay = sc.nextDouble( );
	  setdCostPerDay();

//IS THE FOLLOWING RIGHT OR AM I WAY OFF BASE?

	  System.out.println("\nHere is your information, thank you.\n");
	  toString(s1);
	  System.out.println(s1); 
	  String checkIn = new String(" ");
	  return checkIn;
   }//closes check in

//SAME QUESTION AS WITH checkIn METHOD

   public static String checkOut(){
	  getiRoomNumber();
	  Room();
		 if(Room.equals (null)){
			System.out.println("\nThat room is empty, "
				  + " please try again.");
			printFull();
		 }//closes if
		 else{
			dTotal = dCostPerDay * iDays;
			System.out.println("\nTotal Cost of Stay: $  " + dTotal);
			Room = null;
		 }//closes else
	  String checkOut = new String(" ");
	  return  checkOut;
   }//closes check out

//I DID THE printEmpty AND THE printFull METHODS DIFFERENTLY TRYING TO
//SEE WHICH ONE IS BEST (OR WILL WORK) I THINK WHERE I AM SCREWING UP IS
//THE (NULL) PART IS THIS CORRECT AND IF SO WHAT EXACTLY IS WRONG?

   public static String printEmpty()throws Exception{
	  String printEmpty = new String (" ");

	  try{
		 do{
			if(Room.equals(null)) {
			   toString(s2);	   
			   System.out.println("\n" + s2);
			   i++;
			   continue;
			}			   
		 }while(i <= 821); 

	  }//closes try
	 
	  catch(Exception e){		
	  }	
	  return  printEmpty;
   }//closes print empty

//HERE IS THE OTHER WAY THAT I TRIED TO DO IT BUT IT IS NOT WANTING ME
//TO USE THE VARIABLE int i ??? HOW SHOULD I FIX OR SHOULD I USE THE ONE
//BEFORE THIS?

   public static String printFull(){
	  for(int i = 1; i <= 820; i++){
		 if (!(Room.equals(null))) {
			toString(s1);
			System.out.println("\n" + s1);
			continue;
		 }//closes if	
	  }//closes for
	  return s1;
   }//closes print full
	
}//closes hotel 

//I HAVE NOT INCLUDED TO MUCH HERE BECAUSE I AM UNSURE OF A FEW THINGS AND
//TYPING A 160 LINES OR MORE OF REPETITIVE CODE, ONLY TO FIND OUT THAT I DON'T
//NEED IT? NOT SO GOOD. I HAVE LOOKED AND LOOKED BUT HAVE NOT BEEN ABLE TO FIND
//A GOOD EXAMPLE OR REFERENCE FOR THE KIND OF ARRAY THAT I NEED TO BUILD.
//FIRST I TRIED TO DO A 2D ARRAY WITH THE FLOORS BEING THE ROWS AND THE ROOMS
//THE COLUMNS BUT TRYING TO BE ABLE TO HAVE THE USER INPUT INTO IT AT THE INDEXS
//I COULD NOT MAKE WORK, SO I DECIDED TO DO IT SIMPLE LIKE THIS, HOWEVER STILL
//HAVING PROBLEMS,
//
// 1. DO I NEED TO DECLARE EACH OF THE ROOM OBJECTS FOR EXAMPLE rm101?
//
// 2. IF SO HOW WHAT SYNTAX DO I USE TO PUT THAT OBJECT INTO THE ARRAY?
//
// 3. IS THE SYNTAX THAT I USED BELOW OK?
//
// 4. WHAT SYNTAX DO I USE TO LET THE USER INPUT THE INFORMATION?
//
// 5. DO I NOT WORRY ABOUT IT HERE AND JUST LET THE CONSTRUCTOR FOR THE ROOM
//	DO IT? AND THEN WHEN THAT ROOM IS UPDATED WITH THE USERS INFO WILL IT
//	AUTOMATICALLY UPDATE THE OBJECT IN THE ARRAY IF I HAVE DECLARED IT?
//
// 6 . I KNOW THAT WITH AN INT ARRAY FOR EXAMPLE THAT YOU CAN DECLARE IT
//	INCREMENTLY BUT IT WILL NOT LET ME DO IT WITH OBJECTS, RIGHT?
//
// 7. AND LAST BUT NOT LEAST...CAN I HAVE THE ARRAY DOWN HERE OR SHOULD I
//	HAVE IT IN THE THE MAIN AND IF SO SHOULD I USE A METHOD LIKE I DID HERE?
//	PLEASE HELP! ALL EXAMPLES ARE ABOUT PROGRAMER ADDING INFO INTO ARRAYS NOT
//	USER DURING RUNTIME AND NONE OF THE FEW OBJECT ARRAY EXAMPLES LIKE THIS.

class Array{
   public  Array(){
   Room[] Room = new Room[821]; 
		Room[101] = new Room (101);
		Room[102] = new Room (102);
		Room[103] = new Room (103);
		Room[104] = new Room (104);
		Room[105] = new Room (105);
		Room[106] = new Room (106);
		Room[107] = new Room (107);
		Room[108] = new Room (108);			
   }
}



Is This A Good Question/Topic? 0
  • +

Replies To: object arrays, user input w/ scanner, calling methods, Oh My!

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: object arrays, user input w/ scanner, calling methods, Oh My!

Posted 11 May 2008 - 08:04 PM

A lot of things to say:

First this code is wrong

  public void setiRoomNumber(int newiRoomNumber)throws Exception{
	 if((iRoomNumber>=101)&&(iRoomNumber<=120)){
	   this.iRoomNumber= newiRoomNumber; 
	 }
	 if((iRoomNumber>=201)&&(iRoomNumber<=220)){
		this.iRoomNumber= newiRoomNumber;
	 }
	 if((iRoomNumber>=301)&&(iRoomNumber<=320)){
		this.iRoomNumber= newiRoomNumber;
	 }
	 if((iRoomNumber>=401)&&(iRoomNumber<=420)){
		this.iRoomNumber= newiRoomNumber;
	 }
	 if((iRoomNumber>=501)&&(iRoomNumber<=520)){
		this.iRoomNumber= newiRoomNumber;
	 }
	 if((iRoomNumber>=601)&&(iRoomNumber<=620)){
		this.iRoomNumber= newiRoomNumber;
	 }
	 if((iRoomNumber>=701)&&(iRoomNumber<=720)){
		this.iRoomNumber= newiRoomNumber;
	 }
	 if((iRoomNumber>=801)&&(iRoomNumber<=820)){
		this.iRoomNumber= newiRoomNumber;
	 }
	 else{
		RoomException re = new RoomException( );
		re.setMessage("Sorry this is not a valid room number, "
			+ " please try again");
		throw re;
	 }
  }//closes setiRoomNumber



can be written like

  public void setiRoomNumber(int newiRoomNumber)throws Exception{
	 if((iRoomNumber>=101)&&(iRoomNumber<=120)){
	   this.iRoomNumber= newiRoomNumber; 
	 }
	 else if((iRoomNumber>=201)&&(iRoomNumber<=220)){
		this.iRoomNumber= newiRoomNumber;
	 }
	 else if((iRoomNumber>=301)&&(iRoomNumber<=320)){
		this.iRoomNumber= newiRoomNumber;
	 }
	 else if((iRoomNumber>=401)&&(iRoomNumber<=420)){
		this.iRoomNumber= newiRoomNumber;
	 }
	 else if((iRoomNumber>=501)&&(iRoomNumber<=520)){
		this.iRoomNumber= newiRoomNumber;
	 }
	 else if((iRoomNumber>=601)&&(iRoomNumber<=620)){
		this.iRoomNumber= newiRoomNumber;
	 }
	 else if((iRoomNumber>=701)&&(iRoomNumber<=720)){
		this.iRoomNumber= newiRoomNumber;
	 }
	 else if((iRoomNumber>=801)&&(iRoomNumber<=820)){
		this.iRoomNumber= newiRoomNumber;
	 }
	 else{
		RoomException re = new RoomException( );
		re.setMessage("Sorry this is not a valid room number, "
			+ " please try again");
		throw re;
	 }
  }//closes setiRoomNumber



but better

  public void setiRoomNumber(int newiRoomNumber)throws Exception{
	 int floor = newiRoomeNumber;
	 if(floor < 1 || floor > 8)
		throw new RoomException("the floor is not valid")'
	 int room = newiRoomNumber % 100;
	if(room < 1 || room > 20)
		 throw new RoomException("Room number invalid");
	this.iRoomNumber = newiRoomNumber;
}



More to come
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: object arrays, user input w/ scanner, calling methods, Oh My!

Posted 11 May 2008 - 08:13 PM

Your RoomException class is not included...

I guess it will loo
k like that

class RoomException extends Exception {

   // you will need a constructor with the message
   RoomException(String message) {
		super(message);
   }
}



lot easier then:

RoomException e = new RoomException();
e.setMessage("Blablabla");



you can make

throw new RoomException("Invalid floor number");

OK that should keep you busy for a while
Was This Post Helpful? 0
  • +
  • -

#4 xaosotter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-May 08

Re: object arrays, user input w/ scanner, calling methods, Oh My!

Posted 11 May 2008 - 08:23 PM

Thanks!
Would that last one work with a regular array or could I use it in a 2d array with the floor being the rows and the rooms being the columns that way I would not have to have such a huge array for only 160 objects?

Oh, here is the RoomException class
import java.io.*;

public class RoomException extends Exception{
  private String message = "This is a Room Exception, please try again";
  public RoomException(){
  }
	@Override
  public String getMessage(){
	 return this.message;
  }
  public void setMessage(String message){
	  this.message = message;
  }
}


Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: object arrays, user input w/ scanner, calling methods, Oh My!

Posted 11 May 2008 - 08:27 PM

Your try is not at the goog place

	  System.out.print("\nPlease enter your selection from the menu:  ");
	  iChoice = sc.nextInt( );			 //gathers value from user for variable
									 //try catch block in case user inputs something other than an int.
	  try{
		 switch(iChoice){
			case 1:
			   System.out.println("\nYou have started the check-in process.");



this will work

String str = sc.next();
int iChoice = -1;
try {
	iChoice = Integer.parse(str);
}
catch(NumberFormatException e) {
	System.out.println("You should enter an Integer");
}

if(iChoice != -1) {
	 switch(iChoice) {
		....
	 }
}


System.out.pr
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1