简体   繁体   中英

Java: Defining a static read method an abstract superclass and its child class?

I am very much stuck on an assignment regarding inheritance, abstract classes, and static methods.

We must define an abstract class House. We must define a child class RentalHouse. We must define a child class OwnerOccupiedHouse.

So first all the House class:

public abstract class House {

    private Address address;
    private int nRooms;
    private int salePrice;
    private boolean available;

    public House(Address address, int nRooms, int salePrice, boolean available) {
        this.address = address;
        this.nRooms = nRooms;
        this.salePrice = salePrice;
        this.available = available;
    }

    public Address getAddress() {
        return this.address;
    }


    public int getNRooms() {
        return this.nRooms;
    }


    public int getSalePrice() {
        return this.salePrice;
    }

    public boolean getAvailable() {
        return this.available;
    }

    public static House read(Scanner sc) {
        String line = sc.next();
        if(line.equals("FOR RENT")) {
            return RentalHouse.read(sc);
        }
        else if(line.equals("RENTED"){
            return RentalHouse.read(sc);
        }
        else if(line.equals("FOR SALE"){
            return OwnerOccupiedHouse.read(sc);
        }
        else{ 
            //if(line.equals("SOLD"))
            return OwnerOccupiedHouse.read(sc);
        }

    }
}

The question is regarding the read method. Ofcourse a House object cannot be instantiated because of its abstractness. However a child object can be created that is assigned to type House. In the assignment I have to read from a text file with specific format:

"FOR SALE"
an address
7 rooms
price 1000
"SOLD"
another address
3 rooms
price 500

The assignment states as soon as you know whether you are dealing with a RentalHouse or an OwnerOccupiedHouse. So as soon as we read "FOR SALE" or "SOLD" for example we must use a read method defined in these classes. We can find the problem I encountered in these read methods.

In order for the reader to keep it a bit more understandable I will only show the RentalHouse case as the main principle would be the same for both classes.

So here we have the RentalHouse class:

public class RentalHouse extends House {

    public RentalHouse(Address address, int nRooms, int salePrice, boolean available) {
        super(address, nRooms, salePrice, available);
    }

    public static RentalHouse read(Scanner sc){
        Address address = sc.next()
        int nRooms = sc.nextInt()
        sc.next()
        sc.next()
        int SalePrice = sc.nextInt()
        return new RentalHouse(address, nRooms, SalePrice, **availability**);
    }

The problem I encounter in this read method is that we actually have to decide whether its availability is true or false. "FOR RENT" is true and "RENTED" is false for example. However we encountered this line already in the read method of the abstract superclass House.

Therefore my question is: How do I create a read method for House and for RentalHouse with regard to the attribute available such that I can create a RentalHouse object?

Kevin Anderson's response in comments will work, but just in case you cannot change the signature of the read method, an easy alternative seems to be this.

In the read method of RentalHouse or OwnerOccupiedHouse, always set the availability to true.

Change the read method of House in this way:

    public static House read(Scanner sc) {

        House h = null;
        String line = sc.next();

        if(line.equals("FOR RENT")) {
            h = RentalHouse.read(sc);
        }
        else if(line.equals("RENTED"){
            h = RentalHouse.read(sc);
            h.available = false;
        }
        else if(line.equals("FOR SALE"){
            h = OwnerOccupiedHouse.read(sc);
        }
        else { 
            //if(line.equals("SOLD"))
            h = OwnerOccupiedHouse.read(sc);
            h.available = false;
        }

        return h;

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM