Results 1 to 4 of 4
  1. #1
    sjaakie is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default problem with using classes and output

    I have a problem with my hotel check-in script. I want to build in a function that shows the guest already checked-in. The status() method want to do this however right now when i check in a person first and then check it in the menu the following is printed:

    this is what it is doing:
    Java Code:
    	void status() {
    		System.out.println(kamer1.gast);	
    	}
    and this is the out
    Gast@8020cc

    But it needs to be the name that is put in before. Any ideas?

    This is the code:

    Java Code:
    import java.util.Scanner;
    
    class Gast {
    	String naam;
    	
    	Gast(String nieuweNaam) {
    		naam = nieuweNaam;
    	}
    }
    
    class Kamer {
    	
    	Gast gast;
    	
    	Kamer() {
    		gast = null;
    	}
    
    	
    }
    
    
    
    class Hotel{
    
    	Kamer kamer1, kamer2, kamer3, kamer4;
    	
    	Hotel (){
    		kamer1 = new Kamer();	
    		kamer2 = new Kamer();
    		kamer3 = new Kamer();
    		kamer4 = new Kamer();
    	}
    	
    	void status() {
    		System.out.println(kamer1.gast);	
    	}
    	
    	void checkIn(Gast nieuweGast) {
    		
    		
    		if (kamer1.gast == null) {		
    		kamer1.gast = nieuweGast;
    		System.out.println("Gast " + nieuweGast.naam + " krijgt kamer 1");
    		return;
    		}
    		
    		else if (kamer2.gast == null) {		
    		kamer2.gast = nieuweGast;
    		System.out.println("Gast " + nieuweGast.naam + " krijgt kamer 2");
    		return;
    		}
    		
    		else if (kamer3.gast == null) {		
    		kamer3.gast = nieuweGast;
    		System.out.println("Gast " + nieuweGast.naam + " krijgt kamer 3");
    		return;
    		}
    		
    		else if (kamer4.gast == null) {		
    		kamer4.gast = nieuweGast;
    		System.out.println("Gast " + nieuweGast.naam + " krijgt kamer 4");
    		return;
    		}
    		
    		else {
    		System.out.println("hotel is vol");
    		return;
    		}
    
    		
    	}                        
    	
    }
    
    
    											
    public class bopgave5 {	
    	
    	public static void main(String[] args) {
    		
    		boolean opnieuw = false;
    		Hotel hotel = new Hotel();
    		do {
    			int invoer = menu();
    					
    			if (invoer == 1){
    				hotel.status();		
    			}
    			
    			else if (invoer == 2){
    				Scanner voornaam = new Scanner(System.in);
    				Scanner achternaam = new Scanner(System.in);
    				System.out.println("Voornaam :");
    				String vnaam = voornaam.nextLine();	
    				System.out.println("Achternaam :");
    				String anaam = achternaam.nextLine();
    				String naam = vnaam + " " + anaam;
    				
    				hotel.checkIn(new Gast(naam));
    
    				opnieuw = true;
    			}
    			
    			else if (invoer == 4)
    				opnieuw = false;
    			
    			else
    			opnieuw = true;
    		}
    		while (opnieuw == true);
    													
    	}
    	
    	public static int menu (){
    		Scanner sc = new Scanner(System.in);
    		
    		
    		System.out.println("MENU:	[1] Statusoverzicht");
    		System.out.println("	[2] Check-in");
    		System.out.println("	[3] Check-out");
    		System.out.println("	[4] Einde");
    		System.out.print("Uw invoer: ");
    		int invoer = sc.nextInt();	
    				
    		return invoer;
    		
    	}
    	
     	
    }

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    override the toString method in your Gast class or write System.out.println(kamer1.gast.naam); (it would be better if you define getters)

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by sjaakie View Post
    I have a problem with my hotel check-in script. I want to build in a function that shows the guest already checked-in. The status() method want to do this however right now when i check in a person first and then check it in the menu the following is printed:

    this is what it is doing:
    Java Code:
    	void status() {
    		System.out.println(kamer1.gast);	
    	}
    and this is the out
    Gast@8020cc
    You're trying to print a Gast object; a Gast object doesn't know how to give a String representation of itself so it inherits the toString() method from the Object class (you saw what that method does). If you want a more friendly representation of a Gast object your should override that toString() method in the Gast class. The following simple method will do:

    Java Code:
    public class Gast {
       ...
       public String toString() {
          return naam;
       }
    }
    kind regards,

    Jos

  4. #4
    sjaakie is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    thanks very much for your input and help! posted anotherquestion in another thread because it is somewhat unrealted. THanks again!

Similar Threads

  1. problem with output of code
    By amosexy in forum New To Java
    Replies: 15
    Last Post: 07-05-2010, 07:57 PM
  2. xml output problem
    By tsili in forum XML
    Replies: 2
    Last Post: 05-24-2010, 01:27 AM
  3. problem with output of jar file
    By nishant.4545 in forum Advanced Java
    Replies: 2
    Last Post: 07-04-2009, 05:00 PM
  4. Output Problem
    By jazz2k8 in forum New To Java
    Replies: 4
    Last Post: 05-26-2008, 11:59 AM
  5. Output problem
    By jvasilj1 in forum New To Java
    Replies: 0
    Last Post: 01-31-2008, 07:39 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
  •