Is it possible to apply a search function when using Get and Set?
Sorry, I'm new to Java and there's probably a very simple answer to this.
At the moment, I'm printing all the results out in individual JOptionPane
s.
Here's my code so far:
public class Main {
public static void main(String[] args) {
//Create new Person objects
Address p1 = new Address("27","Abbey View","Hexham","NE46 1EQ");
Address p2 = new Address("15", "Chirdon Crescent", "Hexham", "NE46 1LE");
Address p3 = new Address("6", "Causey Brae", "Hexham", "NE46 1DB");
Details c1 = new Details();
Details c2 = new Details();
Details c3 = new Details();
//Send some messages to the objects
c1.setBeds("3 ");
c2.setBeds("6");
c3.setBeds("4");
c1.setPrice("175,000");
c2.setPrice("300,00");
c3.setPrice("250,000");
c1.setType("Terraced");
c2.setTyp开发者_如何学运维e("Bungalow");
c3.setType("Detached");
//Set up the association
p1.ownsDetails(c1);
p2.ownsDetails(c2);
p3.ownsDetails(c3);
//Print result
p1.printDetails();
p2.printDetails();
p3.printDetails();
//Finally quit
System.exit(0);
}
}
Any help would be appreciated, thanks.
Instead of having three separate variables, you may find it helpful to have an array:
Address p[] = new Address[3];
p[0] = new Address("27","Abbey View","Hexham","NE46 1EQ");
p[1] = new Address("15", "Chirdon Crescent", "Hexham", "NE46 1LE");
p[2] = new Address("6", "Causey Brae", "Hexham", "NE46 1DB");
Now it's possible to write a loop to examine each one:
for(int i = 0; i < p.length; i++) {
if (p[i].getSomething().equals(thingToSearch)) {
// ... it's a match!
}
}
Yes, you can create a custom JDialog
or JFrame
that has multiple input fields and multiple results that appear due to searching.
Ideally, your objects would need to be in a list or array to do the searching,
so that you can loop over all of them and find matches.
精彩评论