Hello I want to find a way to get the index of the arraylist properties using one or two of the objects.
Property Class: `
public class Property implements Serializable{
ArrayList <Task> tasks = new ArrayList<Task>();
String type; //Condo, HDB, Landed
String name;
int size;
boolean rental;
String postcode;
String block;
String unit;
public Property(String type, String name, int size, boolean rental, String postcode, String block, String unit) {
this.tasks = tasks;
this.type = type;
this.name = name;
this.size = size;
this.rental = rental;
this.postcode = postcode;
this.block = block;
this.unit = unit;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public ArrayList<Task> getTasks() {
return tasks;
}
public String getName() {
return name;
}
public int getSize() {
return size;
}
public boolean isRental() {
return rental;
}
public String getPostcode() {
return postcode;
}
public String getBlock() {
return block;
}
public String getUnit() {
return unit;
}
public boolean getRental(){
return this.rental;
}
public void setName(String name) {
this.name = name;
}
public void setSize(int size) {
this.size = size;
}
public void setRental(boolean rental) {
this.rental = rental;
}
public void setPostcode(String postcode) {
this.postcode = postcode;
}
public void setBlock(String block) {
this.block = block;
}
public void setUnit(String unit) {
this.unit = unit;
}
Property Manager Class where most of the setting up of the file and stuff goes on.
public class PropertyManager {
private static String fileName="Properties.ser";
public static ArrayList <Property> properties;
public static void main(String args[]){
// check if file exists
//load it into properties arraylist
// if no create and empty properties arraylist
FileInputStream fis;
try{
fis = new FileInputStream(fileName);
ObjectInputStream ois;
ois = new ObjectInputStream(fis);
properties=(ArrayList <Property>)ois.readObject();
fis.close();
System.out.println("Loaded in a file with "+properties.size()+" properties");
} catch (Exception e){
System.out.println("There is no datafile - creating a new one");
//e.printStackTrace();
//properties=new ArrayList <Property>();
testData();
}
MainScreen ms=new MainScreen();
ms.setVisible(true);
}
public void AddProperty(String fName ){
}
public static void SavePropertiesToFile(){
try{
FileOutputStream outputFile = new FileOutputStream(fileName);
ObjectOutputStream oos;
oos = new ObjectOutputStream(outputFile);
oos.writeObject(properties);
outputFile.close();
System.out.println("Data file "+fileName+" has been saved");
} catch (Exception e){
System.out.println("There was an error saving the file:");
e.printStackTrace();
//properties=new ArrayList <Property>();
testData();
}
}
static void testData(){
properties=new ArrayList<Property>();
Random rand=new Random();
Task t;
Property p;
for (int i=0;i<10;i++){
//public Property(String name, int size, boolean rental, String postcode, String block, String unit) {
p=new Property("CONDO","Property_"+i,20200,true,"280009","14","01-05");
//public Task(char type, int priority, String description) {
t=new Task('M',3,"This s a dummy task"+rand.nextInt());
t=new Task('M',3,"This s a dummy task"+rand.nextInt());
t=new Task('M',3,"This s a dummy task"+rand.nextInt());
p.getTasks().add(t);
properties.add(p);
}
}
}
I want to find a way where I can enter a name of a property and click find then it will load up the entire property that was entered. Then I can edit or delete the property using that.
So I wanted to find the Index to do so, but if there is a different method instead of finding the index I would not mind trying that out.
I also saw some posts about overriding the equals() or something but I have no clue how to do so.
Use a proper data structure. If you want to lookup properties by name then I would recommend to use a java.util.Map
where the key is the property name and the value the property itself.
In PropertyManager
declare properties as follows:
public static Map<String, Property> properties;
Then your testData()
method will look as this:
static void testData(){
properties=new HashMap<>();
Random rand=new Random();
Task t;
Property p;
for (int i=0;i<10;i++){
String propertyName = "Property_"+i;
//public Property(String name, int size, boolean rental, String postcode, String block, String unit) {
p=new Property("CONDO",propertyName,20200,true,"280009","14","01-05");
//public Task(char type, int priority, String description) {
t=new Task('M',3,"This s a dummy task"+rand.nextInt());
p.getTasks().add(t);
properties.put(propertyName, p);
}
}
Now you can easily look up the property by name:
Property property = properties.get(name);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.