英文:
Proper way to print objects from ArrayList within Criteria
问题
以下是翻译好的内容:
HouseListTester 类
package RealEstateListings;
import java.util.*;
public class HouseListTester {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("欢迎来到 Mike 的房屋列表");
System.out.println("请输入房屋列表的文件名:");
String sourceFolder = "C:\\Users\\micha\\Documents\\eclipse-workspace\\Real Estate Listings\\src\\RealEstateListings\\";
HouseList fileName = new HouseList(sourceFolder + input.next());
System.out.println();
System.out.println("请输入您的搜索条件");
System.out.print("最低价格:");
int minPrice = input.nextInt();
System.out.print("最高价格:");
int maxPrice = input.nextInt();
System.out.print("最小面积:");
int minArea = input.nextInt();
System.out.print("最大面积:");
int maxArea = input.nextInt();
System.out.print("最小卧室数量:");
int minBedrooms = input.nextInt();
System.out.print("最大卧室数量:");
int maxBedrooms = input.nextInt();
Criteria userCriteria = new Criteria(minPrice, maxPrice, minArea, maxArea, minBedrooms, maxBedrooms);
fileName.printHouses(userCriteria);
}
}
HouseList 类
package RealEstateListings;
import java.util.*;
import java.io.*;
public class HouseList {
public ArrayList<House> houseList;
public HouseList(String fileName) {
houseList = new ArrayList<House>();
try {
Scanner sc = new Scanner(new File(fileName));
while (sc.hasNextLine()) {
House newListing = new House(sc.next(), sc.nextInt(), sc.nextInt(), sc.nextInt());
houseList.add(newListing);
}
} catch (FileNotFoundException e) {
System.out.println("找不到文件。");
}
System.out.println(houseList.size() + " 个房屋已创建。");
}
public void printHouses(Criteria c) {
for (int i = 0; i < houseList.size(); i++)
if (houseList.get(i).satisfies(c))
System.out.println(houseList.get(i).toString());
}
public String getHouse(Criteria C) {
return "";
}
}
House 类
package RealEstateListings;
public class House {
String address;
int price, area, numberOfBedrooms;
public House(String addr, int salePrice, int saleArea, int numBedrooms) {
this.address = addr;
this.price = salePrice;
this.area = saleArea;
this.numberOfBedrooms = numBedrooms;
}
public String getAddress() {
return this.address;
}
public int getPrice() {
return this.price;
}
public int getArea() {
return this.area;
}
public int getNumberOfBedrooms() {
return this.numberOfBedrooms;
}
public boolean satisfies(Criteria c) {
if (c.getMinimumPrice() <= this.getPrice() && c.getMaximumPrice() >= this.getPrice())
if (c.getMinimumArea() <= this.getArea() && c.getMaximumArea() >= this.getArea())
if (c.getMinNumBedrooms() <= this.getNumberOfBedrooms() && c.getMaxNumBedrooms() >= this.getNumberOfBedrooms())
return true;
return false;
}
public String toString() {
return this.getAddress();
}
}
Criteria 类
package RealEstateListings;
public class Criteria {
int minimumPrice, maximumPrice, minimumArea, maximumArea;
int minimumNumberOfBedrooms, maximumNumberOfBedrooms;
public Criteria(int minPrice, int maxPrice, int minArea, int maxArea, int minBedrooms, int maxBedrooms) {
this.minimumPrice = minPrice;
this.maximumPrice = maxPrice;
this.minimumArea = minArea;
this.maximumArea = maxArea;
this.minimumNumberOfBedrooms = minBedrooms;
this.maximumNumberOfBedrooms = maxBedrooms;
}
public int getMinimumPrice() {
return minimumPrice;
}
public int getMaximumPrice() {
return maximumPrice;
}
public int getMinimumArea() {
return minimumArea;
}
public int getMaximumArea() {
return maximumArea;
}
public int getMinNumBedrooms() {
return minimumNumberOfBedrooms;
}
public int getMaxNumBedrooms() {
return maximumNumberOfBedrooms;
}
}
您提供的代码已经被翻译成了中文。如果您有任何进一步的问题,请随时提问。
英文:
I am going to try my best to explain what I'm trying to do. First, here are my classes:
HouseListTester Class
package RealEstateListings;
import java.util.*;
public class HouseListTester {
public static void main(String[] args) {
//create scanner for user input via console
Scanner input = new Scanner(System.in);
System.out.println("Welcome to Mike's House Listing");
System.out.println("Please enter the file name of the house list: ");
String sourceFolder = "C:\\Users\\micha\\Documents\\eclipse-workspace\\Real Estate Listings\\src\\RealEstateListings\\";
HouseList fileName = new HouseList(sourceFolder+input.next());
System.out.println();
System.out.println("Please enter your search criteria");
System.out.print("Minimum price: ");
int minPrice = input.nextInt();
System.out.print("Maximum price: ");
int maxPrice = input.nextInt();
System.out.print("Minimum area: ");
int minArea = input.nextInt();
System.out.print("Maximum area: ");
int maxArea = input.nextInt();
System.out.print("Minimum number of bedrooms: ");
int minBedrooms = input.nextInt();
System.out.print("Maximum number of bedrooms: ");
int maxBedrooms = input.nextInt();
Criteria userCriteria = new Criteria(minPrice, maxPrice, minArea, maxArea, minBedrooms, maxBedrooms);
fileName.printHouses(userCriteria);
}
}
HouseList Class
package RealEstateListings;
import java.util.*;
import java.io.*;
public class HouseList {
public ArrayList<House>houseList;
public HouseList(String fileName) {
houseList = new ArrayList<House>();
try {
//create scanner to read input
Scanner sc = new Scanner(new File(fileName));
while(sc.hasNextLine()) {
//input reads to parameters address price area numBedrooms
House newListing = new House(sc.next(), sc.nextInt(), sc.nextInt(), sc.nextInt());
//add newListing to houseList array
houseList.add(newListing);
}
}
catch(FileNotFoundException e)
{
System.out.println("File was not found.");
}
System.out.println(houseList.size()+" houses created.");
}
public void printHouses(Criteria c) {
for(int i = 0; i<houseList.size();i++)
if(houseList.get(i).satisfies(c))
System.out.println(houseList.get(i).toString());
}
public String getHouse(Criteria C) {
return "";
}
}
House Class
package RealEstateListings;
public class House {
String address;
int price,area,numberOfBedrooms;
public House(String addr, int salePrice, int saleArea, int numBedrooms) {
this.address = addr;
this.price = salePrice;
this.area = saleArea;
this.numberOfBedrooms = numBedrooms;
}
public String getAddress() {return this.address;}
public int getPrice() {return this.price;}
public int getArea() {return this.area;}
public int getNumberOfBedrooms() {return this.numberOfBedrooms;}
public boolean satisfies(Criteria c) {
if(c.getMinimumPrice() <= this.getPrice() && c.getMaximumPrice() >= this.getPrice())
if(c.getMaximumArea() <= this.getArea() && c.getMaximumArea() >= this.getArea())
if(c.getMinNumBedrooms() <= this.getNumberOfBedrooms() && c.getMaxNumBedrooms() >= this.getNumberOfBedrooms())
return true;
return false;
}
public String toString() {return this.getAddress();}
}
Criteria Class
package RealEstateListings;
public class Criteria {
int minimumPrice, maximumPrice, minimumArea, maximumArea;
int minimumNumberOfBedrooms, maximumNumberOfBedrooms;
public Criteria(int minPrice, int maxPrice, int minArea, int maxArea, int minBedrooms, int maxBedrooms) {
this.minimumPrice = minPrice;
this.maximumPrice = maxPrice;
this.minimumArea = minArea;
this.maximumArea = maxArea;
this.minimumNumberOfBedrooms = minBedrooms;
this.maximumNumberOfBedrooms = maxBedrooms;
}
public int getMinimumPrice() {return minimumPrice;}
public int getMaximumPrice() {return maximumPrice;}
public int getMinimumArea() {return minimumArea;}
public int getMaximumArea() {return maximumArea;}
public int getMinNumBedrooms() {return minimumNumberOfBedrooms;}
public int getMaxNumBedrooms() {return maximumNumberOfBedrooms;}
}
My current console output, after running:
Welcome to Mike's House Listing
Please enter the file name of the house list:
houses.txt
25 houses created.
Please enter your search criteria
Minimum price: 1
Maximum price: 1000000
Minimum area: 1
Maximum area: 1000000
Minimum number of bedrooms: 1
Maximum number of bedrooms: 1000
The houses.txt file includes a string, and three integers on every line, I've tested that the ArrayList is being created correctly, so my problem doesn't stem from there.
Question/Where I am confused:
I am attempting to use the printHouses function located in the HouseList Class. I am looking to print out all the houses which satisfy the criteria, from which the user has entered in the main method located in the HouseListTester Class. However, upon the user entering the criteria, no houses are listed. I am unsure as to what is causing this happen, I suspect it is something to do with me improperly calling to the toString function in the House Class. Or perhaps I am improperly calling to the printHouses function from the main method. I called to the printHouses function, using an object (fileName) which was first used to create an ArrayList of houses, and later, reused the same object to call to the printHouses function.
My concern is that no houses are printed at all, I believe my satisfied (located in Criteria Class) is working properly, and I also know the userCriteria object (located in HouseListTester) is created correctly from me testing other functions from that class on the newly created object.
Why aren't the houses which satisfy my said criteria being printed?
Thanks
答案1
得分: 1
Your problem lies in your satisfies
method in your House
class, instead of this
public boolean satisfies(Criteria c) {
if(c.getMinimumPrice() <= this.getPrice() && c.getMaximumPrice() >= this.getPrice())
if(c.getMaximumArea() <= this.getArea() && c.getMaximumArea() >= this.getArea())
if(c.getMinNumBedrooms() <= this.getNumberOfBedrooms() && c.getMaxNumBedrooms() >= this.getNumberOfBedrooms())
return true;
return false;
}
You'll need this
public boolean satisfies(Criteria c) {
if(c.getMinimumPrice() <= this.getPrice() && c.getMaximumPrice() >= this.getPrice())
if(c.getMinimumArea() <= this.getArea() && c.getMaximumArea() >= this.getArea())
if(c.getMinNumBedrooms() <= this.getNumberOfBedrooms() && c.getMaxNumBedrooms() >= this.getNumberOfBedrooms())
return true;
return false;
}
Notice this line
if(c.getMaximumArea() <= this.getArea() && c.getMaximumArea() >= this.getArea())
Should be
if(c.getMinimumArea() <= this.getArea() && c.getMaximumArea() >= this.getArea())
英文:
Your problem lies in your satisfies
method in your House
class, instead of this
public boolean satisfies(Criteria c) {
if(c.getMinimumPrice() <= this.getPrice() && c.getMaximumPrice() >= this.getPrice())
if(c.getMaximumArea() <= this.getArea() && c.getMaximumArea() >= this.getArea())
if(c.getMinNumBedrooms() <= this.getNumberOfBedrooms() && c.getMaxNumBedrooms() >= this.getNumberOfBedrooms())
return true;
return false;
}
You'll need this
public boolean satisfies(Criteria c) {
if(c.getMinimumPrice() <= this.getPrice() && c.getMaximumPrice() >= this.getPrice())
if(c.getMinimumArea() <= this.getArea() && c.getMaximumArea() >= this.getArea())
if(c.getMinNumBedrooms() <= this.getNumberOfBedrooms() && c.getMaxNumBedrooms() >= this.getNumberOfBedrooms())
return true;
return false;
}
Notice this line
if(<s>c.getMaximumArea()</s> <= this.getArea() && c.getMaximumArea() >= this.getArea())
Should be
if(<b>c.getMinimumArea()</b> <= this.getArea() && c.getMaximumArea() >= this.getArea())
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论