|Join Nairaland / LOGIN! / Trending / Recent / New|
Stats: 2,773,713 members, 6,605,869 topics. Date: Saturday, 27 November 2021 at 01:31 PM
|Java Collections Question by bigboyslim(m): 3:39pm On Mar 02, 2012|
Need some help with this challenge. I have a text file which contains the following information in this order
Name ! Category ! Description ! Cost
Content of the file is below:
I need to generate two reports.
1. Total cost per Person
2. Total cost per Per Category Per Person
I am fairly familiar with the plumbing of the program. I just wanted to know from experts what Java collections come to your mind when attempting to solve this type of problem.
I have used arrays heavily in this program but I feel there might be other classes that will serve my purpose better.
|Re: Java Collections Question by Fayimora(m): 8:17pm On Mar 02, 2012|
For starters 4 arrays!
|Re: Java Collections Question by Ghenghis(m): 6:56pm On Mar 03, 2012|
Array ke ? He needs a Map or 2 (HashMap).
|Re: Java Collections Question by Fayimora(m): 7:25pm On Mar 03, 2012|
[size=3pt]Its a trick to test the OP's level of expertise! Tell someone, that doesn't understand Arrays or even Lists, to use a Map is . . . . . . [/size]
|Re: Java Collections Question by Ghenghis(m): 8:49am On Mar 04, 2012|
Ironically is Map would provide an easier solution. I know where you're going with the array thing, there are 4 fields.
He'll then start juggling indexes ,
Simple solution : Associative array(a.k.a HashMap)
so instead of using indexes he simply uses his search key to find the record his interested in.
Fayimora:You learn to write good code by reading good code written by others. Not by shielding developers from standard/basic data structures.
|Re: Java Collections Question by Fayimora(m): 9:58am On Mar 04, 2012|
Hmm you don't get the whole strategy then. My Algorithms and Data Structures prof said this: "NEVER use arrays! However, until you understand arrays, never use Lists."
HE NEEDS TO KNOW HOW TO JOGGLE THE INDICES! Its COMPULSORY!
I know as someone who has gone past the level of zipping arrays, you might be thinking Maps, quite typical. However, you should remember that you started from somewhere. You dont want to push someone to Maps when the best DS he knows is an Array.
Looking at the problem properly, using HaspMaps wouldn't be necessary. You can use 3 arrays(ignoring the description), 1 containing the names, the other containing the corresponding category and a final one containing the corresponding Cost! I guess you should know what to do. However as a challenge, loop through all three arrays at once and get all the information you need, in O(n)(ignore this for now if you dont know what it means).
An alternative, would be to create a small class that holds some data(attributes to store values from file). You would end up with an array of <some_object>. Again you just need to loop once.
I prefer the latter solution as the former is a bit dirty, however the former is a procedural approach! If it suits your needs then use it,
I have used arrays heavily in this program but. . . . .In what sense? posy your code here ==> http://pastie.org
[size=3pt]Let me tell you an experience I had. Someone from University of Liverpool mailed me(how she knew me is still a mystery) asking me to write some code to fit a spec for her. I rejected and she refused to leave my mailbox alone. I felt bad for her after a few days and since the deadline was in less that 24hrs, I decided to help. I asked her how far they had gone(syllabus wise) so I don't write code no-one would question. She said anything would be fine. My code was simple but complex. It was a C++ project. She failed the assignment. Not because I wrote bad code but because I didn't know that she knew nothing about OOP! I felt bad and still feel bad even thought she didn't blaming me. You should try to avoid such stuffs. [/size]
|Re: Java Collections Question by Mobinga: 11:39am On Mar 04, 2012|
On that Boss poo.
OP see also the split(); method in the String class and the StringTokenizer Class.
|Re: Java Collections Question by Ghenghis(m): 11:45am On Mar 04, 2012|
Mobinga:If you insist
|Re: Java Collections Question by bigboyslim(m): 3:04pm On Mar 04, 2012|
What I did
1. Used One temporary array to read in each Line of the file one at a time
2. Used the String.split method to split the line into 4
3. Stored each of the four coloumns into 4 separate arrays. I still retained the initial order of the file even though using 4 separate arrays
4. Then i extracted information from the 4 separate arrays
I had 2 hours to do this. Suffice to say it took me close to 10hours. If there ever was a poster boy for inefficiency.
Anyways. Can someone show how this can be done otherwise.
|Re: Java Collections Question by Fayimora(m): 3:28pm On Mar 04, 2012|
Start by using 3 arrays. You dont need to store the the description. Also, PASTE YOUR CODE HERE => http://www.pastie.org and post the link to it here. I want your code because I want to see what you have done , and then walk you through making a your solution awesome. You are going to have to do this a lot so you can aswel start learning.
|Re: Java Collections Question by bigboyslim(m): 3:42pm On Mar 04, 2012|
I can forward to you if you provide an email.
|Re: Java Collections Question by Fayimora(m): 4:52pm On Mar 04, 2012|
NO emails, Again, post it here(http://www.pastie.org) and give us a link to it!
|Re: Java Collections Question by Kobojunkie: 1:25pm On Mar 05, 2012|
@Poster, you really do not require heavy plumbing here if the file in question is in the format which you have it above.
Taking a look at it. it is '|' delimited, so you could simply read each line into an Array [/b]of strings, or maybe create an object such as
public string name ;
public string itemlist;
public string item;
public string cost ;
and then on reading each line, use the String split() method to split this and assign each in order to a new instance of Record object. Add your record object to an [b]ArrayList, and you are good to go.
The simple way is simply having an [b]Array [/b]of strings. However this might be tedious if you do not understand how to work well with loops, split() and indexing. Overall, no matter which of these two you go with, it should take a couple of minutes to put together as long as you are comfortable with reading Java class entries, and reading from files.
|Re: Java Collections Question by Fayimora(m): 1:55pm On Mar 05, 2012|
Kobojunkie:Read my post above. Specifically, the hidden one!
|Re: Java Collections Question by Kobojunkie: 2:33pm On Mar 05, 2012|
^^^ NO, thanks !
|Re: Java Collections Question by bigboyslim(m): 4:11pm On Mar 05, 2012|
Thanks to you guys both
@Fayimora. Sorry I can't paste the code on that site. I don't know how that site operates. I submitted the code to someone and if they do a search and find it online, I could innocently get penalized for plagiarism.
Thanks for your analysis. That was really insightful. Creating a new class and storing the objects of that class in an ArrayList would have worked magic for me. I'll still try to solve the question using that approach once i get the time.
In my solution I created a new class which had 4 arrays and worked off of that using crude for and if loops. The code worked but I believe your approach would have reduced dev time by a great deal.
Thanks again to both of you.
|Re: Java Collections Question by Fayimora(m): 6:44pm On Mar 05, 2012|
Funny how you can't post your code on PASTIE! No offence but doesn't seem to me like you want to learn! You just want an easy way out! In that case, how about a 20 line solution with 1 loop.
|Re: Java Collections Question by bigboyslim(m): 7:23pm On Mar 05, 2012|
Sir, no offence taken. I just gave you the reason why I don't want to paste the code on there or any other public sites for that matter. But you haven't tried to dispel my fears by saying "Oh never mind, they won't find it if they search" or something of that nature.
You are right I want the easy way out, but not in the way you think. I want to learn the easy way out.
|Re: Java Collections Question by bigboyslim(m): 2:16am On Apr 09, 2012|
I finally had some time to refactor the code. I have pasted it here -> http://www.pastie.org/3752966
Let me know what you think.
|Re: Java Collections Question by segsalerty(m): 12:48pm On Apr 09, 2012|
@Poster/Bigboyslim.................... the best thing u need it go for Kobojunkie solution, why the stress again since he put u thru a way out? oh my gosh
|Re: Java Collections Question by bigboyslim(m): 2:50pm On Apr 09, 2012|
I know what you mean. I would actually appreciate everybody's comment on the code. I refactored the code using kobojunkie's suggestion and the approach reduced the number of lines in my code(and hopefully execution time) by a great deal. So I have to say big thanks to Kobo first and foremost.
Anyways, all comments are welcome
|Re: Java Collections Question by segsalerty(m): 6:30pm On Apr 09, 2012|
|Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health |
religion celebs tv-movies music-radio literature webmasters programming techmarket
Nairaland - Copyright © 2005 - 2021 Oluwaseun Osewa. All rights reserved. See How To Advertise. 114