Results 1 to 9 of 9
Thread: print/search function in bluej?
- 05-22-2012, 08:34 PM #1
Member
- Join Date
- May 2012
- Posts
- 4
- Rep Power
- 0
print/search function in bluej?
hi,
i have been trying to do this myself for the past month, i have the "objects first with java" book and still cannot find what i am looking for.
i need to know what is the code for my stock class that is required to:
search for and print all objects that match the term the user enters
i.e - when i select this function it should pop up a message that allows me to enter a string value. it then searches through my arraylist for any objects with matching string values and prints theme
i remember using this function before but cannot remember the code.
i have 2 classes car and stock
car:
stock:Java Code:/** * The Car class represents a car object. Information about the car is stored and can be retrieved. */ public class Car { private String make; private String model; private String reg; private int paid; private int sold; /** * Initializing the Car class. */ public Car(String theMake, String theModel, String theReg, int PricePaid, int PriceSold) { make = theMake; model = theModel; reg = theReg; paid = PricePaid; sold = PriceSold; } /** * Print details about this Car */ public void print() { System.out.println("Make: " + make + " - Model: " + model + " - Reg: " + reg + " - Paid: " + paid + " - Sold: " + sold + "."); } }
the problem is that i want a search function that will print out a list of results e.g. search for "ford" and it will print out all the cars of make "ford"Java Code:import java.util.ArrayList; import java.util.Iterator; /** * The Stock class is a functions like a database. It is the facility to store all the information * on cars that delboy has. */ public class Stock { private ArrayList<Car> cars; /** * Construct an empty Stock database. */ public Stock() { cars = new ArrayList<Car>(); } /** * Add a new car to the Stock class. */ public void addCar(Car theCar) { cars.add(theCar); } /** * Print a list of all stored Cars. */ public void listCars() { Iterator<Car> it = cars.iterator(); while(it.hasNext()) { System.out.println(it.next()); } } }
- 05-23-2012, 06:49 AM #2
Re: print/search function in bluej?
Cross posted
print/search function in bluej? - CodeGuru Forums
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 05-23-2012, 06:58 PM #3
Member
- Join Date
- May 2012
- Posts
- 4
- Rep Power
- 0
Re: print/search function in bluej?
yes its cross posted but only because i need an answer quickly
-
Re: print/search function in bluej?
So it doesn't bother you if a volunteer at one of your cross-post site spends time trying to answer the question without realizing that it's already been answered elsewhere?
As for me, I'd prefer not answering questions of folks that don't care about my time. I'd much prefer they told me up front if they were cross-posting and where.
-
Re: print/search function in bluej?
- 05-23-2012, 07:25 PM #6
Member
- Join Date
- May 2012
- Posts
- 4
- Rep Power
- 0
Re: print/search function in bluej?
i would close the other post once i received a working answer and surely the is only one correct answer to my question which is clearly detailed above. i mentioned what resources i have used as well i really didnt think it was such a difficult problem.
- 05-23-2012, 07:59 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
Re: print/search function in bluej?
It is quite difficult; for starters, your first comment for the Car class isn't true:
My question: how can any information be retrieved? Through the String representation of this class? That'd be hacking. There is absolutly nothing in your code of your classes that helps a wanted search or print functionality. We are not going to do all that work for you.Java Code:/** * The Car class represents a car object. Information about the car is stored and can be retrieved. */
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-23-2012, 08:16 PM #8
Member
- Join Date
- May 2012
- Posts
- 4
- Rep Power
- 0
Re: print/search function in bluej?
i thought that by using the stock class where the arraylist is all the information about the car is stored once it has been added. does that not mean that you only need the printing searching functions in the stock class?
and also there is a print function that prints out the details of that car.
Java Code:/** * Print details about this Car */ public void print() { System.out.println("Make: " + make + " - Model: " + model + " - Reg: " + reg + " - Paid: " + paid + " - Sold: " + sold + "."); } }
- 05-23-2012, 08:42 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
Re: print/search function in bluej?
That Stock class doesn't have access to the details of a Car object either (the properties are private and there are no getXXX() methods in the Car class). Trying to obtain the values of the properties through its String representation (the print() method) is a hack.
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
How to ad a search function
By david.ny in forum NetBeansReplies: 2Last Post: 12-03-2011, 08:43 PM -
Simple search-function: what am I doing wrong?
By Bas in forum New To JavaReplies: 4Last Post: 07-23-2009, 09:45 PM -
Search function for ArrayList?
By javanoobie in forum New To JavaReplies: 11Last Post: 04-17-2009, 08:38 PM -
How to make print function for single label?
By makpandian in forum AWT / SwingReplies: 2Last Post: 02-12-2009, 05:26 PM -
make search function ike eclipse search in window->preference
By i4ba1 in forum Advanced JavaReplies: 5Last Post: 08-26-2008, 03:43 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks