Results 1 to 4 of 4
- 10-10-2010, 01:59 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 8
- Rep Power
- 0
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; } }
- 10-10-2010, 02:03 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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
- 10-10-2010, 02:11 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 8
- Rep Power
- 0
With your first comment you mean:?
"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?Java Code:class Gast { String vnaam; String anaam; Gast(String nieuweVNaam, String nieuweANaam) { vnaam = nieuweVNaam; anaam = nieuweANaam } }
- 10-10-2010, 02:48 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
uploading applet with multiple classes help
By alacn in forum New To JavaReplies: 3Last Post: 08-02-2010, 06:37 AM -
Help with multiple frames/classes
By Celletti in forum AWT / SwingReplies: 1Last Post: 04-28-2010, 03:18 AM -
How to use multiple timer classes in swings
By theone3nu in forum AWT / SwingReplies: 12Last Post: 12-30-2008, 02:30 AM -
More efficient in memory? Multiple Jars or Single Jars with lot's of Classes
By dark_cybernetics in forum New To JavaReplies: 0Last Post: 08-19-2008, 04:44 PM -
How to split a string into multiple lines of x characters each
By JackJ in forum New To JavaReplies: 3Last Post: 12-17-2007, 02:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks