使用 Criteria 内正确打印 ArrayList 中的对象的方法:

huangapple go评论74阅读模式
英文:

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(&quot;Welcome to Mike&#39;s House Listing&quot;);
System.out.println(&quot;Please enter the file name of the house list: &quot;);
String sourceFolder = &quot;C:\\Users\\micha\\Documents\\eclipse-workspace\\Real Estate Listings\\src\\RealEstateListings\\&quot;;
HouseList fileName = new HouseList(sourceFolder+input.next());
System.out.println();
System.out.println(&quot;Please enter your search criteria&quot;);
System.out.print(&quot;Minimum price: &quot;);
int minPrice = input.nextInt();
System.out.print(&quot;Maximum price: &quot;);
int maxPrice = input.nextInt();
System.out.print(&quot;Minimum area: &quot;);
int minArea = input.nextInt();
System.out.print(&quot;Maximum area: &quot;);
int maxArea = input.nextInt();
System.out.print(&quot;Minimum number of bedrooms: &quot;);
int minBedrooms = input.nextInt();
System.out.print(&quot;Maximum number of bedrooms: &quot;);
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&lt;House&gt;houseList;
public HouseList(String fileName) {
houseList = new ArrayList&lt;House&gt;();
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(&quot;File was not found.&quot;);
}
System.out.println(houseList.size()+&quot; houses created.&quot;);
}
public void printHouses(Criteria c) {
for(int i = 0; i&lt;houseList.size();i++) 
if(houseList.get(i).satisfies(c))
System.out.println(houseList.get(i).toString());
}
public String getHouse(Criteria C) {
return &quot;&quot;;
}
}

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() &lt;= this.getPrice() &amp;&amp; c.getMaximumPrice() &gt;= this.getPrice())
if(c.getMaximumArea() &lt;= this.getArea() &amp;&amp; c.getMaximumArea() &gt;= this.getArea()) 
if(c.getMinNumBedrooms() &lt;= this.getNumberOfBedrooms() &amp;&amp; c.getMaxNumBedrooms() &gt;= 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&#39;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() &lt;= this.getPrice() &amp;&amp; c.getMaximumPrice() &gt;= this.getPrice())
if(c.getMaximumArea() &lt;= this.getArea() &amp;&amp; c.getMaximumArea() &gt;= this.getArea()) 
if(c.getMinNumBedrooms() &lt;= this.getNumberOfBedrooms() &amp;&amp; c.getMaxNumBedrooms() &gt;= this.getNumberOfBedrooms())
return true;
return false;
}

You'll need this

public boolean satisfies(Criteria c) {
if(c.getMinimumPrice() &lt;= this.getPrice() &amp;&amp; c.getMaximumPrice() &gt;= this.getPrice())
if(c.getMinimumArea() &lt;= this.getArea() &amp;&amp; c.getMaximumArea() &gt;= this.getArea()) 
if(c.getMinNumBedrooms() &lt;= this.getNumberOfBedrooms() &amp;&amp; c.getMaxNumBedrooms() &gt;= 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())

huangapple
  • 本文由 发表于 2020年10月10日 05:16:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/64287425.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定