• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JDBC assessment part #1 complete, feedback appreciated

 
Billy Sclater
Ranch Hand
Posts: 145
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I mentioned, I am currently doing a coding exercise for a job opportunity. I have included a brief explanation of part 1 of the exercise along with 'some' of the code (just 2 methods). They tell me that I will evaluated on: java coding standards, use of control statements (if, for, while, switch, etc.), use of the Collection API, Utilisation of OOPs concepts, the efficiency of the code, exception Handling, Usage of Design Patterns and Test Cases (preferably in JUnit). I'd really appreciate some feedback on the quality of the code with regards to the criteria mentioned!

I'll just explain what I'm trying to do first. My objective is to read some data about a number of people from a file. The data is as follows: an id number, the persons first and last name, their street name and city name. After I have read the data and stored it in a collection, I wish to supply a getter method to allow the data about each person to be read in String format. Eventually, but not here, the getter will be used by another class.

I decided to make 2 classes: a Person class, each person object has instance variables containing the data. This class also contains getter and setter methods for reading and writing this data. The 2nd class is a DataFileReader class which supplies a method for putting the file into a collection. This method utilises the Person class's set methods.

Here is the readPersonFile method from the DataFileReader class. What it does is to read a file 'Person.data' which contains 4 rows & 5 columns of info. The first line contains the column headings 'id, FirstName, LastName and so on'. The next 3 lines contain all the juicy data! As you can see, a String[] array stores each row, and then each array(row) is added to an ArrayList. I chose an ArrayList as it is dynamically sized (the data file could be any size), and also I can generically type the Arraylist to <Person> to ensure nothing inappropriate is added to it. The method returns the ArrayList.



Here is the getter method of the Person class. It takes the ArrayList of Person objects from the readPersonFile method, and converts it into an ArrayList of String (ArrayLists). Each String ArrayList represents each individual Person object containing that Person's data eg/their data 'id, lastname etc'. The Person objects are converted to Strings so that they can be read from the console if desired.



Here is a test case method for the readPersonFile method:


 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34202
341
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two things to improve:
  • Variable names - "l" and "l2" are not good variable names. They don't communicate much and require the reader to remember which is which.
  • The JUnit test only checks the code doesn't blow up. It could use some assertEquals() statements to confirmt eh correct data was read/parsed


  • None of this has to do with JDBC though. Was there a JDBC part to the assignment? Or did you assume you should use a file instead of a database?
     
    E Armitage
    Rancher
    Posts: 989
    9
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    ArrayList<ArrayList<String>> is bad. You don't need that getPerson method at all (It's also not a getter method). Just write a reasonable toString method in your Person class.
    Also, test questions that involve IO usually have a significant weight devoted to checking if you are able to close all streams that you opened correctly in a finally block or using try with resources.
    Programs that read data from files usually break when the files are not in the expected format. You should therefore add checks that ensure that the split array has at least 5 entries before doing columns[4].
    You also mentioned having getters and setters but you have code like p.city which is not using the getters and shows that you have not defined the fields as private. You are probably expected to define the fields as private.
     
    Billy Sclater
    Ranch Hand
    Posts: 145
    Eclipse IDE Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for the feedback! Actually, I came across a great YT tutorial last night all about JDBC. I going to watch it through, and then carry on with the coding exercise later tonight (:
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic