Java Appointment Book

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2008
Posts: 42
Reputation: JimD C++ Newb is an unknown quantity at this point 
Solved Threads: 0
JimD C++ Newb JimD C++ Newb is offline Offline
Light Poster

Java Appointment Book

 
0
  #1
Sep 25th, 2009
Good evening, all!

My current assignment is a small Appointment Book written in Java. It should scroll through the times of the day, and display the appointment (if any) for that particular time of day.

I can scroll through the different times with no issue, but I can't seem to get the appointment(s) to display correctly. Any appointment I put in is displayed for EVERY hour, not just the one it is intended for.

This is a portion from my main.java file:

The saveButton saves the appointment
nameField is the name of the text field holding the appointment

  1. if (e.getSource()==this.saveButton){
  2. String temptext = (nameField.getText());
  3. appointments.setName(temptext);
  4. nameField.setText(appointments.getName());
  5. }

And this portion is the part of my constructor file that is giving me the issue:

  1. public void setName(String appt){
  2. setName(appt);
  3. }
  4. public String getName(){
  5. return appt;
  6. }

Any help on what I'm missing would be greatly appreciated!
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,678
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 226
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is online now Online
Posting Virtuoso

Re: Java Appointment Book

 
1
  #2
Sep 25th, 2009
Can you see something wrong with this code:

public void setName(String appt){
        setName(appt);
    }

You calling the same method. When you call setName it will call setName which is inside it, which will call setName which is inside it, which will call setName which is inside it, . . . .

You need to assign the property name inside the method:
  1. public void setName(String appt){
  2. name = appt;
  3. }
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 42
Reputation: JimD C++ Newb is an unknown quantity at this point 
Solved Threads: 0
JimD C++ Newb JimD C++ Newb is offline Offline
Light Poster

Re: Java Appointment Book

 
0
  #3
Sep 25th, 2009
Progress! Your fix certainly eliminated the repeated error I was receiving (totally missed that - tyvm!!)

However the same appointment is still stored at every time segment throughout the book.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,678
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 226
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is online now Online
Posting Virtuoso

Re: Java Appointment Book

 
0
  #4
Sep 25th, 2009
Post more code. Skip the presentation code and post for start just the code with the logic.
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 42
Reputation: JimD C++ Newb is an unknown quantity at this point 
Solved Threads: 0
JimD C++ Newb JimD C++ Newb is offline Offline
Light Poster

Re: Java Appointment Book

 
0
  #5
Sep 25th, 2009
I'm hope I am correct in assuming this is what you are looking for. Again, thank you for taking the time to look at this.

This is from the constructor file:

  1. package AppointmentBook;
  2.  
  3. import javax.swing.Icon;
  4. import javax.swing.JOptionPane;
  5.  
  6. /**
  7.  *
  8.  * @author Jim
  9.  */
  10. public class AppointmentBook {
  11.  
  12. private int hour[];
  13. private int time, position = 0;
  14. private int max;
  15. private String[] text;
  16. private String result;
  17. private String appt;
  18. private String temptext;
  19.  
  20. public AppointmentBook(int start, int end) {
  21. int j = start;
  22. int[] timeArray; //Array for appointment times
  23. String[] stringArray; //Array for appointments
  24.  
  25. int temp = end - start; //Specified time period
  26. temp = temp/100;
  27. temp++;
  28.  
  29. timeArray = new int[temp]; //Space for arrays
  30. stringArray = new String[temp];
  31.  
  32. for (int i=0;i<temp;i++) { //Default values assigned
  33. stringArray[i] = "none";
  34. timeArray[i] = j;
  35. j += 100;
  36. }
  37.  
  38. time = 0; //Update state variables
  39. max = temp - 1; //All these are defined globally
  40. hour = timeArray;
  41. text = stringArray;
  42.  
  43.  
  44. }
  45. public String getTime(){
  46. time = hour[position]; //time at that spot in the array
  47. String result = ""; //the time we're on
  48. result = Integer.toString(time); //change Int to String
  49. return result; //return value
  50. }
  51. public void advPosition(boolean b){ //Instead of void next() and void prev()
  52. //I used one function to do both
  53. if (b)
  54. {
  55. position++; //cycle through the array
  56. if (position > max) //If 1500 is the time then...
  57. {
  58. position=max; //Time stops cycling forward through the hours
  59. }
  60.  
  61. }
  62. else
  63. {
  64. position--;
  65. if (position < 0) //If 800 is the time then...
  66. {
  67. position=0; //Time stops cycling backward through the hours
  68. }
  69. }
  70.  
  71.  
  72. }
  73.  
  74. public void setName(String appt){
  75. temptext = appt;
  76. }
  77. public String getName(){
  78. return appt;
  79. }
  80.  
  81. }

And this is from the Main.java file:

  1.  
  2. public void actionPerformed(ActionEvent e) {
  3.  
  4.  
  5. if (e.getSource()==this.nextButton){
  6. appointments.advPosition(true);
  7. timeField.setText(appointments.getTime());
  8. }
  9. if (e.getSource()==this.prevButton){
  10. appointments.advPosition(false);
  11. timeField.setText(appointments.getTime());
  12. }
  13. if (e.getSource()==this.saveButton){
  14. String temptext = (nameField.getText());
  15. appointments.setName(temptext);
  16. //nameField.setText(appointments.getName());
  17. }
  18.  
  19.  
  20. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,678
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 226
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is online now Online
Posting Virtuoso

Re: Java Appointment Book

 
0
  #6
Sep 25th, 2009
First of all look at this again:
  1. public void setName(String appt){
  2. temptext = appt;
  3. }
  4. public String getName(){
  5. return appt;
  6. }

You return the appt variable but in the code that variable never takes value. You just declare it and it always returns null. But you set the value to the temptext variable; shouldn't you return that instead?
Also these 2 appt variables are NOT the same:
  1. private Strign appt = null;
  2.  
  3. public void setName(String appt){ // appt exists only inside this method
  4. // it overrides the global
  5. temptext = appt;
  6. }
When you call the set-method the global variable appt doesn't take value.
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,678
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 226
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is online now Online
Posting Virtuoso

Re: Java Appointment Book

 
0
  #7
Sep 25th, 2009
And a new suggestion if you can't get you code to work.
I find your solution very confusing and not object oriented. I believe that you problem is that when it comes to time you return values from the array, but when it comes to name you declare an array but you use a single String variable which you set and return. Therefor all the times will have the same name from the method "getName".
Shouldn't you have a method that depending on the time or its position you set the at the appropriate position the name at the StringArray?

I don't know your whole logic but you can try this:
  1. // SINGLE CLASS WITH JUST PROPERTIES
  2.  
  3. public class Appointment {
  4. private int time = 0;
  5. private String name = "";
  6.  
  7. public Appointment() {
  8.  
  9. }
  10.  
  11. // add get,set methods
  12.  
  13. }

Then have a class like this:
  1. public class AppointmentBook {
  2. private int position = 0;
  3. private int max = 0;
  4. private Appointment [] appointments = null;
  5.  
  6. public Appointment(int max) {
  7. // this.max is the global
  8. // max is the local from the argument
  9. this.max = max;
  10.  
  11. //appointments is the global
  12. appointments = new Appointment[max];
  13. }
  14.  
  15. // create methods that add and get appointments to the array by increasing the position variable
  16. // also check the value of position in case you have too much appointment
  17. // add whatever functionality you want
  18. }

examle:
  1. Appointment ap = new Appointment();
  2. ap.setName("Name");
  3. ap.setTime(200);
  4.  
  5. Appointment [] appointS = new Appointment[5];
  6. appointS[0] = ap;
  7.  
  8. // OR
  9.  
  10. appointS[1] = new Appointment();
  11. appointS[1].setName("Name 2");
  12. appointS[1].setTime(300);

Remember when you declare an array its elements are undeclared.
This: Appointment [] appointS = new Appointment[5]; will create an array but its elements (appointS[0], ... ) are null. You need to give them values like at the example
Last edited by javaAddict; Sep 25th, 2009 at 5:57 am.
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC