diff --git a/homework/src/HW1/submissions/partA/acyutaraman/.gitkeep b/homework/src/HW1/submissions/partA/acyutaraman/.gitkeep new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/homework/src/HW1/submissions/partA/acyutaraman/.gitkeep @@ -0,0 +1 @@ + diff --git a/homework/src/HW1/submissions/partA/acyutaraman/airbnb/BookingService.java b/homework/src/HW1/submissions/partA/acyutaraman/airbnb/BookingService.java new file mode 100644 index 0000000..6c31454 --- /dev/null +++ b/homework/src/HW1/submissions/partA/acyutaraman/airbnb/BookingService.java @@ -0,0 +1,5 @@ +package airbnb; + +public interface BookingService { + int calculateTotalPrice(Hotel hotel, int numberOfNights); +} \ No newline at end of file diff --git a/homework/src/HW1/submissions/partA/acyutaraman/airbnb/BookingServiceImpl.java b/homework/src/HW1/submissions/partA/acyutaraman/airbnb/BookingServiceImpl.java new file mode 100644 index 0000000..b0da2c5 --- /dev/null +++ b/homework/src/HW1/submissions/partA/acyutaraman/airbnb/BookingServiceImpl.java @@ -0,0 +1,9 @@ +package airbnb; + +public class BookingServiceImpl implements BookingService { + @Override + public int calculateTotalPrice(Hotel hotel, int numberOfNights) { + int totalPrice = hotel.calculateTotalPrice(numberOfNights); + return totalPrice; + } +} \ No newline at end of file diff --git a/homework/src/HW1/submissions/partA/acyutaraman/airbnb/DiscountedHotel.java b/homework/src/HW1/submissions/partA/acyutaraman/airbnb/DiscountedHotel.java new file mode 100644 index 0000000..328a3c4 --- /dev/null +++ b/homework/src/HW1/submissions/partA/acyutaraman/airbnb/DiscountedHotel.java @@ -0,0 +1,9 @@ +package airbnb; + +public class DiscountedHotel implements Hotel { + + @Override + public int calculateTotalPrice(int numberOfNights) { + return 100*numberOfNights - 50; + } +} \ No newline at end of file diff --git a/homework/src/HW1/submissions/partA/acyutaraman/airbnb/FiveStarHotel.java b/homework/src/HW1/submissions/partA/acyutaraman/airbnb/FiveStarHotel.java new file mode 100644 index 0000000..b504d12 --- /dev/null +++ b/homework/src/HW1/submissions/partA/acyutaraman/airbnb/FiveStarHotel.java @@ -0,0 +1,10 @@ +package airbnb; + +public class FiveStarHotel implements Hotel { + + @Override + public int calculateTotalPrice(int numberOfNights) { + return 200*numberOfNights; + } + +} diff --git a/homework/src/HW1/submissions/partA/acyutaraman/airbnb/Hotel.java b/homework/src/HW1/submissions/partA/acyutaraman/airbnb/Hotel.java new file mode 100644 index 0000000..ee0c4b0 --- /dev/null +++ b/homework/src/HW1/submissions/partA/acyutaraman/airbnb/Hotel.java @@ -0,0 +1,5 @@ +package airbnb; + +public interface Hotel { + int calculateTotalPrice(int numberOfNights); +} \ No newline at end of file diff --git a/homework/src/HW1/submissions/partA/acyutaraman/airbnb/Main.java b/homework/src/HW1/submissions/partA/acyutaraman/airbnb/Main.java new file mode 100644 index 0000000..20a89d4 --- /dev/null +++ b/homework/src/HW1/submissions/partA/acyutaraman/airbnb/Main.java @@ -0,0 +1,21 @@ +package airbnb; + +public class Main { + public static void main(String[] args) { + Hotel regularHotel = new RegularHotel(); + Hotel discountedHotel = new DiscountedHotel(); + Hotel fiveStarHotel = new FiveStarHotel(); + + BookingService bookingService = new BookingServiceImpl(); + int regularHotelTotalPrice = bookingService.calculateTotalPrice(regularHotel, 3); + int discountedHotelTotalPrice = bookingService.calculateTotalPrice(discountedHotel, 3); + int fiveStarHotelTotalPrice = bookingService.calculateTotalPrice(fiveStarHotel, 3); + + System.out.println("Regular Hotel Total Price: $" + regularHotelTotalPrice); + System.out.println("Discounted Hotel Total Price: $" + discountedHotelTotalPrice); + System.out.println("Five Star Hotel Total Price: $" + fiveStarHotelTotalPrice); + + + + } +} \ No newline at end of file diff --git a/homework/src/HW1/submissions/partA/acyutaraman/airbnb/README.md b/homework/src/HW1/submissions/partA/acyutaraman/airbnb/README.md new file mode 100644 index 0000000..6dd1569 --- /dev/null +++ b/homework/src/HW1/submissions/partA/acyutaraman/airbnb/README.md @@ -0,0 +1,7 @@ +1) Airbnb violated the Open/Closed principle in SOLID. The DiscountedHotel class was a subclass of the Hotel class, and it overrides its calculateTotalPrice() method. Despite doing this, +the DiscountedHotel class can still access the calculateTotalPrice method of its superclass. Therefore, it violates the Open/Closed principle in SOLID. + +2) I fixed this violation by removing the Hotel class and instead creating a Hotel interface. I, then, created a Regular Hotel class to replace the previous Hotel class. I then made +both DiscountedHotel and the new RegularHotel classes implement the new Hotel interface. The Hotel interface has the abstract method calculateTotalPrice, which is implemented differently +in both DiscountedHotel and RegularHotel. I also added a new FiveStarHotel class that implements Hotel and charges at a higher hourly rate than RegularHotel. Its higher rate is displayed in +its calculateTotalPrice() method. diff --git a/homework/src/HW1/submissions/partA/acyutaraman/airbnb/RegularHotel.java b/homework/src/HW1/submissions/partA/acyutaraman/airbnb/RegularHotel.java new file mode 100644 index 0000000..06564d6 --- /dev/null +++ b/homework/src/HW1/submissions/partA/acyutaraman/airbnb/RegularHotel.java @@ -0,0 +1,11 @@ +package airbnb; + +public class RegularHotel implements Hotel{ + + @Override + public int calculateTotalPrice(int numberOfNights) { + + return 100*numberOfNights; + } + +} diff --git a/homework/src/HW1/submissions/partA/acyutaraman/doordash/Customer.java b/homework/src/HW1/submissions/partA/acyutaraman/doordash/Customer.java new file mode 100644 index 0000000..18f6958 --- /dev/null +++ b/homework/src/HW1/submissions/partA/acyutaraman/doordash/Customer.java @@ -0,0 +1,14 @@ +package doordash; + +public class Customer { + + private String name; + + public Customer(String name) { + this.name = name; + } + + public String getName() { + return name; + } +} diff --git a/homework/src/HW1/submissions/partA/acyutaraman/doordash/DeliveryService.java b/homework/src/HW1/submissions/partA/acyutaraman/doordash/DeliveryService.java new file mode 100644 index 0000000..2d771cc --- /dev/null +++ b/homework/src/HW1/submissions/partA/acyutaraman/doordash/DeliveryService.java @@ -0,0 +1,8 @@ +package doordash; + +public interface DeliveryService { + + void deliverFood(Restaurant restaurant, Customer customer); + + void cancelDelivery(Restaurant restaurant, Customer customer); +} diff --git a/homework/src/HW1/submissions/partA/acyutaraman/doordash/FoodDeliveryAndTrackingService.java b/homework/src/HW1/submissions/partA/acyutaraman/doordash/FoodDeliveryAndTrackingService.java new file mode 100644 index 0000000..022805e --- /dev/null +++ b/homework/src/HW1/submissions/partA/acyutaraman/doordash/FoodDeliveryAndTrackingService.java @@ -0,0 +1,20 @@ +package doordash; + +public class FoodDeliveryAndTrackingService implements DeliveryService, TrackingService { + + @Override + public void deliverFood(Restaurant restaurant, Customer customer) { + System.out.println("Food delivered from " + restaurant.getName() + " to " + customer.getName()); + } + + @Override + public void trackPackage(String trackingNumber) { + System.out.println("Package with tracking number " + trackingNumber + " is being tracked."); + } + + @Override + public void cancelDelivery(Restaurant restaurant, Customer customer) { + System.out.println("Food delivery canceled: from " + restaurant.getName() + " to " + customer.getName()); + + } +} diff --git a/homework/src/HW1/submissions/partA/acyutaraman/doordash/README.md b/homework/src/HW1/submissions/partA/acyutaraman/doordash/README.md new file mode 100644 index 0000000..3f72ff0 --- /dev/null +++ b/homework/src/HW1/submissions/partA/acyutaraman/doordash/README.md @@ -0,0 +1,8 @@ + +1) Doordash violates the Interface Segregation Principle of SOLID because it keeps both the deliverFood() and trackPackage() methods +in the DeliveryService interface instead of separating the methods across two different interfaces(a deliveryService and a trackingService interface). + +2) To solve this, I created a new TrackingService interface and moved the trackPackage() method from the original DeliveryService interface to it. +I then made the FoodDeliveryAndTrackingService class implement both the TrackingService and the DeliveryService interfaces, and had it implement +both the deliverFood() and trackPackage() methods. I even added a cancelDelivery() method to DeliveryService and concretely implemented it in +FoodDeliveryAndTrackingService. diff --git a/homework/src/HW1/submissions/partA/acyutaraman/doordash/Restaurant.java b/homework/src/HW1/submissions/partA/acyutaraman/doordash/Restaurant.java new file mode 100644 index 0000000..c233573 --- /dev/null +++ b/homework/src/HW1/submissions/partA/acyutaraman/doordash/Restaurant.java @@ -0,0 +1,14 @@ +package doordash; + +public class Restaurant { + + private String name; + + public Restaurant(String name) { + this.name = name; + } + + public String getName() { + return name; + } +} diff --git a/homework/src/HW1/submissions/partA/acyutaraman/doordash/TrackingService.java b/homework/src/HW1/submissions/partA/acyutaraman/doordash/TrackingService.java new file mode 100644 index 0000000..4be8713 --- /dev/null +++ b/homework/src/HW1/submissions/partA/acyutaraman/doordash/TrackingService.java @@ -0,0 +1,6 @@ +package doordash; + +public interface TrackingService { + + void trackPackage(String trackingNumber); +} diff --git a/homework/src/HW1/submissions/partA/acyutaraman/facebook/Post.java b/homework/src/HW1/submissions/partA/acyutaraman/facebook/Post.java new file mode 100644 index 0000000..d467018 --- /dev/null +++ b/homework/src/HW1/submissions/partA/acyutaraman/facebook/Post.java @@ -0,0 +1,7 @@ +package facebook; + +public interface Post { + + void display(); + +} diff --git a/homework/src/HW1/submissions/partA/acyutaraman/facebook/Post1.java b/homework/src/HW1/submissions/partA/acyutaraman/facebook/Post1.java new file mode 100644 index 0000000..7043721 --- /dev/null +++ b/homework/src/HW1/submissions/partA/acyutaraman/facebook/Post1.java @@ -0,0 +1,28 @@ +package facebook; + +//This was the original post class, that was implemented for our "Facebook". + +class Post1 implements Post { + private String text; + + public Post1(String text) + { + this.text = text; + } + + public void setText(String text) + { + this.text = text; + } + + public String getText() + { + return text; + } + // Constructor, getters, and setters for text + + public void display() { + System.out.println(text); + // Code for displaying text post + } +} \ No newline at end of file diff --git a/homework/src/HW1/submissions/partA/acyutaraman/facebook/Post2.java b/homework/src/HW1/submissions/partA/acyutaraman/facebook/Post2.java new file mode 100644 index 0000000..bd08ac8 --- /dev/null +++ b/homework/src/HW1/submissions/partA/acyutaraman/facebook/Post2.java @@ -0,0 +1,84 @@ +package facebook; + + +//This is the new post class, that was implemented for our "Facebook" to help support images and videos. + +public class Post2 implements Post{ + + private String text; + private String imageUrl; + + private String videoUrl; + private boolean isImage; + + private boolean isVideo; + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + public String getImageUrl() { + return imageUrl; + } + + public void setImageUrl(String imageUrl) { + this.imageUrl = imageUrl; + } + + public String getVideoUrl() { + return videoUrl; + } + + public void setVideoUrl(String videoUrl) { + this.videoUrl = videoUrl; + } + + public boolean isImage() { + return isImage; + } + + public void setImage(boolean image) { + isImage = image; + } + + public boolean isVideo() { + return isVideo; + } + + public void setVideo(boolean video) { + isVideo = video; + } + + public Post2(String text) { + this.text = text; + } + + public Post2(String imageUrl, boolean isImage) { + if(isImage) { + this.imageUrl = imageUrl; + this.isImage = true; + } + } + + public Post2(String videoUrl, boolean isImage, boolean isVideo){ + if(isVideo && !isImage) { + this.videoUrl = videoUrl; + this.isImage = false; + this.isVideo = true; + } + } + + + public void display() { + + if(isImage()) + System.out.println(imageUrl); + if(isVideo()) + System.out.println(videoUrl); + System.out.println(text); + } +} \ No newline at end of file diff --git a/homework/src/HW1/submissions/partA/acyutaraman/facebook/README.md b/homework/src/HW1/submissions/partA/acyutaraman/facebook/README.md new file mode 100644 index 0000000..11d466d --- /dev/null +++ b/homework/src/HW1/submissions/partA/acyutaraman/facebook/README.md @@ -0,0 +1,6 @@ + +1) This class violates the Dependency Inversion Principle because of how it doesn't have any abstract interfaces to base the Post1 and Post2 off of. If you were to make the Post1 class +the superclass of Post2, Post2 would still be able to access methods from Post1 that it shouldn't be accessing. In this scenario, that would lead to a violation of the Open/Closed principle. + +2) I fixed this by creating an abstract interface called Post that both Post1 and Post2 implement. The Post interface has one method called display, which is implemented differently in +Post1 and Post2. I added getter and setter methods for the text in Post1. For the display method in Post1, it just prints out the text in the post. For the display method in Post2, It returns the video url and the text in separate lines if it is a video post, it returns the image url and the text if it is an image post, and it returns just the text if it is neither a video post or an audio post. diff --git a/homework/src/HW1/submissions/partA/acyutaraman/paypal/Account.java b/homework/src/HW1/submissions/partA/acyutaraman/paypal/Account.java new file mode 100644 index 0000000..3db7919 --- /dev/null +++ b/homework/src/HW1/submissions/partA/acyutaraman/paypal/Account.java @@ -0,0 +1,13 @@ +package paypal; + +public class Account { + private String accountID; + + public Account(String accountID) { + this.accountID = accountID; + } + + public String getAccountID() { + return accountID; + } +} \ No newline at end of file diff --git a/homework/src/HW1/submissions/partA/acyutaraman/paypal/Main.java b/homework/src/HW1/submissions/partA/acyutaraman/paypal/Main.java new file mode 100644 index 0000000..156345c --- /dev/null +++ b/homework/src/HW1/submissions/partA/acyutaraman/paypal/Main.java @@ -0,0 +1,9 @@ +package paypal; + +public class Main { + public static void main(String[] args) { + PayPalGateway paymentProcessor = new PaymentProcessor(); + Account account = new Account("1"); + paymentProcessor.processPayment(account,100.0, 101); + } +} \ No newline at end of file diff --git a/homework/src/HW1/submissions/partA/acyutaraman/paypal/PayPalGateway.java b/homework/src/HW1/submissions/partA/acyutaraman/paypal/PayPalGateway.java new file mode 100644 index 0000000..0c6d8df --- /dev/null +++ b/homework/src/HW1/submissions/partA/acyutaraman/paypal/PayPalGateway.java @@ -0,0 +1,6 @@ +package paypal; + +public interface PayPalGateway { + void processPayment(Account account, double amount); + void processPayment(Account account, double amount, double balance); +} \ No newline at end of file diff --git a/homework/src/HW1/submissions/partA/acyutaraman/paypal/PaymentProcessor.java b/homework/src/HW1/submissions/partA/acyutaraman/paypal/PaymentProcessor.java new file mode 100644 index 0000000..b43dfa0 --- /dev/null +++ b/homework/src/HW1/submissions/partA/acyutaraman/paypal/PaymentProcessor.java @@ -0,0 +1,23 @@ +package paypal; + +public class PaymentProcessor implements PayPalGateway{ + + + + @Override + public void processPayment(Account account, double amount) { + System.out.println("Processing payment of $" + amount + " for acocunt " + account.getAccountID() + " using PayPal."); + } + + @Override + public void processPayment(Account account, double amount, double balance) { + + if(balance >= amount) + System.out.println("Processing payment of $" + amount + " for acocunt " + account.getAccountID() + " using PayPal."); + else + System.out.println("For account " + account + ": the amount you are attempting to pay is more than that which is present in your balance." ); + } + + + +} \ No newline at end of file diff --git a/homework/src/HW1/submissions/partA/acyutaraman/paypal/README.md b/homework/src/HW1/submissions/partA/acyutaraman/paypal/README.md new file mode 100644 index 0000000..9e51be3 --- /dev/null +++ b/homework/src/HW1/submissions/partA/acyutaraman/paypal/README.md @@ -0,0 +1,8 @@ + +1) Paypal violates the Dependency Inversion principle because of how it lacks an interface/ abstract entity as a base entity. It just has a PayPalGateWay object used in PayPalProcessor, +without any inheritance or abstraction involved. + +2) I fixed this violation by making the PayPalGateWay an interface with the abstract method processPayment. I then had the PayPalProcessor class implement the interface, and had +the default processMethod() method print out the original line that was supposed to be printed out. I added functionality in the sense that I overloaded the processMethod() method. +The original one takes in only the account and the amount, and prints out a line. I added a new version of that method that takes in the account, the amount, and the balance. This +new version prints out an error message if the amount(for the payment) is greater than the balance in the user's account. diff --git a/homework/src/HW1/submissions/partA/acyutaraman/uber/Main.java b/homework/src/HW1/submissions/partA/acyutaraman/uber/Main.java new file mode 100644 index 0000000..d597694 --- /dev/null +++ b/homework/src/HW1/submissions/partA/acyutaraman/uber/Main.java @@ -0,0 +1,15 @@ +package uber; + +public class Main { + public static void main(String[] args) { + Ride ride = new Ride(1,1); + User user = new User("jill"); + user.setUsername("john_doe"); + + RideManager rideManager = new RideManager(); + + double fare = rideManager.calculateRideFare(ride); + + rideManager.sendNotification(user, "Your ride fare is: $" + fare); + } +} \ No newline at end of file diff --git a/homework/src/HW1/submissions/partA/acyutaraman/uber/README.md b/homework/src/HW1/submissions/partA/acyutaraman/uber/README.md new file mode 100644 index 0000000..480cd5e --- /dev/null +++ b/homework/src/HW1/submissions/partA/acyutaraman/uber/README.md @@ -0,0 +1,10 @@ + +1) Uber violates the Single Responsibility Principle of the SOLID principles. It does so because of its two methods, which are calculateRideFare() and sendNotification(). The calculateRideFate() method involves accessing the Ride class and making a calculation based off of that. The sendNotification() method accesses and intends to make modifications to the +User class. Since two different classes are being directly altered by the RideManagerClass, it violates the Single Responsiblity Principle. + +2) I fixed this SOLID violation by creating the classes RideCalculator and NotificationSender, and having them handle the calculateRideFare() and sendNotifcation() methods respectively. +I placed a constructor for the RideCalculator class that takes in the amounts for minute_rate, mile_rate, and base_rate. The class then uses those values to calculate the fare value of +the given ride in its calculateRideFare() method. I also added message stack in the User class to contain all the messages. I added methods to initialize, add to, and read from the messages +stack. I then had the sendNotifcation() method in NotificationSender call the given User's addMessage() method to add the given message. It then prints out the given line it was initially +supposed to print. In the RideManager class, I initialized a RideCalculator object and a NotifcationSende object. For its calculateRideFare() method, it calls the calculateRideFare() method +for its RideCalculator object returns that value. That is how I added functionality to the RideManager class and fixed its violations. I also modified the main class. diff --git a/homework/src/HW1/submissions/partA/acyutaraman/uber/RideManager.java b/homework/src/HW1/submissions/partA/acyutaraman/uber/RideManager.java new file mode 100644 index 0000000..8f56ff4 --- /dev/null +++ b/homework/src/HW1/submissions/partA/acyutaraman/uber/RideManager.java @@ -0,0 +1,127 @@ +package uber; + +import java.util.Stack; + +class RideManager { + private final double BASE_FARE = 5.0; // Base fare in dollars + private final double PER_MILE_RATE = 2.0; // Fare per mile in dollars + private final double PER_MINUTE_RATE = 0.5; // Fare per minute in dollars + + RideCalculator rd = new RideCalculator(BASE_FARE, PER_MILE_RATE, PER_MINUTE_RATE); + NotificationSender nf = new NotificationSender(); + double calculateRideFare(Ride ride) { + return rd.calculateRideFare(ride); + } + + void sendNotification(User user, String message) { + // Code for sending notifications to the user + + nf.sendNotification(user, message); + } + + + +} + +class NotificationSender { + + void sendNotification(User user, String message) + { + user.addMessage(message); + System.out.println("Notification sent to user: " + user.getUsername() + " - " + message); + } +} + +class RideCalculator { + + double base; + double milerate; + double minuterate; + + public RideCalculator(double base, double milerate, double minuterate) + { + this.base = base; + this.milerate = milerate; + this.minuterate = minuterate; + } + + public double calculateRideFare(Ride ride) + { + + + return base + milerate * ride.getDistanceInMiles() + minuterate*ride.getDurationInMinutes(); + } + + + + + +} +class Ride { + private double distanceInMiles; + private int durationInMinutes; + + public Ride(double distanceInMiles, int durationInMinutes) { + this.distanceInMiles = distanceInMiles; + this.durationInMinutes = durationInMinutes; + } + + + public double getDistanceInMiles() { + return distanceInMiles; + } + + public void setDistanceInMiles(double distanceInMiles) { + this.distanceInMiles = distanceInMiles; + } + + public int getDurationInMinutes() { + return durationInMinutes; + } + + public void setDurationInMinutes(int durationInMinutes) { + this.durationInMinutes = durationInMinutes; + } + + + +} + +class User { + // User details + + private Stack messages; + private String username; + + public User(String username) + { + this.username = username; + this.messages = new Stack(); + } + public String getUsername() { + return username; + } + + public String getLastMessage() + { + if(!messages.isEmpty()) + { + return messages.peek(); + } + return "No messages left"; + } + + public void addMessage(String message) + { + messages.push(message); + } + public void setInbox() + { + this.messages = new Stack(); + } + public void setUsername(String username) { + this.username = username; + } + + +} diff --git a/homework/src/HW1/submissions/partB/acyutaraman/README.md b/homework/src/HW1/submissions/partB/acyutaraman/README.md new file mode 100644 index 0000000..58bf814 --- /dev/null +++ b/homework/src/HW1/submissions/partB/acyutaraman/README.md @@ -0,0 +1,3 @@ +Link to my repo for PART B: + +https://github.com/Achur260/PartBJavaFX/tree/main diff --git a/homework/src/HW1/violations/airbnb/Hotel.java b/homework/src/HW1/violations/airbnb/Hotel.java index 12db905..9379c4a 100644 --- a/homework/src/HW1/violations/airbnb/Hotel.java +++ b/homework/src/HW1/violations/airbnb/Hotel.java @@ -1,8 +1,9 @@ package HW1.violations.airbnb; public class Hotel { - int calculateTotalPrice(int numberOfNights) { - return numberOfNights * 100; + int calculateTotalPrice(int numberOfNights) + { + return numberOfNights*100; } }