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.
The idea behind homework is you do it yourself and learn from the experience. If you show us what you've done so far and explain where you are stuck we will help you.
thank you for responding however 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 have 2 classes car and stock
car:
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 + ".");
}
}
stock:
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());
}
}
}
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"
Bookmarks