Answer to Question #324730 in Java | JSP | JSF for GEO

Question #324730

Create a class named Apartment that holds an apartment number, number of bedrooms, number of baths, and rent amount. Create a constructor that accepts values for each data field. Also create a get method for each field. Write an application that creates at least five Apartment objects. Then prompt a user to enter a minimum number of bedrooms required, a minimum number of baths required, and a maximum rent that the user is willing to pay. Display data for all the Apartment objects that meet the user’s criteria or an appropriate message if no such apartments are available. Save the files as Apartment.java and TestApartments.java

1
Expert's answer
2022-04-06T15:39:58-0400
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class Program {
    public static void main(String[] args) {
        Random r=new Random();

        List<Apartment>list=new ArrayList<>();
        for(int i=0; i<5; i++){
            int r1FlatNumb=r.nextInt((1000)+1);
            list.add(new Apartment(r1FlatNumb,r.nextInt(10)+1,
                    r.nextInt(10)+1,r.nextInt(1000,10000)));
        }

        Scanner scan=new Scanner(System.in);
        System.out.print("Enter a minimum number of bedrooms you required: ");
        int userBedrooms=scan.nextInt();
        System.out.print("Enter a minimum number of baths you required: ");
        int userBaths=scan.nextInt();
        System.out.print("And enter the maximum rent you are willing to pay: ");
        int userCost=scan.nextInt();

        for(Apartment ssss : list) {
            if (userBedrooms <=ssss.getBedroomNumb()) {
                if (userBaths <=ssss.getBathNumb()) {
                    if (userCost >=ssss.getCost()) {
                        System.out.println(ssss.toString());
                    }
                }
            }
        }

    }
}
class Apartment{
    int flatNumb, bedroomNumb, bathNumb, cost;

    Apartment(int flatNumb, int bedroomNumb, int bathNumb, int cost){
        this.bathNumb=bathNumb;
        this.bedroomNumb=bedroomNumb;
        this.cost=cost;
        this.flatNumb=flatNumb;
    }
    int getFlatNumb(){
        return this.flatNumb;
    }
    int getBedroomNumb(){
        return this.bedroomNumb;
    }
    int getBathNumb(){
        return this.bathNumb;
    }
    double getCost(){
        return this.cost;
    }

    @Override
    public String toString() {
        return "Apartment you can rent: " +
                "flatNumb=" + flatNumb +
                ", bedroomNumb=" + bedroomNumb +
                ", bathNumb=" + bathNumb +
                ", cost=" + cost ;
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS