Results 1 to 4 of 4
  1. #1
    Ronan23 is offline Member
    Join Date
    Mar 2013
    Posts
    5
    Rep Power
    0

    Default View HouseForSale(Subclass) by id number

    Hi there im having a bit of a problem, I have a House class which contains an arraylist of rooms(Room class). There is also a Property Menu class which asks the user to choose between a range of options .ie add a house.

    There is also a HouseForSale class which contains saleable houses.
    Houses that are for sale implement an interface called Viewable. Viewable defines a single method called view() which displays the house details and the guide price (view() can call toString()).

    The part of the task I am having trouble with is this:

    Add a menu option in PropertyMenu to view a saleable house by house id number. You may find it
    useful to employ the instanceof operator to ensure that the House is Saleable.


    This is my property menu code so far:

    Java Code:
    import java.util.Scanner;
    import java.util.ArrayList;
    import java.io.*;
    
    public class PropertyMenu {
      
      ArrayList<House> houses = new ArrayList<House>();
    
      
      public static void main (String[] args)  throws IOException {
        PropertyMenu menu = new PropertyMenu();
        menu.runMenu();
      }
      
      public void runMenu() throws IOException {
        
        char selection = ' ';
        Scanner s = new Scanner(System.in);
        
        while (selection != 'e') {
          System.out.println("Property System");
          System.out.println("---------------"); 
            
          System.out.println("a.  Add a house");
          System.out.println("l.  Load a property file");
          System.out.println("v.  View a house");
          System.out.println("s.  View saleable houses");
          System.out.println("d.  Display all houses");
          System.out.println("r.  Remove a house");
          System.out.println("e.  Exit");
          System.out.println();
          System.out.println("Please choose from above (enter a, l, ..., e): ");
          selection = s.next().toLowerCase().charAt(0);
          
          switch (selection) {
            case 'a':
              Scanner sc = new Scanner(System.in);
        char choice = ' ';
        
         while (!(choice == 'Y' || choice == 'N')) {
             System.out.println("Is this house for sale?:");
        choice = sc.next().toUpperCase().charAt(0);
           } 
        
        if (choice == 'Y') {
          houses.add(new HouseForSale());
                 
                }
        if (choice == 'N') {
        houses.add(new House());
        }
              
              break;
    
                
            case 'l':
              loadFromFile();
              break;
            case 'v':
              viewHouse();
              break;
              case 's':
              viewSaleHouse();
              break;
              
            case 'd':
              viewAllHouses();
              break;
              case 'w':
                writeToFile();
                break;
            case 'r':
              removeHouse();
              break;
            case 'e':
              System.out.println("Goodbye");
              break;
            default:
              System.out.println("Incorrect option.");
          }
        }
          System.out.println("Exiting program.");
      }
      
        
      public void writeToFile()  throws IOException {
    
        
        try{
        String file = "house.dat";
          FileWriter filewrite = new FileWriter (file);
         BufferedWriter writer = new BufferedWriter (filewrite);
          PrintWriter outFile = new PrintWriter (writer);
          
    
        
        System.out.println ("Write the name of the output file:");
        Scanner sc = new Scanner(System.in);
        String choice = sc.next();
    
    outFile.print (houses);
      
          outFile.println ();
                writer.close();
                System.out.println ("Output file has been created: " + file);
        }
       catch (FileNotFoundException exception)
             {
                System.out.println ("File not found: ");
       }
      }
    
    
      
      public void viewHouse() {
        
         if (houses.isEmpty())
           System.out.println("House Database empty");
         else {
           System.out.println("Enter property id:");
           
           Scanner s = new Scanner(System.in);
           int id = s.nextInt();
           
           boolean found = false;
           
           for (House h : houses)
             if (id == h.getIdNum()) {
               System.out.print(h);
               found = true;
               break;
             }
           if (found == false) System.out.println("Could not find property " + id);
         }
      }
      
        public void viewSaleHouse() {
    
              if (houses.isEmpty())
           System.out.println("Houses for sale Database empty");
         else {
           System.out.println("Enter property id:");
           
           Scanner s = new Scanner(System.in);
           int id = s.nextInt();
           
           boolean found = false;
           
           for (House sale : houses)
             if (id == sale.getIdNum()) {
               System.out.print(sale);
               found = true;
               break;
             }
           if (found == false) System.out.println("Could not find property " + id);
         }
    
          }
      
      public void removeHouse() {
        
         if (houses.isEmpty())
           System.out.println("House Database empty");
         else {
           System.out.println("Enter property id:");
           
           Scanner s = new Scanner(System.in);
           int id = s.nextInt();
           
           boolean found = false;
           
           for (int i=0; i < houses.size(); i++) // need index this time
             if (id == houses.get(i).getIdNum()) {
               houses.remove(i);
               System.out.print("House " + id + " has been removed");
               found = true;
               break;
             }
           if (found == false) System.out.println("Could not find property " + id);
         }
      }
      public void viewAllHouses() {
         for (House h : houses)
               System.out.print(h);
         }
      
      public void loadFromFile()  throws IOException {
        Scanner scan = new Scanner(new File("property.dat"));
    
        while (scan.hasNext()) {
          
          String address = scan.nextLine();
          String houseType = scan.nextLine();
          int numRooms = scan.nextInt();
          scan.nextLine(); // for CR/NL
          
          char isRoomInfo = scan.nextLine().charAt(0); // Y or N?
          String roomInfo = null;
          if (isRoomInfo == 'Y')
            roomInfo = scan.nextLine();
        
          houses.add(new House(address, numRooms, houseType, roomInfo));
        }
    
      }
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,967
    Rep Power
    19

    Default Re: View HouseForSale(Subclass) by id number

    What bit of it are you having problems with?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Ronan23 is offline Member
    Join Date
    Mar 2013
    Posts
    5
    Rep Power
    0

    Default Re: View HouseForSale(Subclass) by id number

    I dont really know how to implement the instanceof operator to make sure the house is saleable

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,967
    Rep Power
    19

    Default Re: View HouseForSale(Subclass) by id number

    Java Code:
    if (someVariable instanceof HouseForSale) {
    }
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 8
    Last Post: 01-21-2012, 01:14 AM
  2. Dice help. posting the number of times a number is rolled.
    By cookiejarvus in forum New To Java
    Replies: 13
    Last Post: 12-04-2011, 11:08 PM
  3. Replies: 1
    Last Post: 10-28-2011, 08:18 AM
  4. How enter this mode-gui/design-view/code-view
    By lse123 in forum NetBeans
    Replies: 0
    Last Post: 02-28-2010, 09:09 PM
  5. Updating a view using actions in a seperate view
    By xcallmejudasx in forum Eclipse
    Replies: 0
    Last Post: 10-24-2008, 09:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •