• Post Reply Bookmark Topic Watch Topic
  • New Topic

Vector sorting, reading in data from .xml file  RSS feed

 
Donny House
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have spent hours looking at forums and everyone mentions adding to a vector and they mostly use just 1-5 ect. I cannot find anything to help my particular problem. I am new to computer programming and am slipping behind. I will type the assignment I was given and attach what I've got so far. I'm done everything except for the sort method. Can someone please help me understand how to write this code. All I know is I should be using Collections and Comparators but don't know how to implement them. Please advise. ***Sorry it's so long, can't upload ZIP or Rar files ***

THIS JAVA PROGRAMMING ASSIGNMENT SHOULD HELP YOU PRACTICE AND
UNDERSTAND THE FOLLOWING:

- MASS DATA PROCESSING USING SIMPLE DATA STRUCTURES LEARNED
IN COMPUTER SCIENCE I, SUCH AS ARRAYS AND VECTORS

- CONTROL OF PERMANENT DATA STORAGE AND I/O USING FILES

- USE AND CONTROL OF GUI AND ITS LINKS WITH DATA PROCESSING


YOU ARE TO DESIGN A JAVA GUI APPLICATION TO HELP MANAGE CLIENTS'
DATA FOR A COMPANY.

FOR SIMPLICITY, EACH CLIENT'S DATA INCLUDE ONLY A NAME, AN ID AND
A PHONE NUMBER. DATA ARE STORED IN FILES AND CAN BE OPENED, READ,
DISPLAYED, EDITED AND SAVED FOR FURTHER PROCESSING IN THE FUTURE.
ARRAYS OR VECTORS ARE USED TO ORGANIZE AND MANAGE THESE DATA AFTER
THEY ARE INPUT FROM FILES.

A MENU IS REQUIRED TO HANDLE THE FOLLOWING FILE AND DATA OPERATIONS:

A. FILE
- OPEN
- SAVE
- EXIT

B. DATA
- EDIT (NAME/ID/PHONE)
- SORT (BY NAME/ID/PHONE)
- SEARCH FOR CLIENTS (BY NAME/ID/PHONE)

Client:


Client List:




Data Window:



File Operations:



Search Window:



Clients.XML

  • <clients>
    <client>
    <name>jack lin</name>
    <id>xyz-1234</id>
    <phone>178-111-1111</phone>
    </client>
    <client>
    <name>milk lin</name>
    <id>117-1111</id>
    <phone>187-111-1111</phone>
    </client>
    <client>
    <name>bread lin</name>
    <id>111-1198</id>
    <phone>111-111-1111</phone>
    </client>
    <client>
    <name>jack butter</name>
    <id>145-1111</id>
    <phone>111-111-1451</phone>
    </client>
    <client>
    <name>jack cookie man</name>
    <id>111-xypq</id>
    <phone>133-111-2222</phone>
    </client>
    <client>
    <name>shop lin</name>
    <id>111-1188</id>
    <phone>111-111-1111</phone>
    </client>
    <client>
    <name>crazy lin</name>
    <id>111-1199</id>
    <phone>111-111-1111</phone>
    </client>
    <client>
    <name>jack weird</name>
    <id>111-1110</id>
    <phone>111-111-1110</phone>
    </client>
    <client>
    <name>jack ball</name>
    <id>111-1711</id>
    <phone>111-171-1111</phone>
    </client>
    <client>
    <name>mary lin</name>
    <id>111-1191</id>
    <phone>111-191-1111</phone>
    </client>
    <client>
    <name>jack off</name>
    <id>111-1211</id>
    <phone>111-121-1111</phone>
    </client>
    <client>
    <name>jack on</name>
    <id>111-1011</id>
    <phone>111-101-1111</phone>
    </client>
    </clients>

  •  
    Ulf Dittmer
    Rancher
    Posts: 42972
    73
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That's too much code to go through and try to figure out what it does and does not do, and why. I'm guessing the problem is with sorting the "clientData" Vector? If so, then your Client class needs to implement the Comparator interface. The important method is "compare", which compares two object of class Client and returns -1, 0 or 1, depending on which one is "larger" than the other one. (The "equals" method is not important, you can have it return false in all cases, and be done with it.)

    Does this help?

    (As an aside, if you're writing new code, don't use the obsolete Vector class - use ArrayList instead. But it'll work with Vector, too.)
     
    Greg Charles
    Sheriff
    Posts: 3015
    12
    Firefox Browser IntelliJ IDE Java Mac Ruby
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Well, I don't want to read through all your code, but I don't want to leave you out in the cold either.

    The basic idea is that any list of objects can be sorted, so long as you explain how to sort them. In other words, you define the natural order. Numerically by ID? Alphabetically by last name? Campaign contributions in descending dollar amounts? It's up to you. You put this logic into a class that implements the Comparator interface, in the method compare(Object o1, Object o2). Generally speaking, you only compare two objects of the same class, but that's not mandated by the interface. This method returns -1 if o1 should come first, 1 if o2 should come first, and 0 if it doesn't matter. For arcane (but important) reasons, you should also implement an equals() method that returns true if and only if compare() returns 0.

    That's it. You put your objects in a List, call Collections.sort(List, Comparator) (giving it an instance of the comparator class you wrote) and bingo! Your list is sorted for you.

    ETA: I think Ulf may have mixed up the roles of Comparable and Comparator here. You can either have Client implement Comparable and write a compareTo() method, or you can make a separate class that implement Comparator and write a compare() method to compare two Client objects. You're free to use whichever way seems easiest to you. I agree that you should use ArrayList instead of Vector unless you're being coerced!
     
    Campbell Ritchie
    Marshal
    Posts: 56553
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Agree with Ulf and Greg. But it's not quite compare(Object o1, Object o2) in the Comparator interface. That is now described as Comparator<T> so you can instruct the compiler what T means and use the compare(T t1, T t2) method.

    More details in the generics section in the Java Tutorials. Fortunately you can probably get away with simple generics in this instance without needing bounds and super or extends. That means you only need to read the generics bits you actually understand
     
    Donny House
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for the feed back. Sorry for all the coding. Like I said, I'm new to it and didn't know what was relavent to show. I wanted to just attach the .java files in a zip but it wouldnt let me. And as for using Vectors, I know, it's annoying, but it's what my comp sci II teacher wants.
     
    Campbell Ritchie
    Marshal
    Posts: 56553
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Welcome to JavaRanch and you're welcome

    The java.util.Vector class is often regarded as a legacy class, but it's not actually deprecated. Probably best only to use methods which you can also find in the java.util.List interface; if you look in the link quoted, it says "specified by xxx in interface List". Don't use the constructor which takes capacityIncrement.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!