hm, Irgendwas geht da �belst schief.
double wird erkannt, single nicht.
Das Datum schein wahllos zu funktionieren oder nicht.
ich h�nge einfach mal alle Klassen an die email dran. Vielleicht suche ich
das Problem an der falschen Stelle. OnlineBooker ist die Hauptklasse:
Funktion callback aktivieren und los gehts (da keine main funktion da ist).
readin interssiert sch�tze ich mal kaum und answerer macht mir st�ndig
Probleme. da sind auch ein paar debug Ausgaben eingearbeitet.
--
Moritz Bellach
Baumgartenstr. 36
65232 Taunusstein
+++ Sparen Sie mit GMX DSL +++ http://www.gmx.net/de/go/dsl
AKTION f�r Wechsler: DSL-Tarife ab 3,99 EUR/Monat + Startguthaben
BEGIN:VCARD
VERSION:2.1
ADR;HOME:;;Baumgartenstr. 36;Taunusstein;;65232;DE
BDAY:19860814
EMAIL;PREF;INTERNET;H:[EMAIL PROTECTED]
FN:Moritz Bellach
LABEL;HOME:Baumgartenstr. 36
Taunusstein, 65232
DE
N:Bellach;Moritz;;;;
TEL;HOME;VOICE:06128968693
TEL;PREF;CELL;VOICE;H:01791041564
REV:20050106T183512Z
END:VCARD
/**
* the answerer tries to find out what the user wants and checks if the wish
can be fullfilled.
*
* @author Moritz Bellach
* @version 2004-12-29v1.0
*/
import java.util.HashMap;
import java.util.HashSet;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
import java.lang.Integer;
public class answerer
{
private String answers[][]; //the calendar
private ArrayList stdAnswers; //standard answers if nothing is found
private Random noGen; //randomizer for standard answers
private int month; //needed for creating the calendar
private int rooms1; //how many single rooms the hotel has got
private int rooms2; //how many double rooms the hotel has got
private String year; //the year the calendat shall be made for
private String preanswer; //needed vor creating the calendar-dates in
correct format
private int aday;
private int aroom;
private int yday;
/**
* rm1 is the number of single rooms the hotel has got.
* rm2 is the number of double rooms the hotel has got.
* y is the year the calendar shall be made for.
*/
public answerer(int rm1, int rm2, String y)
{
yday=1;
rooms1=rm1;
rooms2=rm2;
year=y;
answers=new String[365][3];
stdAnswers=new ArrayList();
month=1; //month is needed for the calendar
fillanswers(); //create answers: standard ones and the calendarian
noGen=new Random();
aday=0;
aroom=0;
//debug method:
//getCalendar();
}
/**
* some standard answers are filled into the HashMap stdAnswers.
* then a calendar is created in the HashMap answers. the key is the
date, the value is the number of rooms which are still bookable on this date.
*/
private void fillanswers(){
//some standard answers
stdAnswers.add("We have only rooms with 2 beds.");
stdAnswers.add("Well... maybe you should learn better English?!");
stdAnswers.add("Do not try to foul me!");
stdAnswers.add("Just they the date, do not mind the details. The
format is YYYY-MM-DD.");
stdAnswers.add("You can book rooms only for this year");
//create a calendar...
while(month<13){
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
fill(31);
break;
case 4:
case 6:
case 9:
case 11:
fill(30);
break;
case 2:
fill(28);
break;
}
month=month+1;
}
}
private void fill(int max){
int day=1;
while(day<(max+1)){
preanswer=year+"-"; //...format it correctly...
if(month<10){preanswer=preanswer+"0";}
preanswer=preanswer+month+"-";
if(day<10){preanswer=preanswer+"0";}
preanswer=preanswer+day;
answers[yday-1][0]=preanswer;
answers[yday-1][1]=Integer.toString(rooms1);
answers[yday-1][2]=Integer.toString(rooms2); //...and fill
it with information about how many rooms are available
day++;
yday++;
}
}
/**
* if cnt=1 a helping hint about how to write the date is returned.
else a standard answer is chosen and returned.
*/
private String chStdAnswer(int cnt){
if(cnt<2){ //if this is the first try...
return "Please use the date format YYYY-MM-DD and say if you want
a single or a double room.";} //...explain how to write the date
else{ //else use some standard answer
int index=noGen.nextInt(stdAnswers.size());
return (String)stdAnswers.get(index);}
}
/**
* HashSet wishwords contains the collection of seperated words the user typed
in. Int cnt specifies which try of the user this is (This is importend for
method chStdAnswer).
* The method tries to book a room on a specific date. It returns a answer
String which describes if a rooms could be booked or not, or if there was not
found a date in wishwords.
*/
public String getAnswer(HashSet wishwords, int cnt){
int count=cnt; //cnt is needed for the standard answers
Iterator it=wishwords.iterator();
while(it.hasNext() && (aday==0 || aroom==0)){
String wishword=(String)it.next(); //get the next word
//print debug statement
System.out.println("wishword: "+wishword);
int day=1;
for(day=1; day<366; day++){
if(wishword.equals(answers[day-1][0])){
aday=day;
//print debug statement
System.out.println("Found date: "+wishword+" State of
aday: "+Integer.toString(aday));
}
day++;
}
if(wishword.equals("single") || wishword.equals("1") ||
wishword.equals("one")){
aroom=1;
//print debug statement
System.out.println("Found 'single' statement: "+wishword+"
State of aroom: "+Integer.toString(aroom));
}
if(wishword.equals("double") || wishword.equals("2") ||
wishword.equals("two")){
aroom=2;
//print debug statement
System.out.println("Found 'double' statement: "+wishword+"
State of aroom: "+Integer.toString(aroom));
}
}
if(aday>0 && aroom>0){
if(Integer.parseInt(answers[aday-1][aroom])>0){
answers[aday-1][aroom]=Integer.toString(Integer.parseInt(answers[aday-1][aroom])-1);
return "We could book you a room for "+aroom+" persons on
"+answers[aday-1][0];
}
else {return "Unfortunately all rooms on "+answers[aday-1][0]+"
for "+aroom+" persons are already booked";}
}
return chStdAnswer(count); //if not understood, return a standard
answer
}
private void getCalendar(){
int i=0;
while(i<365) {
System.out.println("day: "+answers[i][0]+" single rooms:
"+answers[i][1]+" double rooms: "+answers[i][2]);
i++;
}
}
}
/**
* Top-level Class for an online booking system of a hotel.
*
* @author Moritz Bellach
* @version 2004-12-29v2.0
*/
import java.util.HashSet;
public class OnlineBooker
{
private readin leser;
private answerer beantworter;
private int count;
private int rooms1;
private int rooms2;
private String year;
/**
* no is the number of rooms the hotel has got.
* y is the year the calendar shall be made for.
*/
public OnlineBooker(int rm1, int rm2, String y)
{
leser=new readin(); //create a new reader
count=0; //
rooms1=rm1;
rooms2=rm2;
year=y;
beantworter=new answerer(rooms1, rooms2, y); //create a new answerer
}
/**
* count has only one purpose: show if this is the first try or not
*/
private int getCount(){return count;}
/**
* start the whole thing (if there would be a main function it would create a
OnlineBooker object and then do the stuff that now is done by callback)
*/
public void callback() {
boolean done=false;
System.out.println("Wecome to the Online-Booking System. Please type in your
booking wishes."); //greeting
System.out.println("To quit type 'bye'.");
while(!done) {
count++;
HashSet wish=leser.getWish(); //get the wish read in and seperated
if(wish.contains("bye")) {done=true;} //abort if "bye" is typed in
else {System.out.println(beantworter.getAnswer(wish, count));} //get an answer
}
System.out.println("Thank you very much for using the Online-Booking System.");
//end
}
}
/**
* readin reads a string typed in at the commandline and provides it as a
HashSet.
*
* @author Moritz Bellach
* @version 2004-12-29v1.0
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.StringTokenizer;
public class readin
{
private BufferedReader reader;
/**
* readin does not need any parameters
*/
public readin()
{
reader=new BufferedReader(new InputStreamReader(System.in)); //sets up
the objects for reading from the commandline
}
/**
*takes the wish from method readwish and returns it as seperated words in a
HashSet.
*/
public HashSet getWish(){
System.out.print("Your wish:> ");
String wishline=readwish().trim().toLowerCase(); //read the wish
StringTokenizer tokenizer=new StringTokenizer(wishline); //seperate the words
HashSet wishwords=new HashSet(); //create a new hashset
while(tokenizer.hasMoreTokens()){
wishwords.add(tokenizer.nextToken());} //fill hashset with seperated words
return wishwords;
}
/**
*gets the wish by reading from the comandline and returns it as a String.
*/
private String readwish(){
String line="";
try{line=reader.readLine();} //read a line
catch(java.io.IOException exc){System.out.println("Lesefehler:
"+exc.getMessage());} //catch any errors
return line;
}
}