Solved

How to save and print elements in vectors?

Posted on 2003-11-19
33
660 Views
Last Modified: 2010-03-31

I really need your help in this project..I'm running out of time..

I have a program which has these classes: Manager, Employee, Complaint, ManagerUI, EmployeeUI and HelpDeskUI..

When first run the program, A HelpDeskUI will appear to the user. the user will have to choose his position.. either (manager) or (employee)..

If I click on theEmployee button, An EmployeeUI will appear..In this window I have to enter: my name, my ID, problem type (whether Software or hardware problem) and I will have some space in which I can specify the details of my problem..when I finish I have to click on (submit) button..once I do that all the fields must be cleared and I can enter another problem..

If I finish submitting my complaints, I will have to close the window..and I must be able to see the HelpDeskUI again..so when another user click on the Employee button, he will be able to record his problem (note that all the different complaints must be stored in an array)

Now when the manager use the program..he will click on the Manager Employee and will be able to see his window..In his he has two lists..one that have complaints arranged by their unique number..and the other list shows the name of four technicians..the manager must be able to assign each complaint to a technician..so when the manager choose say (Complaint 1) and choose ("Bill") from the technicians list, he will click on the (Show Complaints) buttons to be able to see ALL the complaints that have been recorded..like this [in a textArea Field]:


Complaint no.     Employee id     complaint type     assigned technician     status        Date
1                       567                 software             Bill                             pending
2                       785                 hardware            Ed                             fixed
3                       324                 hardware

The manager will be able to select a complaint to change its status or to show the details recorded by employees..


Sorry if the question is too long..but I really need a fast clear help..

My first question is about vectors..In the complaint class I will have to create a vector which will have all the information about a complaint..but how will I be able to print all the information in the textArea field when the manager will click on the button..Is there a loop and if yes where? Also how will all the complaints be saved in a vector?

0
Comment
Question by:swet
  • 17
  • 10
  • 3
  • +1
33 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9779459
Add the class to the Vector and serialize it:

http://java.sun.com/docs/books/tutorial/essential/io/serialization.html
0
 

Author Comment

by:swet
ID: 9779469

Sorry..in the 12th line..I mean (all the information must be stored in a vector) Not in an array
0
 
LVL 4

Expert Comment

by:vk33
ID: 9779484
Vector implements Collection interface, you can use iterators to loop through the content.
Adding element is quite straight-forward - add(Object) method.

Regards!
0
 

Author Comment

by:swet
ID: 9779528

Thanks..But could you help me with the code please..

now in the Complaint class..How will I create my vector and pass parameters to it?
0
 
LVL 4

Expert Comment

by:vk33
ID: 9779573
Do you have any class containing the info about compliant? E.g.:

public class Record {
   private int id;
   private int empID;
   private String type;
   ...
   public Record(id,empID,type,...) {
      this.id = id;
      this.empID = empID;
      this.type = type;
      ...
   }
}

Then you instantiate records and add them to your vector:

Vector vector = new Vector();
vector.add(new Record(1,1,"software",...");
vector.add(new Record(2,2,"hardware",...");
0
 

Author Comment

by:swet
ID: 9779596

Thanks,,that helped alot..

I have all the information in the Complaint class..

but about the last part which is creating the vector..where do I have to put it?

is in the same class..or in one of the userinterfaces classes?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9779611
I should perhaps remind people to be much less specific about giving code - for obvious reasons...
0
 
LVL 4

Expert Comment

by:vk33
ID: 9779688
well, CEHJ is right, I cannot post the entire solution. Especially when experiencing lack of info. ;)

You should put the vector manipulations where you need them. For example in you GUI classes, preferrably actions handling.

Regards!
0
 

Author Comment

by:swet
ID: 9779714

Sorry..but I nearly give up..It is just my first few months to study Java..

how will I be able to display all the information entered by different users at the end..?
Where will the different informtion be stored?


Sorry for annoying..



0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9779732
Produce a good amount of the work yourself. Once you've done that, we can help.
0
 

Author Comment

by:swet
ID: 9779746

Ok..thanks,,
0
 
LVL 2

Expert Comment

by:SuperKarateMonkey
ID: 9780076
To loop through a vector is pretty easy, but it's a fairly important algorithm to have in your toolbox:


***********************************************************************

// To add stuff:

Vector myVec = new java.util.Vector();

for( int i = 0; i < item.length; i++ )
{
  myVec.add( item[i] );
}

// The above code will add the elements of an array,  item[], to myVec one at a time.  It will not add
// primitves like int, char, and long, though, only full-fledged Objects.  To add a series of separate
// Objects, not in an array:

myVec.add( object1 );
myVec.add( object2 );
myVec.add( object3 );
myVec.add( object4 );

// To loop through and get them back:

Iterator it = myVec.iterator();

while( it.hasNext() )
{
  Object o = it.next();
  // The next() method returns only an Object class, regardless of how the class went in.  You have
  // to re-cast it as whatever it was when it went in, in this example, String.

  String s = (String)o;

  // Now do whatever you have to with the String.
}

Let's make it clear:  This is not a solution.  It may or may not be good code for what you're trying to do.  It's simply an example of working with Vectors, which are critical things to know, (ArrayLists are even better, but that's another matter,) when you program java.
0
 

Author Comment

by:swet
ID: 9781405

rhanks  SuperKarateMonkey,,


now this is part of my Complaint class

====================

package helpdesk;

import java.util.*;
import java.sql.Date;

public class Complaint {

  public static Vector complaintVec = new Vector();
  private int complaintNum;
  private String complaintType;
  private String complaintStatus;
  private int employeeNum;
  private Date complaintDate;
  private String complaintDetail;
  private String techName;

  public Complaint(int compNum, int empNum, String compStatus, String compType,
                   Date compDate, String compDetail, String techName) {
    complaintVec.add(this);

  }

  public Complaint(int eId) {
    employeeNum = eId;
  }

  public int getComplaintNum() {
    return complaintNum;
  }

  public String getComplaintType() {
    return complaintType;
  }

  public String getComplaintStatus() {
    return complaintStatus;
  }

 }
===========================

I just want to ensure that Im using vectors in the right way..
Is there anything missing?

0
 
LVL 2

Expert Comment

by:SuperKarateMonkey
ID: 9781476
Yes, I'm afraid so.

1.  In the constructor, you're having your vector add the original object.  But you're not actually add any of the variables passed INTO the constructor!  Let me ask you:  What do you think happens to all those passed variables?  Do you do anything with them?

2.  You have a whole slew of private variables you neither need nor want.  Get rid of every instance variable except for complaintVec.

3.  For each passed variable in the constructor, add it to the complaintVec, but remember that you can't insert primitives.  You need to use the primitive wrapper classes.  Instead of calling:

complaintVec.add( compNum );

You need to first wrap the integer in an Integer object, like this:

complaintVec.add( new Integer( compNum ) );

Try re-writing the code and get back on this.
0
 

Author Comment

by:swet
ID: 9781533

Oh..thank you very much..I really can't deny your help..
but..Im sorry..It is the first time I use vectors..

now will this be right?

================


public class Complaint {

  public static Vector complaintVec = new Vector();
 
  public Complaint(int compNum, int empNum, String compStatus, String compType,
                   Date compDate, String compDetail, String techName) {
    complaintVec.add(new Integer(compNum));
    complaintVec.add(new Integer(empNum));
    complaintVec.add(new String(compStatus));
    complaintVec.add(new String(compType));
    complaintVec.add(new Date(compDate));
    complaintVec.add(new String(compDetail));
    complaintVec.add(new String(techNum));



  }
0
 
LVL 2

Expert Comment

by:SuperKarateMonkey
ID: 9781678
See, that's more like it.  Couple more things now:  First of all, you should make the Vector private.  It's nobody's business but the Object's.

Secondly, don't recreate Strings or Dates, only the primitives.  It's just a waste of objects.  The only reason you'd want to create new ones is if you are planning on modifying the old ones, as opposed to never using/looking/changing them again.  And for String, not even THAT matters, b/c it's immutable.  You never alter a String, you just create a new one and point your old variable reference at the new one.

Last, you need getter methods, like

getCompNum()
getEmpNum()
getCompStatus()

etc...

Here's the trick, though:  To retreive them you need to use the Vector.get() method.

Look it up in the Java API at java.sun.com and give me some code.  Also remember:

The Vector.add() method accepts Object as it's argument, i.e. Vector.add( Object o ).
They go in as generic Objects, ergo they come out the same way.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:swet
ID: 9781735

Im afraid I can't understand your second point..
do you mean that I have delete all the strings and dates from the (add) methods?



and for the getting methods..can you please see the program that I have posted up..
because it has some of my get methods..I want to ensure that they are right..
0
 
LVL 2

Expert Comment

by:SuperKarateMonkey
ID: 9781743
No, I'm saying you don't need to create new ones.

complaintVec.add( compStatus );

is fine.  You don't need

complaintVec.add( new String( compStatus ) );
0
 

Author Comment

by:swet
ID: 9781805

aha..thanks..
sorry if I'm bothering you with my questions..
now this is my class
================
import java.util.*;
import java.sql.Date;

public class Complaint {

  public static Vector complaintVec = new Vector();
  private int complaintNum;
  private String complaintType;
  private String complaintStatus;
  private int employeeNum;
  private Date complaintDate;
  private String complaintDetail;
  private String techName;

  public Complaint(int compNum, int empNum, String compStatus, String compType,
                   Date compDate, String compDetail, String techName) {
    complaintVec.add(new Integer(compNum));
   complaintVec.add(new Integer(empNum));
   complaintVec.add(compStatus);
   complaintVec.add(compType);
   complaintVec.add(compDate);
   complaintVec.add(compDetail);
   complaintVec.add(techName);


  }

  public Complaint(int eId) {
    employeeNum = eId;
  }

  public int getComplaintNum() {
    return complaintNum;
  }

  public String getComplaintType() {
    return complaintType;
  }

  public String getComplaintStatus() {
    return complaintStatus;
  }

  public int getEmployeeNum() {
    return employeeNum;
  }

  public Date getComplaintDate() {
    return complaintDate;
  }

  public String getComplaintDetail() {
    return complaintDetail;
  }

  public String getTechName() {
    return techName;
  }

  public void setComplaintNum(int cNum) {
    complaintNum = cNum;
  }

  public void setComplaintType(String cType) {
    complaintType = cType;
  }

  public void setComplaintStatus(String cStatus) {
    complaintStatus = cStatus;
  }

  public void setEmployeeNum(int eNum) {
    employeeNum = eNum;
  }

  public void setComplaintDate(Date cDate) {
    complaintDate = cDate;
  }

  public void setComplaintDetail(String cDetail) {
    complaintDetail = cDetail;
  }

  public void setTechName(String tName) {
    techName = tName;
  }

}
============================

If I deleted the initialization part at the beginning


private int complaintNum;
private String complaintType;
.
.
.
..I would have errors in all the get and set methods..so what I have to do?
0
 
LVL 2

Expert Comment

by:SuperKarateMonkey
ID: 9781838
Yes, b/c you want to get using the original vector for the getter and setter methods.  Isn't that the assignment?

Shitcan all these variables:

  private int complaintNum;
  private String complaintType;
  private String complaintStatus;
  private int employeeNum;
  private Date complaintDate;
  private String complaintDetail;
  private String techName;

Instead, for the getXxx methods, just use the Vector.get() method at the right index point.

And for the setXxx methods use the Vector.remove() followed by the Vector.add() at the right index point.

If you're required to use a Vector, then this is the way to go.  If you only need it to work, then knock yourself out with the extra variables.
0
 

Author Comment

by:swet
ID: 9781866

I'm afraid ><..I really can't understand the get and set methods..

this is what I found in the Java reference

=============================

get public Object get(int index)
Returns the element at the specified position in this Vector.
0
 

Author Comment

by:swet
ID: 9781900

Do you mean something like this..


  public static Complaint getComplaintObj(int i) {
    return (Complaint) complaintVec.get(i);
  }
0
 
LVL 2

Expert Comment

by:SuperKarateMonkey
ID: 9782036
You data is being held inside of the vector.  For example, the Complaint Number is held at index = 0, (java indexes start at 0, not 1.)  So if you want to call the getComplaintNum() method, you'd write it like this:

public int getComplaintNum()
{
  Integer temp = (Integer)( complaintVec.get( 0 ) );
  return temp.intValue();
}

public String getCompStatus()
{
  String temp = (String)( complaintVec.get( 2 ) );
  return temp;
}

By the same token, you can use the methods:

public void remove( int index )

to remove an element, and

public void add( Object o, int index )

to add a replacement object.

These two would be called inside the setXxx methods.
0
 

Author Comment

by:swet
ID: 9782066

Aha..that helps alot..

now in the EmployeeUI..once the program validate the name and id of the user..I have to create the object (Complaint) and pass all the parameters to it..but can you give me a clue on how wil this be dons?
0
 
LVL 2

Expert Comment

by:SuperKarateMonkey
ID: 9782117
I have no idea how your UI accepts input from the user.  Are there text boxes?  If so, and you're using JTextFields, then you use the method:

public String getText() to get it's value.

If you're using something else, then look the component up in the API.  If it can accept text, it probably extends JTextComponent, which is where the method above comes from.
0
 

Author Comment

by:swet
ID: 9782156

yes,, I am using JTextFields and I know how to get text from them..

the problem is that different users will enter their information and all of these must be stored..The manager will be able to display all the different information in a JTextArea..I can't understand how will this work
0
 
LVL 2

Expert Comment

by:SuperKarateMonkey
ID: 9782197
Well, there's two processes here:

1.  User registers a complaint.  We just talked about that.  You read each value of each appropriate JTextField, then use the constructor to cobble together a new Complaint object.

2.  The user views all existing complaints.  For that, you should probably have ANOTHER Vector filled with complaints.  Whenever you need to display all complaints, just loop through the Vector, using an Iterator, and spit each value out into the JTextArea.

You could delimit them with tabs and newlines.
For each VALUE of a PARTICULAR COMPLAINT, separate them with '\t'
For each SEPARATE COMPLAINT, separate them with '\n'

It occurs to me now that THIS is what you really want to be using a Vector for, since I imagine eventually these complaints get resolved and you want to remove them from the list.  Still, you can use the Vector internally as well.  It's a pain, but not a true bug.
0
 

Author Comment

by:swet
ID: 9785114

now in the Complaint class I have this
========================

 public Complaint(int compNum, int empNum, String compStatus, String compType,
                   Date compDate, String compDetail, String techName) {
    complaintVec.add(new Integer(compNum));
    complaintVec.add(new Integer(empNum));
    complaintVec.add(compStatus);
    complaintVec.add(compType);
    complaintVec.add(compDate);
    complaintVec.add(compDetail);
    complaintVec.add(techName);

  }

  public int getCompNum() {
    Integer temp = (Integer) (complaintVec.get(0));
    return temp.intValue();
  }

  public int getEmpNum() {
    Integer temp = (Integer) (complaintVec.get(1));
    return temp.intValue();
  }

  public String getCompStatus() {
    String temp = (String) (complaintVec.get(2));
    return temp;
  }

  public String getCompType() {
    String temp = (String) (complaintVec.get(3));
    return temp;
  }

  public Date getCompDate() {
    String temp = (Date) (complaintVec.get(4));
    return temp;
  }

  public String getCompDetail() {
    String temp = (String) (complaintVec.get(5));
    return temp;
  }

  public String getTechName() {
    String temp = (String) (complaintVec.get(6));
    return temp;
  }

==============================

and in the EmployeeUI Class I have this to create a new Customer

void btnSubmit_actionPerformed(ActionEvent e) {
    Calendar cal = Calendar.getInstance();
    String emplName = txtName.getText();
    int emplId = Integer.parseInt(txtId.getText());
    String comptStatus = "Pending";
    Date todayDate = cal.getTime();
    String comptType = txtType.getText();
    String compDetail = txtDetails.getText();
    Complaint c = new Complaint (    ,emplId, comptStatus, comptType, todayDate, compDetail, "");
  }

===============================

I want to ask about the first parameter which will passes (CompNum)..this should be calculated aytomatically.but how could I do that?
0
 

Author Comment

by:swet
ID: 9786547

 SuperKarateMonkey..I need your help..Do I need to use two vectors..or can I just use one for the complaints..
=============
Also..I tried to create the comaplint..but when I use the showMessageDiallog..I got null for all the strings and 0 for all the integers..
I don't know why this happened!

=================

Complaint c;
    Vector complaintVec = new Vector();
    Calendar cal = Calendar.getInstance();
    String emplName = txtName.getText();
    int emplId = Integer.parseInt(txtId.getText());
    String comptStatus = "Pending";
    Date todayDate = cal.getTime();
    String comptType = txtType.getText();
    String compDetail = txtDetails.getText();

c = new Complaint(emplId, comptStatus, comptType, todayDate, compDetail, "");
    complaintVec.add(c);
    JOptionPane.showMessageDialog(null, c.toString()); //if I print c or comlaintVec I got same result
    txtName.setText("");
    txtId.setText("");
    txtType.setText("");
    txtDetails.setText("");
    txtName.requestFocus();

0
 
LVL 2

Expert Comment

by:SuperKarateMonkey
ID: 9788528
>>  SuperKarateMonkey..I need your help..Do I need to use two vectors..or can I just use one for the complaints..

I have no idea what "just use one for the complaints" means.  Does that mean you want to use one for the LIST OF COMPLAINTS, or one for the ELEMENTS OF EACH COMPLAINT, or something else?  And your choice of variable names:  complaintVec for the internal vector inside the complaint, AND THE SAME NAME for the list of complaints, that's not good.



>>  Also..I tried to create the comaplint..but when I use the showMessageDiallog..I got null for all the strings and 0 for all the integers..
I don't know why this happened!

Well, I'm pretty sure there's more than one bug here, but to start:

c.toString() is not going to dump out the contents of the fields of your complaint.  You'd have to actually implement that method to do that.  Right now it'll just do something unintelligible, like list out the constituent classes.  You'd want to override the default toString() method:


//  This is a method OF the Complaint Object.
public String toString()
{
  Iterator i = complaintVec.iterator();                          // The internal constituent elements of the complaint.
  StringBuffer mother_buffer = new StringBuffer();
  while( i.hasNext() )
  {
    Object o = i.next();
    if( o instanceof Integer )
      mother_buffer.append( ((Integer)o).intValue() );
    else
      mother_buffer.append( o );

    mother_buffer.append( " " );
  }
  return mother_buffer.toString();
}
0
 

Author Comment

by:swet
ID: 9795131
thanks..

but I mean that I only want to use ONE vector for the list of complaints..can I do that? and if yes could you please give me a clue on how will the Complaint class will look like?
0
 

Author Comment

by:swet
ID: 9795199

now the last piece of code works..but If another person enters his complaint..he will get a message of all the ingormation about the previous complaints that have been entered as well..could you please help me with this..
0
 
LVL 2

Accepted Solution

by:
SuperKarateMonkey earned 110 total points
ID: 9797719
OK, two Questions here:

Q:  How will the Complaint class look?
A:  Just like BEFORE we adapted it to use a Vector:

public class Complaint
{
  private int complaintNum;
  private String complaintType;
  private String complaintStatus;
  private int employeeNum;
  private Date complaintDate;
  private String complaintDetail;
  private String techName;

  public Complaint( int cNum, String cType, String cStatus, int eNum, String cDetail, String tName )
  {
    this.complaintNum = cNum;
    etc, etc, etc...
  }
}

Q:  Another person is seeing the old complaint:
A:  I can't positively figure what's happening unless I see the code, but lemme ask you this:  Where are you getting the Complaint object to populate the pop-up?  Are you just calling the method:

complaintVec.get( int index )

Because if you are, you'll need to be specifying the index to obtain.  And if you want to get the last submitted Complaint, you should really be calling one of these two methods:

complaintVec.get( complaintVec.size() - 1 );
complaintVec.lastElement();

either one will give you the last element in the Vector, which should be the last object you just added to said Vector.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
What is JNDI datasource in spring 1 38
topping3 challenge 14 50
wordappend challenge 8 85
Requested array size exceeds VM limit 3 50
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now