added lab3 ex1

This commit is contained in:
Louis Heredero 2024-10-07 11:01:02 +02:00
parent 1ab845574a
commit 5fbcd72ea6
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
16 changed files with 277 additions and 0 deletions

View File

@ -0,0 +1,5 @@
package lab3_abstract_factory.ex1;
public abstract class AbstractDoor {
public abstract int getPrice();
}

View File

@ -0,0 +1,5 @@
package lab3_abstract_factory.ex1;
public abstract class AbstractWall {
public abstract int getPrice();
}

View File

@ -0,0 +1,5 @@
package lab3_abstract_factory.ex1;
public abstract class AbstractWindows {
public abstract int getPrice();
}

View File

@ -0,0 +1,26 @@
package lab3_abstract_factory.ex1;
import lab3_abstract_factory.ex1.house_factories.DutchHouseFactory;
import lab3_abstract_factory.ex1.house_factories.GermanHouseFactory;
import lab3_abstract_factory.ex1.house_factories.SwissWoodChaletFactory;
public class ArchitectOffice {
public static void main(String[] args) {
House house1 = new House("Modern German house",
new GermanHouseFactory());
house1.buildHouse();
house1.calculatePrice();
House house2 = new House("Typical dutch house",
new DutchHouseFactory());
house2.buildHouse();
house2.calculatePrice();
House house3 = new House("Swiss wood chalet",
new SwissWoodChaletFactory());
house3.buildHouse();
house3.calculatePrice();
System.out.println(house1);
System.out.println(house2);
System.out.println(house3);
}
}

View File

@ -0,0 +1,46 @@
package lab3_abstract_factory.ex1;
public class House {
private String name;
private HouseFactory houseFactory;
private AbstractWall wall;
private AbstractWindows windows;
private AbstractDoor door;
public House(String name, HouseFactory houseFactory) {
this.name = name;
this.houseFactory = houseFactory;
}
public void buildHouse() {
wall = houseFactory.buildWall();
windows = houseFactory.buildWindows();
door = houseFactory.buildDoor();
System.out.println(
"Building a house with "
+ wall
+ ", "
+ windows
+ " and a "
+ door
);
}
public void calculatePrice() {
int total = 0;
total += wall.getPrice();
total += windows.getPrice();
total += door.getPrice();
System.out.println("Total price: " + total);
}
@Override
public String toString() {
return "House '" + name + "' (\n"
+ " Walls: " + wall + "\n"
+ " Windows: " + windows + "\n"
+ " Door: " + door + "\n"
+ ")";
}
}

View File

@ -0,0 +1,7 @@
package lab3_abstract_factory.ex1;
public abstract class HouseFactory {
public abstract AbstractWall buildWall();
public abstract AbstractWindows buildWindows();
public abstract AbstractDoor buildDoor();
}

View File

@ -0,0 +1,15 @@
package lab3_abstract_factory.ex1.doors;
import lab3_abstract_factory.ex1.AbstractDoor;
public class LargeDoor extends AbstractDoor {
@Override
public int getPrice() {
return 50;
}
@Override
public String toString() {
return "large door";
}
}

View File

@ -0,0 +1,15 @@
package lab3_abstract_factory.ex1.doors;
import lab3_abstract_factory.ex1.AbstractDoor;
public class ThinDoor extends AbstractDoor {
@Override
public int getPrice() {
return 25;
}
@Override
public String toString() {
return "thin door";
}
}

View File

@ -0,0 +1,26 @@
package lab3_abstract_factory.ex1.house_factories;
import lab3_abstract_factory.ex1.AbstractDoor;
import lab3_abstract_factory.ex1.AbstractWall;
import lab3_abstract_factory.ex1.AbstractWindows;
import lab3_abstract_factory.ex1.HouseFactory;
import lab3_abstract_factory.ex1.doors.LargeDoor;
import lab3_abstract_factory.ex1.walls.BricksWall;
import lab3_abstract_factory.ex1.windows.FramedWindows;
public class DutchHouseFactory extends HouseFactory {
@Override
public AbstractWall buildWall() {
return new BricksWall();
}
@Override
public AbstractWindows buildWindows() {
return new FramedWindows();
}
@Override
public AbstractDoor buildDoor() {
return new LargeDoor();
}
}

View File

@ -0,0 +1,26 @@
package lab3_abstract_factory.ex1.house_factories;
import lab3_abstract_factory.ex1.AbstractDoor;
import lab3_abstract_factory.ex1.AbstractWall;
import lab3_abstract_factory.ex1.AbstractWindows;
import lab3_abstract_factory.ex1.HouseFactory;
import lab3_abstract_factory.ex1.doors.LargeDoor;
import lab3_abstract_factory.ex1.walls.GlassWall;
import lab3_abstract_factory.ex1.windows.FullHeightWindows;
public class GermanHouseFactory extends HouseFactory {
@Override
public AbstractWall buildWall() {
return new GlassWall();
}
@Override
public AbstractWindows buildWindows() {
return new FullHeightWindows();
}
@Override
public AbstractDoor buildDoor() {
return new LargeDoor();
}
}

View File

@ -0,0 +1,26 @@
package lab3_abstract_factory.ex1.house_factories;
import lab3_abstract_factory.ex1.AbstractDoor;
import lab3_abstract_factory.ex1.AbstractWall;
import lab3_abstract_factory.ex1.AbstractWindows;
import lab3_abstract_factory.ex1.HouseFactory;
import lab3_abstract_factory.ex1.doors.ThinDoor;
import lab3_abstract_factory.ex1.walls.WoodWall;
import lab3_abstract_factory.ex1.windows.FramedWindows;
public class SwissWoodChaletFactory extends HouseFactory {
@Override
public AbstractWall buildWall() {
return new WoodWall();
}
@Override
public AbstractWindows buildWindows() {
return new FramedWindows();
}
@Override
public AbstractDoor buildDoor() {
return new ThinDoor();
}
}

View File

@ -0,0 +1,15 @@
package lab3_abstract_factory.ex1.walls;
import lab3_abstract_factory.ex1.AbstractWall;
public class BricksWall extends AbstractWall {
@Override
public int getPrice() {
return 10;
}
@Override
public String toString() {
return "brick walls";
}
}

View File

@ -0,0 +1,15 @@
package lab3_abstract_factory.ex1.walls;
import lab3_abstract_factory.ex1.AbstractWall;
public class GlassWall extends AbstractWall {
@Override
public int getPrice() {
return 100;
}
@Override
public String toString() {
return "glass walls";
}
}

View File

@ -0,0 +1,15 @@
package lab3_abstract_factory.ex1.walls;
import lab3_abstract_factory.ex1.AbstractWall;
public class WoodWall extends AbstractWall {
@Override
public int getPrice() {
return 1;
}
@Override
public String toString() {
return "wood walls";
}
}

View File

@ -0,0 +1,15 @@
package lab3_abstract_factory.ex1.windows;
import lab3_abstract_factory.ex1.AbstractWindows;
public class FramedWindows extends AbstractWindows {
@Override
public int getPrice() {
return 100;
}
@Override
public String toString() {
return "framed windows";
}
}

View File

@ -0,0 +1,15 @@
package lab3_abstract_factory.ex1.windows;
import lab3_abstract_factory.ex1.AbstractWindows;
public class FullHeightWindows extends AbstractWindows {
@Override
public int getPrice() {
return 200;
}
@Override
public String toString() {
return "full-height windows";
}
}