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

    Default Problem with multiple string in classes

    OK i have this program for a hotel check-in system for my CS class. I know i need to do things myself, and i have come far already, but still have the following problem.

    However i have a problem that i can't seem to fix. Right now one can input a surname and lastname. both strings will be combined and put into the Gast class.

    however, i have to hadle both surname and lastname seperatly. However i can't seem to get it done.

    this is the part where it puts them together in one and then send them to the rest. However, how can i make it so that two string are sent and stored throughout the program?

    Java Code:
                                    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));

    this is my 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() {
      if (kamer1.gast == null){
       System.out.println("Kamer 1: Vrij");
      }
      else 
       System.out.println("Kamer 1: " + kamer1.gast.naam); 	
    
      if (kamer2.gast == null){
       System.out.println("Kamer 2: Vrij");
      }
      else 
       System.out.println("Kamer 2: " + kamer2.gast.naam); 	
    
      if (kamer3.gast == null){
       System.out.println("Kamer 3: Vrij");
      }
      else 
       System.out.println("Kamer 3: " + kamer3.gast.naam); 	
    		
      if (kamer4.gast == null){
       System.out.println("Kamer 4: Vrij");
      }
      else 
       System.out.println("Kamer 4: " + kamer4.gast.naam); 	
     }
    	
     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("Het hotel is vol, check een gast uit.");
       return;
      }
      
     }      
    	
    void checkOut(){
    		
    		try {
    		System.out.print("Kamernummer vertrekkende gast: ");
    		Scanner kamernr = new Scanner(System.in);
    		int kamernummer = kamernr.nextInt();
    		
    		if (kamernummer == 1 && kamer1.gast != null) {	
    			System.out.println("Gast " + kamer1.gast.naam + " is uitgechecked");
    			kamer1 = new Kamer();		
    			return;
    		}
    		
    		else if (kamernummer == 1 && kamer1.gast == null) {	
    			System.out.println("De kamer is al vrij");	
    			return;
    		}
    		
    		else if (kamernummer == 2 && kamer2.gast != null) {	
    			System.out.println("Gast " + kamer2.gast.naam + " is uitgechecked");
    			kamer2 = new Kamer();		
    			return;
    		}
    		
    		else if (kamernummer == 2 && kamer2.gast == null) {	
    			System.out.println("Kamer 2 is al vrij");	
    			return;
    		}
    		
    		else if (kamernummer == 3 && kamer3.gast != null) {	
    			System.out.println("Gast " + kamer3.gast.naam + " is uitgechecked");
    			kamer3 = new Kamer();		
    			return;
    		}
    		
    		else if (kamernummer == 3 && kamer3.gast == null) {	
    			System.out.println("Kamer 3 is al vrij");	
    			return;
    		}
    		
    		else if (kamernummer == 4 && kamer4.gast != null) {	
    			System.out.println("Gast " + kamer4.gast.naam + " is uitgechecked");
    			kamer4 = new Kamer();		
    			return;
    		}
    		
    		else if (kamernummer == 4 && kamer4.gast == null) {	
    			System.out.println("Kamer 4 is al vrij");	
    			return;
    		}
    		
    		else {
    			System.out.println("Ongeldige invoer.");
    			return;
    		}
    		}
    		
    		catch (java.util.InputMismatchException e) {
    			System.out.println("Ongeldige invoer.");
    			return;																					// na foutmelding wordt de InvoerUitvoer methode opnieuw gestart
    		}
    		
    	}
    	
    }
    
    
    											
    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 == 3){
    				hotel.checkOut();	
    			}
    			
    			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
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    You Gast class should have two properties (member variables) vnaam and anaam. A Gast constructor should take two parameters, one for each name part. Only the toString() method should combine those two names. Also implement two getXName() methods, one for each name part.

    kind regards,

    Jos

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

    Default

    With your first comment you mean:?

    Java Code:
    class Gast {
     String vnaam;
     String anaam;
    	
     Gast(String nieuweVNaam, String nieuweANaam) {
      vnaam = nieuweVNaam;
      anaam = nieuweANaam
     }
    }
    "Only the toString() method should combine those two names. Also implement two getXName() methods, one for each name part." I don't understand really. Could you maybe explain a little more?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by sjaakie View Post
    "Only the toString() method should combine those two names. Also implement two getXName() methods, one for each name part." I don't understand really. Could you maybe explain a little more?
    I mean this:

    Java Code:
    public class Gast { 
       ...
       public String getVNaam() { return vnaam; }
       public String getANaam() { return anamm; }
       public String toString() { return vnaam+" "+anaam; }
    }
    Your other modifications are fine with me.

    kind regards,

    Jos

Similar Threads

  1. uploading applet with multiple classes help
    By alacn in forum New To Java
    Replies: 3
    Last Post: 08-02-2010, 06:37 AM
  2. Help with multiple frames/classes
    By Celletti in forum AWT / Swing
    Replies: 1
    Last Post: 04-28-2010, 03:18 AM
  3. How to use multiple timer classes in swings
    By theone3nu in forum AWT / Swing
    Replies: 12
    Last Post: 12-30-2008, 02:30 AM
  4. Replies: 0
    Last Post: 08-19-2008, 04:44 PM
  5. Replies: 3
    Last Post: 12-17-2007, 02:35 AM

Posting Permissions

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