From 7c614b0c5c603dd1f76f07bd9af67aebbe511f37 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Mon, 11 Nov 2024 09:41:57 +0100 Subject: [PATCH] added lab13 ex1 --- src/lab13_proxy/ex1/Image.java | 27 ++++++++++++++++++++ src/lab13_proxy/ex1/ImageProxy.java | 24 +++++++++++++++++ src/lab13_proxy/ex1/Main.java | 19 ++++++++++++++ src/lab13_proxy/ex1/RegistrationService.java | 15 +++++++++++ src/lab13_proxy/ex1/User.java | 13 ++++++++++ 5 files changed, 98 insertions(+) create mode 100644 src/lab13_proxy/ex1/Image.java create mode 100644 src/lab13_proxy/ex1/ImageProxy.java create mode 100644 src/lab13_proxy/ex1/Main.java create mode 100644 src/lab13_proxy/ex1/RegistrationService.java create mode 100644 src/lab13_proxy/ex1/User.java diff --git a/src/lab13_proxy/ex1/Image.java b/src/lab13_proxy/ex1/Image.java new file mode 100644 index 0000000..ea8ac75 --- /dev/null +++ b/src/lab13_proxy/ex1/Image.java @@ -0,0 +1,27 @@ +package lab13_proxy.ex1; + +public class Image { + protected final String path; + protected final String resolution; + protected boolean loaded = false; + + public Image(String path, String resolution) { + this.path = path; + this.resolution = resolution; + } + + public void load() { + if (!loaded) { + System.out.println("Image " + path + " is loaded in " + resolution + " resolution"); + loaded = true; + } + } + + public boolean isLoaded() { + return loaded; + } + + public void showImage(User user) { + System.out.println("Image " + path + " is shown in " + resolution + " resolution for user " + user.getName()); + } +} diff --git a/src/lab13_proxy/ex1/ImageProxy.java b/src/lab13_proxy/ex1/ImageProxy.java new file mode 100644 index 0000000..86ef4ce --- /dev/null +++ b/src/lab13_proxy/ex1/ImageProxy.java @@ -0,0 +1,24 @@ +package lab13_proxy.ex1; + +public class ImageProxy extends Image { + private final Image lowResImage; + private final Image highResImage; + + public ImageProxy(String path) { + super(path, "high"); + lowResImage = new Image(path, "low"); + highResImage = new Image(path, "high"); + } + + public void showImage(User user) { + System.out.println(user.getName() + " selects preview image " + path + " and wants to see its full resolution."); + if (RegistrationService.isRegistered(user)) { + highResImage.load(); + highResImage.showImage(user); + } else { + System.out.println("User " + user.getName() + " is not registered. Showing preview image in low resolution"); + lowResImage.load(); + lowResImage.showImage(user); + } + } +} diff --git a/src/lab13_proxy/ex1/Main.java b/src/lab13_proxy/ex1/Main.java new file mode 100644 index 0000000..a9c7d6f --- /dev/null +++ b/src/lab13_proxy/ex1/Main.java @@ -0,0 +1,19 @@ +package lab13_proxy.ex1; + +public class Main { + public static void main(String[] args) { + User jean = new User("Jean"); + User paul = new User("Paul"); + User pierre = new User("Pierre"); + + RegistrationService.register(paul); + + Image highResolutionImage1 = new ImageProxy("sample/veryHighResPhoto1.jpeg"); + Image highResolutionImage2 = new ImageProxy("sample/veryHighResPhoto2.jpeg"); + Image highResolutionImage3 = new ImageProxy("sample/veryHighResPhoto3.jpeg"); + + highResolutionImage1.showImage(jean); + highResolutionImage2.showImage(paul); + highResolutionImage3.showImage(pierre); + } +} diff --git a/src/lab13_proxy/ex1/RegistrationService.java b/src/lab13_proxy/ex1/RegistrationService.java new file mode 100644 index 0000000..253aef6 --- /dev/null +++ b/src/lab13_proxy/ex1/RegistrationService.java @@ -0,0 +1,15 @@ +package lab13_proxy.ex1; + +import java.util.ArrayList; + +public class RegistrationService { + private static final ArrayList users = new ArrayList<>(); + public static void register(User user) { + System.out.println(user.getName() + " is now registered"); + users.add(user); + } + + public static boolean isRegistered(User user) { + return users.contains(user); + } +} diff --git a/src/lab13_proxy/ex1/User.java b/src/lab13_proxy/ex1/User.java new file mode 100644 index 0000000..c17e117 --- /dev/null +++ b/src/lab13_proxy/ex1/User.java @@ -0,0 +1,13 @@ +package lab13_proxy.ex1; + +public class User { + private final String name; + + public User(String name) { + this.name = name; + } + + public String getName() { + return name; + } +}