Problem Description Implement the following classos according to the class diagr
ID: 3606741 • Letter: P
Question
Problem Description Implement the following classos according to the class diagrams. And you may need to implement set / get methods or other methods il necessary You are required to define function parameters for the Factory constructor (..) and ProductFactory constructor (...) in order to create objects of them properly. All classes need to implement display method After you implement all the classes and the methods specified in the class diagram, write a main method to create objects of the classes you defined and print out their contents. Interface Interlace FactoryConstants NUM PRODUCTS 100 DISCOUNT-02 Displayable +display): void Abstract Abstract Factory tproducts: Products -factoryName:String NUM PRODUCTS Product ( Factory setProductlpos: int, product: Product) double Concrete Concrete Concreto Building buildingName: String -area : double +Building ( ProductFactory buildings: Building0 +ProductFactory(...) +find +findAllProductsMore Thanprice: double) model: String price: doublo +Computer ( buildingName:String area : double model: String price : double Productp +getPrice) : double +getArea0 : double Page 1 of 1Explanation / Answer
Hello. I have a solution for your question.
//Displayable.java
public interface Displayable {
public void display();
}
//FactoryConstants.java
public interface FactoryConstants {
static final int NUM_PRODUCTS=100;
static final double DISCOUNT=0.2;
}
//Product.java
public abstract class Product implements Displayable {
private String productName;
public Product(String productName) {
this.productName = productName;
}
public abstract double getDiscountedPrice();
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
}
//Computer.java
public class Computer extends Product{
private String model;
private double price;
public Computer(String productName, String model, double price) {
super(productName);
this.model = model;
this.price = price;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public double getDiscountedPrice() {
/**
* returning discounted price
*/
return price-(price*FactoryConstants.DISCOUNT);
}
@Override
public void display() {
System.out.println("Product name: "+getProductName());
System.out.println("Mode: "+model);
System.out.println("Price: "+price);
System.out.println("Discounted price: "+getDiscountedPrice());
}
}
//Building.java
public class Building implements Displayable {
private String buildingName;
private double area;
public Building(String buildingName, double area) {
this.buildingName = buildingName;
this.area = area;
}
public String getBuildingName() {
return buildingName;
}
public void setBuildingName(String buildingName) {
this.buildingName = buildingName;
}
public double getArea() {
return area;
}
public void setArea(double area) {
this.area = area;
}
@Override
public void display() {
System.out.println("Building name: "+buildingName);
System.out.println("Area: "+area);
}
}
//Factory.java
public abstract class Factory implements Displayable {
Product[] products;
private String factoryName;
public Factory(Product[] products, String factoryName) {
this.products = products;
this.factoryName = factoryName;
}
public Factory(String factoryName) {
this.factoryName = factoryName;
/**
* creating a new product array of size FactoryConstants.NUM_PRODUCTS
*/
products=new Product[FactoryConstants.NUM_PRODUCTS];
}
public Product[] getProducts() {
return products;
}
public void setProducts(Product[] products) {
this.products = products;
}
public String getFactoryName() {
return factoryName;
}
public void setFactoryName(String factoryName) {
this.factoryName = factoryName;
}
public void setProduct(int pos,Product product){
/**
* adding a new product in the position pos
*/
if(pos<products.length){
products[pos]=product;
System.out.println("Product added: "+product.getProductName());
}else{
System.out.println("Cannot be added in this position");
}
}
}
//ProductFactory.java
public class ProductFactory extends Factory {
Building[] buildings;
public ProductFactory(String factoryName,Building[] buildings) {
super(factoryName);
this.buildings=buildings;
}
/**
*
* @return the largest building
*/
public Building findLargestBuilding(){
Building big=null;
if(buildings.length!=0){
big=buildings[0];
for(int i=0;i<buildings.length;i++){
if(buildings[i]!=null && big !=null){ /*checking if they contain null values*/
if(buildings[i].getArea()>big.getArea()){
big=buildings[i];
}
}
}
}
return big;
}
/**
*
* @param price
* @return
* returns an array of products with price more than 'price'
*/
public Product[] findAllProductsMoreThan(double price){
Product[] prods=getProducts();
Product[] requiredproducts=new Product[prods.length];
int index=0;
for(int i=0;i<prods.length;i++){
if(prods[i]!=null){
if(prods[i].getDiscountedPrice()>price){
requiredproducts[index]=prods[i];
index++;
}
}
}
return requiredproducts;
}
/**
* displaying the complete details about the factory
*/
@Override
public void display() {
System.out.println("........................");
System.out.println("Welcome to "+getFactoryName());
System.out.println("...Product Details...");
Product[] prods=getProducts();
for (Product product : prods) {
if(product!=null){
product.display();
}
}
System.out.println("...Buildings Details...");
for(Building building: buildings){
if(building!=null){
building.display();
}
}
System.out.println("........................");
}
}
//Driver.java
public class Driver {
public static void main(String[] args) {
/**
* creating few buildings
*/
Building building1=new Building("ABC", 1899.5);
Building building2=new Building("B two", 2500.5);
Building building3=new Building("MainStreet building", 3000.0);
Building building4=new Building("B four", 1200.6);
/**
* creating an array of defined buildings
*/
Building[] buildings={building1,building2,building3,building4};
/**
* creating a ProductFactory
*/
ProductFactory factory=new ProductFactory("My Factory",buildings);
/**
* creating few products
*/
Product p1=new Computer("Apple", "Mac Air", 249.0);
Product p2=new Computer("Lenovo", "G 500", 149);
Product p3=new Computer("Lenovo", "G 450", 140.5);
Product p4=new Computer("Samsung", "X 123", 200.0);
Product p5=new Computer("Lenovo", "G 620", 175.3);
Product p6=new Computer("Samsung", "X Play", 250.5);
Product p7=new Computer("Asus", "Max", 149);
Product p8=new Computer("Asus", "Rogue", 300);
Product p9=new Computer("Dell", "Game", 290);
/**
* adding products into factory
*/
factory.setProduct(0, p1);
factory.setProduct(1, p2);
factory.setProduct(2, p3);
factory.setProduct(3, p4);
factory.setProduct(4, p5);
factory.setProduct(5, p6);
factory.setProduct(6, p7);
factory.setProduct(7, p8);
factory.setProduct(8, p9);
/**
* printing the details about factory
*/
factory.display();
/**
* finding the largest building
*/
Building largest=factory.findLargestBuilding();
System.out.println("...Largest building details...");
largest.display();
/*
* finding the products with price greater than 200.0
*/
System.out.println("...Products with price greater than 200.0");
Product[] prods=factory.findAllProductsMoreThan(200.0);
for(Product p:prods){
if(p!=null){
p.display();
}
}
}
}
/*output*/
Product added: Apple
Product added: Lenovo
Product added: Lenovo
Product added: Samsung
Product added: Lenovo
Product added: Samsung
Product added: Asus
Product added: Asus
Product added: Dell
........................
Welcome to My Factory
...Product Details...
Product name: Apple
Mode: Mac Air
Price: 249.0
Discounted price: 199.2
Product name: Lenovo
Mode: G 500
Price: 149.0
Discounted price: 119.2
Product name: Lenovo
Mode: G 450
Price: 140.5
Discounted price: 112.4
Product name: Samsung
Mode: X 123
Price: 200.0
Discounted price: 160.0
Product name: Lenovo
Mode: G 620
Price: 175.3
Discounted price: 140.24
Product name: Samsung
Mode: X Play
Price: 250.5
Discounted price: 200.4
Product name: Asus
Mode: Max
Price: 149.0
Discounted price: 119.2
Product name: Asus
Mode: Rogue
Price: 300.0
Discounted price: 240.0
Product name: Dell
Mode: Game
Price: 290.0
Discounted price: 232.0
...Buildings Details...
Building name: ABC
Area: 1899.5
Building name: B two
Area: 2500.5
Building name: MainStreet building
Area: 3000.0
Building name: B four
Area: 1200.6
........................
...Largest building details...
Building name: MainStreet building
Area: 3000.0
...Products with price greater than 200.0
Product name: Samsung
Mode: X Play
Price: 250.5
Discounted price: 200.4
Product name: Asus
Mode: Rogue
Price: 300.0
Discounted price: 240.0
Product name: Dell
Mode: Game
Price: 290.0
Discounted price: 232.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.