Link to home
Start Free TrialLog in
Avatar of swet
swet

asked on

How to save and print elements in vectors?


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?

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Add the class to the Vector and serialize it:

http://java.sun.com/docs/books/tutorial/essential/io/serialization.html
Avatar of swet
swet

ASKER


Sorry..in the 12th line..I mean (all the information must be stored in a vector) Not in an array
Vector implements Collection interface, you can use iterators to loop through the content.
Adding element is quite straight-forward - add(Object) method.

Regards!
Avatar of swet

ASKER


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?
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",...");
Avatar of swet

ASKER


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?
I should perhaps remind people to be much less specific about giving code - for obvious reasons...
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!
Avatar of swet

ASKER


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..



Produce a good amount of the work yourself. Once you've done that, we can help.
Avatar of swet

ASKER


Ok..thanks,,
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.
Avatar of swet

ASKER


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?

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.
Avatar of swet

ASKER


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));



  }
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.
Avatar of swet

ASKER


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..
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 ) );
Avatar of swet

ASKER


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?
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.
Avatar of swet

ASKER


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.
Avatar of swet

ASKER


Do you mean something like this..


  public static Complaint getComplaintObj(int i) {
    return (Complaint) complaintVec.get(i);
  }
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.
Avatar of swet

ASKER


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?
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.
Avatar of swet

ASKER


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
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.
Avatar of swet

ASKER


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?
Avatar of swet

ASKER


 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();

>>  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();
}
Avatar of swet

ASKER

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?
Avatar of swet

ASKER


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..
ASKER CERTIFIED SOLUTION
Avatar of SuperKarateMonkey
SuperKarateMonkey

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial