Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Instructions Using the HouseException.java and the House.java from A01-House you will implement a driver class called RealEstate.java to manipulate an Array of House objects. This

Instructions

Using the HouseException.java and theHouse.java from A01-House you will implement a driver class called RealEstate.java to manipulate an Array of House objects. This time you will be creating the driver class.

Your driver class should:

  • be named should be RealEstate.java
  • work with your House.java and HouseException.java
  • properly validate user input
  • not crash (always use try/catch).

Implementing the looping menu in the driver class.

  • When requesting user input, you must print a user-friendly message requesting it. Your code must be user-friendly.
  • Your code should continue to loop until the user selects 0. If the program terminates at any other point for any other reason you will lose points.
  • If the user enters an out of range menu option number, you should print a carefully crafted error message explaining the problem and print the menu again.
  • If the user enters anything other than a number, you should catch the exception, print a carefully crafted error message explaining the problem and print the menu again.

Implementing the menu options

If the user selects 1, (add a house)

  • Your code should read the house information from the user using the Scanner class.
  • You should validate everything using the House and HouseException and in case of invalid input print the appropriate message and go back to print the menu again.
  • If the user information is valid then you should make sure that there is no other house with the same MLS. In other words, you should make sure there are no duplicate houses/MLSs. If the MLS already exists you should print a message saying so and go back to the main menu, and continue to loop.
    • For the purpose of this assignments a duplicate house is a house with the same mls as another regardless of the other instance variables.

If the user selects 2, (remove a house)

  • You should read the MLS number
  • if the input is not valid, print an error message and loop back into the menu
  • You should search the array looking for a house matching the mls entered
  • If a match is found you should remove the house. You must print an appropriate message and loop back into the menu
    • when removing a house from the array you should be careful about leaving holes in the middle. They may result in an exception if they are not carefully handled.
  • it there is no match you should print an appropriate message for the user and loop back into the menu.

If the user selects 3 (print houses that cost less than a given price)

  • read the price from the user
  • You should validate it and in case of invalid input print the appropriate message and go back to print the menu again, or the main menu.
  • If the price is valid then you should should search the array and print all the houses with a price smaller than the price entered by the user and print them to the screen

If the user selects 4 (print all the houses)

  • you may use the enhanced for loop.
  • Make sure that the output is properly formatted; add a title to your printout!

For both print methods (options 3 and 4): You should never print any null positions. If you do this you will get points deducted.

If the user selects 0 .

  • you will print a "thank you" message and end program execution

Other considerations

  • The array should not contain duplicate mls houses at any time.
  • Feel free to use any of the Arrays class methods we learned this week in class.
  • You should use the proper exceptions, like InputMismatchException or HouseException, etc as appropriate
  • You should always use try/catch and your code should not crash
  • YOU MAY NOT USE ANY OTHER DATA STRUCTURES EXCEPT ARRAYS. You may use only ONE array of House objects.

public class HouseException extends Exception {

/** The message that outputs when the exception happens. */ private String message = "";

/** * Constructs a HouseException. */ // Empty constructor public HouseException() { } /** Mutator method. */ public void setMessage(String newMessage) { this.message = newMessage; }

/** Accessor method. */ public String getMessage() { return this.message; } // Closes main.

} // Closes class.

(---------------------)

(--------------------)

public class House { /** Instance Variable(s) mls, bedrooms, price, and seller.*/ // Integer ranges between House h = new House (10001,0,1000000,"tototo")10001 and 99999 private int mls = 0; // Integer ranging from 0 to 5. private int bedrooms = 0; // Double ranging from $0 to $1,000,000 private double price = 0; // Must be at least 2 non-blank characters long private String seller = ""; /** * Constructs a House object. * @exception HouseException If mls integer does not ranges between 10001 and 99999. * If bedrooms integer does not ranges from 0 to 5. * If If the price is not from $0 to $1,000,000 * If the seller is not at least 2 non-blank characters long. */ public House(int mls, int bedrooms, double price, String seller) throws HouseException { // Construtor Call the mutator and then // the mutator valid the instance variable or // throught the expcetion if is invalid. this.setMls(mls); this.setBedrooms(bedrooms); this.setPrice(price); this.setSeller(seller); } /** Accessor/Get methods. */ public int getMls() { return this.mls; } public int getBedrooms() { return this.bedrooms; } public double getPrice() { return this.price; } public String getSeller() { return this.seller; }

/** Mutator methods for product House mls. * Ensures the mls integer ranges between 10001 and 99999. * * @param newMls The new mls. * @exception HouseException If newMls length is between 10001 and 99999. */ public void setMls(int newMls) throws HouseException { // Valid mls integer is between 10001 and 99999. if (newMls >= 10001 && newMls <= 99999){ this.mls = newMls; } else { // New mls is invalid, throw a HouseException. HouseException he = new HouseException(); he.setMessage("Error: Mls integer has to be between 10001 and 99999"); throw he; } } // Closes setMls() /** Mutator methods for product House bedrooms. * Ensures the mls integer ranges from 0 to 5. * * @param newBedrooms The new bedrooms. * @exception HouseException If newBedrooms integer is from 0 and 5. */ public void setBedrooms(int newBedrooms) throws HouseException { // Valid bedrooms integer is from 0 and 5. if (newBedrooms >= 0 && newBedrooms <= 5){ this.bedrooms = newBedrooms; } else { // New bedrooms is invalid, throw a HouseException. HouseException he = new HouseException(); he.setMessage("Error: Bedrooms integer has to be from 0 to 5"); throw he; } } // Closes setBedrooms() /** Mutator methods for product House price. * Ensures the price integer ranges from 0 to 1,000,000. * * @param newPrice The new price. * @exception HouseException If newPrice integer is from 0 to 1,000,000. */ public void setPrice(double newPrice) throws HouseException { // Valid price integer is from 0 to 1,000,000. if (newPrice >= 0 && newPrice <= 1000000){ this.price = newPrice; } else { // New bedrooms is invalid, throw a HouseException. HouseException he = new HouseException(); he.setMessage("Error: Price integer has to be from 0 to 1,000,000"); throw he; } } // Closes setPrice() /** Mutator methods for product House seller. * Ensures the seller is at least 2 non-blank characters long. * * @param newBedrooms The new seller. * @exception HouseException If newBedrooms integer must be at least 2 non-blank characters long. */ public void setSeller(String newSeller) throws HouseException { // Valid seller integer must be at least 2 non-blank characters long. if (newSeller.trim().length() > 2){ this.seller = newSeller; } else { // New bedrooms is invalid, throw a HouseException. HouseException he = new HouseException(); he.setMessage("Error: Seller integer must be at" + "least 2 non-blank characters long."); throw he; } } // Closes setSeller() /** tooString Method */ public String toString() { String output = ""; // String with all the instance variables on separate lines. output += " Mls: " + mls; output += " Bedrooms: " + bedrooms; // Price properly formatted with dollar sign and two decimals. DecimalFormat formatter = new DecimalFormat(" Price: $#,000.00"); output += formatter.format(this.price); output += " Seller: " + seller; // Return the built string to where this method was called. return output; } // Closes main.

} // Closes class.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data And Hadoop Fundamentals Tools And Techniques For Data Driven Success

Authors: Mayank Bhushan

2nd Edition

9355516665, 978-9355516664

More Books

Students also viewed these Databases questions

Question

Technology. Refer to Case

Answered: 1 week ago