The following block of code I try to print properties, whose location, monthlyRent, numberOfBedroomsdefined in a class Property, are these parameters method LettingAgent class. When I compiled, the error occurs in the if line, where it is indicated that a non-static method, for example getlocation(), cannot refer to a static context.
public Property( String addressinput, char locationinput, double Rentinput, int Bedsinput )
{
address = addressinput;
location = locationinput;
monthlyRent = Rentinput;
numberOfBedrooms = Bedsinput;
occupied = false;
tenantName = "";
}
public String getAdress( )
{
return address;
}
public char getLocation( )
{
return location;
}
public double getmonthlyRent( )
{
return monthlyRent;
}
public int getnumberOfBedrooms( )
{
return numberOfBedrooms;
}
public boolean isoccupied( )
{
return occupied;
}
public String gettenantName( )
{
return tenantName;
}
public void setmonthlyRent( double newRent )
{
monthlyRent = newRent;
}
public void addtenantName(String newTenant)
{
if(occupied == false) {
tenantName = newTenant;
occupied = true;
}
else {
System.out.println(" The property is already occupied");
}
}
public void removetenantName( )
{
if( occupied == true) {
tenantName = "";
occupied = false;
}
else {
System.out.println(" The property is new and not occupied");
}
}
public void printProperty( )
{
System.out.println( "The details of the property are as follow" );
System.out.println("Address:" + address );
switch(location){
case 'n': case 'N': System.out.println("Location: North london" );break;
case 's': case 'S': System.out.println("Location: South london" );break;
case 'e': case 'E': System.out.println("Location: East london" );break;
case 'w': case 'W': System.out.println("Location: West london" );
}
System.out.println("Monthly-Rent:" + monthlyRent );
System.out.println("Number of Bedrooms:" + numberOfBedrooms );
System.out.println("Status of property:" + occupied );
if(occupied == true) {
System.out.println("Tenant Name:" + tenantName );
}
else {
System.out.println("Property is empty at moment" );
}
}
}
LettingAgent class (calling class)
import java.util.ArrayList;
public class LettingAgent
{
private ArrayList<Property>agproperty;
private int Propnumber;
public LettingAgent()
{
agproperty = new ArrayList<Property>();
Propnumber = 0;
}
public int getPropnumber(){
return Propnumber;
}
public void addNewProperty(String addressinput, char locationinput, double Rentinput, int Bedsinput)
{
Property newProperty = new Property(addressinput,locationinput,Rentinput,Bedsinput);
agproperty.add(newProperty);
}
public void removalOfProperty(int Propnumber)
{
if( Propnumber<agproperty.size()){
agproperty.remove(Propnumber);
System.out.println("The property has been removed");
}
else{
System.out.println("The Property number is not valid");
}
}
public void addTenant(int Propnumber, String newName, Property newProperty)
{
if( Propnumber<agproperty.size()){
agproperty.get(Propnumber);
newProperty.addtenantName(newName);
}
else{
System.out.println("The Property number is not valid");
}
}
public void removeTenant(int Propnumber,Property newProperty)
{
if( Propnumber<agproperty.size()){
agproperty.get(Propnumber);
newProperty.removetenantName( );
}
else{
System.out.println("The Property number is not valid");
}
}
public void searchProperty(String Address)
{
for(Property Property : agproperty){
if (agproperty.get(Propnumber).equals( Address)){
System.out.println("The details of the property is as follow"+ Property);
}
else{
System.out.println("The property is not in the Letting agent Stock");
}
}
}
public void printListOfUnoccupiedProperty( char Location, double maxmonthlyRent, int miniNoBeds)
{
for(Property property : agproperty){
if((Property.getLocation().equals(Location))&&(Property.get(monthlyRent).equals(maxmonthlyRent))&&(agproperty.get(numberOfBedrooms).equals(miniNoBeds))){
System.out.Println(Propnumber);
Property.printProperty();
}
else{
System.out.println("The Property number is not valid");
}
}
}
}
source
share