Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion homework/src/HW1/submissions/partA/README.md

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package HW1.submissions.partA.erictran.airbnb;

public interface BookingService {
int calculateTotalPrice(Hotel hotel, int numberOfNights);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package HW1.submissions.partA.erictran.airbnb;

public class BookingServiceImpl implements BookingService {
@Override
public int calculateTotalPrice(Hotel hotel, int numberOfNights) {
int totalPrice = hotel.calculateTotalPrice(numberOfNights);
if (hotel instanceof DiscountedHotel) {
totalPrice -= 50;
}
return totalPrice;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package HW1.submissions.partA.erictran.airbnb;

public class DiscountedHotel extends Hotel {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package HW1.submissions.partA.erictran.airbnb;

public interface ExtraFeesService {
int calculateExtraGuestsFees(Hotel hotel, int numberOfGuests);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package HW1.submissions.partA.erictran.airbnb;

public class ExtraFeesServiceImpl implements ExtraFeesService{
@Override
public int calculateExtraGuestsFees(Hotel hotel, int numberOfGuests) {
return hotel.calculateExtraGuestsFees(numberOfGuests);
}
}
14 changes: 14 additions & 0 deletions homework/src/HW1/submissions/partA/erictran/airbnb/Hotel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package HW1.submissions.partA.erictran.airbnb;

public class Hotel {
int calculateTotalPrice(int numberOfNights) {
return numberOfNights * 100;
}

int calculateExtraGuestsFees(int numberOfGuests) {
if (numberOfGuests > 6) {
return (numberOfGuests - 6) * 30;
}
return 0;
}
}
20 changes: 20 additions & 0 deletions homework/src/HW1/submissions/partA/erictran/airbnb/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package HW1.submissions.partA.erictran.airbnb;

public class Main {
public static void main(String[] args) {
Hotel regularHotel = new Hotel();
DiscountedHotel discountedHotel = new DiscountedHotel();

BookingService bookingService = new BookingServiceImpl();
ExtraFeesService extraFeesService = new ExtraFeesServiceImpl();
int regularHotelTotalPrice = bookingService.calculateTotalPrice(regularHotel, 3);
int discountedHotelTotalPrice = bookingService.calculateTotalPrice(discountedHotel, 3);
int regularHotelExtraGuestsFees = extraFeesService.calculateExtraGuestsFees(regularHotel, 7);
int discountedHotelExtraGuestsFees = extraFeesService.calculateExtraGuestsFees(discountedHotel, 8);

System.out.println("Regular Hotel Total Price: $" + regularHotelTotalPrice);
System.out.println("Discounted Hotel Total Price: $" + discountedHotelTotalPrice);
System.out.println("Regular Hotel Extra Guests Fees: $" + regularHotelExtraGuestsFees);
System.out.println("Discounted Hotel Extra Guests Fees: $" + discountedHotelExtraGuestsFees);
}
}
46 changes: 46 additions & 0 deletions homework/src/HW1/submissions/partA/erictran/airbnb/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# 'Airbnb' Violation of SOLID

This code violates the "L" in SOLID, which stands for Liskov Substitution Principle
<br />
<br />
It officially states that "Objects of a superclass shall be replaceable with objects of its subclasses without breaking the application."
In other words, if you have a reference to a base type object, you should be able to use it to refer to a subtype object without any issues.
In this case, the 'DiscountedHotel' class changes the behavior of the 'calculateTotalPrice()' method.
Consider how the 'calculateTotalPrice' method in the 'Hotel' class returns the number of nights multiplied by the nightly rate while in the 'DiscountedHotel' class
it returns the total price minus a discount.
<br />
<br />
Hence, we cannot substitute a 'DiscountedHotel' object for a 'Hotel' object without breaking the code.
For example, if we passed a 'DiscountedHotel' object to the bookService.calculateTotalPrice() method, the total price may be calculated incorrectly.
<br />
<br />
A better design approach for this is to move the discount logic to the 'BookingServiceImpl' class.
<br />
<br />
Lets start off by removing the 'calculateTotalPrice' method from the 'DiscountedHotel' class:
```
public class DiscountedHotel extends Hotel {

}
```
Now, lets refactor the 'calculateTotalPrice' in the 'BookingServiceImpl' class so that it checks if the current instance is a 'DiscountedHotel' object before applying the discount.
```
public class BookingServiceImpl implements BookingService {
@Override
public int calculateTotalPrice(Hotel hotel, int numberOfNights) {
int totalPrice = hotel.calculateTotalPrice(numberOfNights);
if (hotel instanceof DiscountedHotel) {
totalPrice -= 50;
}
return totalPrice;
}
}
```
After making the following changes, the code now adheres to the Liskov Substitution Principle of SOLID.
<br />
<br />
We have a Main class to test the new code. It should produce the following output:
```
Regular Hotel Total Price: $300
Discounted Hotel Total Price: $250
```
13 changes: 13 additions & 0 deletions homework/src/HW1/submissions/partA/erictran/doordash/Customer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package HW1.submissions.partA.erictran.doordash;

public class Customer {
private String name;

public Customer(String name) {
this.name = name;
}

public String getName() {
return name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package HW1.submissions.partA.erictran.doordash;

public interface DeliveryService {
void deliverFood(Restaurant restaurant, Customer customer);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package HW1.submissions.partA.erictran.doordash;

public class FoodDeliveryService implements DeliveryService {
@Override
public void deliverFood(Restaurant restaurant, Customer customer) {
System.out.println("Food delivered from " + restaurant.getName() + " to " + customer.getName());
}
}
17 changes: 17 additions & 0 deletions homework/src/HW1/submissions/partA/erictran/doordash/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package HW1.submissions.partA.erictran.doordash;

public class Main {
public static void main(String[] args) {
Restaurant restaurant = new Restaurant("Chick-fil-a");
Customer customer = new Customer("Bobby");

DeliveryService deliveryService = new FoodDeliveryService();
PackageTrackingService trackingService = new PackageTrackingService();

deliveryService.deliverFood(restaurant, customer);

trackingService.trackPackage("123456789");

trackingService.calculateEstimatedDeliveryTime(100, 55);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package HW1.submissions.partA.erictran.doordash;

public class PackageTrackingService implements TrackingService {
@Override
public void trackPackage(String trackingNumber) {
System.out.println("Package with tracking number " + trackingNumber + " is being tracked.");
}

@Override
public void calculateEstimatedDeliveryTime(double distanceInMiles, double deliverySpeedMPH) {
double estimatedDeliveryTime = distanceInMiles / deliverySpeedMPH;
System.out.printf("Package will be delivered in approximately %.2f hours.\n", estimatedDeliveryTime);
}
}
62 changes: 62 additions & 0 deletions homework/src/HW1/submissions/partA/erictran/doordash/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# 'DoorDash' Violation of SOLID

This code violates the "I" in SOLID, which stands for Interface Segregation Principle.
<br />
<br />
It officially states that "Clients should not be forced to depend on methods they do not use."
Examining the "FoodDeliveryAndTrackingService" class, it has two responsibilities, one
being delivering food and the other being tracking packages.
Though, clients that only need to deliver food do not need to depend on the trackPackage() method.
<br />
<br />
A better design approach is to split the "FoodDeliveryAndTrackingService" class into two.
For example, we can split the code into "FoodDeliveryService" and "PackageTrackingService."
<br />
<br />
We need to alter 'DeliveryService' interface so that it focuses on Food Delivery only:
```
package doordash;

public interface DeliveryService {
void deliverFood(Restaurant restaurant, Customer customer);
}
```
We create a new 'FoodDeliveryService' class that focuses on Food Delivery only:
```
package doordash;

public class FoodDeliveryService implements DeliveryService {
@Override
public void deliverFood(Restaurant restaurant, Customer customer) {
System.out.println("Food delivered from " + restaurant.getName() + " to " + customer.getName());
}
}
```
We create a new 'TrackingService' interface so that it focuses on Package Tracking only:
```
package doordash;

public interface TrackingService {
void trackPackage(String trackingNumber);
}
```
We create a new 'PackageTrackingService' class that focuses on Package Tracking only:
```
package doordash;

public class PackageTrackingService implements TrackingService {
@Override
public void trackPackage(String trackingNumber) {
System.out.println("Package with tracking number " + trackingNumber + " is being tracked.");
}
}
```
After making the following changes, the code now adheres to the Interface Segregation Principle.
of SOLID.
<br />
<br />
We have a Main class to test the new code. It should produce the following output:
```
Food delivered from Chick-fil-a to Bobby
Package with tracking number 123456789 is being tracked.
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package HW1.submissions.partA.erictran.doordash;

public class Restaurant {
private String name;

public Restaurant(String name) {
this.name = name;
}

public String getName() {
return name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package HW1.submissions.partA.erictran.doordash;

public interface TrackingService {
void trackPackage(String trackingNumber);

void calculateEstimatedDeliveryTime(double distanceInMiles, double deliverySpeedMPH);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package HW1.submissions.partA.erictran.facebook;

public class ImagePost extends Post {
private String imageUrl;

public ImagePost(String text, String imageUrl) {
super(text);
this.imageUrl = imageUrl;
}

public String getImageUrl() {
return imageUrl;
}

public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}

@Override
public void display() {
System.out.print("Image post: ");
System.out.println(imageUrl);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package HW1.submissions.partA.erictran.facebook;

public class LiveVideoPost extends Post {
private String liveVideoUrl;

public LiveVideoPost(String text, String liveVideoUrl) {
super(text);
this.liveVideoUrl = liveVideoUrl;
}

public String getLiveVideoUrl() {
return liveVideoUrl;
}

public void setLiveVideoUrl(String videoUrl) {
this.liveVideoUrl = videoUrl;
}

@Override
public void display() {
System.out.print("Video post: ");
System.out.println(liveVideoUrl);
}
}
32 changes: 32 additions & 0 deletions homework/src/HW1/submissions/partA/erictran/facebook/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package HW1.submissions.partA.erictran.facebook;

public class Main {
public static void main(String[] args) {
TextPost textPost = new TextPost("This is a text post.");
ImagePost imagePost = new ImagePost("Check out this image!", "https://example.com/image.jpg");
VideoPost videoPost = new VideoPost("Watch this video!", "https://example.com/video.mp4");
LiveVideoPost liveVideoPost = new LiveVideoPost("Join my stream!", "rtmp://facebook.com/live");

System.out.println("Text Post Test:");
textPost.display();
System.out.println("\nImage Post Test:");
imagePost.display();
System.out.println("\nVideo Post Test:");
videoPost.display();
System.out.println("\nLive Video Post Test:");
liveVideoPost.display();

textPost.setText("Updated text post.");
System.out.println("\nUpdated Text Post:");
textPost.display();
imagePost.setImageUrl("https://example.com/image2.jpg");
System.out.println("\nUpdated Image Post:");
imagePost.display();
videoPost.setVideoUrl("https://example.com/video2.mp4");
System.out.println("\nUpdated Video Post:");
videoPost.display();
liveVideoPost.setLiveVideoUrl("rtmp://facebook.com/live2");
System.out.println("\nUpdated Live Video Post:");
liveVideoPost.display();
}
}
21 changes: 21 additions & 0 deletions homework/src/HW1/submissions/partA/erictran/facebook/Post.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package HW1.submissions.partA.erictran.facebook;

abstract class Post {
protected String text;

public Post(String text) {
this.text = text;
}

public void setText(String text) {
this.text = text;
}

public String getText() {
return text;
}

public void display() {
System.out.println(text);
}
}
Loading