I require some help with a program I've got to write, the theory behind it is based around OOP, but I'm having trouble grasping what exactly I should be doing.

I'm not entirely sure how to call the addProperty method from the Property class, or even that I've written out how that method should look, comments, advice?

Regards, Charles.

Main Class.

import java.util.*;

public class A2 {
  public static void main (String[] args) {

//Data field.
    Property properties[] = new Property[10];
    boolean exit;
    int count;
    String rootmenu;
    exit = false;
    Scanner input = new Scanner(System.in);


//Program body.
    while (exit != true) {

// Menu.
      System.out.println("Menu:");
      System.out.println("1. Add Properties to the system");
      System.out.println("2. Enter Rental Agreement information (provide: propertyID, tenantID, Start Date)");
      System.out.println("3. Terminate a Rental agreement (provide: propertyID, tenantID, End Date)");
      System.out.println("4. Search for Properties(based on weekly_rental_rate)");
      System.out.println("5. List Properties(not visible to clients)");
      System.out.println("6. List Properties (currently rented out)");
      System.out.println("7. List Properties(available for rent)");
      System.out.println("8. Display the details(1 property or all)");
      System.out.println("X. Exit");

  //Menu handler.
      rootmenu = input.nextLine();

      if (rootmenu.equalsIgnoreCase ("1") == true) {
        System.out.println("\n1 reached.\n");
      }
      if (rootmenu.equalsIgnoreCase ("2") == true) {
        System.out.println("\n2 reached.\n");
      }
      if (rootmenu.equalsIgnoreCase ("3") == true) {
        System.out.println("\n3 reached.\n");
      }
      if (rootmenu.equalsIgnoreCase ("4") == true) {
        System.out.println("\n4 reached.\n");
      }
      if (rootmenu.equalsIgnoreCase ("5") == true) {
        System.out.println("\n5 reached.\n");
      }
      if (rootmenu.equalsIgnoreCase ("6") == true) {
        System.out.println("\n6 reached.\n");
      }
      if (rootmenu.equalsIgnoreCase ("7") == true) {
        System.out.println("\n7 reached.\n");
      }
      if (rootmenu.equalsIgnoreCase ("8") == true) {
        System.out.println("\n8 reached.\n");
      }

  //Incorrect menu input handler.  Couldn't use switch, nested ifchecks quickest option.
      if (rootmenu.equalsIgnoreCase ("X") == false) {
        if (rootmenu.equalsIgnoreCase ("1") == false) {
          if (rootmenu.equalsIgnoreCase ("2") == false) {
            if (rootmenu.equalsIgnoreCase ("3") == false) {
              if (rootmenu.equalsIgnoreCase ("4") == false) {
                if (rootmenu.equalsIgnoreCase ("5") == false) {
                  if (rootmenu.equalsIgnoreCase ("6") == false) {
                    if (rootmenu.equalsIgnoreCase ("7") == false) {
                      if (rootmenu.equalsIgnoreCase ("8") == false) {
                        System.out.println("\nIncorrect input, please try again.\n");
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }

  //Exit statement.
      if (rootmenu.equalsIgnoreCase ("X") == true) {
        exit = true;
      }
    }
  }
}

Property Class.

import java.util.*;

public class Property {

//Data field
  private String propertyID;
  private String description;
  private String location;
  private char avaliability; // A for avaliable, R for rented.
  private double weeklyRental;
  private boolean visibility;
  private String tenantID;
  private String startDate;
  private String endDate;
  Scanner input = new Scanner(System.in);

//Constructors
  public Property(String propertyID, String description, String location, double weeklyRental) {
    this.propertyID = propertyID;
    this.description = description;
    this.location = location;
    this.weeklyRental = weeklyRental;
    avaliability = 'A';
    visibility = false;
  }

//Method
  public void addProperty(String propertyID, String description, String location, double weeklyRental) {
    System.out.println("Enter Property ID:");
    propertyID = input.nextLine();
    System.out.println("Enter Description:");
    description = input.nextLine();
    System.out.println("Enter Location:");
    location = input.nextLine();
    System.out.println("Enter weekly rental rate:");
    weeklyRental = input.nextDouble();
    new Property("propertyID", "description", "location", 0.0);
  }
}

Recommended Answers

All 6 Replies

how to call the addProperty method from the Property class

Can you explain a bit more?
The addProperty method is in the Property class.
There shouldn't be any problem calling it from the Property class.

Can you explain what the app is supposed to do?
What is the addProperty() method supposed to do?

I'm supposed to call the addProperty part from the main class. I'm pretty sure my Method in Property.java is wrong, so I changed it to this..

public void addProperty(String propertyID, String description, String location, double weeklyRental) {
    propertyID = propertyID;
    description = description;
    location = location;
    weeklyRental = weeklyRental;
    new Property("propertyID", "description", "location", 0.00);
  }

The method is supposed to store variables in an array that I can call back again from the main class.

Can you explain what the app is supposed to do?

What is the addProperty() method supposed to do?
Your code for addProperty() stores values into the current instance of the class and then creates a new instance of the class which is immediately destroyed/lost when the method exits.

The 'add' prefix to the name, implies that it is adding something. The code looks more like what would be in a method prefixed by 'set'. The code "sets" the values of the object.

I think you should back away from doing coding and spend some time thinking about what the app and the classes are supposed to do.

Can you explain what the app is supposed to do?

What is the addProperty() method supposed to do?
Your code for addProperty() stores values into the current instance of the class and then creates a new instance of the class which is immediately destroyed/lost when the method exits.

The 'add' prefix to the name, implies that it is adding something. The code looks more like what would be in a method prefixed by 'set'. The code "sets" the values of the object.

I think you should back away from doing coding and spend some time thinking about what the app and the classes are supposed to do.

I understand what I need to do I just don't really understand how to do it.

From A2 I need store values in an array, to do this I'm supposed to call the Property class, the values I need to store are propertyID, description, location, availability, weeklyRental and visibility, these all need to be stored in a way that they're linked relative to each other.

I need store values in an array

The Property class is probably what is stored in the array.

Do you know how to store objects in an array?

The contents of the Property object are: propertyID, description, location, availability, weeklyRental and visibility. These are passed to the constructor to create a new Property object.

I understand what I need to do

Make a list, one thing on a line, of what you need to do. The "thing"s should be very simple operations.
Then go thru the list and figure out how to do each thing.

From A2

What is A2? That is poor name for a class. Use some word(s) that describe what it is for.

Thanks for your help NormR1, I've figured out what I was doing wrong and have revised how this code works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.