帮助解决 Java 错误,

发布于 2024-10-12 09:50:17 字数 5310 浏览 4 评论 0原文

所有这些都是在 bluej 中编译和运行的,所以不需要 main 方法,我收到很多错误,而且我是 java 新手,所以我不知道如何修复它们。任何帮助将不胜感激。

这个有错误`import java.util.*;

public class LettingAgent{

private ArrayList<Property>myProperty;
private int propertyNo;


public LettingAgent(){
    myProperty = new ArrayList<Property>();
    propertyNo = 0;
}

public void addProperty(String newAddress, char newLocation, int newMonthlyRent, int newNoOfBedrooms){
    Property newProperty = new Property(newAddress, newLocation, newMonthlyRent, newNoOfBedrooms);
    myProperty.add(newProperty);
}


public void removeProperty(int propertyNo){
    if(propertyNo>myProperty.size() || propertyNo<0){
        System.out.println("Invalid Property Number!");

    }else{
        myProperty.remove(propertyNo);
        System.out.println("Property Successfully removed.");            
    }
}


public void addTenant(int propertyNo, String tenantName){ 
    if(propertyNo>myProperty.size() || propertyNo<0){
        System.out.println("Invalid Property Number!");          
    }else{
        myProperty.get(propertyNo);
        //Property.newTenant(tenantName);            
    }
}


public void removeTenant(int propertyNo){
    if( propertyNo<myProperty.size()){
        myProperty.get(propertyNo);
        //Property.removeTenant();
    }else{
        System.out.println("Invalid Property Number!");
    }
}
public void searchProperty(String searchAddress){
    int propertyNumber = 0;     
    for(Property Property : myProperty){
        myProperty.get(propertyNumber);            
        if (myProperty.get(propertyNo).equals(searchAddress)){
            System.out.println("Property Details: "+ Property);
        }
        else{
            System.out.println("No Such Property.");
        }

        if(propertyNumber>myProperty.size()){
            System.out.println("No Such Property.");           
        }else{
            break;
        }           

    }

}
public void propertyList(Char location, int maxRent, int minBedrooms){

    for(Property Property : myProperty){
        if((Property.getLocation().equals(location))&& (Property.get(newMonthlyRent).equals(maxRent))&& (myProperty.get(newNoOfBedrooms).equals(minBedrooms))){
            System.out.println(propertyNo);
            Property.displayDetails();                            
        }else{
            System.out.println("Property Does Not Fit Search Criteria!");
        }        
    }                    
}

}

`

这是关联的类`//属性类 公共类属性{ //初始化变量 私有字符串地址; 私有字符位置; 私人 int 月租; 私人 int noOfBedrooms; 私有布尔值被占用; 私有字符串租户名称;

//constructor, to create a new property
public Property( String newAddress, char newLocation,int newMonthlyRent, int newNoOfBedrooms){ 
    address = newAddress;
    location = newLocation;
    monthlyRent = newMonthlyRent;
    noOfBedrooms = newNoOfBedrooms;
    occupied = false;
    tenantName = "";
}

//method to return the address of created property
public String getAddress(){
    return address;
}
//method to return the location of created property    
public char getLocation(){
    return location;
}
//method to return the rent of created property    
public int getMonthlyRent(){
    return monthlyRent;
}
//method to return the number of bedrooms of created property   
public int getNoOfBedrooms(){
    return noOfBedrooms;
}
//method to return the occupied status of created property    
public boolean getOccupied(){
    return occupied;
}
//method to return the name of the tenant of created property    
public String getTenantName(){
    return tenantName;
}
//method to set a new monthly rent
public void newRentRate(int newMonthlyRent){
    monthlyRent = newMonthlyRent;
}
//method to set a new tenant, but only works if current property is empty
public void newTenant(String newTenant){
    if(occupied){
        System.out.println(" ERROR! Property is already occupied. ");
    }else{
        tenantName = newTenant;
        occupied = true;       
    }

}
//method used to remove tenant and reset the occupied status to false
public void removeTenant(){
    if(occupied){
        tenantName = " ";
        occupied = false;
    }else{
        System.out.println("ERROR! Property is already empty.  ");
    }
}
//method implements a switch statement to switch the char type in to a more understandable string type
// and also then displays the full property details.
public void displayDetails(){
    String fullLocation;
    switch(location){
        case 'n': case 'N': fullLocation = " North London"; break;
        case 's': case 'S': fullLocation = " South London"; break;
        case 'e': case 'E': fullLocation = " East London"; break;
        case 'w': case 'W': fullLocation = " West London"; break;
            default: fullLocation = " unknown";
    }

    System.out.println("Address :" + address);
    System.out.println("Location :" + fullLocation);
    System.out.println("Monthly Rent : " + monthlyRent);
    System.out.println("Number of bedrooms : " + noOfBedrooms);
    System.out.println("Occupied Status : " + occupied);

    if(occupied){
        System.out.println("Tenants Name : " + tenantName);
    }else{
        System.out.println(" ");
    }

}

} `

All of this is compiled and run in bluej, so no main method is required, i am recieving many errors and i am new to java so i do not know how to fix them. Any help would be great appreciated.

This one has errors`import java.util.*;

public class LettingAgent{

private ArrayList<Property>myProperty;
private int propertyNo;


public LettingAgent(){
    myProperty = new ArrayList<Property>();
    propertyNo = 0;
}

public void addProperty(String newAddress, char newLocation, int newMonthlyRent, int newNoOfBedrooms){
    Property newProperty = new Property(newAddress, newLocation, newMonthlyRent, newNoOfBedrooms);
    myProperty.add(newProperty);
}


public void removeProperty(int propertyNo){
    if(propertyNo>myProperty.size() || propertyNo<0){
        System.out.println("Invalid Property Number!");

    }else{
        myProperty.remove(propertyNo);
        System.out.println("Property Successfully removed.");            
    }
}


public void addTenant(int propertyNo, String tenantName){ 
    if(propertyNo>myProperty.size() || propertyNo<0){
        System.out.println("Invalid Property Number!");          
    }else{
        myProperty.get(propertyNo);
        //Property.newTenant(tenantName);            
    }
}


public void removeTenant(int propertyNo){
    if( propertyNo<myProperty.size()){
        myProperty.get(propertyNo);
        //Property.removeTenant();
    }else{
        System.out.println("Invalid Property Number!");
    }
}
public void searchProperty(String searchAddress){
    int propertyNumber = 0;     
    for(Property Property : myProperty){
        myProperty.get(propertyNumber);            
        if (myProperty.get(propertyNo).equals(searchAddress)){
            System.out.println("Property Details: "+ Property);
        }
        else{
            System.out.println("No Such Property.");
        }

        if(propertyNumber>myProperty.size()){
            System.out.println("No Such Property.");           
        }else{
            break;
        }           

    }

}
public void propertyList(Char location, int maxRent, int minBedrooms){

    for(Property Property : myProperty){
        if((Property.getLocation().equals(location))&& (Property.get(newMonthlyRent).equals(maxRent))&& (myProperty.get(newNoOfBedrooms).equals(minBedrooms))){
            System.out.println(propertyNo);
            Property.displayDetails();                            
        }else{
            System.out.println("Property Does Not Fit Search Criteria!");
        }        
    }                    
}

}

`

this is the associated class `//Property class
public class Property{
//initialised variables
private String address;
private char location;
private int monthlyRent;
private int noOfBedrooms;
private boolean occupied;
private String tenantName;

//constructor, to create a new property
public Property( String newAddress, char newLocation,int newMonthlyRent, int newNoOfBedrooms){ 
    address = newAddress;
    location = newLocation;
    monthlyRent = newMonthlyRent;
    noOfBedrooms = newNoOfBedrooms;
    occupied = false;
    tenantName = "";
}

//method to return the address of created property
public String getAddress(){
    return address;
}
//method to return the location of created property    
public char getLocation(){
    return location;
}
//method to return the rent of created property    
public int getMonthlyRent(){
    return monthlyRent;
}
//method to return the number of bedrooms of created property   
public int getNoOfBedrooms(){
    return noOfBedrooms;
}
//method to return the occupied status of created property    
public boolean getOccupied(){
    return occupied;
}
//method to return the name of the tenant of created property    
public String getTenantName(){
    return tenantName;
}
//method to set a new monthly rent
public void newRentRate(int newMonthlyRent){
    monthlyRent = newMonthlyRent;
}
//method to set a new tenant, but only works if current property is empty
public void newTenant(String newTenant){
    if(occupied){
        System.out.println(" ERROR! Property is already occupied. ");
    }else{
        tenantName = newTenant;
        occupied = true;       
    }

}
//method used to remove tenant and reset the occupied status to false
public void removeTenant(){
    if(occupied){
        tenantName = " ";
        occupied = false;
    }else{
        System.out.println("ERROR! Property is already empty.  ");
    }
}
//method implements a switch statement to switch the char type in to a more understandable string type
// and also then displays the full property details.
public void displayDetails(){
    String fullLocation;
    switch(location){
        case 'n': case 'N': fullLocation = " North London"; break;
        case 's': case 'S': fullLocation = " South London"; break;
        case 'e': case 'E': fullLocation = " East London"; break;
        case 'w': case 'W': fullLocation = " West London"; break;
            default: fullLocation = " unknown";
    }

    System.out.println("Address :" + address);
    System.out.println("Location :" + fullLocation);
    System.out.println("Monthly Rent : " + monthlyRent);
    System.out.println("Number of bedrooms : " + noOfBedrooms);
    System.out.println("Occupied Status : " + occupied);

    if(occupied){
        System.out.println("Tenants Name : " + tenantName);
    }else{
        System.out.println(" ");
    }

}

}
`

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过度放纵 2024-10-19 09:50:18

Property 类没有定义方法 get()

Property.get(newMonthlyRent).equals( 更改为 Property.getMonthlyRent().equals(

另外,您似乎正在将 Property单个属性与 ArrayList myProperty,其中包含所有属性的列表

ArrayList 上的 get() 方法将返回单个 Property 实例,因此在您的 addTenant() 方法中,使用 myProperty.get(propertyNo).newTenant(tenantName);


UPDATE

Property.getLocation( ) 返回一个 char 而不是 Stringchar 没有 equals()。将

Property.getLocation().equals(location) 更改为 Property.getLocation() == location

The class Property does not have a method get() defined.

Change Property.get(newMonthlyRent).equals( to Property.getMonthlyRent().equals(

Also, it seems that you are mixing Property a single property with the ArrayList myProperty which holds a list of all the properties

The get() method on the ArrayList will return the single Property instance, so in your addTenant() method, use myProperty.get(propertyNo).newTenant(tenantName);


UPDATE

Property.getLocation() returns a char not a String. A char doesn't have an equals() method.

Change Property.getLocation().equals(location) to Property.getLocation() == location

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文