├── .gitignore ├── README.md ├── assignments └── Assignment-1.md ├── class_notes ├── concurrency │ ├── Concurrency_1__Process_and_Threads.pdf │ ├── Concurrency_2__Executors_and_Callables.pdf │ └── Concurrency_3__Synchronisation_.pdf ├── java │ └── Java__Advanced_concepts_1.pdf └── oop │ ├── LLD_1__Intro_to_LLD_and_OOPs.pdf │ ├── LLD_2__Access_Modifiers_and_Constructors.pdf │ ├── LLD_3__Inheritance_and_Polymorphism.pdf │ └── LLD_4__Abstract_Classes_and_Association.pdf ├── code ├── Concurrency │ └── src │ │ ├── Main.java │ │ ├── NumberPrinter.java │ │ ├── PrintSomething.java │ │ ├── annotations │ │ ├── DeprecatedDemo.java │ │ ├── Main.java │ │ ├── MyAnnotations.java │ │ ├── OverrideDemo.java │ │ └── SuppressWarningsDemo.java │ │ ├── callables │ │ ├── Client.java │ │ └── GreetingReturner.java │ │ ├── exceptionhandling │ │ ├── ApkNotFoundException.java │ │ ├── ManifestChecker.java │ │ └── ReadFile.java │ │ ├── finalclass │ │ ├── Client.java │ │ ├── Counter1.java │ │ ├── Demo.java │ │ ├── Main.java │ │ ├── ObjectsDemo.java │ │ └── StringDemo.java │ │ ├── framework │ │ ├── Client.java │ │ └── NumberPrinter.java │ │ ├── generics │ │ ├── Main.java │ │ └── Printer.java │ │ ├── iterables │ │ ├── Main.java │ │ └── Node.java │ │ ├── mergesort │ │ ├── Main.java │ │ └── MergeSorter.java │ │ ├── mutex │ │ ├── Adder.java │ │ ├── Counter.java │ │ ├── Main.java │ │ └── Subtractor.java │ │ ├── reflection │ │ ├── Dummy.java │ │ └── Main.java │ │ ├── semaphores │ │ ├── Consumer.java │ │ ├── Main.java │ │ └── Producer.java │ │ ├── sync │ │ ├── Adder.java │ │ ├── Counter.java │ │ ├── Main.java │ │ └── Subtractor.java │ │ └── synchronization │ │ ├── Adder.java │ │ ├── Counter.java │ │ ├── Main.java │ │ └── Subtractor.java ├── DesignPatterns │ ├── demo_better_log.txt │ ├── demo_log.txt │ └── src │ │ ├── Main.java │ │ ├── adapter │ │ ├── BankApiAdapter.java │ │ ├── Client.java │ │ ├── ICICIBankAdapter.java │ │ ├── PhonePe.java │ │ ├── YesBankAdapter.java │ │ └── thirdparty │ │ │ ├── icici │ │ │ └── ICICIBankApi.java │ │ │ └── yesbank │ │ │ └── YesBankApi.java │ │ ├── builder │ │ ├── basic │ │ │ ├── Client.java │ │ │ ├── Student.java │ │ │ └── StudentData.java │ │ └── v2 │ │ │ ├── A.java │ │ │ ├── Client.java │ │ │ └── Student.java │ │ ├── decorator │ │ ├── ChocolateSyrup.java │ │ ├── Client.java │ │ ├── IceCream.java │ │ ├── OrangeCone.java │ │ └── VanillaScoop.java │ │ ├── factory │ │ ├── method │ │ │ ├── Client.java │ │ │ ├── NYPizzaStore.java │ │ │ ├── OriginalPizzaStore.java │ │ │ └── PizzaStore.java │ │ └── simple │ │ │ ├── Main.java │ │ │ ├── Pizza.java │ │ │ ├── PizzaStore.java │ │ │ └── SimplePizzaFactory.java │ │ ├── observer │ │ ├── Client.java │ │ ├── Display1.java │ │ ├── Display2.java │ │ ├── Observer.java │ │ ├── SimpleSwingExample.java │ │ ├── Subject.java │ │ └── WeatherData.java │ │ ├── prototype │ │ ├── DronaConfig.java │ │ ├── RegistryConfig.java │ │ ├── ScalerClient.java │ │ └── SpecialConfig.java │ │ └── singleton │ │ ├── basic │ │ ├── Main.java │ │ └── Singleton.java │ │ ├── concurrent │ │ └── ConcurrentSingleton.java │ │ └── logger │ │ ├── BetterLogger.java │ │ ├── Client.java │ │ └── Logger.java ├── IntroToLLD │ ├── .idea │ │ ├── .gitignore │ │ ├── misc.xml │ │ ├── modules.xml │ │ ├── uiDesigner.xml │ │ └── vcs.xml │ ├── IntroToLLD.iml │ └── src │ │ ├── Main.java │ │ ├── abstractclasses │ │ ├── Loggable.java │ │ ├── Main.java │ │ ├── Student.java │ │ └── User.java │ │ ├── accessmodifier │ │ ├── Box.java │ │ └── Client.java │ │ ├── constructors │ │ ├── Main.java │ │ ├── Performance.java │ │ └── Student.java │ │ ├── inheritance │ │ ├── Instructor.java │ │ ├── LeadInstructor.java │ │ ├── Main.java │ │ ├── Student.java │ │ └── User.java │ │ ├── interfaces │ │ ├── Flamable.java │ │ ├── Flyable.java │ │ ├── Main.java │ │ ├── RedBird.java │ │ └── Rocket.java │ │ ├── introtooops │ │ ├── Main.java │ │ └── Student.java │ │ ├── otheraccess │ │ └── Client.java │ │ └── polymorphism │ │ ├── Animal.java │ │ ├── Dog.java │ │ ├── Main.java │ │ └── Mammals.java └── MachineCoding │ ├── .gitignore │ ├── Splitwise │ ├── .gitignore │ ├── README.md │ ├── mvnw │ ├── mvnw.cmd │ ├── pom.xml │ ├── src │ │ ├── main │ │ │ ├── java │ │ │ │ └── com │ │ │ │ │ └── scaler │ │ │ │ │ └── splitwise │ │ │ │ │ ├── SplitwiseApplication.java │ │ │ │ │ ├── commands │ │ │ │ │ ├── Command.java │ │ │ │ │ ├── CommandExecutor.java │ │ │ │ │ ├── CommandKeywords.java │ │ │ │ │ └── SettleUpUserCommand.java │ │ │ │ │ ├── controllers │ │ │ │ │ └── SettleUpController.java │ │ │ │ │ ├── dtos │ │ │ │ │ ├── SettleUpRequest.java │ │ │ │ │ └── SettleUpResponse.java │ │ │ │ │ ├── models │ │ │ │ │ ├── BaseModel.java │ │ │ │ │ ├── Expense.java │ │ │ │ │ ├── ExpenseType.java │ │ │ │ │ ├── ExpenseUser.java │ │ │ │ │ ├── ExpenseUserType.java │ │ │ │ │ ├── Group.java │ │ │ │ │ └── User.java │ │ │ │ │ ├── repositories │ │ │ │ │ ├── ExpenseRepository.java │ │ │ │ │ ├── GroupRepository.java │ │ │ │ │ └── UserRepository.java │ │ │ │ │ ├── services │ │ │ │ │ └── SettleUpService.java │ │ │ │ │ └── strategies │ │ │ │ │ ├── HeapSettleUpStrategy.java │ │ │ │ │ ├── NewSettleUpStrategy.java │ │ │ │ │ └── SettleUpStrategy.java │ │ │ └── resources │ │ │ │ └── application.properties │ │ └── test │ │ │ └── java │ │ │ └── com │ │ │ └── scaler │ │ │ └── splitwise │ │ │ └── SplitwiseApplicationTests.java │ └── target │ │ └── classes │ │ ├── application.properties │ │ └── com │ │ └── scaler │ │ └── splitwise │ │ ├── SplitwiseApplication.class │ │ ├── commands │ │ ├── Command.class │ │ ├── CommandExecutor.class │ │ ├── CommandKeywords.class │ │ └── SettleUpUserCommand.class │ │ ├── controllers │ │ └── SettleUpController.class │ │ ├── dtos │ │ ├── SettleUpRequest.class │ │ └── SettleUpResponse.class │ │ ├── models │ │ ├── BaseModel.class │ │ ├── Expense.class │ │ ├── ExpenseType.class │ │ ├── ExpenseUser.class │ │ ├── ExpenseUserType.class │ │ ├── Group.class │ │ └── User.class │ │ ├── repositories │ │ ├── ExpenseRepository.class │ │ ├── GroupRepository.class │ │ └── UserRepository.class │ │ ├── services │ │ └── SettleUpService.class │ │ └── strategies │ │ ├── HeapSettleUpStrategy.class │ │ ├── NewSettleUpStrategy.class │ │ └── SettleUpStrategy.class │ ├── book-my-show.md │ ├── bookmyshow │ ├── .gitignore │ ├── .mvn │ │ └── wrapper │ │ │ ├── maven-wrapper.jar │ │ │ └── maven-wrapper.properties │ ├── mvnw │ ├── mvnw.cmd │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── example │ │ │ │ └── bookmyshow │ │ │ │ ├── BookmyshowApplication.java │ │ │ │ ├── controllers │ │ │ │ ├── BookingController.java │ │ │ │ └── CustomerController.java │ │ │ │ ├── dtos │ │ │ │ ├── CreateBookingRequest.java │ │ │ │ ├── CreateBookingResponse.java │ │ │ │ ├── CreateCustomerRequest.java │ │ │ │ └── CreateCustomerResponse.java │ │ │ │ ├── models │ │ │ │ ├── BaseModel.java │ │ │ │ ├── Booking.java │ │ │ │ ├── BookingSeatStatus.java │ │ │ │ ├── City.java │ │ │ │ ├── Customer.java │ │ │ │ ├── Hall.java │ │ │ │ ├── Movie.java │ │ │ │ ├── MovieShow.java │ │ │ │ ├── MovieShowSeat.java │ │ │ │ ├── ScreenType.java │ │ │ │ ├── Seat.java │ │ │ │ ├── SeatType.java │ │ │ │ └── Theatre.java │ │ │ │ ├── repositories │ │ │ │ ├── BookingRepository.java │ │ │ │ ├── CustomerRepository.java │ │ │ │ ├── MovieShowRepository.java │ │ │ │ └── MovieShowSeatRepository.java │ │ │ │ └── services │ │ │ │ ├── BookingService.java │ │ │ │ └── CustomerService.java │ │ └── resources │ │ │ └── application.properties │ │ └── test │ │ └── java │ │ └── com │ │ └── example │ │ └── bookmyshow │ │ └── BookmyshowApplicationTests.java │ ├── parkinglot │ ├── .gitignore │ ├── README.md │ └── src │ │ ├── ParkingLotApplication.java │ │ ├── controllers │ │ └── TicketController.java │ │ ├── dto │ │ ├── GenerateTicketRequest.java │ │ └── GenerateTicketResponse.java │ │ ├── exceptions │ │ ├── GateNotFoundException.java │ │ └── NoParkingSpotsAvailableException.java │ │ ├── models │ │ ├── Gate.java │ │ ├── GateType.java │ │ ├── Invoice.java │ │ ├── ParkingAttendant.java │ │ ├── ParkingFloor.java │ │ ├── ParkingLot.java │ │ ├── ParkingSpot.java │ │ ├── Payment.java │ │ ├── PaymentMode.java │ │ ├── PaymentStatus.java │ │ ├── SpotStatus.java │ │ ├── Ticket.java │ │ ├── Vehicle.java │ │ └── VehicleType.java │ │ ├── repositories │ │ ├── ParkingSpotRepository.java │ │ └── TicketRepository.java │ │ ├── services │ │ ├── EntryGateService.java │ │ ├── SlotAllocationService.java │ │ └── TicketService.java │ │ └── strategies │ │ ├── CostCalculatingStrategy.java │ │ └── ParkingSpotFindingStrategy.java │ ├── springdemo │ ├── .gitignore │ ├── .mvn │ │ └── wrapper │ │ │ ├── maven-wrapper.jar │ │ │ └── maven-wrapper.properties │ ├── mvnw │ ├── mvnw.cmd │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── scaler │ │ │ │ └── springdemo │ │ │ │ ├── SpringdemoApplication.java │ │ │ │ └── models │ │ │ │ ├── BaseModel.java │ │ │ │ ├── City.java │ │ │ │ └── SpringUser.java │ │ └── resources │ │ │ └── application.properties │ │ └── test │ │ └── java │ │ └── com │ │ └── scaler │ │ └── springdemo │ │ └── SpringdemoApplicationTests.java │ └── tictactoe │ ├── .gitignore │ └── src │ ├── Main.java │ ├── controllers │ └── GameController.java │ ├── models │ ├── Board.java │ ├── Bot.java │ ├── BotDifficultyLevel.java │ ├── Cell.java │ ├── CellStatus.java │ ├── Game.java │ ├── GameState.java │ ├── Move.java │ ├── Player.java │ └── PlayerType.java │ ├── services │ ├── BoardService.java │ └── GameService.java │ └── strategies │ ├── bot │ ├── BotPlayingStrategy.java │ ├── BotPlayingStrategyFactory.java │ └── EasyBotPlayingStrategy.java │ └── winningstrategies │ ├── ColWinningStrategy.java │ ├── CornerWinningStrategy.java │ ├── RowWinningStrategy.java │ └── WinningStrategy.java ├── contrib ├── Laxminarayan-Katker.md ├── Nadeem-Shaik.md ├── RishabShinghal.md ├── aman-choudhary.md ├── amol-b.md ├── arvind-singh.md ├── bibek-sahoo.md ├── biswajit-saha.md ├── gautham-s.md ├── kaviyarasan-subbaiyan.md ├── mahesh-phutane.md ├── manabendra-dey.md ├── nage1234.md ├── nikhil-lund.md ├── nuthan-chandra.md ├── pooja-t.md ├── prasant-samal.md ├── pratik-dutta.md ├── prerna-dutta.md ├── rahul-grover.md ├── ritik-gupta.md ├── sanket-kumar.md ├── santosh-jeergi.md ├── saurabh-kamble.md └── soumyakanti-giri.md ├── reading_list ├── concurrency-1-2.md ├── dp-1-singleton.md ├── dp-2-builder.md ├── dp-3-facade.md ├── dp-4-flyweight.md ├── dp-5-adapter.md ├── dp-6-factory.md ├── dp-7-prototype.md ├── dp-8-strategy.md ├── lld-1-oops-intro.md ├── lld-2-access-modifiers-constructors.md ├── lld-3-polymorphism-inheritance.md ├── lld-4-interfaces-abstract-classes.md ├── worksheet-solutions.md └── worksheet.md └── solutions ├── Biswajit8 ├── MainClass.java ├── Rectangle.java ├── Shape.java └── Triangle.java ├── Mangai94 ├── Program.cs ├── Rectangle.cs ├── Shape.cs └── Triangle.cs ├── amanchoudhary168 ├── Main.java ├── Node.java ├── Rectangle.java ├── Shape.java └── Triangle.java ├── arvindsin02 ├── Main.java ├── Rectangle.java ├── Shape.java └── Triangle.java ├── duttapratik ├── Main.java ├── Rectangle.java ├── Shape.java └── Triangle.java ├── gauthams89 ├── Main.java ├── Rectangle.java ├── Shape.java └── Triangle.java ├── kaviyarasans ├── Main.java ├── Rectangle.java ├── Shape.java └── Triangle.java ├── laxminarayankatker └── src │ └── solutions │ └── laxminarayankatker │ ├── Main.java │ ├── Rectangle.java │ ├── Shape.java │ └── Triangle.java ├── maheshphutane ├── Main.java ├── Rectangle.java ├── Shape.java └── Triangle.java ├── nadeemShaik7050 ├── Main.java ├── Rectangle.java ├── Shape.java └── Triangle.java ├── nage1234 ├── Rectangle.java ├── Shape.java ├── ShapeFactory.java └── Triangle.java ├── nikhillund ├── Main.java ├── Rectangle.java ├── Shape.java └── Triangle.java ├── nuthanc ├── Main.java ├── Rectangle.java ├── Shape.java └── Triangle.java ├── okSanky ├── Main.java ├── Rectangle.java ├── Shape.java └── Triangle.java ├── osoiri ├── Base.java ├── Rectangle.java ├── Shape.java └── Triangle.java ├── poojathalur ├── Main.java ├── Rectangle.java ├── Shape.java └── Triangle.java ├── rahulgrover99 └── Main.java ├── rish8089 ├── Client.java ├── Rectangle.java ├── Shape.java └── Triangle.java ├── ritik2117 ├── Main.java ├── Rectangle.java ├── Shape.java └── Triangle.java ├── samalprasant123 ├── Rectangle.java ├── Shape.java ├── TestShapes.java └── Triangle.java ├── santoshjeergi ├── Client.java ├── Rectangle.java ├── Shape.java └── Triangle.java └── skamble89 ├── Rectangle.java ├── Shape.java ├── Test.java └── Triangle.java /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | code/IntroToLLD/out 3 | code/Concurrency/out 4 | code/DesignPatterns/out 5 | *.iml 6 | .idea -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | A one-stop repository for resources related to LLD. -------------------------------------------------------------------------------- /class_notes/concurrency/Concurrency_1__Process_and_Threads.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahulgrover99/May23-LLD/7b8cd9d98f217c961fde010831d869ea62c52548/class_notes/concurrency/Concurrency_1__Process_and_Threads.pdf -------------------------------------------------------------------------------- /class_notes/concurrency/Concurrency_2__Executors_and_Callables.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahulgrover99/May23-LLD/7b8cd9d98f217c961fde010831d869ea62c52548/class_notes/concurrency/Concurrency_2__Executors_and_Callables.pdf -------------------------------------------------------------------------------- /class_notes/concurrency/Concurrency_3__Synchronisation_.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahulgrover99/May23-LLD/7b8cd9d98f217c961fde010831d869ea62c52548/class_notes/concurrency/Concurrency_3__Synchronisation_.pdf -------------------------------------------------------------------------------- /class_notes/java/Java__Advanced_concepts_1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahulgrover99/May23-LLD/7b8cd9d98f217c961fde010831d869ea62c52548/class_notes/java/Java__Advanced_concepts_1.pdf -------------------------------------------------------------------------------- /class_notes/oop/LLD_1__Intro_to_LLD_and_OOPs.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahulgrover99/May23-LLD/7b8cd9d98f217c961fde010831d869ea62c52548/class_notes/oop/LLD_1__Intro_to_LLD_and_OOPs.pdf -------------------------------------------------------------------------------- /class_notes/oop/LLD_2__Access_Modifiers_and_Constructors.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahulgrover99/May23-LLD/7b8cd9d98f217c961fde010831d869ea62c52548/class_notes/oop/LLD_2__Access_Modifiers_and_Constructors.pdf -------------------------------------------------------------------------------- /class_notes/oop/LLD_3__Inheritance_and_Polymorphism.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahulgrover99/May23-LLD/7b8cd9d98f217c961fde010831d869ea62c52548/class_notes/oop/LLD_3__Inheritance_and_Polymorphism.pdf -------------------------------------------------------------------------------- /class_notes/oop/LLD_4__Abstract_Classes_and_Association.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahulgrover99/May23-LLD/7b8cd9d98f217c961fde010831d869ea62c52548/class_notes/oop/LLD_4__Abstract_Classes_and_Association.pdf -------------------------------------------------------------------------------- /code/Concurrency/src/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.concurrent.Executors; 2 | 3 | public class Main { 4 | public static void main(String[] args) throws InterruptedException { 5 | System.out.println("Printing numbers in " + Thread.currentThread().getName()); 6 | for (int i = 1; i <= 100; i++) { 7 | NumberPrinter numberPrinter = new NumberPrinter(i); 8 | Thread t = new Thread(numberPrinter); 9 | t.start(); 10 | // t.join(); 11 | } 12 | } 13 | } -------------------------------------------------------------------------------- /code/Concurrency/src/NumberPrinter.java: -------------------------------------------------------------------------------- 1 | public class NumberPrinter implements Runnable { 2 | 3 | int i; 4 | 5 | NumberPrinter(int i) { 6 | this.i = i; 7 | } 8 | 9 | @Override 10 | public void run() { 11 | System.out.println(i + 12 | " is being printed on Thread " + Thread.currentThread().getName()); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /code/Concurrency/src/PrintSomething.java: -------------------------------------------------------------------------------- 1 | public class PrintSomething implements Runnable{ 2 | @Override 3 | public void run() { 4 | System.out.println("I am running on thread " + Thread.currentThread().getName()); 5 | 6 | for (int i = 0; i < 100; i++) { 7 | System.out.println(i); 8 | } 9 | } 10 | } 11 | 12 | 13 | /* 14 | 15 | Scheduler 16 | 17 | -- List of tasks 18 | -- addingTask 19 | 20 | 21 | Task 22 | -- tim 23 | 24 | 25 | */ -------------------------------------------------------------------------------- /code/Concurrency/src/annotations/DeprecatedDemo.java: -------------------------------------------------------------------------------- 1 | package annotations; 2 | 3 | class FooBar { 4 | @Deprecated() 5 | void foo() { 6 | System.out.println("foo"); 7 | } 8 | 9 | void bar() { 10 | System.out.println("bar"); 11 | } 12 | } 13 | 14 | public class DeprecatedDemo { 15 | public static void main(String[] args) { 16 | FooBar fooBar = new FooBar(); 17 | fooBar.bar(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /code/Concurrency/src/annotations/Main.java: -------------------------------------------------------------------------------- 1 | package annotations; 2 | 3 | 4 | public class Main { 5 | } 6 | 7 | -------------------------------------------------------------------------------- /code/Concurrency/src/annotations/MyAnnotations.java: -------------------------------------------------------------------------------- 1 | package annotations; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | public class MyAnnotations { 9 | @Retention(RetentionPolicy.RUNTIME) 10 | @Target(ElementType.METHOD) 11 | public @interface ImportantMethod{} 12 | } 13 | -------------------------------------------------------------------------------- /code/Concurrency/src/annotations/OverrideDemo.java: -------------------------------------------------------------------------------- 1 | package annotations; 2 | 3 | class ParentClass { 4 | public void getName() { 5 | System.out.println("Rahul"); 6 | } 7 | } 8 | 9 | class ChildClass extends ParentClass { 10 | @Override 11 | public void getName() { 12 | System.out.println("Hehe"); 13 | } 14 | } 15 | 16 | public class OverrideDemo { 17 | public static void main(String[] args) { 18 | ChildClass childClass = new ChildClass(); 19 | childClass.getName(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /code/Concurrency/src/annotations/SuppressWarningsDemo.java: -------------------------------------------------------------------------------- 1 | package annotations; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | public class SuppressWarningsDemo { 7 | 8 | public static void main(String[] args) { 9 | SuppressWarningsDemo swDemo = new SuppressWarningsDemo(); 10 | swDemo.testSuppressWarning(); 11 | } 12 | 13 | // @SuppressWarnings({"unchecked"}) 14 | public void testSuppressWarning() { 15 | Map testMap = new HashMap(); 16 | testMap.put(1, "Item_1"); 17 | testMap.put(2, "Item_2"); 18 | testMap.put(3, "Item_3"); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /code/Concurrency/src/callables/Client.java: -------------------------------------------------------------------------------- 1 | package callables; 2 | 3 | import java.util.concurrent.ExecutionException; 4 | import java.util.concurrent.ExecutorService; 5 | import java.util.concurrent.Executors; 6 | import java.util.concurrent.Future; 7 | 8 | public class Client { 9 | 10 | public static void main(String[] args) throws ExecutionException, InterruptedException { 11 | ExecutorService executorService = Executors.newFixedThreadPool(4); 12 | for (int i = 0; i < 10; i++) { 13 | GreetingReturner gr = new GreetingReturner(); 14 | Future greetingFuture = executorService.submit(gr); 15 | // System.out.println(greetingFuture.get()); 16 | } 17 | 18 | 19 | 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /code/Concurrency/src/callables/GreetingReturner.java: -------------------------------------------------------------------------------- 1 | package callables; 2 | 3 | 4 | import java.util.Random; 5 | import java.util.concurrent.Callable; 6 | 7 | public class GreetingReturner implements Callable { 8 | @Override 9 | public String call() { 10 | Random random = new Random(); 11 | 12 | if (random.nextBoolean()) { 13 | return "Hello from " + Thread.currentThread().getName(); 14 | } else { 15 | return "Bye from " + Thread.currentThread().getName(); 16 | } 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /code/Concurrency/src/exceptionhandling/ApkNotFoundException.java: -------------------------------------------------------------------------------- 1 | package exceptionhandling; 2 | 3 | public class ApkNotFoundException extends Exception { 4 | 5 | ApkNotFoundException() { 6 | 7 | } 8 | 9 | ApkNotFoundException(String errorMessage) { 10 | super(errorMessage); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /code/Concurrency/src/exceptionhandling/ManifestChecker.java: -------------------------------------------------------------------------------- 1 | package exceptionhandling; 2 | 3 | import java.io.FileNotFoundException; 4 | import java.io.FileReader; 5 | 6 | class Demo { 7 | int age = 5; 8 | 9 | @Override 10 | protected void finalize() throws Throwable { 11 | super.finalize(); 12 | System.out.println("Object getting cleaned up"); 13 | } 14 | } 15 | 16 | public class ManifestChecker { 17 | 18 | static void validateApk(String path) throws ApkNotFoundException { 19 | try { 20 | FileReader fileReader = new FileReader(path); 21 | } catch (FileNotFoundException e) { 22 | throw new ApkNotFoundException("APK not found at the path specified."); 23 | } 24 | 25 | } 26 | 27 | public static void main(String[] args) { 28 | try { 29 | validateApk("/Users/rahulgrover/Scaler/LLD/May23/code/Concurrency/src/exceptionhandling/ReadFile.java"); 30 | } catch (ApkNotFoundException e) { 31 | throw new RuntimeException(e); 32 | } finally { 33 | System.out.println("happy"); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /code/Concurrency/src/exceptionhandling/ReadFile.java: -------------------------------------------------------------------------------- 1 | package exceptionhandling; 2 | 3 | import java.io.FileNotFoundException; 4 | import java.io.FileReader; 5 | 6 | // A -> B -> C 7 | // A a = new B(); 8 | 9 | 10 | public class ReadFile { 11 | 12 | public static void main(String[] args) { 13 | try { 14 | readFile(); 15 | // System.out.println(1/0); 16 | System.out.println("File is found"); 17 | } catch(RuntimeException e) { 18 | System.out.println("please dont divide by zero"); 19 | } catch (Exception e) { 20 | System.out.println("Exception"); 21 | } 22 | 23 | } 24 | 25 | static void readFile() throws FileNotFoundException { 26 | FileReader fileReader = new FileReader( 27 | "/Users/rahulgrover/Scaler/LLD/May23/code/Concurrency/" + 28 | "src/exceptionhandling/Scakler.java"); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /code/Concurrency/src/finalclass/Client.java: -------------------------------------------------------------------------------- 1 | package finalclass; 2 | 3 | class ChildClass extends ObjectsDemo{ 4 | public ChildClass(int x, int y) { 5 | super(x, y); 6 | } 7 | } 8 | 9 | 10 | 11 | 12 | public class Client { 13 | 14 | Integer i; 15 | 16 | 17 | 18 | 19 | 20 | public static void main(String[] args) { 21 | ObjectsDemo objectsDemo = new ObjectsDemo(1, 2); 22 | ObjectsDemo objectsDemo1 = new ObjectsDemo(1, 2); 23 | 24 | System.out.println(objectsDemo); 25 | System.out.println(objectsDemo1); 26 | 27 | ChildClass childClass = new ChildClass(1, 2); 28 | 29 | if (objectsDemo.equals(childClass)) { 30 | System.out.println("EQUAL"); 31 | } else { 32 | System.out.println("NOT EQUAL"); 33 | } 34 | 35 | 36 | 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /code/Concurrency/src/finalclass/Counter1.java: -------------------------------------------------------------------------------- 1 | package finalclass; 2 | 3 | public class Counter1 { 4 | Integer count=0; 5 | 6 | public void increment(int i) { 7 | // Lock on value 0 8 | // value changed to 1 9 | synchronized (count){ 10 | count+=i; 11 | } 12 | 13 | } 14 | 15 | public void decrement(int i) { 16 | // Wanting a lock on value 0 17 | synchronized (count){ 18 | count-=i; 19 | } 20 | } 21 | 22 | public int getCount() { 23 | return count; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /code/Concurrency/src/finalclass/Main.java: -------------------------------------------------------------------------------- 1 | package finalclass; 2 | 3 | class Counter{ 4 | private int count; 5 | 6 | // Object level lock taken 7 | synchronized void increment() {} 8 | synchronized void decrement() {} 9 | 10 | int getCount() { 11 | return count; 12 | } 13 | } 14 | 15 | public class Main { 16 | 17 | 18 | 19 | // Thread1 20 | public static void main(String[] args) { 21 | Counter counter1 = new Counter(); 22 | Counter counter2 = new Counter(); 23 | 24 | // Can this be possible concurrently? 25 | // Thread 1 26 | counter1.decrement(); 27 | // Thread 2 28 | counter1.increment(); 29 | 30 | // NOT POSSIBLE --> Same object --> object level locks 31 | 32 | 33 | // Can this be possible concurrently? 34 | // Thread 1 35 | counter1.decrement(); 36 | // Thread 2 37 | counter2.increment(); 38 | 39 | // Yes since different objects 40 | 41 | // Thread 1 42 | counter1.decrement(); 43 | // Thread 2 44 | counter1.getCount(); 45 | 46 | // Yes, since the second method is not synchronized. 47 | 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /code/Concurrency/src/finalclass/ObjectsDemo.java: -------------------------------------------------------------------------------- 1 | package finalclass; 2 | 3 | public class ObjectsDemo { 4 | 5 | private int x; 6 | private int y; 7 | 8 | public ObjectsDemo(int x, int y) { 9 | this.x = x; 10 | this.y = y; 11 | } 12 | 13 | public int getX() { 14 | return x; 15 | } 16 | 17 | public void setX(int x) { 18 | this.x = x; 19 | } 20 | 21 | public int getY() { 22 | return y; 23 | } 24 | 25 | public void setY(int y) { 26 | this.y = y; 27 | } 28 | 29 | @Override 30 | public String toString() { 31 | return "ObjectsDemo{" + 32 | "x=" + x + 33 | ", y=" + y + 34 | '}'; 35 | } 36 | 37 | @Override 38 | public boolean equals(Object obj) { 39 | if (obj == null) return false; 40 | 41 | if (!(obj instanceof ObjectsDemo) ){ 42 | return false; 43 | } 44 | 45 | if (!obj.getClass().getName().equals(ObjectsDemo.class.getName())) return false; 46 | 47 | if (this.x == ((ObjectsDemo)obj).x && this.y == ((ObjectsDemo)obj).y) { 48 | return true; 49 | } 50 | return false; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /code/Concurrency/src/finalclass/StringDemo.java: -------------------------------------------------------------------------------- 1 | package finalclass; 2 | 3 | public class StringDemo { 4 | 5 | public static void main(String[] args) { 6 | 7 | // String are stored in String Constant Pool 8 | // Strings are Immutable. 9 | // Security 10 | // Thread Safety (since multiple reads) 11 | // Saves space. 12 | 13 | // Both x and y --> refer to the same strings that are stored in SCP 14 | 15 | final String rahul = new String("Rahul"); 16 | 17 | String x = "Rahul"; 18 | String y = "Rahul"; 19 | 20 | // y += "hehe"; 21 | 22 | // System.out.println(x + y); 23 | 24 | String z = "Rahul"; 25 | 26 | System.out.println(z); 27 | 28 | 29 | StringBuilder stringBuilder = new StringBuilder("Rahul"); 30 | System.out.println(stringBuilder); 31 | stringBuilder.append("12"); 32 | System.out.println(stringBuilder); 33 | 34 | 35 | // StringBuffer 36 | 37 | 38 | 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /code/Concurrency/src/framework/Client.java: -------------------------------------------------------------------------------- 1 | package framework; 2 | 3 | import java.util.concurrent.ExecutorService; 4 | import java.util.concurrent.Executors; 5 | 6 | public class Client { 7 | 8 | public static void main(String[] args) { 9 | ExecutorService executorService = Executors.newFixedThreadPool(10); 10 | 11 | for (int i = 1; i <= 100; i++) { 12 | NumberPrinter np = new NumberPrinter(i); 13 | executorService.submit(np); 14 | } 15 | executorService.shutdown(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /code/Concurrency/src/framework/NumberPrinter.java: -------------------------------------------------------------------------------- 1 | package framework; 2 | 3 | public class NumberPrinter implements Runnable { 4 | 5 | int i; 6 | 7 | NumberPrinter(int i) { 8 | this.i = i; 9 | } 10 | 11 | @Override 12 | public void run() { 13 | System.out.println(i + 14 | " is being printed on Thread " + Thread.currentThread().getName()); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /code/Concurrency/src/generics/Main.java: -------------------------------------------------------------------------------- 1 | package generics; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Printer ip = new Printer<>(2L); 6 | 7 | ip.print(); 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /code/Concurrency/src/generics/Printer.java: -------------------------------------------------------------------------------- 1 | package generics; 2 | 3 | public class Printer { 4 | T objToPrint; 5 | 6 | public Printer(T objToPrint) { 7 | this.objToPrint = objToPrint; 8 | } 9 | 10 | public void print() { 11 | System.out.println(objToPrint); 12 | objToPrint.doubleValue(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /code/Concurrency/src/iterables/Main.java: -------------------------------------------------------------------------------- 1 | package iterables; 2 | 3 | import java.util.Iterator; 4 | 5 | public class Main { 6 | 7 | public static void main(String[] args) { 8 | Node tail = new Node(1, null); 9 | Node n1 = new Node(2, tail); 10 | Node n2 = new Node(3, n1); 11 | Node n3 = new Node(4, n2); 12 | Node n4 = new Node(5, n3); 13 | 14 | // while (n4 != null) { 15 | // System.out.println(n4.data); 16 | // n4 = n4.next; 17 | // } 18 | // Syntactic sugar 19 | for (int val: n4) { 20 | System.out.println(val); 21 | } 22 | 23 | Iterator itr = n4.iterator(); 24 | 25 | while (itr.hasNext()) { 26 | System.out.println(itr.next()); 27 | } 28 | 29 | } 30 | 31 | 32 | 33 | 34 | } 35 | -------------------------------------------------------------------------------- /code/Concurrency/src/iterables/Node.java: -------------------------------------------------------------------------------- 1 | package iterables; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Iterator; 5 | 6 | public class Node implements Iterable { 7 | 8 | int data; 9 | Node next; 10 | 11 | public Node(int data, Node next) { 12 | this.data = data; 13 | this.next = next; 14 | } 15 | 16 | @Override 17 | public Iterator iterator() { 18 | return new NodeIterator(this); 19 | } 20 | 21 | static class NodeIterator implements Iterator { 22 | 23 | private Node node; 24 | 25 | NodeIterator(Node node) { 26 | this.node = node; 27 | } 28 | 29 | @Override 30 | public boolean hasNext() { 31 | 32 | if (node == null) { 33 | return false; 34 | } else { 35 | return true; 36 | } 37 | 38 | } 39 | 40 | @Override 41 | public Integer next() { 42 | Integer val = node.data; 43 | node = node.next; 44 | return val; 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /code/Concurrency/src/mergesort/Main.java: -------------------------------------------------------------------------------- 1 | package mergesort; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.List; 6 | import java.util.concurrent.ExecutorService; 7 | import java.util.concurrent.Executors; 8 | 9 | public class Main { 10 | public static void main(String[] args) throws Exception { 11 | ExecutorService executorService = Executors.newFixedThreadPool(13); 12 | ArrayList arrayList = new ArrayList<>(Arrays.asList(2, 3, 1, 4, 8, 1, 5, 7, 8, 9, 10, 3, 4)); 13 | MergeSorter mergeSorter = new MergeSorter(arrayList, executorService); 14 | System.out.println(mergeSorter. 15 | ()); 16 | executorService.shutdown(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /code/Concurrency/src/mutex/Adder.java: -------------------------------------------------------------------------------- 1 | package mutex; 2 | 3 | import java.util.concurrent.locks.Lock; 4 | 5 | public class Adder implements Runnable{ 6 | 7 | Counter counter; 8 | Lock lock; 9 | 10 | Adder(Counter counter, Lock lock) { 11 | this.counter = counter; 12 | this.lock = lock; 13 | } 14 | 15 | @Override 16 | public void run() { 17 | for (int i = 0; i < 100000; i++) { 18 | // Shared data 19 | lock.lock(); 20 | counter.count += i; 21 | lock.unlock(); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /code/Concurrency/src/mutex/Counter.java: -------------------------------------------------------------------------------- 1 | package mutex; 2 | 3 | public class Counter { 4 | int count = 0; 5 | } 6 | -------------------------------------------------------------------------------- /code/Concurrency/src/mutex/Main.java: -------------------------------------------------------------------------------- 1 | package mutex; 2 | 3 | import java.util.concurrent.locks.Lock; 4 | import java.util.concurrent.locks.ReentrantLock; 5 | 6 | public class Main { 7 | 8 | public static void main(String[] args) throws Exception { 9 | Counter counter = new Counter(); 10 | Lock lock = new ReentrantLock(); 11 | 12 | Adder adder = new Adder(counter, lock); 13 | Subtractor subtractor = new Subtractor(counter, lock); 14 | 15 | Thread t1 = new Thread(adder); 16 | Thread t2 = new Thread(subtractor); 17 | // Object 18 | // Creating the threads. 19 | t1.start(); 20 | t2.start(); 21 | 22 | // Wait for both the threads to complete. 23 | t1.join(); 24 | t2.join(); 25 | 26 | System.out.println("The count value is: " + counter.count); 27 | 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /code/Concurrency/src/mutex/Subtractor.java: -------------------------------------------------------------------------------- 1 | package mutex; 2 | 3 | import java.util.concurrent.locks.Lock; 4 | 5 | public class Subtractor implements Runnable { 6 | 7 | Counter counter; 8 | 9 | Lock lock; 10 | 11 | Subtractor(Counter counter, Lock lock) { 12 | this.counter = counter; 13 | this.lock = lock; 14 | } 15 | 16 | @Override 17 | public void run() { 18 | for (int i = 0; i < 100000; i++) { 19 | // Shared data 20 | lock.lock(); 21 | counter.count -= i; 22 | lock.unlock(); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /code/Concurrency/src/reflection/Dummy.java: -------------------------------------------------------------------------------- 1 | package reflection; 2 | 3 | import annotations.MyAnnotations.ImportantMethod; 4 | 5 | 6 | public class Dummy { 7 | private int age; 8 | private String name; 9 | 10 | public Dummy(int age, String name) { 11 | this.age = age; 12 | this.name = name; 13 | } 14 | 15 | public int getAge() { 16 | return age; 17 | } 18 | 19 | public void setAge(int age) { 20 | this.age = age; 21 | } 22 | 23 | public String getName() { 24 | return name; 25 | } 26 | 27 | public void setName(String name) { 28 | this.name = name; 29 | } 30 | 31 | public void print() { 32 | System.out.println(name + " " + age); 33 | } 34 | 35 | // @ImportantMethod 36 | private void privatePrint() { 37 | System.out.println(name + " private " + age); 38 | } 39 | 40 | public static void iAmPublicStatic() { 41 | System.out.println("I am public static."); 42 | } 43 | 44 | private static void iAmPrivateStatic() { 45 | System.out.println("I am private static."); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /code/Concurrency/src/semaphores/Consumer.java: -------------------------------------------------------------------------------- 1 | package semaphores; 2 | 3 | import java.util.Queue; 4 | import java.util.concurrent.Semaphore; 5 | import java.util.concurrent.locks.Lock; 6 | 7 | public class Consumer implements Runnable { 8 | 9 | Queue store; 10 | String name; 11 | Semaphore ps; 12 | Semaphore cs; 13 | 14 | 15 | Consumer(String name, Queue store, Semaphore ps, Semaphore cs) { 16 | this.name = name; 17 | this.store = store; 18 | this.cs = cs; 19 | this.ps = ps; 20 | } 21 | 22 | @Override 23 | public void run() { 24 | try { 25 | cs.acquire(); 26 | } catch (InterruptedException e) { 27 | throw new RuntimeException(e); 28 | } 29 | System.out.printf("[%d] Consumer %s consumed a shirt in Thread %s\n", 30 | store.size(), name, Thread.currentThread().getName()); 31 | store.remove(); 32 | ps.release(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /code/Concurrency/src/semaphores/Main.java: -------------------------------------------------------------------------------- 1 | package semaphores; 2 | 3 | import java.util.LinkedList; 4 | import java.util.Queue; 5 | import java.util.concurrent.Semaphore; 6 | 7 | public class Main { 8 | public static void main(String[] args) { 9 | Queue queue = new LinkedList<>(); 10 | 11 | Semaphore ps = new Semaphore(1000); 12 | Semaphore cs = new Semaphore(0); 13 | 14 | for (int i = 0; i < 1000; i++) { 15 | Producer p = new Producer("P" + i, queue, 5, ps, cs); 16 | Consumer c = new Consumer("C" + i, queue, ps, cs); 17 | 18 | Thread tp = new Thread(p); 19 | Thread tc = new Thread(c); 20 | 21 | tc.start(); 22 | tp.start(); 23 | 24 | } 25 | 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /code/Concurrency/src/semaphores/Producer.java: -------------------------------------------------------------------------------- 1 | package semaphores; 2 | 3 | 4 | import java.util.Queue; 5 | import java.util.concurrent.Semaphore; 6 | import java.util.concurrent.locks.Lock; 7 | 8 | public class Producer implements Runnable { 9 | 10 | String name; 11 | int cap; 12 | Queue store; 13 | Semaphore ps; 14 | Semaphore cs; 15 | 16 | 17 | Producer(String name, Queue store, int cap, Semaphore ps, Semaphore cs) { 18 | this.name = name; 19 | this.store = store; 20 | this.cap = cap; 21 | this.ps = ps; 22 | this.cs = cs; 23 | } 24 | 25 | @Override 26 | public void run() { 27 | try { 28 | ps.acquire(); 29 | } catch (InterruptedException e) { 30 | throw new RuntimeException(e); 31 | } 32 | System.out.printf("[%d] Producer %s produced a shirt in Thread %s\n", 33 | store.size(), name, Thread.currentThread().getName()); 34 | store.add(new Object()); 35 | cs.release(); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /code/Concurrency/src/sync/Adder.java: -------------------------------------------------------------------------------- 1 | package sync; 2 | 3 | public class Adder implements Runnable{ 4 | 5 | Counter counter; 6 | 7 | Adder(Counter counter) { 8 | this.counter = counter; 9 | } 10 | 11 | @Override 12 | public void run() { 13 | for (int i = 0; i < 10000; i++) { 14 | // Shared data 15 | // synchronized (counter) { 16 | // The key will only be required for sync methods 17 | Counter.increment(i); 18 | // } 19 | 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /code/Concurrency/src/sync/Counter.java: -------------------------------------------------------------------------------- 1 | package sync; 2 | 3 | public class Counter { 4 | private static int count = 0; 5 | 6 | public int getCount() { 7 | return count; 8 | } 9 | 10 | synchronized public void decrement(int i) { 11 | synchronized (Counter.class) { 12 | count -= i; 13 | } 14 | 15 | } 16 | 17 | synchronized static public void increment(int i) { 18 | count += i; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /code/Concurrency/src/sync/Main.java: -------------------------------------------------------------------------------- 1 | package sync; 2 | 3 | import java.util.Hashtable; 4 | import java.util.concurrent.ConcurrentHashMap; 5 | 6 | public class Main { 7 | 8 | public static void main(String[] args) throws Exception { 9 | Counter counter = new Counter(); 10 | Adder adder = new Adder(counter); 11 | Subtractor subtractor = new Subtractor(counter); 12 | 13 | Thread t1 = new Thread(adder); 14 | Thread t2 = new Thread(subtractor); 15 | 16 | // Creating the threads. 17 | t1.start(); 18 | t2.start(); 19 | 20 | // Wait for both the threads to complete. 21 | t1.join(); 22 | t2.join(); 23 | 24 | System.out.println("The count value is: " + counter.getCount()); 25 | 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /code/Concurrency/src/sync/Subtractor.java: -------------------------------------------------------------------------------- 1 | package sync; 2 | 3 | public class Subtractor implements Runnable { 4 | 5 | Counter counter; 6 | 7 | Subtractor(Counter counter) { 8 | this.counter = counter; 9 | } 10 | 11 | @Override 12 | public void run() { 13 | for (int i = 0; i < 10000; i++) { 14 | // Shared data 15 | synchronized (counter) { 16 | counter.decrement(i); 17 | } 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /code/Concurrency/src/synchronization/Adder.java: -------------------------------------------------------------------------------- 1 | package synchronization; 2 | 3 | public class Adder implements Runnable{ 4 | 5 | Counter counter; 6 | 7 | Adder(Counter counter) { 8 | this.counter = counter; 9 | } 10 | 11 | @Override 12 | public void run() { 13 | for (int i = 0; i < 10000; i++) { 14 | // Shared data 15 | counter.count += i; 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /code/Concurrency/src/synchronization/Counter.java: -------------------------------------------------------------------------------- 1 | package synchronization; 2 | 3 | public class Counter { 4 | int count = 0; 5 | } 6 | -------------------------------------------------------------------------------- /code/Concurrency/src/synchronization/Main.java: -------------------------------------------------------------------------------- 1 | package synchronization; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) throws Exception { 6 | Counter counter = new Counter(); 7 | 8 | Adder adder = new Adder(counter); 9 | Subtractor subtractor = new Subtractor(counter); 10 | 11 | Thread t1 = new Thread(adder); 12 | Thread t2 = new Thread(subtractor); 13 | 14 | // Creating the threads. 15 | t1.start(); 16 | t2.start(); 17 | 18 | // Wait for both the threads to complete. 19 | t1.join(); 20 | t2.join(); 21 | 22 | System.out.println("The count value is: " + counter.count); 23 | 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /code/Concurrency/src/synchronization/Subtractor.java: -------------------------------------------------------------------------------- 1 | package synchronization; 2 | 3 | public class Subtractor implements Runnable { 4 | 5 | Counter counter; 6 | 7 | Subtractor(Counter counter) { 8 | this.counter = counter; 9 | } 10 | 11 | @Override 12 | public void run() { 13 | for (int i = 0; i < 10000; i++) { 14 | // Shared data 15 | counter.count -= i; 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /code/DesignPatterns/demo_better_log.txt: -------------------------------------------------------------------------------- 1 | DEPOSIT (0001): 80.5$ 2 | WITHDRAW (0002): 100.0$ 3 | TRANSFER (0001->0003): 40.0$ 4 | DEPOSIT (0004): 56.74$ 5 | WITHDRAW (0005): 30.0$ 6 | -------------------------------------------------------------------------------- /code/DesignPatterns/demo_log.txt: -------------------------------------------------------------------------------- 1 | DEPOSIT (0004): 56.74$ 2 | RWITHDRAW (0005): 30.0$ 3 | .0$ 4 | -------------------------------------------------------------------------------- /code/DesignPatterns/src/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | System.out.println("Hello world!"); 4 | } 5 | } -------------------------------------------------------------------------------- /code/DesignPatterns/src/adapter/BankApiAdapter.java: -------------------------------------------------------------------------------- 1 | package adapter; 2 | 3 | public interface BankApiAdapter { 4 | double getBalance(String accountNumber); 5 | 6 | boolean sendMoney(String fromAccount, 7 | String toAccount, 8 | double amount); 9 | } 10 | -------------------------------------------------------------------------------- /code/DesignPatterns/src/adapter/Client.java: -------------------------------------------------------------------------------- 1 | package adapter; 2 | 3 | public class Client { 4 | 5 | public static void main(String[] args) { 6 | 7 | YesBankAdapter yesBankAdapter = new YesBankAdapter(); 8 | ICICIBankAdapter iciciBankAdapter = new ICICIBankAdapter(); 9 | 10 | PhonePe phonePe = new PhonePe(yesBankAdapter); 11 | 12 | phonePe.getBalance(); 13 | 14 | 15 | 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /code/DesignPatterns/src/adapter/ICICIBankAdapter.java: -------------------------------------------------------------------------------- 1 | package adapter; 2 | 3 | import adapter.thirdparty.icici.ICICIBankApi; 4 | 5 | public class ICICIBankAdapter implements BankApiAdapter{ 6 | 7 | private ICICIBankApi iciciBankApi = new ICICIBankApi(); 8 | @Override 9 | public double getBalance(String accountNumber) { 10 | iciciBankApi.printSomething(); 11 | return 0; 12 | } 13 | 14 | @Override 15 | public boolean sendMoney(String fromAccount, String toAccount, double amount) { 16 | iciciBankApi.printSomething(); 17 | return false; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /code/DesignPatterns/src/adapter/PhonePe.java: -------------------------------------------------------------------------------- 1 | package adapter; 2 | 3 | public class PhonePe { 4 | 5 | private BankApiAdapter bankApiAdapter; 6 | 7 | PhonePe(BankApiAdapter bankApiAdapter) { 8 | this.bankApiAdapter = bankApiAdapter; 9 | } 10 | 11 | void getBalance() { 12 | bankApiAdapter.getBalance("12221"); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /code/DesignPatterns/src/adapter/YesBankAdapter.java: -------------------------------------------------------------------------------- 1 | package adapter; 2 | 3 | import adapter.thirdparty.yesbank.YesBankApi; 4 | 5 | public class YesBankAdapter implements BankApiAdapter { 6 | 7 | private YesBankApi yesBankApi = new YesBankApi(); 8 | 9 | @Override 10 | public double getBalance(String accountNumber) { 11 | yesBankApi.doSomething(); 12 | return 0; 13 | } 14 | 15 | @Override 16 | public boolean sendMoney(String fromAccount, String toAccount, double amount) { 17 | yesBankApi.doSomething(); 18 | return false; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /code/DesignPatterns/src/adapter/thirdparty/icici/ICICIBankApi.java: -------------------------------------------------------------------------------- 1 | package adapter.thirdparty.icici; 2 | 3 | public class ICICIBankApi { 4 | 5 | public void printSomething() { 6 | System.out.println("ICICI bank did something."); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /code/DesignPatterns/src/adapter/thirdparty/yesbank/YesBankApi.java: -------------------------------------------------------------------------------- 1 | package adapter.thirdparty.yesbank; 2 | 3 | public class YesBankApi { 4 | 5 | public void doSomething() { 6 | System.out.println("Yes bank did something."); 7 | } 8 | 9 | } 10 | -------------------------------------------------------------------------------- /code/DesignPatterns/src/builder/basic/Client.java: -------------------------------------------------------------------------------- 1 | package builder.basic; 2 | 3 | public class Client { 4 | 5 | public static void main(String[] args) { 6 | StudentData studentData = new StudentData(); 7 | studentData.setAge(22); 8 | studentData.setName("ABC"); 9 | studentData.setPsp(90.0); 10 | studentData.setBatchName("Super"); 11 | studentData.setGradYear(2015); 12 | 13 | Student student = new Student(studentData); 14 | System.out.println(student); 15 | 16 | 17 | 18 | // 1. StudentData tightly related to Student class. 19 | // 2. 20 | 21 | 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /code/DesignPatterns/src/builder/basic/Student.java: -------------------------------------------------------------------------------- 1 | package builder.basic; 2 | 3 | public class Student { 4 | 5 | private String name; 6 | private int age; 7 | private double psp; 8 | private String batchName; 9 | private int gradYear; 10 | private int yoe; 11 | 12 | Student(StudentData studentData) { 13 | this.name = studentData.getName(); 14 | this.age = studentData.getAge(); 15 | this.psp = studentData.getPsp(); 16 | this.batchName = studentData.getBatchName(); 17 | this.gradYear = studentData.getGradYear(); 18 | this.yoe = studentData.getYoe(); 19 | validate(); 20 | } 21 | 22 | @Override 23 | public String toString() { 24 | return "Student{" + 25 | "name='" + name + '\'' + 26 | ", age=" + age + 27 | ", psp=" + psp + 28 | ", batchName='" + batchName + '\'' + 29 | ", gradYear=" + gradYear + 30 | ", yoe=" + yoe + 31 | '}'; 32 | } 33 | 34 | private void validate() { 35 | if (this.gradYear > 2022) { 36 | throw new IllegalArgumentException("Grad year is invalid"); 37 | } 38 | if (this.age > 90 || this.age < 10) { 39 | throw new IllegalArgumentException("Not valid age."); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /code/DesignPatterns/src/builder/basic/StudentData.java: -------------------------------------------------------------------------------- 1 | package builder.basic; 2 | 3 | public class StudentData { 4 | 5 | private String name; 6 | private int age; 7 | private double psp; 8 | private String batchName; 9 | private int gradYear; 10 | private int yoe; 11 | 12 | public int getYoe() { 13 | return yoe; 14 | } 15 | 16 | public void setYoe(int yoe) { 17 | this.yoe = yoe; 18 | } 19 | 20 | public String getName() { 21 | return name; 22 | } 23 | 24 | public void setName(String name) { 25 | this.name = name; 26 | } 27 | 28 | public int getAge() { 29 | return age; 30 | } 31 | 32 | public void setAge(int age) { 33 | this.age = age; 34 | } 35 | 36 | public double getPsp() { 37 | return psp; 38 | } 39 | 40 | public void setPsp(double psp) { 41 | this.psp = psp; 42 | } 43 | 44 | public String getBatchName() { 45 | return batchName; 46 | } 47 | 48 | public void setBatchName(String batchName) { 49 | this.batchName = batchName; 50 | } 51 | 52 | public int getGradYear() { 53 | return gradYear; 54 | } 55 | 56 | public void setGradYear(int gradYear) { 57 | this.gradYear = gradYear; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /code/DesignPatterns/src/builder/v2/A.java: -------------------------------------------------------------------------------- 1 | package builder.v2; 2 | 3 | public class A { 4 | 5 | 6 | 7 | class B { 8 | 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /code/DesignPatterns/src/decorator/ChocolateSyrup.java: -------------------------------------------------------------------------------- 1 | package decorator; 2 | 3 | public class ChocolateSyrup implements IceCream { 4 | IceCream iceCream; 5 | int cost = 15; 6 | String name = "Chocolate Syrup"; 7 | 8 | ChocolateSyrup(IceCream iceCream) { 9 | this.iceCream = iceCream; 10 | } 11 | 12 | @Override 13 | public int getCost() { 14 | return iceCream.getCost() + cost; 15 | } 16 | 17 | @Override 18 | public String getDescription() { 19 | return iceCream.getDescription() + name; 20 | } 21 | } -------------------------------------------------------------------------------- /code/DesignPatterns/src/decorator/Client.java: -------------------------------------------------------------------------------- 1 | package decorator; 2 | 3 | public class Client { 4 | 5 | 6 | public static void main(String[] args) { 7 | IceCream iceCream = new VanillaScoop(new ChocolateSyrup(new OrangeCone())); 8 | 9 | System.out.println(iceCream.getCost()); 10 | System.out.println(iceCream.getDescription()); 11 | } 12 | 13 | 14 | } 15 | -------------------------------------------------------------------------------- /code/DesignPatterns/src/decorator/IceCream.java: -------------------------------------------------------------------------------- 1 | package decorator; 2 | 3 | public interface IceCream { 4 | 5 | int getCost(); 6 | 7 | String getDescription(); 8 | 9 | } 10 | -------------------------------------------------------------------------------- /code/DesignPatterns/src/decorator/OrangeCone.java: -------------------------------------------------------------------------------- 1 | package decorator; 2 | 3 | public class OrangeCone implements IceCream{ 4 | 5 | IceCream iceCream; 6 | int cost = 10; 7 | String name = "Orange Cone"; 8 | 9 | // Parameter less constructor for the base class. 10 | OrangeCone() { 11 | 12 | } 13 | 14 | OrangeCone(IceCream iceCream) { 15 | this.iceCream = iceCream; 16 | } 17 | 18 | @Override 19 | public int getCost() { 20 | if (iceCream == null) return cost; 21 | return iceCream.getCost() + cost; 22 | } 23 | 24 | @Override 25 | public String getDescription() { 26 | if (iceCream == null) return name; 27 | return iceCream.getDescription() + name; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /code/DesignPatterns/src/decorator/VanillaScoop.java: -------------------------------------------------------------------------------- 1 | package decorator; 2 | 3 | public class VanillaScoop implements IceCream{ 4 | IceCream iceCream; 5 | int cost = 25; 6 | String name = "Vanilla Scoop"; 7 | 8 | VanillaScoop(IceCream iceCream) { 9 | this.iceCream = iceCream; 10 | } 11 | 12 | @Override 13 | public int getCost() { 14 | return iceCream.getCost() + cost; 15 | } 16 | 17 | @Override 18 | public String getDescription() { 19 | return iceCream.getDescription() + name; 20 | } 21 | } -------------------------------------------------------------------------------- /code/DesignPatterns/src/factory/method/Client.java: -------------------------------------------------------------------------------- 1 | package factory.method; 2 | 3 | public class Client { 4 | 5 | public static void main(String[] args) { 6 | PizzaStore originalPizzaStore = new OriginalPizzaStore(); 7 | originalPizzaStore.orderPizza("M"); 8 | 9 | PizzaStore newPizzaStore = new NYPizzaStore(); 10 | newPizzaStore.orderPizza("M"); 11 | 12 | } 13 | 14 | 15 | } 16 | -------------------------------------------------------------------------------- /code/DesignPatterns/src/factory/method/NYPizzaStore.java: -------------------------------------------------------------------------------- 1 | package factory.method; 2 | 3 | import factory.simple.Pizza; 4 | 5 | import java.util.List; 6 | 7 | public class NYPizzaStore extends PizzaStore{ 8 | 9 | @Override 10 | protected Pizza createPizza(String type) { 11 | Pizza pizza; 12 | if (type.equals("Mushroom")) { 13 | pizza = new Pizza("Mushroom", "Normal", "Tomato", List.of()); 14 | } else if (type.equals("Veggie")) { 15 | pizza = new Pizza("Cheese", "Thick", "Garlic", List.of("Onions", "Capsicum")); 16 | } else { 17 | pizza = new Pizza("NYSpecial", "Normal", "Tomato", List.of()); 18 | } 19 | return pizza; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /code/DesignPatterns/src/factory/method/OriginalPizzaStore.java: -------------------------------------------------------------------------------- 1 | package factory.method; 2 | 3 | import factory.simple.Pizza; 4 | 5 | import java.util.List; 6 | 7 | public class OriginalPizzaStore extends PizzaStore{ 8 | @Override 9 | protected Pizza createPizza(String type) { 10 | return new Pizza("Margarita", "Normal", "Tomato", List.of()); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /code/DesignPatterns/src/factory/method/PizzaStore.java: -------------------------------------------------------------------------------- 1 | package factory.method; 2 | 3 | 4 | import factory.simple.Pizza; 5 | 6 | public abstract class PizzaStore { 7 | 8 | Pizza orderPizza(String type) { 9 | 10 | Pizza pizza = createPizza(type); 11 | pizza.prepare(); 12 | pizza.bake(); 13 | pizza.cut(); 14 | pizza.box(); 15 | return pizza; 16 | } 17 | 18 | // Factory Method -- resposible for creating pizza 19 | protected abstract Pizza createPizza(String type); 20 | } 21 | -------------------------------------------------------------------------------- /code/DesignPatterns/src/factory/simple/Main.java: -------------------------------------------------------------------------------- 1 | package factory.simple; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | 6 | PizzaStore pizzaStore = new PizzaStore(); 7 | 8 | pizzaStore.orderPizza("Veggie"); 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /code/DesignPatterns/src/factory/simple/Pizza.java: -------------------------------------------------------------------------------- 1 | package factory.simple; 2 | 3 | import java.util.List; 4 | 5 | public class Pizza { 6 | String name; 7 | String dough; 8 | String sauce; 9 | List toppings; 10 | 11 | public Pizza(String name, String dough, String sauce, List toppings) { 12 | this.name = name; 13 | this.dough = dough; 14 | this.sauce = sauce; 15 | this.toppings = toppings; 16 | } 17 | 18 | public void prepare() { 19 | System.out.println("Preparing " + name); 20 | System.out.println("Kneading dough... " + dough); 21 | System.out.println("Adding sauce... " + sauce); 22 | System.out.println("Adding toppings: "); 23 | for(String topping: toppings) { 24 | System.out.println(topping); 25 | } 26 | } 27 | 28 | public void bake() { 29 | System.out.println("Baking pizza at 350 degrees for 10 minutes."); 30 | } 31 | 32 | public void cut() { 33 | System.out.println("Cutting the pizza in diagnol slices."); 34 | } 35 | 36 | public void box() { 37 | System.out.println("Placing pizza in official PizzaStore box."); 38 | } 39 | 40 | String getName() { 41 | return name; 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /code/DesignPatterns/src/factory/simple/PizzaStore.java: -------------------------------------------------------------------------------- 1 | package factory.simple; 2 | 3 | import java.util.List; 4 | 5 | 6 | // V1 -> We are only having 1 pizza that we prepare. 7 | // PizzaStore is responsible for creating different Pizza instances. 8 | public class PizzaStore { 9 | 10 | Pizza orderPizza(String type) { 11 | 12 | Pizza pizza; 13 | pizza = SimplePizzaFactory.createPizza(type); 14 | 15 | 16 | pizza.prepare(); 17 | pizza.bake(); 18 | pizza.cut(); 19 | pizza.box(); 20 | return pizza; 21 | } 22 | } 23 | 24 | 25 | // 1. Dough and put some vegetables - whatever you want 26 | // 2. Cheese, sauces. 27 | // 3. Bake the pizza 28 | // 4. Cut. 29 | // 5. Box 30 | 31 | 32 | // PizzaStore -> orderPizza() -> Pizza. 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /code/DesignPatterns/src/factory/simple/SimplePizzaFactory.java: -------------------------------------------------------------------------------- 1 | package factory.simple; 2 | 3 | import java.util.List; 4 | 5 | // Factory responsible for creating Pizza instances. 6 | public class SimplePizzaFactory { 7 | 8 | 9 | // Static factory. 10 | 11 | // You might want to avoid static factories, in case you want to override the 12 | // methods. 13 | static Pizza createPizza(String type) { 14 | Pizza pizza; 15 | if (type.equals("Margarita")) { 16 | pizza = new Pizza("Margarita", "Normal", "Tomato", List.of()); 17 | } else if (type.equals("Veggie")) { 18 | pizza = new Pizza("Veggie", "Thin", "Garlic", List.of("Onions", "Capsicum")); 19 | } else { 20 | pizza = new Pizza("Empty", "Normal", "Tomato", List.of()); 21 | } 22 | return pizza; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /code/DesignPatterns/src/observer/Client.java: -------------------------------------------------------------------------------- 1 | package observer; 2 | 3 | public class Client { 4 | 5 | public static void main(String[] args) throws InterruptedException { 6 | WeatherData weatherData = new WeatherData(); 7 | 8 | Observer observer = new Display1(); 9 | Observer observer1 = new Display2(); 10 | 11 | weatherData.addObserver(observer); 12 | weatherData.addObserver(observer1); 13 | 14 | 15 | weatherData.measurementsChanged(); 16 | Thread.sleep(1000); 17 | weatherData.measurementsChanged(); 18 | Thread.sleep(1000); 19 | weatherData.measurementsChanged(); 20 | Thread.sleep(1000); 21 | 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /code/DesignPatterns/src/observer/Display1.java: -------------------------------------------------------------------------------- 1 | package observer; 2 | 3 | public class Display1 implements Observer{ 4 | 5 | private int temperature; 6 | private int humidity; 7 | private int pressure; 8 | 9 | public void print() { 10 | System.out.println("Display1{" + 11 | "temperature=" + temperature + 12 | ", humidity=" + humidity + 13 | ", pressure=" + pressure + 14 | '}'); 15 | } 16 | 17 | @Override 18 | public void update(int temp, int pressure, int humidity) { 19 | this.temperature = temp; 20 | this.humidity = humidity; 21 | this.pressure = pressure; 22 | 23 | print(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /code/DesignPatterns/src/observer/Display2.java: -------------------------------------------------------------------------------- 1 | package observer; 2 | 3 | public class Display2 implements Observer{ 4 | 5 | private int temperature; 6 | private int humidity; 7 | private int pressure; 8 | 9 | public void print() { 10 | System.out.println("Display2{" + 11 | "temperature=" + temperature + 12 | ", humidity=" + humidity + 13 | ", pressure=" + pressure + 14 | '}'); 15 | } 16 | 17 | @Override 18 | public void update(int temp, int pressure, int humidity) { 19 | this.temperature = temp; 20 | this.humidity = humidity; 21 | this.pressure = pressure; 22 | 23 | print(); 24 | } 25 | } -------------------------------------------------------------------------------- /code/DesignPatterns/src/observer/Observer.java: -------------------------------------------------------------------------------- 1 | package observer; 2 | 3 | public interface Observer { 4 | 5 | void update(int temp, int pressure, int humidity); 6 | } 7 | -------------------------------------------------------------------------------- /code/DesignPatterns/src/observer/Subject.java: -------------------------------------------------------------------------------- 1 | package observer; 2 | 3 | public interface Subject { 4 | 5 | void addObserver(Observer observer); 6 | void removeObserver(Observer observer); 7 | void notifyObservers(); 8 | 9 | } 10 | -------------------------------------------------------------------------------- /code/DesignPatterns/src/observer/WeatherData.java: -------------------------------------------------------------------------------- 1 | package observer; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class WeatherData implements Subject{ 7 | 8 | private int temperature; 9 | private int humidity; 10 | private int pressure; 11 | 12 | private List observerList = new ArrayList<>(); 13 | 14 | @Override 15 | public void addObserver(Observer observer) { 16 | observerList.add(observer); 17 | } 18 | 19 | @Override 20 | public void removeObserver(Observer observer) { 21 | observerList.remove(observer); 22 | } 23 | 24 | @Override 25 | public void notifyObservers() { 26 | for (Observer observer: observerList) { 27 | observer.update(temperature, pressure, humidity); 28 | } 29 | } 30 | 31 | void measurementsChanged() { 32 | temperature = (int)(Math.random()*100); 33 | humidity = (int)(Math.random()*100); 34 | pressure = (int)(Math.random()*100); 35 | notifyObservers(); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /code/DesignPatterns/src/prototype/DronaConfig.java: -------------------------------------------------------------------------------- 1 | package prototype; 2 | 3 | public class DronaConfig { 4 | 5 | private String user; 6 | private String password; 7 | private String url; 8 | private int port; 9 | public String sessionName; 10 | public int account; 11 | 12 | public DronaConfig(String user, String password, String url, int port, String sessionName, int account) { 13 | this.user = user; 14 | this.password = password; 15 | this.url = url; 16 | this.port = port; 17 | this.sessionName = sessionName; 18 | this.account = account; 19 | } 20 | 21 | // Copy Constructor 22 | public DronaConfig(DronaConfig other) { 23 | this.user = other.user; 24 | this.password = other.password; 25 | this.url = other.url; 26 | this.port = other.port; 27 | this.sessionName = other.sessionName; 28 | this.account = other.account; 29 | } 30 | 31 | 32 | @Override 33 | public DronaConfig clone() { 34 | return new DronaConfig(this); 35 | } 36 | 37 | 38 | 39 | } 40 | -------------------------------------------------------------------------------- /code/DesignPatterns/src/prototype/RegistryConfig.java: -------------------------------------------------------------------------------- 1 | package prototype; 2 | 3 | import java.util.HashMap; 4 | 5 | public class RegistryConfig { 6 | 7 | private HashMap map; 8 | 9 | public RegistryConfig() { 10 | this.map = new HashMap<>(); 11 | DronaConfig dronaConfig = new DronaConfig( 12 | "abc", "supersecure", "www.com", 13 | 9090, "default", 123); 14 | 15 | DronaConfig specialConf = new SpecialConfig( 16 | "xyz", "notsupersecure", "www.com", 17 | 1010, "default", 123, "securityismyth"); 18 | 19 | this.map.put("simpleConfig", dronaConfig); 20 | this.map.put("specialConfig", specialConf); 21 | 22 | } 23 | 24 | public void register(String key, DronaConfig config) { 25 | map.put(key, config); 26 | } 27 | 28 | public DronaConfig get(String key) { 29 | return map.get(key); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /code/DesignPatterns/src/prototype/ScalerClient.java: -------------------------------------------------------------------------------- 1 | package prototype; 2 | 3 | import org.w3c.dom.ls.LSOutput; 4 | 5 | public class ScalerClient { 6 | 7 | 8 | 9 | public static void main(String[] args) { 10 | // Template object that imagine has been provided. 11 | 12 | RegistryConfig registryConfig = new RegistryConfig(); 13 | 14 | DronaConfig dronaConfig = registryConfig.get("simpleConfig"); 15 | 16 | DronaConfig specialConf = registryConfig.get("specialConfig"); 17 | 18 | // Main Part 19 | 20 | DronaConfig copy = dronaConfig.clone(); 21 | copy.account = 91911; 22 | 23 | DronaConfig copy2 = specialConf.clone(); 24 | 25 | System.out.println("DEBUG"); 26 | 27 | 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /code/DesignPatterns/src/prototype/SpecialConfig.java: -------------------------------------------------------------------------------- 1 | package prototype; 2 | 3 | public class SpecialConfig extends DronaConfig { 4 | 5 | private String specialToken; 6 | 7 | public SpecialConfig(String user, String password, String url, int port, String sessionName, int account, String specialToken) { 8 | super(user, password, url, port, sessionName, account); 9 | this.specialToken = specialToken; 10 | } 11 | 12 | 13 | public SpecialConfig(SpecialConfig config) { 14 | super(config); 15 | this.specialToken = config.specialToken; 16 | } 17 | 18 | @Override 19 | public SpecialConfig clone() { 20 | return new SpecialConfig(this); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /code/DesignPatterns/src/singleton/basic/Main.java: -------------------------------------------------------------------------------- 1 | package singleton.basic; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | Singleton s1 = Singleton.getInstance(); 7 | Singleton s2 = Singleton.getInstance(); 8 | Singleton s3 = Singleton.getInstance(); 9 | Singleton s4 = Singleton.getInstance(); 10 | 11 | 12 | s1.makeConnection(); 13 | s1.makeConnection(); 14 | s2.makeConnection(); 15 | s3.makeConnection(); 16 | s4.makeConnection(); 17 | s1.makeConnection(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /code/DesignPatterns/src/singleton/basic/Singleton.java: -------------------------------------------------------------------------------- 1 | package singleton.basic; 2 | 3 | public class Singleton { 4 | 5 | private int count = 0; 6 | 7 | // Has to be static (referencing from static scope) 8 | private static Singleton INSTANCE = null; 9 | 10 | // Has to be static, we won't be able to get objects otherwise. 11 | public static Singleton getInstance() { 12 | if (INSTANCE == null) { 13 | INSTANCE = new Singleton(); 14 | } 15 | return INSTANCE; 16 | } 17 | 18 | public void makeConnection() { 19 | if (count > 3) System.out.println("False"); 20 | else { 21 | System.out.println("True"); 22 | count++; 23 | } 24 | } 25 | 26 | 27 | private Singleton() {} 28 | 29 | } 30 | -------------------------------------------------------------------------------- /code/DesignPatterns/src/singleton/concurrent/ConcurrentSingleton.java: -------------------------------------------------------------------------------- 1 | package singleton.concurrent; 2 | 3 | 4 | public class ConcurrentSingleton { 5 | 6 | private static ConcurrentSingleton INSTANCE = null; 7 | 8 | public static ConcurrentSingleton getInstance() { 9 | 10 | // If we have a lock here, 11 | // any thread entering this method, would have to get a lock 12 | if (INSTANCE == null) { 13 | synchronized (ConcurrentSingleton.class) { 14 | if (INSTANCE == null) { 15 | INSTANCE = new ConcurrentSingleton(); 16 | } 17 | } 18 | } 19 | return INSTANCE; 20 | } 21 | 22 | private ConcurrentSingleton() {} 23 | 24 | } 25 | -------------------------------------------------------------------------------- /code/DesignPatterns/src/singleton/logger/BetterLogger.java: -------------------------------------------------------------------------------- 1 | package singleton.logger; 2 | 3 | import java.io.FileWriter; 4 | import java.io.IOException; 5 | import java.io.PrintWriter; 6 | 7 | public class BetterLogger { 8 | 9 | private static BetterLogger logger = null; 10 | 11 | private final String logFile = "demo_better_log.txt"; 12 | private PrintWriter writer; 13 | 14 | private BetterLogger() { 15 | try { 16 | FileWriter fw = new FileWriter(logFile); 17 | writer = new PrintWriter(fw, true); 18 | } catch (IOException e) {} 19 | } 20 | 21 | public static synchronized BetterLogger getInstance(){ 22 | if(logger == null) 23 | logger = new BetterLogger(); 24 | return logger; 25 | } 26 | 27 | public void logWithdraw (String account, double amount) { 28 | writer.println("WITHDRAW (" + account + "): " + amount + "$"); 29 | } 30 | 31 | public void logDeposit (String account, double amount) { 32 | writer.println("DEPOSIT (" + account + "): " + amount + "$"); 33 | } 34 | 35 | public void logTransfer (String fromAccount, String toAccount, double amount) { 36 | writer.println("TRANSFER (" + fromAccount + "->" + toAccount + "): " + amount + "$"); 37 | } 38 | 39 | } -------------------------------------------------------------------------------- /code/DesignPatterns/src/singleton/logger/Client.java: -------------------------------------------------------------------------------- 1 | package singleton.logger; 2 | 3 | public class Client { 4 | 5 | public static void main(String[] args) { 6 | Logger logger1 = new Logger(); 7 | Logger logger2 = new Logger(); 8 | Logger logger3 = new Logger(); 9 | 10 | logger1.logDeposit("0001", 80.5); 11 | logger2.logWithdraw("0002", 100); 12 | logger1.logTransfer("0001", "0003", 40); 13 | logger3.logDeposit("0004", 56.74); 14 | logger2.logWithdraw("0005", 30); 15 | 16 | BetterLogger bLogger1 = BetterLogger.getInstance(); 17 | BetterLogger bLogger2 = BetterLogger.getInstance(); 18 | BetterLogger bLogger3 = BetterLogger.getInstance(); 19 | 20 | bLogger1.logDeposit("0001", 80.5); 21 | bLogger2.logWithdraw("0002", 100); 22 | bLogger1.logTransfer("0001", "0003", 40); 23 | bLogger3.logDeposit("0004", 56.74); 24 | bLogger2.logWithdraw("0005", 30); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /code/DesignPatterns/src/singleton/logger/Logger.java: -------------------------------------------------------------------------------- 1 | package singleton.logger; 2 | 3 | import java.io.FileWriter; 4 | import java.io.IOException; 5 | import java.io.PrintWriter; 6 | 7 | public class Logger { 8 | 9 | private final String logFile = "demo_log.txt"; 10 | private PrintWriter writer; 11 | 12 | public Logger() { 13 | try { 14 | FileWriter fw = new FileWriter(logFile); 15 | writer = new PrintWriter(fw, true); 16 | } catch (IOException e) {} 17 | } 18 | 19 | public void logWithdraw (String account, double amount) { 20 | writer.println("WITHDRAW (" + account + "): " + amount + "$"); 21 | } 22 | 23 | public void logDeposit (String account, double amount) { 24 | writer.println("DEPOSIT (" + account + "): " + amount + "$"); 25 | } 26 | 27 | public void logTransfer (String fromAccount, String toAccount, double amount) { 28 | writer.println("TRANSFER (" + fromAccount + "->" + toAccount + "): " + amount + "$"); 29 | } 30 | 31 | } -------------------------------------------------------------------------------- /code/IntroToLLD/.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /code/IntroToLLD/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /code/IntroToLLD/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /code/IntroToLLD/.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /code/IntroToLLD/IntroToLLD.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /code/IntroToLLD/src/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | System.out.println("Hello world!"); 4 | } 5 | } -------------------------------------------------------------------------------- /code/IntroToLLD/src/abstractclasses/Loggable.java: -------------------------------------------------------------------------------- 1 | package abstractclasses; 2 | 3 | public interface Loggable { 4 | void login(); 5 | } 6 | -------------------------------------------------------------------------------- /code/IntroToLLD/src/abstractclasses/Main.java: -------------------------------------------------------------------------------- 1 | package abstractclasses; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | // User u = new User("Rahul"); 6 | 7 | Student student = new Student("Rahul"); 8 | student.login(); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /code/IntroToLLD/src/abstractclasses/Student.java: -------------------------------------------------------------------------------- 1 | package abstractclasses; 2 | 3 | public class Student extends User implements Loggable{ 4 | 5 | Student(String name) { 6 | this.name = name; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /code/IntroToLLD/src/abstractclasses/User.java: -------------------------------------------------------------------------------- 1 | package abstractclasses; 2 | 3 | public abstract class User { 4 | public String name; 5 | 6 | public void login() { 7 | System.out.println("Log in " + name); 8 | } 9 | } 10 | 11 | -------------------------------------------------------------------------------- /code/IntroToLLD/src/accessmodifier/Box.java: -------------------------------------------------------------------------------- 1 | package accessmodifier; 2 | 3 | public class Box { 4 | 5 | 6 | static int time_pass = 10; 7 | public static void printMe() { 8 | System.out.println("I am a box"); 9 | 10 | } 11 | protected int width = 1; 12 | // No access modifier -> default -> Package private 13 | int height = 2; 14 | 15 | public int breadth = 3; 16 | 17 | int getWidth() { 18 | System.out.println(time_pass); 19 | return width; 20 | 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /code/IntroToLLD/src/accessmodifier/Client.java: -------------------------------------------------------------------------------- 1 | package accessmodifier; 2 | 3 | public class Client { 4 | public static void main(String[] args) { 5 | System.out.println(Box.time_pass); 6 | Box.time_pass += 10; 7 | System.out.println(Box.time_pass); 8 | Box.printMe(); 9 | Box b1 = new Box(); 10 | System.out.println(b1.getWidth()); 11 | System.out.println(b1.time_pass); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /code/IntroToLLD/src/constructors/Main.java: -------------------------------------------------------------------------------- 1 | package constructors; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | Performance p1 = new Performance(); // 50 7 | 8 | Student s1 = new Student("Chirag", 10, 50, p1); 9 | Student s2 = new Student(s1); 10 | 11 | System.out.println(s1.performance.marks); 12 | 13 | s2.performance.marks = 100; 14 | 15 | System.out.println(s1.performance.marks); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /code/IntroToLLD/src/constructors/Performance.java: -------------------------------------------------------------------------------- 1 | package constructors; 2 | 3 | public class Performance { 4 | public int marks = 50; 5 | public double psp = 81.7; 6 | 7 | public Performance() { 8 | this.marks = 50; 9 | this.psp = 81.7; 10 | } 11 | 12 | public Performance(Performance performance) { 13 | this.marks = performance.marks; 14 | this.psp = performance.psp; 15 | } 16 | } 17 | 18 | 19 | -------------------------------------------------------------------------------- /code/IntroToLLD/src/constructors/Student.java: -------------------------------------------------------------------------------- 1 | package constructors; 2 | 3 | public class Student { 4 | String name; 5 | private int id; 6 | private int totalProblemsSolved; 7 | 8 | Performance performance; 9 | 10 | // Parametrized constructor 11 | Student(String name, int id, int totalProblemsSolved, Performance performance) { 12 | this.name = name; 13 | this.id = id; 14 | this.totalProblemsSolved = totalProblemsSolved; 15 | this.performance = performance; 16 | } 17 | 18 | // Copy Constructor 19 | Student(Student otherStudent) { 20 | // Shallow copy! 21 | this.name = otherStudent.name; 22 | this.id = otherStudent.id; 23 | this.totalProblemsSolved = otherStudent.totalProblemsSolved; 24 | // for deep copy 25 | // for shallow copy -> this.performance = otherStudent.performance 26 | // this.performance = otherStudent.performance 27 | this.performance = new Performance(otherStudent.performance); 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /code/IntroToLLD/src/inheritance/Instructor.java: -------------------------------------------------------------------------------- 1 | package inheritance; 2 | 3 | public class Instructor extends User { 4 | 5 | String company = "X"; 6 | Instructor(String name) { 7 | // Keyword that calls the constructor of the base class 8 | // super(name); 9 | this.name = name; 10 | // There is no default constructor available in 'inheritance.User' 11 | System.out.println("Constructor of Instructor."); 12 | } 13 | 14 | Instructor() { 15 | System.out.println("Constructor of Instructor."); 16 | } 17 | 18 | void teach() { 19 | System.out.println(" is teaching"); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /code/IntroToLLD/src/inheritance/LeadInstructor.java: -------------------------------------------------------------------------------- 1 | package inheritance; 2 | 3 | public class LeadInstructor extends Instructor{ 4 | LeadInstructor(String name) { 5 | this.name = name; 6 | } 7 | 8 | 9 | } 10 | -------------------------------------------------------------------------------- /code/IntroToLLD/src/inheritance/Main.java: -------------------------------------------------------------------------------- 1 | package inheritance; 2 | 3 | import java.util.List; 4 | 5 | public class Main { 6 | 7 | private static void printName(List users) { 8 | for (User u: users) { 9 | System.out.println(u.name); 10 | } 11 | } 12 | 13 | public static void main(String[] args) { 14 | User instructor = new Instructor("Anshuman"); 15 | // name, age, email, login, teach 16 | instructor.login(); 17 | System.out.println(((Instructor) instructor).company); 18 | // instructor.teach(); 19 | 20 | 21 | // Instructor notInstructor = new User(); 22 | 23 | if (instructor instanceof User) { 24 | System.out.println("Instructor is instance of User"); 25 | } 26 | 27 | printName(List.of(new Student("Sahil"), new Instructor("Rahul"), instructor)); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /code/IntroToLLD/src/inheritance/Student.java: -------------------------------------------------------------------------------- 1 | package inheritance; 2 | 3 | public class Student extends User { 4 | 5 | Student(String name) { 6 | super(name); 7 | } 8 | 9 | void solveProblem() { 10 | System.out.println( "is solving problem."); 11 | } 12 | } 13 | 14 | // user 15 | // instructor, student -------------------------------------------------------------------------------- /code/IntroToLLD/src/inheritance/User.java: -------------------------------------------------------------------------------- 1 | package inheritance; 2 | 3 | public class User { 4 | protected String name = "default"; 5 | String email = "default@google.com"; 6 | int age = 10; 7 | 8 | User(String name) { 9 | this.name = name; 10 | System.out.println("Constructor of User class"); 11 | } 12 | 13 | public User(){ 14 | System.out.println("Constructor of User class"); 15 | } 16 | 17 | void login() { 18 | System.out.println(name + "is logged in."); 19 | } 20 | 21 | 22 | } 23 | -------------------------------------------------------------------------------- /code/IntroToLLD/src/interfaces/Flamable.java: -------------------------------------------------------------------------------- 1 | package interfaces; 2 | 3 | public interface Flamable { 4 | int rocketSpeed = 2; 5 | default void fly() { 6 | System.out.println(rocketSpeed); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /code/IntroToLLD/src/interfaces/Flyable.java: -------------------------------------------------------------------------------- 1 | package interfaces; 2 | 3 | public interface Flyable { 4 | void fly(); 5 | } 6 | -------------------------------------------------------------------------------- /code/IntroToLLD/src/interfaces/Main.java: -------------------------------------------------------------------------------- 1 | package interfaces; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | public class Main { 7 | public static void main(String[] args) { 8 | RedBird bird = new RedBird(); 9 | Rocket rocket = new Rocket(); 10 | 11 | System.out.println(rocket.rocketSpeed); 12 | 13 | 14 | // bird.fly(); 15 | 16 | // Arrays.sort(); 17 | printFly(List.of(rocket)); 18 | 19 | } 20 | 21 | public static void printFly(List flyables) { 22 | for (Flamable flyable: flyables) { 23 | flyable.fly(); 24 | } 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /code/IntroToLLD/src/interfaces/RedBird.java: -------------------------------------------------------------------------------- 1 | package interfaces; 2 | 3 | // Class 'RedBird' must either be declared abstract 4 | // or implement abstract method 'fly()' in 'Flyable' 5 | public class RedBird implements Flyable{ 6 | 7 | @Override 8 | public void fly() { 9 | 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /code/IntroToLLD/src/interfaces/Rocket.java: -------------------------------------------------------------------------------- 1 | package interfaces; 2 | 3 | public class Rocket implements Flyable, Flamable{ 4 | 5 | @Override 6 | public void fly() { 7 | Flamable.super.fly(); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /code/IntroToLLD/src/introtooops/Main.java: -------------------------------------------------------------------------------- 1 | package introtooops; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | // Object 1 6 | Student chirag = new Student(); 7 | 8 | // Object 2 9 | Student sachin = new Student(); 10 | 11 | chirag.solvesProblem(1); 12 | sachin.solvesProblem(2); 13 | 14 | chirag.solvesProblem(3); 15 | sachin.solvesProblem(1); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /code/IntroToLLD/src/introtooops/Student.java: -------------------------------------------------------------------------------- 1 | package introtooops; 2 | 3 | public class Student { 4 | private String name; 5 | private int id; 6 | private int totalProblemsSolved; 7 | 8 | final public int marks; 9 | Student() { 10 | id = 1; 11 | totalProblemsSolved = 2; 12 | name = "Rahul"; 13 | marks = 5; 14 | System.out.println("The constructor was called."); 15 | } 16 | 17 | public void solvesProblem(int count) { 18 | // final String x; 19 | this.totalProblemsSolved += count; 20 | System.out.println(this.name + " solved " + this.totalProblemsSolved + " problems."); 21 | } 22 | 23 | // Constructor 24 | // public Student(String name, int id) { 25 | // this.name = name; 26 | // this.id = id; 27 | // this.totalProblemsSolved = 0; 28 | // } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /code/IntroToLLD/src/otheraccess/Client.java: -------------------------------------------------------------------------------- 1 | package otheraccess; 2 | 3 | import accessmodifier.Box; 4 | 5 | public class Client { 6 | public static void main(String[] args) { 7 | Box b1 = new Box(); 8 | System.out.println(b1.breadth); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /code/IntroToLLD/src/polymorphism/Animal.java: -------------------------------------------------------------------------------- 1 | package polymorphism; 2 | 3 | public class Animal { 4 | int a = 5; 5 | 6 | void print() { 7 | System.out.println("Printing from class Animal: " + a); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /code/IntroToLLD/src/polymorphism/Dog.java: -------------------------------------------------------------------------------- 1 | package polymorphism; 2 | 3 | public class Dog extends Mammals { 4 | Dog() { 5 | this.a = 15; 6 | } 7 | 8 | @Override 9 | void print() { 10 | System.out.println("DHAMAKAAA!"); 11 | super.print(); 12 | System.out.println("Printing from class Dog: " + a); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /code/IntroToLLD/src/polymorphism/Main.java: -------------------------------------------------------------------------------- 1 | package polymorphism; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Animal a = new Animal(); 6 | Animal a2 = new Mammals(); 7 | Animal a3 = new Dog(); 8 | 9 | a.print(); 10 | a2.print(); 11 | // Cannot resolve method 'printMammal' in 'Animal' 12 | 13 | // Ask compiler to shut up and force convert the type Animal 14 | // to Mammals and then call the method. 15 | // ((Mammals) a2).printMammal(); 16 | a3.print(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /code/IntroToLLD/src/polymorphism/Mammals.java: -------------------------------------------------------------------------------- 1 | package polymorphism; 2 | 3 | public class Mammals extends Animal{ 4 | 5 | Mammals() { 6 | this.a = 10; 7 | } 8 | 9 | } 10 | -------------------------------------------------------------------------------- /code/MachineCoding/.gitignore: -------------------------------------------------------------------------------- 1 | ### IntelliJ IDEA ### 2 | out/ 3 | !**/src/main/**/out/ 4 | !**/src/test/**/out/ 5 | 6 | ### Eclipse ### 7 | .apt_generated 8 | .classpath 9 | .factorypath 10 | .project 11 | .settings 12 | .springBeans 13 | .sts4-cache 14 | bin/ 15 | !**/src/main/**/bin/ 16 | !**/src/test/**/bin/ 17 | 18 | ### NetBeans ### 19 | /nbproject/private/ 20 | /nbbuild/ 21 | /dist/ 22 | /nbdist/ 23 | /.nb-gradle/ 24 | 25 | ### VS Code ### 26 | .vscode/ 27 | 28 | ### Mac OS ### 29 | .DS_Store -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | .gradle 3 | build/ 4 | !gradle/wrapper/gradle-wrapper.jar 5 | !**/src/main/**/build/ 6 | !**/src/test/**/build/ 7 | 8 | ### STS ### 9 | .apt_generated 10 | .classpath 11 | .factorypath 12 | .project 13 | .settings 14 | .springBeans 15 | .sts4-cache 16 | bin/ 17 | !**/src/main/**/bin/ 18 | !**/src/test/**/bin/ 19 | 20 | ### IntelliJ IDEA ### 21 | .idea 22 | *.iws 23 | *.iml 24 | *.ipr 25 | out/ 26 | !**/src/main/**/out/ 27 | !**/src/test/**/out/ 28 | 29 | ### NetBeans ### 30 | /nbproject/private/ 31 | /nbbuild/ 32 | /dist/ 33 | /nbdist/ 34 | /.nb-gradle/ 35 | 36 | ### VS Code ### 37 | .vscode/ 38 | -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/src/main/java/com/scaler/splitwise/SplitwiseApplication.java: -------------------------------------------------------------------------------- 1 | package com.scaler.splitwise; 2 | 3 | import com.scaler.splitwise.commands.CommandExecutor; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.boot.CommandLineRunner; 6 | import org.springframework.boot.SpringApplication; 7 | import org.springframework.boot.autoconfigure.SpringBootApplication; 8 | import org.springframework.data.jpa.repository.config.EnableJpaAuditing; 9 | 10 | import java.util.Scanner; 11 | 12 | @SpringBootApplication 13 | @EnableJpaAuditing 14 | public class SplitwiseApplication implements CommandLineRunner { 15 | 16 | Scanner scanner = new Scanner(System.in); 17 | 18 | @Autowired 19 | CommandExecutor commandExecutor; 20 | 21 | public static void main(String[] args) { 22 | SpringApplication.run(SplitwiseApplication.class, args); 23 | } 24 | 25 | @Override 26 | public void run(String... args) throws Exception { 27 | String input = scanner.nextLine(); 28 | commandExecutor.execute(input); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/src/main/java/com/scaler/splitwise/commands/Command.java: -------------------------------------------------------------------------------- 1 | package com.scaler.splitwise.commands; 2 | 3 | public interface Command { 4 | 5 | boolean matches(String string); 6 | void execute(String string); 7 | } 8 | -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/src/main/java/com/scaler/splitwise/commands/CommandExecutor.java: -------------------------------------------------------------------------------- 1 | package com.scaler.splitwise.commands; 2 | 3 | import org.springframework.stereotype.Component; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | 8 | @Component 9 | public class CommandExecutor { 10 | 11 | List commandList = new ArrayList<>(); 12 | 13 | public void addCommand(Command command) { 14 | commandList.add(command); 15 | } 16 | 17 | public void removeCommand(Command command) { 18 | commandList.remove(command); 19 | } 20 | 21 | public void execute(String input) { 22 | for (Command command: commandList) { 23 | if (command.matches(input)) { 24 | command.execute(input); 25 | break; 26 | } 27 | } 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/src/main/java/com/scaler/splitwise/commands/CommandKeywords.java: -------------------------------------------------------------------------------- 1 | package com.scaler.splitwise.commands; 2 | 3 | public class CommandKeywords { 4 | static final String SETTLE_UP = "settle_up"; 5 | } 6 | -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/src/main/java/com/scaler/splitwise/commands/SettleUpUserCommand.java: -------------------------------------------------------------------------------- 1 | package com.scaler.splitwise.commands; 2 | 3 | import com.scaler.splitwise.dtos.SettleUpRequest; 4 | import org.springframework.security.core.parameters.P; 5 | 6 | import java.util.List; 7 | 8 | public class SettleUpUserCommand implements Command{ 9 | @Override 10 | public boolean matches(String string) { 11 | List words = List.of(string.split(" ")); 12 | return words.get(0).equalsIgnoreCase(CommandKeywords.SETTLE_UP) && words.size() == 3; 13 | } 14 | 15 | @Override 16 | public void execute(String string) { 17 | List words = List.of(string.split(" ")); 18 | SettleUpRequest settleUpRequest = new SettleUpRequest(); 19 | settleUpRequest.setUserId(Long.valueOf(words.get(1))); 20 | settleUpRequest.setGroupId(Long.valueOf(words.get(2))); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/src/main/java/com/scaler/splitwise/controllers/SettleUpController.java: -------------------------------------------------------------------------------- 1 | package com.scaler.splitwise.controllers; 2 | 3 | import com.scaler.splitwise.dtos.SettleUpRequest; 4 | import com.scaler.splitwise.dtos.SettleUpResponse; 5 | import com.scaler.splitwise.models.Expense; 6 | import com.scaler.splitwise.services.SettleUpService; 7 | import lombok.AllArgsConstructor; 8 | import org.springframework.stereotype.Controller; 9 | 10 | import java.util.List; 11 | 12 | @Controller 13 | @AllArgsConstructor 14 | public class SettleUpController { 15 | 16 | private SettleUpService settleUpService; 17 | public SettleUpResponse settleUp(SettleUpRequest settleUpRequest) { 18 | List expenseList = settleUpService.settleUp( 19 | settleUpRequest.getUserId(), settleUpRequest.getGroupId()); 20 | return new SettleUpResponse(expenseList); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/src/main/java/com/scaler/splitwise/dtos/SettleUpRequest.java: -------------------------------------------------------------------------------- 1 | package com.scaler.splitwise.dtos; 2 | 3 | import lombok.Data; 4 | 5 | @Data 6 | public class SettleUpRequest { 7 | private Long userId; 8 | private Long groupId; 9 | } 10 | -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/src/main/java/com/scaler/splitwise/dtos/SettleUpResponse.java: -------------------------------------------------------------------------------- 1 | package com.scaler.splitwise.dtos; 2 | 3 | import com.scaler.splitwise.models.Expense; 4 | import lombok.AllArgsConstructor; 5 | import lombok.Data; 6 | 7 | import java.util.List; 8 | 9 | @Data 10 | @AllArgsConstructor 11 | public class SettleUpResponse { 12 | private List expenses; 13 | } 14 | -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/src/main/java/com/scaler/splitwise/models/BaseModel.java: -------------------------------------------------------------------------------- 1 | package com.scaler.splitwise.models; 2 | 3 | import jakarta.persistence.*; 4 | import lombok.Data; 5 | import org.springframework.data.annotation.CreatedDate; 6 | import org.springframework.data.annotation.LastModifiedDate; 7 | import org.springframework.data.jpa.domain.support.AuditingEntityListener; 8 | 9 | import java.util.Date; 10 | 11 | import static jakarta.persistence.GenerationType.IDENTITY; 12 | 13 | @MappedSuperclass 14 | @Data 15 | @EntityListeners(value = AuditingEntityListener.class) 16 | public class BaseModel { 17 | @Id 18 | @GeneratedValue(strategy = IDENTITY) 19 | private Long id; 20 | @LastModifiedDate 21 | @Temporal(value = TemporalType.TIMESTAMP) 22 | private Date lastModifiedAt; 23 | @CreatedDate 24 | @Temporal(TemporalType.DATE) 25 | private Date createdAt; 26 | } 27 | -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/src/main/java/com/scaler/splitwise/models/Expense.java: -------------------------------------------------------------------------------- 1 | package com.scaler.splitwise.models; 2 | 3 | import jakarta.persistence.Entity; 4 | import jakarta.persistence.ManyToOne; 5 | import jakarta.persistence.OneToMany; 6 | import lombok.Data; 7 | 8 | import java.util.List; 9 | 10 | @Entity 11 | @Data 12 | public class Expense extends BaseModel{ 13 | private String description; 14 | private Double totalAmount; 15 | private ExpenseType expenseType; 16 | @ManyToOne 17 | private Group group; 18 | @OneToMany 19 | List paidBy; 20 | @OneToMany 21 | List owedBy; 22 | } 23 | -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/src/main/java/com/scaler/splitwise/models/ExpenseType.java: -------------------------------------------------------------------------------- 1 | package com.scaler.splitwise.models; 2 | 3 | public enum ExpenseType { 4 | REAL, 5 | PAYMENT, 6 | } 7 | -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/src/main/java/com/scaler/splitwise/models/ExpenseUser.java: -------------------------------------------------------------------------------- 1 | package com.scaler.splitwise.models; 2 | 3 | import jakarta.persistence.Entity; 4 | import jakarta.persistence.ManyToOne; 5 | import lombok.Data; 6 | 7 | @Entity 8 | @Data 9 | public class ExpenseUser extends BaseModel { 10 | @ManyToOne 11 | private Expense expense; 12 | @ManyToOne 13 | private User user; 14 | private ExpenseUserType expenseUserType; 15 | private Double amount; 16 | } 17 | -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/src/main/java/com/scaler/splitwise/models/ExpenseUserType.java: -------------------------------------------------------------------------------- 1 | package com.scaler.splitwise.models; 2 | 3 | public enum ExpenseUserType { 4 | PAID, 5 | OWED 6 | } 7 | -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/src/main/java/com/scaler/splitwise/models/Group.java: -------------------------------------------------------------------------------- 1 | package com.scaler.splitwise.models; 2 | 3 | import jakarta.persistence.Entity; 4 | import jakarta.persistence.ManyToMany; 5 | import jakarta.persistence.ManyToOne; 6 | import jakarta.persistence.OneToMany; 7 | import lombok.Data; 8 | 9 | import java.util.List; 10 | 11 | @Entity(name = "splitwise_groups") 12 | @Data 13 | public class Group extends BaseModel { 14 | private String name; 15 | @ManyToMany 16 | private List users; 17 | @ManyToOne 18 | private User adminUser; 19 | @OneToMany 20 | private List expenses; 21 | } 22 | -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/src/main/java/com/scaler/splitwise/models/User.java: -------------------------------------------------------------------------------- 1 | package com.scaler.splitwise.models; 2 | 3 | import jakarta.persistence.Entity; 4 | import lombok.Data; 5 | 6 | @Entity(name = "users") 7 | @Data 8 | public class User extends BaseModel{ 9 | private String name; 10 | private String phone; 11 | private String password; 12 | } 13 | -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/src/main/java/com/scaler/splitwise/repositories/ExpenseRepository.java: -------------------------------------------------------------------------------- 1 | package com.scaler.splitwise.repositories; 2 | 3 | import com.scaler.splitwise.models.Expense; 4 | import com.scaler.splitwise.models.Group; 5 | import org.springframework.data.jpa.repository.JpaRepository; 6 | import org.springframework.stereotype.Repository; 7 | 8 | import java.util.Date; 9 | import java.util.List; 10 | 11 | @Repository 12 | public interface ExpenseRepository extends JpaRepository { 13 | // All expenses for a particular group and a particular user 14 | List findAllByGroup(Group group); 15 | 16 | // List findAllByCreatedAt(Date createdAt); 17 | } 18 | -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/src/main/java/com/scaler/splitwise/repositories/GroupRepository.java: -------------------------------------------------------------------------------- 1 | package com.scaler.splitwise.repositories; 2 | 3 | import com.scaler.splitwise.models.Group; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | import org.springframework.stereotype.Repository; 6 | 7 | @Repository 8 | public interface GroupRepository extends JpaRepository { 9 | } 10 | -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/src/main/java/com/scaler/splitwise/repositories/UserRepository.java: -------------------------------------------------------------------------------- 1 | package com.scaler.splitwise.repositories; 2 | 3 | import com.scaler.splitwise.models.User; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | import org.springframework.stereotype.Repository; 6 | 7 | @Repository 8 | public interface UserRepository extends JpaRepository { 9 | } 10 | -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/src/main/java/com/scaler/splitwise/strategies/HeapSettleUpStrategy.java: -------------------------------------------------------------------------------- 1 | package com.scaler.splitwise.strategies; 2 | 3 | import com.scaler.splitwise.models.Expense; 4 | import org.springframework.beans.factory.annotation.Qualifier; 5 | import org.springframework.stereotype.Component; 6 | 7 | import java.util.List; 8 | 9 | @Component 10 | @Qualifier("Old") 11 | public class HeapSettleUpStrategy implements SettleUpStrategy { 12 | @Override 13 | public List settleUp(List expensesToSettle) { 14 | 15 | // TODO: Implement DSA question. 16 | 17 | // 1. Find all users amount to be paid, or get back. 18 | // 2. Divide users with +ve sum in 1 heap and -ve in other. 19 | // 3. Keep popping out from both the heaps, and create a transaction 20 | // type of expense. 21 | 22 | // A -> 450 B <- 250 23 | // Insert A back with 200 amount to the first heap 24 | // Create an expense A paid 200 B owed 200 and add it to the ans list. 25 | 26 | // 4. Return list of expense when the heaps get empty. 27 | 28 | return null; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/src/main/java/com/scaler/splitwise/strategies/NewSettleUpStrategy.java: -------------------------------------------------------------------------------- 1 | package com.scaler.splitwise.strategies; 2 | 3 | import com.scaler.splitwise.models.Expense; 4 | import org.springframework.beans.factory.annotation.Qualifier; 5 | import org.springframework.stereotype.Component; 6 | 7 | import java.util.List; 8 | 9 | @Component 10 | @Qualifier("GopalQualifier") 11 | public class NewSettleUpStrategy implements SettleUpStrategy{ 12 | @Override 13 | public List settleUp(List expensesToSettle) { 14 | return null; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/src/main/java/com/scaler/splitwise/strategies/SettleUpStrategy.java: -------------------------------------------------------------------------------- 1 | package com.scaler.splitwise.strategies; 2 | 3 | import com.scaler.splitwise.models.Expense; 4 | import org.springframework.stereotype.Component; 5 | 6 | import java.util.List; 7 | 8 | @Component 9 | public interface SettleUpStrategy { 10 | List settleUp(List expensesToSettle); 11 | } 12 | -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.jpa.hibernate.ddl-auto=update 2 | spring.datasource.url=jdbc:mysql://127.0.0.1:3306/splitwise 3 | spring.datasource.username=root 4 | spring.datasource.password=rahul999 5 | spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver 6 | spring.jpa.show-sql=true 7 | logging.level.ROOT=INFO 8 | logging.level.org.springframework.orm.jpa=DEBUG 9 | logging.level.org.springframework.transaction=DEBUG -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/src/test/java/com/scaler/splitwise/SplitwiseApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.scaler.splitwise; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SplitwiseApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/target/classes/application.properties: -------------------------------------------------------------------------------- 1 | spring.jpa.hibernate.ddl-auto=update 2 | spring.datasource.url=jdbc:mysql://127.0.0.1:3306/splitwise 3 | spring.datasource.username=root 4 | spring.datasource.password=rahul999 5 | spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver 6 | spring.jpa.show-sql=true 7 | logging.level.ROOT=INFO 8 | logging.level.org.springframework.orm.jpa=DEBUG 9 | logging.level.org.springframework.transaction=DEBUG -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/target/classes/com/scaler/splitwise/SplitwiseApplication.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahulgrover99/May23-LLD/7b8cd9d98f217c961fde010831d869ea62c52548/code/MachineCoding/Splitwise/target/classes/com/scaler/splitwise/SplitwiseApplication.class -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/target/classes/com/scaler/splitwise/commands/Command.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahulgrover99/May23-LLD/7b8cd9d98f217c961fde010831d869ea62c52548/code/MachineCoding/Splitwise/target/classes/com/scaler/splitwise/commands/Command.class -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/target/classes/com/scaler/splitwise/commands/CommandExecutor.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahulgrover99/May23-LLD/7b8cd9d98f217c961fde010831d869ea62c52548/code/MachineCoding/Splitwise/target/classes/com/scaler/splitwise/commands/CommandExecutor.class -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/target/classes/com/scaler/splitwise/commands/CommandKeywords.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahulgrover99/May23-LLD/7b8cd9d98f217c961fde010831d869ea62c52548/code/MachineCoding/Splitwise/target/classes/com/scaler/splitwise/commands/CommandKeywords.class -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/target/classes/com/scaler/splitwise/commands/SettleUpUserCommand.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahulgrover99/May23-LLD/7b8cd9d98f217c961fde010831d869ea62c52548/code/MachineCoding/Splitwise/target/classes/com/scaler/splitwise/commands/SettleUpUserCommand.class -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/target/classes/com/scaler/splitwise/controllers/SettleUpController.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahulgrover99/May23-LLD/7b8cd9d98f217c961fde010831d869ea62c52548/code/MachineCoding/Splitwise/target/classes/com/scaler/splitwise/controllers/SettleUpController.class -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/target/classes/com/scaler/splitwise/dtos/SettleUpRequest.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahulgrover99/May23-LLD/7b8cd9d98f217c961fde010831d869ea62c52548/code/MachineCoding/Splitwise/target/classes/com/scaler/splitwise/dtos/SettleUpRequest.class -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/target/classes/com/scaler/splitwise/dtos/SettleUpResponse.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahulgrover99/May23-LLD/7b8cd9d98f217c961fde010831d869ea62c52548/code/MachineCoding/Splitwise/target/classes/com/scaler/splitwise/dtos/SettleUpResponse.class -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/target/classes/com/scaler/splitwise/models/BaseModel.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahulgrover99/May23-LLD/7b8cd9d98f217c961fde010831d869ea62c52548/code/MachineCoding/Splitwise/target/classes/com/scaler/splitwise/models/BaseModel.class -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/target/classes/com/scaler/splitwise/models/Expense.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahulgrover99/May23-LLD/7b8cd9d98f217c961fde010831d869ea62c52548/code/MachineCoding/Splitwise/target/classes/com/scaler/splitwise/models/Expense.class -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/target/classes/com/scaler/splitwise/models/ExpenseType.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahulgrover99/May23-LLD/7b8cd9d98f217c961fde010831d869ea62c52548/code/MachineCoding/Splitwise/target/classes/com/scaler/splitwise/models/ExpenseType.class -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/target/classes/com/scaler/splitwise/models/ExpenseUser.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahulgrover99/May23-LLD/7b8cd9d98f217c961fde010831d869ea62c52548/code/MachineCoding/Splitwise/target/classes/com/scaler/splitwise/models/ExpenseUser.class -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/target/classes/com/scaler/splitwise/models/ExpenseUserType.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahulgrover99/May23-LLD/7b8cd9d98f217c961fde010831d869ea62c52548/code/MachineCoding/Splitwise/target/classes/com/scaler/splitwise/models/ExpenseUserType.class -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/target/classes/com/scaler/splitwise/models/Group.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahulgrover99/May23-LLD/7b8cd9d98f217c961fde010831d869ea62c52548/code/MachineCoding/Splitwise/target/classes/com/scaler/splitwise/models/Group.class -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/target/classes/com/scaler/splitwise/models/User.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahulgrover99/May23-LLD/7b8cd9d98f217c961fde010831d869ea62c52548/code/MachineCoding/Splitwise/target/classes/com/scaler/splitwise/models/User.class -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/target/classes/com/scaler/splitwise/repositories/ExpenseRepository.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahulgrover99/May23-LLD/7b8cd9d98f217c961fde010831d869ea62c52548/code/MachineCoding/Splitwise/target/classes/com/scaler/splitwise/repositories/ExpenseRepository.class -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/target/classes/com/scaler/splitwise/repositories/GroupRepository.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahulgrover99/May23-LLD/7b8cd9d98f217c961fde010831d869ea62c52548/code/MachineCoding/Splitwise/target/classes/com/scaler/splitwise/repositories/GroupRepository.class -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/target/classes/com/scaler/splitwise/repositories/UserRepository.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahulgrover99/May23-LLD/7b8cd9d98f217c961fde010831d869ea62c52548/code/MachineCoding/Splitwise/target/classes/com/scaler/splitwise/repositories/UserRepository.class -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/target/classes/com/scaler/splitwise/services/SettleUpService.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahulgrover99/May23-LLD/7b8cd9d98f217c961fde010831d869ea62c52548/code/MachineCoding/Splitwise/target/classes/com/scaler/splitwise/services/SettleUpService.class -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/target/classes/com/scaler/splitwise/strategies/HeapSettleUpStrategy.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahulgrover99/May23-LLD/7b8cd9d98f217c961fde010831d869ea62c52548/code/MachineCoding/Splitwise/target/classes/com/scaler/splitwise/strategies/HeapSettleUpStrategy.class -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/target/classes/com/scaler/splitwise/strategies/NewSettleUpStrategy.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahulgrover99/May23-LLD/7b8cd9d98f217c961fde010831d869ea62c52548/code/MachineCoding/Splitwise/target/classes/com/scaler/splitwise/strategies/NewSettleUpStrategy.class -------------------------------------------------------------------------------- /code/MachineCoding/Splitwise/target/classes/com/scaler/splitwise/strategies/SettleUpStrategy.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahulgrover99/May23-LLD/7b8cd9d98f217c961fde010831d869ea62c52548/code/MachineCoding/Splitwise/target/classes/com/scaler/splitwise/strategies/SettleUpStrategy.class -------------------------------------------------------------------------------- /code/MachineCoding/book-my-show.md: -------------------------------------------------------------------------------- 1 | ## Prerequisites 2 | - Have mysql server and workbench installed in your machines. 3 | For reference, [here for mac](https://jojozhuang.github.io/programming/installing-mysql-and-workbench-on-mac/). 4 | - Helper for connecting mysql to spring, [here](https://spring.io/guides/gs/accessing-data-mysql/). 5 | - Hands on starting spring boot application: https://www.scaler.com/topics/spring-boot/spring-boot-application/ 6 | 7 | ## Resources 8 | - https://start.spring.io/ 9 | - https://www.baeldung.com/spring-vs-spring-boot 10 | - https://www.scaler.com/topics/what-is-rest-api/ 11 | - https://www.scaler.com/topics/spring-boot/prerequisites-for-spring-boot/ 12 | - https://www.baeldung.com/maven 13 | - https://www.baeldung.com/ant-maven-gradle 14 | - https://spring.io/guides/gs/accessing-data-mysql/ 15 | - https://shethradhika9.medium.com/roadtowebdev-day-10-understanding-orm-and-using-hibernate-to-interact-with-database-10b1a59e2002 16 | - https://stackoverflow.com/questions/9881611/whats-the-difference-between-jpa-and-hibernate -------------------------------------------------------------------------------- /code/MachineCoding/bookmyshow/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | !**/src/main/**/target/ 5 | !**/src/test/**/target/ 6 | 7 | ### STS ### 8 | .apt_generated 9 | .classpath 10 | .factorypath 11 | .project 12 | .settings 13 | .springBeans 14 | .sts4-cache 15 | 16 | ### IntelliJ IDEA ### 17 | .idea 18 | *.iws 19 | *.iml 20 | *.ipr 21 | 22 | ### NetBeans ### 23 | /nbproject/private/ 24 | /nbbuild/ 25 | /dist/ 26 | /nbdist/ 27 | /.nb-gradle/ 28 | build/ 29 | !**/src/main/**/build/ 30 | !**/src/test/**/build/ 31 | 32 | ### VS Code ### 33 | .vscode/ 34 | -------------------------------------------------------------------------------- /code/MachineCoding/bookmyshow/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahulgrover99/May23-LLD/7b8cd9d98f217c961fde010831d869ea62c52548/code/MachineCoding/bookmyshow/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /code/MachineCoding/bookmyshow/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.4/apache-maven-3.9.4-bin.zip 2 | wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar 3 | -------------------------------------------------------------------------------- /code/MachineCoding/bookmyshow/src/main/java/com/example/bookmyshow/BookmyshowApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.bookmyshow; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.data.jpa.repository.config.EnableJpaAuditing; 6 | 7 | @SpringBootApplication 8 | @EnableJpaAuditing 9 | public class BookmyshowApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(BookmyshowApplication.class, args); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /code/MachineCoding/bookmyshow/src/main/java/com/example/bookmyshow/controllers/BookingController.java: -------------------------------------------------------------------------------- 1 | package com.example.bookmyshow.controllers; 2 | 3 | import com.example.bookmyshow.dtos.CreateBookingRequest; 4 | import com.example.bookmyshow.dtos.CreateBookingResponse; 5 | import com.example.bookmyshow.models.Booking; 6 | import com.example.bookmyshow.services.BookingService; 7 | import lombok.AllArgsConstructor; 8 | import org.springframework.web.bind.annotation.PostMapping; 9 | import org.springframework.web.bind.annotation.RequestBody; 10 | import org.springframework.web.bind.annotation.RequestMapping; 11 | import org.springframework.web.bind.annotation.RestController; 12 | 13 | @RestController 14 | @RequestMapping(path = "/bms/v1") 15 | @AllArgsConstructor 16 | public class BookingController { 17 | 18 | private BookingService bookingService; 19 | 20 | @PostMapping(path = "/bookings") 21 | public CreateBookingResponse createBooking(@RequestBody CreateBookingRequest request) { 22 | // Add validations to ensure all the fields are present in the request. 23 | System.out.println(request.toString()); 24 | Booking booking = bookingService.createBooking( 25 | request.getUserId(), request.getShowId(), request.getSeatIds()); 26 | 27 | return new CreateBookingResponse("SUCCESS"); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /code/MachineCoding/bookmyshow/src/main/java/com/example/bookmyshow/dtos/CreateBookingRequest.java: -------------------------------------------------------------------------------- 1 | package com.example.bookmyshow.dtos; 2 | 3 | import lombok.Data; 4 | 5 | import java.util.List; 6 | 7 | @Data 8 | public class CreateBookingRequest { 9 | private Long userId; 10 | private Long showId; 11 | private List seatIds; 12 | } 13 | -------------------------------------------------------------------------------- /code/MachineCoding/bookmyshow/src/main/java/com/example/bookmyshow/dtos/CreateBookingResponse.java: -------------------------------------------------------------------------------- 1 | package com.example.bookmyshow.dtos; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | 6 | @Data 7 | @AllArgsConstructor 8 | public class CreateBookingResponse { 9 | private String response; 10 | } 11 | -------------------------------------------------------------------------------- /code/MachineCoding/bookmyshow/src/main/java/com/example/bookmyshow/dtos/CreateCustomerRequest.java: -------------------------------------------------------------------------------- 1 | package com.example.bookmyshow.dtos; 2 | 3 | import lombok.Data; 4 | 5 | @Data 6 | public class CreateCustomerRequest { 7 | private String name; 8 | private String email; 9 | private String password; 10 | } 11 | -------------------------------------------------------------------------------- /code/MachineCoding/bookmyshow/src/main/java/com/example/bookmyshow/dtos/CreateCustomerResponse.java: -------------------------------------------------------------------------------- 1 | package com.example.bookmyshow.dtos; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | 6 | @Data 7 | @AllArgsConstructor 8 | public class CreateCustomerResponse { 9 | private String message; 10 | private Long id; 11 | } 12 | -------------------------------------------------------------------------------- /code/MachineCoding/bookmyshow/src/main/java/com/example/bookmyshow/models/BaseModel.java: -------------------------------------------------------------------------------- 1 | package com.example.bookmyshow.models; 2 | 3 | import jakarta.persistence.*; 4 | import lombok.Data; 5 | import org.springframework.data.annotation.CreatedDate; 6 | import org.springframework.data.annotation.LastModifiedDate; 7 | import org.springframework.data.jpa.domain.support.AuditingEntityListener; 8 | 9 | import java.util.Date; 10 | 11 | @MappedSuperclass 12 | @Data 13 | @EntityListeners(AuditingEntityListener.class) 14 | public class BaseModel { 15 | @Id 16 | @GeneratedValue 17 | private Long id; 18 | 19 | @CreatedDate 20 | @Temporal(value = TemporalType.TIMESTAMP) 21 | private Date createdAt; 22 | 23 | @LastModifiedDate 24 | @Temporal(value = TemporalType.TIMESTAMP) 25 | private Date lastModifiedAt; 26 | } 27 | -------------------------------------------------------------------------------- /code/MachineCoding/bookmyshow/src/main/java/com/example/bookmyshow/models/Booking.java: -------------------------------------------------------------------------------- 1 | package com.example.bookmyshow.models; 2 | 3 | import jakarta.annotation.Nullable; 4 | import jakarta.persistence.*; 5 | import lombok.Getter; 6 | import lombok.Setter; 7 | 8 | import java.util.Date; 9 | import java.util.List; 10 | 11 | @Getter 12 | @Setter 13 | @Entity 14 | public class Booking extends BaseModel{ 15 | @ManyToOne 16 | private Customer customer; 17 | @OneToMany 18 | private List showSeats; 19 | @OneToOne 20 | private MovieShow movieShow; 21 | @Column(nullable = true) 22 | private double amount; 23 | } 24 | -------------------------------------------------------------------------------- /code/MachineCoding/bookmyshow/src/main/java/com/example/bookmyshow/models/BookingSeatStatus.java: -------------------------------------------------------------------------------- 1 | package com.example.bookmyshow.models; 2 | 3 | public enum BookingSeatStatus { 4 | BOOKED, 5 | RESERVED, 6 | OUT_OF_ORDER, 7 | AVAILABLE 8 | } 9 | -------------------------------------------------------------------------------- /code/MachineCoding/bookmyshow/src/main/java/com/example/bookmyshow/models/City.java: -------------------------------------------------------------------------------- 1 | package com.example.bookmyshow.models; 2 | 3 | import jakarta.persistence.Entity; 4 | import jakarta.persistence.OneToMany; 5 | import lombok.Getter; 6 | import lombok.Setter; 7 | 8 | import java.util.List; 9 | 10 | @Getter 11 | @Setter 12 | @Entity 13 | public class City extends BaseModel { 14 | private String name; 15 | @OneToMany 16 | private List theatres; 17 | } 18 | -------------------------------------------------------------------------------- /code/MachineCoding/bookmyshow/src/main/java/com/example/bookmyshow/models/Customer.java: -------------------------------------------------------------------------------- 1 | package com.example.bookmyshow.models; 2 | 3 | import jakarta.persistence.Entity; 4 | import jakarta.persistence.OneToMany; 5 | import lombok.*; 6 | 7 | import java.util.List; 8 | 9 | @Data 10 | @Entity 11 | public class Customer extends BaseModel { 12 | private String name; 13 | private String email; 14 | 15 | @OneToMany 16 | private List bookings; 17 | } 18 | -------------------------------------------------------------------------------- /code/MachineCoding/bookmyshow/src/main/java/com/example/bookmyshow/models/Hall.java: -------------------------------------------------------------------------------- 1 | package com.example.bookmyshow.models; 2 | 3 | import jakarta.persistence.Entity; 4 | import jakarta.persistence.OneToMany; 5 | import lombok.Getter; 6 | import lombok.Setter; 7 | 8 | import java.util.List; 9 | 10 | @Setter 11 | @Getter 12 | @Entity 13 | public class Hall extends BaseModel{ 14 | private String name; 15 | @OneToMany 16 | private List seats; 17 | private ScreenType screenType; 18 | } 19 | -------------------------------------------------------------------------------- /code/MachineCoding/bookmyshow/src/main/java/com/example/bookmyshow/models/Movie.java: -------------------------------------------------------------------------------- 1 | package com.example.bookmyshow.models; 2 | 3 | import jakarta.persistence.Entity; 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | 7 | import java.util.Date; 8 | 9 | @Getter 10 | @Setter 11 | @Entity 12 | public class Movie extends BaseModel { 13 | private String description; 14 | private String name; 15 | private String language; 16 | private Date releaseDate; 17 | } 18 | -------------------------------------------------------------------------------- /code/MachineCoding/bookmyshow/src/main/java/com/example/bookmyshow/models/MovieShow.java: -------------------------------------------------------------------------------- 1 | package com.example.bookmyshow.models; 2 | 3 | import jakarta.persistence.Entity; 4 | import jakarta.persistence.ManyToOne; 5 | import lombok.Getter; 6 | import lombok.Setter; 7 | 8 | import java.util.Date; 9 | 10 | @Getter 11 | @Setter 12 | @Entity 13 | public class MovieShow extends BaseModel{ 14 | @ManyToOne 15 | private Movie movie; 16 | private Date startTime; 17 | private int durationMinutes; 18 | @ManyToOne 19 | private Hall hall; 20 | 21 | } 22 | -------------------------------------------------------------------------------- /code/MachineCoding/bookmyshow/src/main/java/com/example/bookmyshow/models/MovieShowSeat.java: -------------------------------------------------------------------------------- 1 | package com.example.bookmyshow.models; 2 | 3 | import jakarta.persistence.Entity; 4 | import jakarta.persistence.ManyToOne; 5 | import lombok.Getter; 6 | import lombok.Setter; 7 | 8 | @Setter 9 | @Getter 10 | @Entity 11 | public class MovieShowSeat extends BaseModel { 12 | @ManyToOne 13 | private MovieShow movieShow; 14 | @ManyToOne 15 | private Seat seat; 16 | private BookingSeatStatus status; 17 | } 18 | -------------------------------------------------------------------------------- /code/MachineCoding/bookmyshow/src/main/java/com/example/bookmyshow/models/ScreenType.java: -------------------------------------------------------------------------------- 1 | package com.example.bookmyshow.models; 2 | 3 | public enum ScreenType { 4 | IMAX, 5 | TWO_D, 6 | THREE_D, 7 | DOLBY 8 | 9 | } 10 | -------------------------------------------------------------------------------- /code/MachineCoding/bookmyshow/src/main/java/com/example/bookmyshow/models/Seat.java: -------------------------------------------------------------------------------- 1 | package com.example.bookmyshow.models; 2 | 3 | import jakarta.persistence.Entity; 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | 7 | @Setter 8 | @Getter 9 | @Entity 10 | public class Seat extends BaseModel { 11 | private int rowNum; 12 | private int colNum; 13 | private SeatType seatType; 14 | 15 | } 16 | -------------------------------------------------------------------------------- /code/MachineCoding/bookmyshow/src/main/java/com/example/bookmyshow/models/SeatType.java: -------------------------------------------------------------------------------- 1 | package com.example.bookmyshow.models; 2 | 3 | public enum SeatType { 4 | SILVER, 5 | GOLD, 6 | PLATINUM, 7 | } 8 | -------------------------------------------------------------------------------- /code/MachineCoding/bookmyshow/src/main/java/com/example/bookmyshow/models/Theatre.java: -------------------------------------------------------------------------------- 1 | package com.example.bookmyshow.models; 2 | 3 | import jakarta.persistence.Entity; 4 | import jakarta.persistence.ManyToOne; 5 | import jakarta.persistence.OneToMany; 6 | import lombok.Getter; 7 | import lombok.Setter; 8 | 9 | import java.util.List; 10 | 11 | @Getter 12 | @Setter 13 | @Entity 14 | public class Theatre extends BaseModel{ 15 | private String name; 16 | private String address; 17 | @OneToMany 18 | private List halls; 19 | } 20 | -------------------------------------------------------------------------------- /code/MachineCoding/bookmyshow/src/main/java/com/example/bookmyshow/repositories/BookingRepository.java: -------------------------------------------------------------------------------- 1 | package com.example.bookmyshow.repositories; 2 | 3 | import com.example.bookmyshow.models.Booking; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | import org.springframework.stereotype.Repository; 6 | 7 | import java.util.Optional; 8 | 9 | @Repository 10 | public interface BookingRepository extends JpaRepository { 11 | 12 | @Override 13 | Optional findById(Long id); 14 | 15 | @Override 16 | Booking save(Booking booking); 17 | 18 | } 19 | -------------------------------------------------------------------------------- /code/MachineCoding/bookmyshow/src/main/java/com/example/bookmyshow/repositories/CustomerRepository.java: -------------------------------------------------------------------------------- 1 | package com.example.bookmyshow.repositories; 2 | 3 | import com.example.bookmyshow.models.Customer; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | import org.springframework.stereotype.Repository; 6 | 7 | import java.util.Optional; 8 | 9 | @Repository 10 | public interface CustomerRepository extends JpaRepository { 11 | 12 | @Override 13 | Optional findById(Long aLong); 14 | 15 | Optional findCustomerByEmail(String email); 16 | 17 | @Override 18 | Customer save(Customer customer); 19 | } 20 | -------------------------------------------------------------------------------- /code/MachineCoding/bookmyshow/src/main/java/com/example/bookmyshow/repositories/MovieShowRepository.java: -------------------------------------------------------------------------------- 1 | package com.example.bookmyshow.repositories; 2 | 3 | import com.example.bookmyshow.models.MovieShow; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | import java.util.Optional; 7 | 8 | public interface MovieShowRepository extends JpaRepository { 9 | 10 | @Override 11 | Optional findById(Long aLong); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /code/MachineCoding/bookmyshow/src/main/java/com/example/bookmyshow/repositories/MovieShowSeatRepository.java: -------------------------------------------------------------------------------- 1 | package com.example.bookmyshow.repositories; 2 | 3 | import com.example.bookmyshow.models.MovieShowSeat; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | import org.springframework.stereotype.Repository; 6 | 7 | @Repository 8 | public interface MovieShowSeatRepository extends JpaRepository { 9 | } 10 | -------------------------------------------------------------------------------- /code/MachineCoding/bookmyshow/src/main/java/com/example/bookmyshow/services/CustomerService.java: -------------------------------------------------------------------------------- 1 | package com.example.bookmyshow.services; 2 | 3 | import com.example.bookmyshow.models.Customer; 4 | import com.example.bookmyshow.repositories.CustomerRepository; 5 | import lombok.AllArgsConstructor; 6 | import org.springframework.stereotype.Component; 7 | import org.springframework.stereotype.Service; 8 | 9 | import java.util.ArrayList; 10 | 11 | @Service 12 | @AllArgsConstructor 13 | public class CustomerService { 14 | private CustomerRepository customerRepository; 15 | 16 | public Customer signUp(String name, String email, String password) { 17 | System.out.println("Service layer called for creating customer."); 18 | 19 | // BCryptPasswordEncoder() 20 | 21 | 22 | Customer customer = new Customer(); 23 | customer.setName(name); 24 | customer.setEmail(email); 25 | customer.setBookings(new ArrayList<>()); 26 | 27 | // Call the repository layer and try to save that to DB. 28 | customerRepository.save(customer); 29 | 30 | return customer; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /code/MachineCoding/bookmyshow/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.jpa.hibernate.ddl-auto=update 2 | spring.datasource.url=jdbc:mysql://127.0.0.1:3306/bookmyshow 3 | spring.datasource.username=root 4 | spring.datasource.password=rahul999 5 | spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver 6 | spring.jpa.show-sql=true 7 | logging.level.ROOT=INFO 8 | logging.level.org.springframework.orm.jpa=DEBUG 9 | logging.level.org.springframework.transaction=DEBUG 10 | -------------------------------------------------------------------------------- /code/MachineCoding/bookmyshow/src/test/java/com/example/bookmyshow/BookmyshowApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.bookmyshow; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class BookmyshowApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /code/MachineCoding/parkinglot/.gitignore: -------------------------------------------------------------------------------- 1 | ### IntelliJ IDEA ### 2 | out/ 3 | !**/src/main/**/out/ 4 | !**/src/test/**/out/ 5 | 6 | ### Eclipse ### 7 | .apt_generated 8 | .classpath 9 | .factorypath 10 | .project 11 | .settings 12 | .springBeans 13 | .sts4-cache 14 | bin/ 15 | !**/src/main/**/bin/ 16 | !**/src/test/**/bin/ 17 | 18 | ### NetBeans ### 19 | /nbproject/private/ 20 | /nbbuild/ 21 | /dist/ 22 | /nbdist/ 23 | /.nb-gradle/ 24 | 25 | ### VS Code ### 26 | .vscode/ 27 | 28 | ### Mac OS ### 29 | .DS_Store -------------------------------------------------------------------------------- /code/MachineCoding/parkinglot/src/controllers/TicketController.java: -------------------------------------------------------------------------------- 1 | package controllers; 2 | 3 | import dto.GenerateTicketRequest; 4 | import dto.GenerateTicketResponse; 5 | import exceptions.GateNotFoundException; 6 | import exceptions.NoParkingSpotsAvailableException; 7 | import models.Ticket; 8 | import services.EntryGateService; 9 | 10 | /** 11 | * Roles of a controller: 12 | * 1. To perform validations. 13 | * 2. Call the correct order of services with appropriate params 14 | * 3. Render the response object. 15 | */ 16 | public class TicketController { 17 | 18 | private EntryGateService entryGateService; 19 | 20 | public TicketController(EntryGateService entryGateService) { 21 | this.entryGateService = entryGateService; 22 | } 23 | 24 | public GenerateTicketResponse generateTicket( 25 | GenerateTicketRequest request) throws NoParkingSpotsAvailableException { 26 | 27 | // Validation checks 28 | if (request.getGate() == null) { 29 | throw new GateNotFoundException(); 30 | } 31 | 32 | Ticket ticket = entryGateService.createTicket(request.getGate(), request.getVehicle()); 33 | 34 | // Rendering the response 35 | return GenerateTicketResponse.builder().ticket(ticket).build(); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /code/MachineCoding/parkinglot/src/dto/GenerateTicketRequest.java: -------------------------------------------------------------------------------- 1 | package dto; 2 | 3 | import lombok.Builder; 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | import models.Gate; 7 | import models.Vehicle; 8 | 9 | @Getter 10 | @Setter 11 | @Builder 12 | public class GenerateTicketRequest { 13 | private Vehicle vehicle; 14 | private Gate gate; 15 | } 16 | -------------------------------------------------------------------------------- /code/MachineCoding/parkinglot/src/dto/GenerateTicketResponse.java: -------------------------------------------------------------------------------- 1 | package dto; 2 | 3 | import lombok.Builder; 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | import models.Ticket; 7 | 8 | @Setter 9 | @Getter 10 | @Builder 11 | public class GenerateTicketResponse { 12 | private Ticket ticket; 13 | } 14 | -------------------------------------------------------------------------------- /code/MachineCoding/parkinglot/src/exceptions/GateNotFoundException.java: -------------------------------------------------------------------------------- 1 | package exceptions; 2 | 3 | public class GateNotFoundException extends RuntimeException { 4 | public GateNotFoundException(String message) { 5 | super(message); 6 | } 7 | 8 | public GateNotFoundException() { 9 | super("The gate not found."); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /code/MachineCoding/parkinglot/src/exceptions/NoParkingSpotsAvailableException.java: -------------------------------------------------------------------------------- 1 | package exceptions; 2 | 3 | public class NoParkingSpotsAvailableException extends Exception { 4 | public NoParkingSpotsAvailableException() { 5 | super("No parking spots left in this parking lot."); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /code/MachineCoding/parkinglot/src/models/Gate.java: -------------------------------------------------------------------------------- 1 | package models; 2 | 3 | import lombok.Builder; 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | 7 | @Getter 8 | @Setter 9 | @Builder 10 | public class Gate { 11 | int gateNumber; 12 | ParkingAttendant parkingAttendant; 13 | GateType gateType; 14 | } 15 | -------------------------------------------------------------------------------- /code/MachineCoding/parkinglot/src/models/GateType.java: -------------------------------------------------------------------------------- 1 | package models; 2 | 3 | public enum GateType { 4 | ENTRY, 5 | EXIT 6 | } 7 | -------------------------------------------------------------------------------- /code/MachineCoding/parkinglot/src/models/Invoice.java: -------------------------------------------------------------------------------- 1 | package models; 2 | 3 | import lombok.Getter; 4 | import lombok.Setter; 5 | 6 | import java.util.Date; 7 | 8 | @Getter 9 | @Setter 10 | public class Invoice { 11 | private int invoiceId; 12 | private Date exitTime; 13 | private Ticket ticket; 14 | private Payment payment; 15 | private Gate exitGate; 16 | private ParkingAttendant parkingAttendant; 17 | } 18 | -------------------------------------------------------------------------------- /code/MachineCoding/parkinglot/src/models/ParkingAttendant.java: -------------------------------------------------------------------------------- 1 | package models; 2 | 3 | import lombok.Builder; 4 | import lombok.Data; 5 | import lombok.Getter; 6 | import lombok.Setter; 7 | 8 | @Data 9 | @Builder 10 | public class ParkingAttendant { 11 | private String name; 12 | private String email; 13 | } 14 | -------------------------------------------------------------------------------- /code/MachineCoding/parkinglot/src/models/ParkingFloor.java: -------------------------------------------------------------------------------- 1 | package models; 2 | 3 | import lombok.Getter; 4 | import lombok.Setter; 5 | 6 | import java.util.List; 7 | 8 | @Setter 9 | @Getter 10 | public class ParkingFloor { 11 | private int floorNumber; 12 | private List parkingSpots; 13 | private double ceilingHeight; 14 | } 15 | -------------------------------------------------------------------------------- /code/MachineCoding/parkinglot/src/models/ParkingLot.java: -------------------------------------------------------------------------------- 1 | package models; 2 | 3 | import lombok.Builder; 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | 7 | import java.util.List; 8 | 9 | @Getter 10 | @Setter 11 | @Builder 12 | public class ParkingLot { 13 | private List parkingFloors; 14 | private List entryGates; 15 | private List exitGates; 16 | private String name; 17 | private String address; 18 | } 19 | -------------------------------------------------------------------------------- /code/MachineCoding/parkinglot/src/models/ParkingSpot.java: -------------------------------------------------------------------------------- 1 | package models; 2 | 3 | import lombok.Builder; 4 | import lombok.Data; 5 | import lombok.Getter; 6 | import lombok.Setter; 7 | 8 | @Data 9 | @Builder 10 | public class ParkingSpot { 11 | private int spotNumber; 12 | private VehicleType supportedVehicleType; 13 | private SpotStatus spotStatus; 14 | private int floorNumber; 15 | private Vehicle vehicle; 16 | } 17 | -------------------------------------------------------------------------------- /code/MachineCoding/parkinglot/src/models/Payment.java: -------------------------------------------------------------------------------- 1 | package models; 2 | 3 | import lombok.Getter; 4 | import lombok.Setter; 5 | 6 | import java.util.Date; 7 | 8 | @Setter 9 | @Getter 10 | public class Payment { 11 | private String reference; 12 | private Double amount; 13 | private PaymentMode paymentMode; 14 | private PaymentStatus paymentStatus; 15 | private Date time; 16 | private Ticket ticket; 17 | } 18 | -------------------------------------------------------------------------------- /code/MachineCoding/parkinglot/src/models/PaymentMode.java: -------------------------------------------------------------------------------- 1 | package models; 2 | 3 | public enum PaymentMode { 4 | DEBIT, 5 | CREDIT, 6 | UPI, 7 | CASH, 8 | } 9 | -------------------------------------------------------------------------------- /code/MachineCoding/parkinglot/src/models/PaymentStatus.java: -------------------------------------------------------------------------------- 1 | package models; 2 | 3 | public enum PaymentStatus { 4 | WAITING, 5 | FAILED, 6 | SUCCESS, 7 | REFUNDED 8 | } 9 | -------------------------------------------------------------------------------- /code/MachineCoding/parkinglot/src/models/SpotStatus.java: -------------------------------------------------------------------------------- 1 | package models; 2 | 3 | public enum SpotStatus { 4 | AVAILABLE, 5 | BLOCKED, 6 | RESERVED 7 | } 8 | -------------------------------------------------------------------------------- /code/MachineCoding/parkinglot/src/models/Ticket.java: -------------------------------------------------------------------------------- 1 | package models; 2 | 3 | import lombok.Builder; 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | import lombok.ToString; 7 | 8 | import java.util.Date; 9 | 10 | @Getter 11 | @Setter 12 | @Builder 13 | @ToString 14 | public class Ticket { 15 | private String id; 16 | private Date entryTime; 17 | private ParkingSpot parkingSpot; 18 | private Gate issuingGate; 19 | private ParkingAttendant parkingAttendant; 20 | private Vehicle vehicle; 21 | } 22 | -------------------------------------------------------------------------------- /code/MachineCoding/parkinglot/src/models/Vehicle.java: -------------------------------------------------------------------------------- 1 | package models; 2 | 3 | import lombok.Builder; 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | 7 | @Getter 8 | @Setter 9 | @Builder 10 | public class Vehicle { 11 | private String regNumber; 12 | private VehicleType vehicleType; 13 | } 14 | -------------------------------------------------------------------------------- /code/MachineCoding/parkinglot/src/models/VehicleType.java: -------------------------------------------------------------------------------- 1 | package models; 2 | 3 | public enum VehicleType { 4 | CAR, 5 | BIKE, 6 | SUV 7 | } 8 | -------------------------------------------------------------------------------- /code/MachineCoding/parkinglot/src/repositories/TicketRepository.java: -------------------------------------------------------------------------------- 1 | package repositories; 2 | 3 | import models.Ticket; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | 8 | public class TicketRepository { 9 | private List tickets = new ArrayList<>(); 10 | 11 | public void save(Ticket ticket) { 12 | tickets.add(ticket); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /code/MachineCoding/parkinglot/src/services/EntryGateService.java: -------------------------------------------------------------------------------- 1 | package services; 2 | 3 | import exceptions.NoParkingSpotsAvailableException; 4 | import models.Gate; 5 | import models.ParkingSpot; 6 | import models.Ticket; 7 | import models.Vehicle; 8 | 9 | public class EntryGateService { 10 | SlotAllocationService slotAllocationService = new SlotAllocationService(); 11 | TicketService ticketService = new TicketService(); 12 | 13 | 14 | public Ticket createTicket(Gate gate, Vehicle vehicle) 15 | throws NoParkingSpotsAvailableException { 16 | 17 | // Step 1: Find an available spot for the vehicle. 18 | ParkingSpot availableParkingSpot = 19 | slotAllocationService.findSpot(vehicle.getVehicleType()); 20 | 21 | if (availableParkingSpot == null) { 22 | throw new NoParkingSpotsAvailableException(); 23 | } 24 | 25 | Ticket ticket = ticketService.createTicket(vehicle, availableParkingSpot, gate); 26 | slotAllocationService.allocateSpot(availableParkingSpot); 27 | 28 | return ticket; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /code/MachineCoding/parkinglot/src/services/SlotAllocationService.java: -------------------------------------------------------------------------------- 1 | package services; 2 | 3 | import models.ParkingSpot; 4 | import models.VehicleType; 5 | import repositories.ParkingSpotRepository; 6 | 7 | public class SlotAllocationService { 8 | private ParkingSpotRepository parkingSpotRepository = new ParkingSpotRepository(); 9 | 10 | public ParkingSpot findSpot(VehicleType vehicleType) { 11 | return parkingSpotRepository.findAvailableSlot(vehicleType); 12 | } 13 | 14 | public void allocateSpot(ParkingSpot parkingSpot) { 15 | parkingSpotRepository.useParkingSpot(parkingSpot); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /code/MachineCoding/parkinglot/src/services/TicketService.java: -------------------------------------------------------------------------------- 1 | package services; 2 | 3 | import models.Gate; 4 | import models.ParkingSpot; 5 | import models.Ticket; 6 | import models.Vehicle; 7 | import repositories.TicketRepository; 8 | 9 | import java.util.Date; 10 | 11 | public class TicketService { 12 | private final TicketRepository ticketRepository = new TicketRepository(); 13 | 14 | public Ticket createTicket(Vehicle vehicle, ParkingSpot spot, Gate gate) { 15 | 16 | Ticket ticket = Ticket 17 | .builder() 18 | .id(String.valueOf(Math.random()*100)) 19 | .entryTime(new Date()) 20 | .issuingGate(gate) 21 | .parkingAttendant(gate.getParkingAttendant()) 22 | .vehicle(vehicle) 23 | .parkingSpot(spot) 24 | .build(); 25 | 26 | ticketRepository.save(ticket); 27 | 28 | return ticket; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /code/MachineCoding/parkinglot/src/strategies/CostCalculatingStrategy.java: -------------------------------------------------------------------------------- 1 | package strategies; 2 | 3 | public interface CostCalculatingStrategy { 4 | Double getCost(); 5 | } 6 | -------------------------------------------------------------------------------- /code/MachineCoding/parkinglot/src/strategies/ParkingSpotFindingStrategy.java: -------------------------------------------------------------------------------- 1 | package strategies; 2 | 3 | import models.ParkingSpot; 4 | 5 | public interface ParkingSpotFindingStrategy { 6 | ParkingSpot getParkingSpot(); 7 | } 8 | -------------------------------------------------------------------------------- /code/MachineCoding/springdemo/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | !**/src/main/**/target/ 5 | !**/src/test/**/target/ 6 | 7 | ### STS ### 8 | .apt_generated 9 | .classpath 10 | .factorypath 11 | .project 12 | .settings 13 | .springBeans 14 | .sts4-cache 15 | 16 | ### IntelliJ IDEA ### 17 | .idea 18 | *.iws 19 | *.iml 20 | *.ipr 21 | 22 | ### NetBeans ### 23 | /nbproject/private/ 24 | /nbbuild/ 25 | /dist/ 26 | /nbdist/ 27 | /.nb-gradle/ 28 | build/ 29 | !**/src/main/**/build/ 30 | !**/src/test/**/build/ 31 | 32 | ### VS Code ### 33 | .vscode/ 34 | -------------------------------------------------------------------------------- /code/MachineCoding/springdemo/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahulgrover99/May23-LLD/7b8cd9d98f217c961fde010831d869ea62c52548/code/MachineCoding/springdemo/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /code/MachineCoding/springdemo/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.3/apache-maven-3.9.3-bin.zip 2 | wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar 3 | -------------------------------------------------------------------------------- /code/MachineCoding/springdemo/src/main/java/com/scaler/springdemo/SpringdemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.scaler.springdemo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringdemoApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringdemoApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /code/MachineCoding/springdemo/src/main/java/com/scaler/springdemo/models/BaseModel.java: -------------------------------------------------------------------------------- 1 | package com.scaler.springdemo.models; 2 | 3 | import jakarta.persistence.*; 4 | import lombok.AllArgsConstructor; 5 | import lombok.Getter; 6 | 7 | import java.util.Date; 8 | 9 | @Getter 10 | @MappedSuperclass 11 | public class BaseModel { 12 | 13 | @Id 14 | @GeneratedValue(strategy = GenerationType.AUTO) 15 | private Long id; 16 | 17 | private Date createdAt; 18 | 19 | private Date updatedAt; 20 | } 21 | -------------------------------------------------------------------------------- /code/MachineCoding/springdemo/src/main/java/com/scaler/springdemo/models/City.java: -------------------------------------------------------------------------------- 1 | package com.scaler.springdemo.models; 2 | 3 | import lombok.Getter; 4 | 5 | import java.util.Date; 6 | 7 | @Getter 8 | public class City{ 9 | 10 | private String name; 11 | 12 | public City(Long id, Date createdAt, Date updatedAt, String name) { 13 | this.name = name; 14 | } 15 | } -------------------------------------------------------------------------------- /code/MachineCoding/springdemo/src/main/java/com/scaler/springdemo/models/SpringUser.java: -------------------------------------------------------------------------------- 1 | package com.scaler.springdemo.models; 2 | 3 | import jakarta.persistence.Entity; 4 | import jakarta.persistence.GeneratedValue; 5 | import jakarta.persistence.Id; 6 | import lombok.Getter; 7 | import lombok.Setter; 8 | 9 | @Getter 10 | @Setter 11 | @Entity 12 | public class SpringUser extends BaseModel { 13 | private String name; 14 | } 15 | -------------------------------------------------------------------------------- /code/MachineCoding/springdemo/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.jpa.hibernate.ddl-auto=update 2 | spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springtest 3 | spring.datasource.username=root 4 | spring.datasource.password=rahul999 5 | spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver 6 | spring.jpa.show-sql: true 7 | -------------------------------------------------------------------------------- /code/MachineCoding/springdemo/src/test/java/com/scaler/springdemo/SpringdemoApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.scaler.springdemo; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringdemoApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /code/MachineCoding/tictactoe/.gitignore: -------------------------------------------------------------------------------- 1 | ### IntelliJ IDEA ### 2 | out/ 3 | !**/src/main/**/out/ 4 | !**/src/test/**/out/ 5 | 6 | ### Eclipse ### 7 | .apt_generated 8 | .classpath 9 | .factorypath 10 | .project 11 | .settings 12 | .springBeans 13 | .sts4-cache 14 | bin/ 15 | !**/src/main/**/bin/ 16 | !**/src/test/**/bin/ 17 | 18 | ### NetBeans ### 19 | /nbproject/private/ 20 | /nbbuild/ 21 | /dist/ 22 | /nbdist/ 23 | /.nb-gradle/ 24 | 25 | ### VS Code ### 26 | .vscode/ 27 | 28 | ### Mac OS ### 29 | .DS_Store -------------------------------------------------------------------------------- /code/MachineCoding/tictactoe/src/controllers/GameController.java: -------------------------------------------------------------------------------- 1 | package controllers; 2 | 3 | import models.Game; 4 | import models.GameState; 5 | import models.Player; 6 | import services.BoardService; 7 | import services.GameService; 8 | import strategies.winningstrategies.ColWinningStrategy; 9 | import strategies.winningstrategies.RowWinningStrategy; 10 | 11 | import java.util.List; 12 | 13 | 14 | // Simple interface to interact with Game for the client. 15 | public class GameController { 16 | 17 | public static Game createGame(int dimension, List playerList) { 18 | return Game.getBuilder().setPlayers(playerList) 19 | .addWinningStrategy(new ColWinningStrategy()) 20 | .addWinningStrategy(new RowWinningStrategy()) 21 | .setDimension(dimension) 22 | .build(); 23 | } 24 | 25 | public void undo() { 26 | } 27 | 28 | public void displayBoard(Game game) { 29 | BoardService.display(game.getBoard()); 30 | } 31 | 32 | public GameState getGameStatus(Game game) { 33 | return game.getGameState(); 34 | } 35 | 36 | public void executeNextMove(Game game) { 37 | GameService gameService = new GameService(game); 38 | gameService.executeNextMove(); 39 | } 40 | 41 | public String getWinner(Game game) { 42 | return game.getWinner().getName(); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /code/MachineCoding/tictactoe/src/models/Board.java: -------------------------------------------------------------------------------- 1 | package models; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class Board { 7 | private List> board; 8 | 9 | public Board(int dimension) { 10 | this.board = new ArrayList<>(); 11 | for (int i = 0; i < dimension; i++) { 12 | this.board.add(new ArrayList<>()); 13 | for (int j = 0; j < dimension; j++) { 14 | this.board.get(i).add(new Cell(i, j)); 15 | } 16 | } 17 | } 18 | 19 | public List> getBoard() { 20 | return board; 21 | } 22 | 23 | public void setBoard(List> board) { 24 | this.board = board; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /code/MachineCoding/tictactoe/src/models/Bot.java: -------------------------------------------------------------------------------- 1 | package models; 2 | 3 | import strategies.bot.BotPlayingStrategy; 4 | import strategies.bot.BotPlayingStrategyFactory; 5 | 6 | import java.util.Scanner; 7 | 8 | public class Bot extends Player { 9 | public Bot(String name, char symbol, int id, 10 | BotDifficultyLevel botDifficultyLevel, Scanner scanner) { 11 | super(name, symbol, id, PlayerType.BOT, scanner); 12 | this.botDifficultyLevel = botDifficultyLevel; 13 | this.botPlayingStrategy = BotPlayingStrategyFactory.getBotPlayingStrategy(botDifficultyLevel); 14 | } 15 | BotDifficultyLevel botDifficultyLevel; 16 | BotPlayingStrategy botPlayingStrategy; 17 | 18 | 19 | 20 | @Override 21 | public Move makeMove(Board board) { 22 | 23 | Move move = botPlayingStrategy.suggestMove(board); 24 | 25 | // NPE Check 26 | if (move == null) { 27 | return null; 28 | } 29 | 30 | System.out.printf("the bot is making a move %d %d.\n", move.getCell().getRow(), move.getCell().getCol()); 31 | 32 | move.getCell().setPlayer(this); 33 | move.getCell().setCellStatus(CellStatus.OCCUPIED); 34 | 35 | return move; 36 | } 37 | } 38 | 39 | -------------------------------------------------------------------------------- /code/MachineCoding/tictactoe/src/models/BotDifficultyLevel.java: -------------------------------------------------------------------------------- 1 | package models; 2 | 3 | public enum BotDifficultyLevel { 4 | EASY, 5 | MEDIUM, 6 | HARD, 7 | } 8 | -------------------------------------------------------------------------------- /code/MachineCoding/tictactoe/src/models/CellStatus.java: -------------------------------------------------------------------------------- 1 | package models; 2 | 3 | public enum CellStatus { 4 | EMPTY, 5 | OCCUPIED, 6 | } 7 | -------------------------------------------------------------------------------- /code/MachineCoding/tictactoe/src/models/GameState.java: -------------------------------------------------------------------------------- 1 | package models; 2 | 3 | public enum GameState { 4 | NEW, 5 | IN_PROGRESS, 6 | WIN, 7 | DRAW, 8 | PAUSE 9 | } 10 | -------------------------------------------------------------------------------- /code/MachineCoding/tictactoe/src/models/Move.java: -------------------------------------------------------------------------------- 1 | package models; 2 | 3 | public class Move { 4 | 5 | private int moveNumber; 6 | private Cell cell; 7 | 8 | public Move(int moveNumber, Cell cell) { 9 | this.moveNumber = moveNumber; 10 | this.cell = cell; 11 | } 12 | 13 | public Move(Cell cell) { 14 | this.cell = cell; 15 | this.moveNumber = 0; 16 | } 17 | 18 | public int getMoveNumber() { 19 | return moveNumber; 20 | } 21 | 22 | public void setMoveNumber(int moveNumber) { 23 | this.moveNumber = moveNumber; 24 | } 25 | 26 | public Cell getCell() { 27 | return cell; 28 | } 29 | 30 | public void setCell(Cell cell) { 31 | this.cell = cell; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /code/MachineCoding/tictactoe/src/models/PlayerType.java: -------------------------------------------------------------------------------- 1 | package models; 2 | 3 | public enum PlayerType { 4 | HUMAN, 5 | BOT 6 | } 7 | -------------------------------------------------------------------------------- /code/MachineCoding/tictactoe/src/services/BoardService.java: -------------------------------------------------------------------------------- 1 | package services; 2 | 3 | import models.Board; 4 | import models.CellStatus; 5 | 6 | public class BoardService { 7 | 8 | public static void display(Board board) { 9 | for (int i = 0; i < board.getBoard().size(); i++) { 10 | for (int j = 0; j < board.getBoard().size(); j++) { 11 | if (board.getBoard().get(i).get(j).getCellStatus() 12 | .equals(CellStatus.EMPTY)) { 13 | System.out.print("| - |"); 14 | } else { 15 | System.out.printf("| %s |", 16 | board.getBoard().get(i).get(j).getPlayer().getSymbol()); 17 | } 18 | } 19 | System.out.println(); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /code/MachineCoding/tictactoe/src/services/GameService.java: -------------------------------------------------------------------------------- 1 | package services; 2 | 3 | import models.Game; 4 | import models.GameState; 5 | import models.Move; 6 | import models.Player; 7 | import strategies.winningstrategies.WinningStrategy; 8 | 9 | public class GameService { 10 | 11 | Game game; 12 | 13 | public GameService(Game game) { 14 | this.game = game; 15 | } 16 | 17 | public void executeNextMove() { 18 | 19 | Player currentPlayer = game.getCurrentPlayer(); 20 | System.out.printf("Player turn: %s\n", currentPlayer.getName()); 21 | 22 | Move move = currentPlayer.makeMove(game.getBoard()); 23 | 24 | if (move == null) { 25 | game.setGameState(GameState.DRAW); 26 | return; 27 | } 28 | 29 | game.addMove(move); 30 | 31 | game.updateBoard(move); 32 | 33 | for (WinningStrategy winningStrategy: game.getWinningStrategies()) { 34 | if (winningStrategy.checkWinner(game.getBoard(), move)) { 35 | System.out.printf("The player %s has won.\n", currentPlayer.getName()); 36 | game.setGameState(GameState.WIN); 37 | game.setWinner(currentPlayer); 38 | break; 39 | } 40 | } 41 | 42 | game.nextPlayerTurn(); 43 | 44 | } 45 | } 46 | 47 | -------------------------------------------------------------------------------- /code/MachineCoding/tictactoe/src/strategies/bot/BotPlayingStrategy.java: -------------------------------------------------------------------------------- 1 | package strategies.bot; 2 | 3 | import models.Board; 4 | import models.Move; 5 | 6 | public interface BotPlayingStrategy { 7 | Move suggestMove(Board board); 8 | } 9 | -------------------------------------------------------------------------------- /code/MachineCoding/tictactoe/src/strategies/bot/BotPlayingStrategyFactory.java: -------------------------------------------------------------------------------- 1 | package strategies.bot; 2 | 3 | import models.BotDifficultyLevel; 4 | 5 | public class BotPlayingStrategyFactory { 6 | 7 | public static BotPlayingStrategy getBotPlayingStrategy(BotDifficultyLevel botDifficultyLevel) { 8 | return new EasyBotPlayingStrategy(); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /code/MachineCoding/tictactoe/src/strategies/bot/EasyBotPlayingStrategy.java: -------------------------------------------------------------------------------- 1 | package strategies.bot; 2 | 3 | import models.Board; 4 | import models.CellStatus; 5 | import models.Move; 6 | 7 | 8 | /** 9 | * Easy bot playing strategy simply returns a move 10 | * to the first cell that is empty. 11 | */ 12 | public class EasyBotPlayingStrategy implements BotPlayingStrategy{ 13 | 14 | @Override 15 | public Move suggestMove(Board board) { 16 | for (int i = 0; i < board.getBoard().size(); i++) { 17 | for (int j = 0; j < board.getBoard().size(); j++) { 18 | if (board.getBoard().get(i).get(j).getCellStatus().equals(CellStatus.EMPTY)) { 19 | return new Move(board.getBoard().get(i).get(j)); 20 | } 21 | } 22 | } 23 | return null; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /code/MachineCoding/tictactoe/src/strategies/winningstrategies/ColWinningStrategy.java: -------------------------------------------------------------------------------- 1 | package strategies.winningstrategies; 2 | 3 | import models.*; 4 | 5 | /** 6 | * ColWinningStrategy returns boolean 7 | * to answer whether the player who played 8 | * the last move has won the game or not. 9 | * 10 | * By checking every cell in that column should have symbol from the Player. 11 | * 12 | */ 13 | public class ColWinningStrategy implements WinningStrategy{ 14 | @Override 15 | public boolean checkWinner(Board board, Move move) { 16 | int columnToCheck = move.getCell().getCol(); 17 | Player player = move.getCell().getPlayer(); 18 | 19 | for (int i = 0; i < board.getBoard().size(); i++) { 20 | Cell cell = board.getBoard().get(i).get(columnToCheck); 21 | if (cell.getCellStatus().equals(CellStatus.EMPTY) 22 | || !cell.getPlayer().equals(player)) { 23 | return false; 24 | } 25 | } 26 | return true; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /code/MachineCoding/tictactoe/src/strategies/winningstrategies/CornerWinningStrategy.java: -------------------------------------------------------------------------------- 1 | package strategies.winningstrategies; 2 | 3 | import models.*; 4 | 5 | public class CornerWinningStrategy implements WinningStrategy{ 6 | @Override 7 | public boolean checkWinner(Board board, Move move) { 8 | Player currentPlayer = move.getCell().getPlayer(); 9 | int boardSize = board.getBoard().size(); 10 | 11 | int[] row = {0, 0, boardSize - 1, boardSize -1}; 12 | int[] col = {boardSize - 1, 0, boardSize - 1, 0}; 13 | 14 | for (int i = 0; i < 4; i++) { 15 | Cell currentCell = board.getBoard().get(row[i]).get(col[i]); 16 | if (currentCell.getCellStatus().equals(CellStatus.EMPTY) 17 | || !currentCell.getPlayer().equals(currentPlayer)) { 18 | return false; 19 | } 20 | } 21 | return true; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /code/MachineCoding/tictactoe/src/strategies/winningstrategies/RowWinningStrategy.java: -------------------------------------------------------------------------------- 1 | package strategies.winningstrategies; 2 | 3 | import models.*; 4 | 5 | public class RowWinningStrategy implements WinningStrategy{ 6 | @Override 7 | public boolean checkWinner(Board board, Move move) { 8 | int rowToCheck = move.getCell().getRow(); 9 | Player player = move.getCell().getPlayer(); 10 | 11 | for (int i = 0; i < board.getBoard().size(); i++) { 12 | Cell cell = board.getBoard().get(rowToCheck).get(i); 13 | if (cell.getCellStatus().equals(CellStatus.EMPTY) 14 | || !cell.getPlayer().equals(player)) { 15 | return false; 16 | } 17 | } 18 | return true; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /code/MachineCoding/tictactoe/src/strategies/winningstrategies/WinningStrategy.java: -------------------------------------------------------------------------------- 1 | package strategies.winningstrategies; 2 | 3 | import models.Board; 4 | import models.Move; 5 | 6 | public interface WinningStrategy { 7 | boolean checkWinner(Board board, Move move); 8 | } 9 | -------------------------------------------------------------------------------- /contrib/Laxminarayan-Katker.md: -------------------------------------------------------------------------------- 1 | 2 | Name : Laxminarayan Katker 3 | Batch : Aug22 Intermediate 4 | Github Repo Link : https://github.com/laxminarayankatker/May23-LLD-RG 5 | -------------------------------------------------------------------------------- /contrib/Nadeem-Shaik.md: -------------------------------------------------------------------------------- 1 | Name : Nadeem Shaik 2 | Batch : Aug22 Intermediate 3 | Github Repo Link : https://github.com/nadeemshaik7050/Scalar_LLD 4 | -------------------------------------------------------------------------------- /contrib/RishabShinghal.md: -------------------------------------------------------------------------------- 1 | Name :- Rishab Shinghal 2 | 3 | Batch :- August 2022 4 | 5 | Part 1 Git repo :- https://github.com/rish8089/ScalerLLDAssignments 6 | 7 | Part 2 Git repo :- https://github.com/rish8089/ScalerLLDAssignment1Part2 8 | -------------------------------------------------------------------------------- /contrib/aman-choudhary.md: -------------------------------------------------------------------------------- 1 | - Name : Aman Choudhary 2 | - Batch : Aug22 Intermediate 3 | - Github Repo Link : https://github.com/amanchoudhary168/May23-LLD/ 4 | -------------------------------------------------------------------------------- /contrib/amol-b.md: -------------------------------------------------------------------------------- 1 | Name: Amol S. Brahme 2 | Batch: Academy Aug22 Intermediate Monday - 2 3 | GitHub Link: https://github.com/amolsbrahme/May23-LLD 4 | -------------------------------------------------------------------------------- /contrib/arvind-singh.md: -------------------------------------------------------------------------------- 1 | * Name : **Arvind Singh** 2 | * Batch : *Academy Aug22 Intermediate Monday -1* 3 | * Github Repo Link : https://github.com/arvindsin02/May23-LLD -------------------------------------------------------------------------------- /contrib/bibek-sahoo.md: -------------------------------------------------------------------------------- 1 | Full Name = Bibekananda Sahoo 2 | Batch Name = Aug22 Intermediate 3 | Github Link = https://github.com/bibekSahoo?tab=repositories -------------------------------------------------------------------------------- /contrib/biswajit-saha.md: -------------------------------------------------------------------------------- 1 | full name: Biswajit Saha 2 | batch name: Academy Aug22 Intermediate Monday -1 3 | github repository link: https://github.com/Biswajit8/May23-LLD -------------------------------------------------------------------------------- /contrib/gautham-s.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
Full NameGautham S
BatchAcademy Aug22 Intermediate Monday
Emailgauthamsundarrajan@outlook.com
Github repohttps://github.com/gauthams89/May23-LLD
19 | -------------------------------------------------------------------------------- /contrib/kaviyarasan-subbaiyan.md: -------------------------------------------------------------------------------- 1 | - Name : **Kaviyarasan Subbaiyan** 2 | - Batch : **Academy Aug22 Intermediate Monday - 2** 3 | - GitHub Repo Link : https://github.com/kaviyarasans/May23-LLD -------------------------------------------------------------------------------- /contrib/mahesh-phutane.md: -------------------------------------------------------------------------------- 1 | - Name : Mahesh Phutane 2 | - Batch : Oct22 Advance 3 | - Github Repo Link : https://github.com/maheshphutane/May23-LLD.git -------------------------------------------------------------------------------- /contrib/manabendra-dey.md: -------------------------------------------------------------------------------- 1 | Name: Manabendra Dey 2 | Batch: Aug22 Intermediate 3 | Github Repo Link: [https://github.com/skamble89/lld-assignments](https://github.com/manab1977/LLDAssigment) 4 | -------------------------------------------------------------------------------- /contrib/nage1234.md: -------------------------------------------------------------------------------- 1 | - Name : Nageswari 2 | - Batch : Aug22 Intermediate 3 | - Github Repo Link : https://github.com/nage1234/May23-LLD 4 | -------------------------------------------------------------------------------- /contrib/nikhil-lund.md: -------------------------------------------------------------------------------- 1 | - Name : Nikhil Lund 2 | - Batch : Aug22 Intermediate 3 | - Github Repo Link : https://github.com/Nikhillund/May23-LLD -------------------------------------------------------------------------------- /contrib/nuthan-chandra.md: -------------------------------------------------------------------------------- 1 | * Name : **Nuthan Chandra** 2 | * Batch : *Aug22 Intermediate* 3 | * Github Repo Link : https://github.com/nuthanc/May23-LLD -------------------------------------------------------------------------------- /contrib/pooja-t.md: -------------------------------------------------------------------------------- 1 | * Name : **Pooja T** 2 | * Batch : *Aug22 Intermediate* 3 | * Github Repo Link : https://github.com/pooja-thalur/May23-LLD -------------------------------------------------------------------------------- /contrib/prasant-samal.md: -------------------------------------------------------------------------------- 1 | - Name : Prasant Samal 2 | - Batch : Aug22 Intermediate 3 | - Github Repo Link : https://github.com/samalprasant123/May23-LLD -------------------------------------------------------------------------------- /contrib/pratik-dutta.md: -------------------------------------------------------------------------------- 1 | - Name : Pratik Dutta 2 | - Batch : Aug22 Intermediate 3 | - Github Repo Link : https://github.com/dutta-pratik/lld-assignments -------------------------------------------------------------------------------- /contrib/prerna-dutta.md: -------------------------------------------------------------------------------- 1 | - Name : Prerna Dutta 2 | - Batch : Academy Aug22 Intermediate Monday -1 3 | - Github Repo Link : https://github.com/prernadutta15/May23-LLD-1 -------------------------------------------------------------------------------- /contrib/rahul-grover.md: -------------------------------------------------------------------------------- 1 | Rahul Grover 2 | Foobar batch -------------------------------------------------------------------------------- /contrib/ritik-gupta.md: -------------------------------------------------------------------------------- 1 | Name: Ritik Gupta 2 | Batch Name: Academy Aug22 Intermediate Monday -1 3 | Link to github repo: https://github.com/ritik2117/hello-world.git 4 | https://github.com/ritik2117/May23-AssignmentPart-3.git -------------------------------------------------------------------------------- /contrib/sanket-kumar.md: -------------------------------------------------------------------------------- 1 | - Name : Sanket Kumar 2 | - Batch : Aug22 Intermediate 3 | - Github Repo Link : https://github.com/okSanky/May23-LLD -------------------------------------------------------------------------------- /contrib/santosh-jeergi.md: -------------------------------------------------------------------------------- 1 | 2 | ##Name : Santosh Jeergi 3 | 4 | Batch : *Aug22 Intermediate* 5 | 6 | Github Repo Link : **https://github.com/santoshjeergi/** 7 | -------------------------------------------------------------------------------- /contrib/saurabh-kamble.md: -------------------------------------------------------------------------------- 1 | - **Name**: Saurabh Kamble 2 | - **Batch**: Aug22 Intermediate 3 | - **Github Repo Link**: [https://github.com/skamble89/lld-assignments](https://github.com/skamble89/lld-assignments) 4 | -------------------------------------------------------------------------------- /contrib/soumyakanti-giri.md: -------------------------------------------------------------------------------- 1 | - Name : Soumyakanti Giri 2 | - Batch : Aug 22 Intermediate 3 | - Github Repo Link : https://github.com/osoiri/May23-LLD 4 | -------------------------------------------------------------------------------- /solutions/Biswajit8/MainClass.java: -------------------------------------------------------------------------------- 1 | package solutions.Biswajit8; 2 | 3 | import java.util.Scanner; 4 | 5 | public class MainClass { 6 | 7 | public static void main(String[] args) { 8 | 9 | Scanner sc = new Scanner(System.in); 10 | System.out.println("Enter shape (Triangle/Rectangle): "); 11 | String inputShape = sc.nextLine(); 12 | System.out.println("Enter height: "); 13 | double h = sc.nextDouble(); 14 | System.out.println("Enter width: "); 15 | double w = sc.nextDouble(); 16 | 17 | Shape shape; 18 | if (inputShape.equalsIgnoreCase("Triangle")) { 19 | shape = new Triangle(h, w); 20 | } else { 21 | shape = new Rectangle(h, w); 22 | } 23 | 24 | printArea(shape); 25 | } 26 | 27 | private static void printArea(Shape shape) { 28 | System.out.println("Area = " + shape.compute_area()); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /solutions/Biswajit8/Rectangle.java: -------------------------------------------------------------------------------- 1 | package solutions.Biswajit8; 2 | 3 | public class Rectangle extends Shape { 4 | 5 | Rectangle(double height, double width) { 6 | super(height, width); 7 | } 8 | 9 | @Override 10 | public double compute_area() { 11 | return this.height * this.width; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /solutions/Biswajit8/Shape.java: -------------------------------------------------------------------------------- 1 | package solutions.Biswajit8; 2 | 3 | public class Shape { 4 | 5 | public double height; 6 | public double width; 7 | 8 | Shape(double height, double width) { 9 | this.height=height; 10 | this.width=width; 11 | } 12 | 13 | public double compute_area() { 14 | return 0.0; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /solutions/Biswajit8/Triangle.java: -------------------------------------------------------------------------------- 1 | package solutions.Biswajit8; 2 | 3 | public class Triangle extends Shape { 4 | 5 | 6 | Triangle(double height, double width) { 7 | super(height, width); 8 | } 9 | 10 | @Override 11 | public double compute_area() { 12 | return 0.5 * this.height * this.width; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /solutions/Mangai94/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using sample; 3 | 4 | namespace c_sharp 5 | { 6 | class Program 7 | { 8 | static void Main(string[] args) 9 | { 10 | string input = "Triangle"; 11 | Shape shape; 12 | if (input == "Triangle") { 13 | shape = new Triangle(10, 15); 14 | } else { 15 | shape = new Rectangle(10, 15); 16 | } 17 | Console.WriteLine(shape.compute_area()); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /solutions/Mangai94/Rectangle.cs: -------------------------------------------------------------------------------- 1 | namespace sample{ 2 | 3 | class Rectangle : Shape 4 | { 5 | public Rectangle(double height, double width) : base(height, width) 6 | { 7 | } 8 | 9 | 10 | public override double compute_area() 11 | { 12 | return (this.height * this.width); 13 | } 14 | } 15 | } -------------------------------------------------------------------------------- /solutions/Mangai94/Shape.cs: -------------------------------------------------------------------------------- 1 | 2 | namespace sample 3 | { 4 | public class Shape 5 | { 6 | public double height; 7 | public double width; 8 | 9 | public Shape(double height, double width) 10 | { 11 | this.height = height; 12 | this.width = width; 13 | } 14 | 15 | public virtual double compute_area() 16 | { 17 | return 0; 18 | } 19 | } 20 | } 21 | 22 | -------------------------------------------------------------------------------- /solutions/Mangai94/Triangle.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace sample{ 4 | 5 | class Triangle : Shape 6 | { 7 | public Triangle(double height, double width): base(height, width) 8 | { 9 | Console.WriteLine("Triangle constrcutor called"); 10 | } 11 | 12 | 13 | public override double compute_area() 14 | { 15 | 16 | return (this.height * this.width) / 2; 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /solutions/amanchoudhary168/Main.java: -------------------------------------------------------------------------------- 1 | package solutions.amanchoudhary168; 2 | 3 | import java.util.Scanner; 4 | 5 | /** 6 | * @author : aman.choudhary 7 | * @version : 1.0.0 8 | * date : 26-05-2023 9 | **/ 10 | public class Main { 11 | public static void main(String[] args) { 12 | Shape shape; 13 | Scanner scan = new Scanner(System.in); 14 | String input = scan.nextLine(); 15 | switch (input){ 16 | case "Triangle" : 17 | double heightT = scan.nextDouble(); 18 | double widthT = scan.nextDouble(); 19 | shape = new Triangle(heightT,widthT); 20 | printArea(shape); 21 | break; 22 | case "Rectangle" : 23 | double heightR = scan.nextDouble(); 24 | double widthR = scan.nextDouble(); 25 | shape = new Rectangle(heightR,widthR); 26 | printArea(shape); 27 | break; 28 | 29 | default:{ 30 | System.out.println("Invalid Choice"); 31 | } 32 | } 33 | 34 | } 35 | 36 | public static void printArea(Shape shape){ 37 | System.out.println(shape.computeArea()); 38 | } 39 | } 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /solutions/amanchoudhary168/Node.java: -------------------------------------------------------------------------------- 1 | package solutions.amanchoudhary168; 2 | 3 | import java.util.Iterator; 4 | 5 | public class Node implements Iterable { 6 | 7 | int data; 8 | Node next; 9 | 10 | public Node(int data) { 11 | this.data = data; 12 | this.next = null; 13 | } 14 | 15 | public Node(int data, Node next) { 16 | this.data = data; 17 | this.next = next; 18 | } 19 | 20 | 21 | @Override 22 | public Iterator iterator() { 23 | return new NodeIterator(this); 24 | } 25 | 26 | private static class NodeIterator implements Iterator { 27 | private Node current; 28 | public NodeIterator(Node node) { 29 | this.current = node; 30 | } 31 | 32 | @Override 33 | public boolean hasNext() { 34 | return current != null; 35 | } 36 | 37 | @Override 38 | public Node next() { 39 | Node temp = current; 40 | current = current.next; 41 | return temp; 42 | } 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /solutions/amanchoudhary168/Rectangle.java: -------------------------------------------------------------------------------- 1 | package solutions.amanchoudhary168; 2 | 3 | /** 4 | * @author : aman.choudhary 5 | * @version : 1.0.0 6 | * date : 26-05-2023 7 | **/ 8 | public class Rectangle extends Shape{ 9 | 10 | public Rectangle(double height,double width){ 11 | super(height,width); 12 | } 13 | 14 | @Override 15 | public double computeArea(){ 16 | return height*width; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /solutions/amanchoudhary168/Shape.java: -------------------------------------------------------------------------------- 1 | package solutions.amanchoudhary168; 2 | 3 | /** 4 | * @author : aman.choudhary 5 | * @version : 1.0.0 6 | * date : 26-05-2023 7 | **/ 8 | public class Shape { 9 | protected double height; 10 | protected double width; 11 | 12 | public Shape(double height,double width){ 13 | this.height = height; 14 | this.width = width; 15 | } 16 | 17 | public double computeArea(){ 18 | return 0; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /solutions/amanchoudhary168/Triangle.java: -------------------------------------------------------------------------------- 1 | package solutions.amanchoudhary168; 2 | 3 | /** 4 | * @author : aman.choudhary 5 | * @version : 1.0.0 6 | * date : 26-05-2023 7 | **/ 8 | public class Triangle extends Shape{ 9 | 10 | public Triangle(double height,double width){ 11 | super(height,width); 12 | } 13 | 14 | @Override 15 | public double computeArea(){ 16 | return 0.5d*height*width; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /solutions/arvindsin02/Main.java: -------------------------------------------------------------------------------- 1 | package solutions.arvindsin02; 2 | import java.util.Scanner; 3 | class Main { 4 | public static void main(String[] args) { 5 | System.out.println("Enter the Shape name: Rectangle/Triangle"); 6 | Scanner input = new Scanner(System.in); 7 | String shapeName = input.nextLine(); 8 | 9 | System.out.println("Enter the height"); 10 | double height = input.nextDouble(); 11 | 12 | System.out.println("Enter the width"); 13 | double width = input.nextDouble(); 14 | double area; 15 | if(shapeName.equals("Rectangle")) { 16 | Rectangle r = new Rectangle(height, width); 17 | area = r.compute_area(); 18 | } else { 19 | Triangle t = new Triangle(height, width); 20 | area = t.compute_area(); 21 | } 22 | 23 | System.out.println("Area of the shape"+shapeName+" is: "+area); 24 | 25 | } 26 | } -------------------------------------------------------------------------------- /solutions/arvindsin02/Rectangle.java: -------------------------------------------------------------------------------- 1 | package solutions.arvindsin02; 2 | 3 | class Rectangle extends Shape { 4 | 5 | Rectangle(double height, double width) { 6 | super(height, width); 7 | } 8 | @Override 9 | double compute_area() { 10 | return this.height*this.width; 11 | } 12 | } -------------------------------------------------------------------------------- /solutions/arvindsin02/Shape.java: -------------------------------------------------------------------------------- 1 | package solutions.arvindsin02; 2 | 3 | class Shape { 4 | double height; 5 | double width; 6 | 7 | Shape() { 8 | height = 0.0d; 9 | width = 0.0d; 10 | } 11 | Shape(double height, double width) { 12 | this.height = height; 13 | this. width = width; 14 | } 15 | 16 | double compute_area() { 17 | return height*width; 18 | } 19 | 20 | 21 | } -------------------------------------------------------------------------------- /solutions/arvindsin02/Triangle.java: -------------------------------------------------------------------------------- 1 | package solutions.arvindsin02; 2 | 3 | class Triangle extends Shape { 4 | Triangle(double height, double width) { 5 | super(height, width); 6 | } 7 | @Override 8 | double compute_area() { 9 | return (0.5*this.height*this.width); 10 | } 11 | } -------------------------------------------------------------------------------- /solutions/duttapratik/Main.java: -------------------------------------------------------------------------------- 1 | package solutions.duttapratik; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Main { 6 | 7 | static void printArea(Shape s){ 8 | double area = s.compute_area(); 9 | System.out.println("Area is "+ area); 10 | } 11 | 12 | public static void main(String args[]){ 13 | Scanner sc = new Scanner(System.in); 14 | System.out.println("Enter the Shape name as either Triangle or Rectangle"); 15 | String input = sc.next(); 16 | System.out.println("Enter height: "); 17 | double h = sc.nextDouble(); 18 | System.out.println("Enter width: "); 19 | double w = sc.nextDouble(); 20 | Shape shape; 21 | if (input.equals("Triangle")) { 22 | shape = new Triangle(h, w); 23 | } else { 24 | shape = new Rectangle(h, w); 25 | } 26 | 27 | printArea(shape); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /solutions/duttapratik/Rectangle.java: -------------------------------------------------------------------------------- 1 | package solutions.duttapratik; 2 | 3 | public class Rectangle extends Shape{ 4 | Rectangle(double height, double width){ 5 | super(height, width); 6 | } 7 | double compute_area(){ 8 | return this.height*this.width; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /solutions/duttapratik/Shape.java: -------------------------------------------------------------------------------- 1 | package solutions.duttapratik; 2 | 3 | public class Shape { 4 | protected double height; 5 | protected double width; 6 | Shape(double height, double width){ 7 | this.height = height; 8 | this.width = width; 9 | } 10 | double compute_area(){ 11 | return 0; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /solutions/duttapratik/Triangle.java: -------------------------------------------------------------------------------- 1 | package solutions.duttapratik; 2 | 3 | public class Triangle extends Shape { 4 | Triangle(double height, double width){ 5 | super(height, width); 6 | } 7 | double compute_area(){ 8 | return 0.5*this.width*this.height; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /solutions/gauthams89/Main.java: -------------------------------------------------------------------------------- 1 | package solutions.gauthams89; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | 7 | public class Main { 8 | public static void main(String[] args) throws IOException { 9 | BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 10 | var out = System.out; 11 | out.print("Do you want to enter dimention of Rectangle or Triangle? :"); 12 | String shapeInp = in.readLine(); 13 | boolean isTriangle = shapeInp.toLowerCase().equalsIgnoreCase("triangle"); 14 | 15 | if(isTriangle) { 16 | out.print("Enter Base: "); 17 | } 18 | else { 19 | out.print("Enter Width: "); 20 | } 21 | 22 | double width = Double.parseDouble(in.readLine()); 23 | 24 | if(isTriangle) { 25 | out.print("Enter Perpendicular height: "); 26 | } 27 | else { 28 | out.print("Enter Height: "); 29 | } 30 | 31 | double height= Double.parseDouble(in.readLine()); 32 | 33 | Shape shape = isTriangle? 34 | new Triangle(height, width): 35 | new Rectangle(height, width); 36 | 37 | double area = shape.compute_area(); 38 | out.println(String.format("Area of %s is %.2f", shapeInp, area)); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /solutions/gauthams89/Rectangle.java: -------------------------------------------------------------------------------- 1 | package solutions.gauthams89; 2 | 3 | public class Rectangle extends Shape { 4 | 5 | public Rectangle(double height, double width) { 6 | super(height, width); 7 | } 8 | 9 | @Override 10 | public double compute_area() { 11 | return width * height; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /solutions/gauthams89/Shape.java: -------------------------------------------------------------------------------- 1 | package solutions.gauthams89; 2 | 3 | public class Shape { 4 | double height; 5 | double width; 6 | 7 | public Shape(double height, double width) { 8 | super(); 9 | this.height = height; 10 | this.width = width; 11 | } 12 | 13 | public double compute_area() { 14 | return 0; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /solutions/gauthams89/Triangle.java: -------------------------------------------------------------------------------- 1 | package solutions.gauthams89; 2 | 3 | public class Triangle extends Shape { 4 | 5 | public Triangle(double height, double width) { 6 | super(height, width); 7 | } 8 | 9 | @Override 10 | public double compute_area() { 11 | return height * width / 2; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /solutions/kaviyarasans/Main.java: -------------------------------------------------------------------------------- 1 | package solutions.kaviyarasans; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Main { 6 | 7 | public static void main(String[] args) { 8 | 9 | Scanner scanner = new Scanner(System.in); 10 | 11 | System.out.println("Enter the Shape like 'Triangle' or 'Rectangle' to calculate Area :"); 12 | String input = scanner.next(); 13 | System.out.println("Enter the height :"); 14 | double h = scanner.nextDouble(); 15 | System.out.println("Enter the width :"); 16 | double w = scanner.nextDouble(); 17 | 18 | Shape shape; 19 | if (input.equals("Triangle")) { 20 | shape = new Triangle(h, w); 21 | } else { 22 | shape = new Rectangle(h, w); 23 | } 24 | 25 | printArea(shape); 26 | } 27 | 28 | private static void printArea(Shape shape) { 29 | System.out.println("Area : " + shape.compute_area()); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /solutions/kaviyarasans/Rectangle.java: -------------------------------------------------------------------------------- 1 | package solutions.kaviyarasans; 2 | 3 | public class Rectangle extends Shape{ 4 | public Rectangle(double length, double width) { 5 | super(length, width); 6 | } 7 | 8 | @Override 9 | public double compute_area() { 10 | return height * width; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /solutions/kaviyarasans/Shape.java: -------------------------------------------------------------------------------- 1 | package solutions.kaviyarasans; 2 | 3 | public class Shape { 4 | 5 | public double height; 6 | 7 | public double width; 8 | 9 | public Shape(double height, double width) { 10 | this.height = height; 11 | this.width = width; 12 | } 13 | 14 | public double compute_area() { 15 | return 0; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /solutions/kaviyarasans/Triangle.java: -------------------------------------------------------------------------------- 1 | package solutions.kaviyarasans; 2 | 3 | public class Triangle extends Shape{ 4 | public Triangle(double height, double base) { 5 | super(height, base); 6 | } 7 | 8 | @Override 9 | public double compute_area() { 10 | return 0.5 * height * width; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /solutions/laxminarayankatker/src/solutions/laxminarayankatker/Rectangle.java: -------------------------------------------------------------------------------- 1 | package solutions.laxminarayankatker; 2 | 3 | public class Rectangle extends Shape{ 4 | 5 | public Rectangle() 6 | { 7 | super(); 8 | } 9 | public Rectangle(double height,double width) 10 | { 11 | super(height, width); 12 | // this.height = height; 13 | // this.width = width; 14 | } 15 | public double compute_area() 16 | { 17 | return this.height*this.width; 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /solutions/laxminarayankatker/src/solutions/laxminarayankatker/Shape.java: -------------------------------------------------------------------------------- 1 | package solutions.laxminarayankatker; 2 | 3 | public class Shape { 4 | double height; 5 | double width; 6 | public Shape() 7 | { 8 | this.height=0; 9 | this.width=0; 10 | } 11 | public Shape(double height,double width) 12 | { 13 | this.height = height; 14 | this.width = width; 15 | } 16 | public double compute_area() 17 | { 18 | return 0; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /solutions/laxminarayankatker/src/solutions/laxminarayankatker/Triangle.java: -------------------------------------------------------------------------------- 1 | package solutions.laxminarayankatker; 2 | 3 | public class Triangle extends Shape { 4 | 5 | public Triangle() { 6 | super(); 7 | } 8 | public Triangle(double height,double width) { 9 | super(height, width); 10 | } 11 | public double compute_area() 12 | { 13 | return 0.5*(this.height*this.width); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /solutions/maheshphutane/Main.java: -------------------------------------------------------------------------------- 1 | package solutions.maheshphutane; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Shape shape; 8 | Scanner sc = new Scanner(System.in); 9 | double h = sc.nextDouble(); 10 | double w = sc.nextDouble(); 11 | String areaOf = sc.next(); 12 | 13 | if(areaOf.equals("Triangle")){ 14 | shape = new Triangle(h,w); 15 | }else{ 16 | shape = new Rectangle(h,w); 17 | } 18 | printArea(shape); 19 | } 20 | 21 | private static void printArea(Shape shape) { 22 | System.out.println(shape.compute_area()); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /solutions/maheshphutane/Rectangle.java: -------------------------------------------------------------------------------- 1 | package solutions.maheshphutane; 2 | 3 | public class Rectangle extends Shape { 4 | double height,width; 5 | public Rectangle(double height,double width){ 6 | super(height,width); 7 | this.height = height; 8 | this.width = width; 9 | } 10 | 11 | public double compute_area(){ 12 | return this.height*this.width; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /solutions/maheshphutane/Shape.java: -------------------------------------------------------------------------------- 1 | package solutions.maheshphutane; 2 | 3 | public class Shape { 4 | double height; 5 | double width; 6 | 7 | public Shape(double height, double width) { 8 | this.height = height; 9 | this.width = width; 10 | } 11 | 12 | public double compute_area(){ 13 | return 0; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /solutions/maheshphutane/Triangle.java: -------------------------------------------------------------------------------- 1 | package solutions.maheshphutane; 2 | 3 | public class Triangle extends Shape { 4 | double height,width; 5 | public Triangle(double height,double width){ 6 | super(height,width); 7 | this.height = height; 8 | this.width = width; 9 | } 10 | public double compute_area(){ 11 | return height*width; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /solutions/nadeemShaik7050/Main.java: -------------------------------------------------------------------------------- 1 | package solutions.nadeemShaik7050; 2 | 3 | import java.util.*; 4 | 5 | public class Main { 6 | 7 | public static void main(String[] args) { 8 | Scanner sc=new Scanner(System.in); 9 | System.out.println("Enter Shape"); 10 | String input=sc.nextLine(); 11 | 12 | System.out.println("Enter height of Shape"); 13 | double h=sc.nextDouble(); 14 | 15 | System.out.println("Enter width of Shape"); 16 | double w=sc.nextDouble(); 17 | 18 | Shape shape; 19 | if (input.equals("Triangle")) { 20 | shape = new Triangle(h, w); 21 | } else { 22 | shape = new Rectangle(h, w); 23 | } 24 | 25 | printArea(shape); 26 | 27 | } 28 | 29 | public static void printArea(Shape shape) { 30 | System.out.println("Area of the given Shape is "+ shape.compute_area()); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /solutions/nadeemShaik7050/Rectangle.java: -------------------------------------------------------------------------------- 1 | package solutions.nadeemShaik7050; 2 | 3 | public class Rectangle extends Shape { 4 | 5 | Rectangle(double height,double width){ 6 | super(height, width); 7 | } 8 | 9 | @Override 10 | public double compute_area(){ 11 | return (height*width); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /solutions/nadeemShaik7050/Shape.java: -------------------------------------------------------------------------------- 1 | package solutions.nadeemShaik7050; 2 | 3 | public class Shape { 4 | public double height; 5 | public double width; 6 | 7 | Shape(double height,double width){ 8 | this.height=height; 9 | this.width=width; 10 | } 11 | 12 | public double compute_area(){ 13 | return 0.0; 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /solutions/nadeemShaik7050/Triangle.java: -------------------------------------------------------------------------------- 1 | package solutions.nadeemShaik7050; 2 | 3 | public class Triangle extends Shape{ 4 | 5 | Triangle(double height,double width){ 6 | super(height, width); 7 | } 8 | 9 | @Override 10 | public double compute_area(){ 11 | return (0.5*height*width); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /solutions/nage1234/Rectangle.java: -------------------------------------------------------------------------------- 1 | package solutions.nage1234; 2 | 3 | public class Rectangle extends Shape{ 4 | public Rectangle(double h, double w) { 5 | super(h, w); 6 | } 7 | 8 | @Override 9 | public double compute_area() { 10 | return this.height * this.width; 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /solutions/nage1234/Shape.java: -------------------------------------------------------------------------------- 1 | package solutions.nage1234; 2 | 3 | public class Shape { 4 | protected double height; 5 | protected double width; 6 | public Shape() { 7 | 8 | } 9 | public Shape(double height, double width) { 10 | this.height = height; 11 | this.width = width; 12 | } 13 | 14 | public double compute_area() { 15 | return 0; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /solutions/nage1234/ShapeFactory.java: -------------------------------------------------------------------------------- 1 | package solutions.nage1234; 2 | 3 | public class ShapeFactory { 4 | public static final String RECTANGLE = "Rectangle"; 5 | public static final String TRIANGLE = "Triangle"; 6 | public void callShape(String shapeName, double h, double w) { 7 | Shape s = null; 8 | if(shapeName.equals(RECTANGLE)) { 9 | s = new Rectangle(h, w); 10 | } else if(shapeName.equals(TRIANGLE)) { 11 | s = new Triangle(h, w); 12 | } else { 13 | s = new Shape(); 14 | } 15 | System.out.println("Area Computed for "+shapeName+" is "+s.compute_area()); 16 | } 17 | 18 | 19 | public static void main(String[] args) { 20 | // TODO Auto-generated method stub 21 | ShapeFactory sf = new ShapeFactory(); 22 | sf.callShape(RECTANGLE, 12, 15); 23 | sf.callShape(TRIANGLE, 10, 5); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /solutions/nage1234/Triangle.java: -------------------------------------------------------------------------------- 1 | package solutions.nage1234; 2 | 3 | public class Triangle extends Shape{ 4 | public Triangle(double h, double w) { 5 | super(h, w); 6 | } 7 | 8 | @Override 9 | public double compute_area() { 10 | return this.height * this.width * 0.5; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /solutions/nikhillund/Main.java: -------------------------------------------------------------------------------- 1 | package solutions.nikhillund; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | try (Scanner sc = new Scanner(System.in)) { 8 | String input = sc.next(); 9 | int h = sc.nextInt(); 10 | int w = sc.nextInt(); 11 | 12 | Shape shape; 13 | if (input.equalsIgnoreCase("Triangle")) { 14 | shape = new Triangle(h, w); 15 | } else { 16 | shape = new Rectangle(h, w); 17 | } 18 | printArea(shape); 19 | } 20 | } 21 | 22 | private static void printArea(Shape shape) { 23 | System.out.println(shape.compute_area()); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /solutions/nikhillund/Rectangle.java: -------------------------------------------------------------------------------- 1 | package solutions.nikhillund; 2 | 3 | public class Rectangle extends Shape{ 4 | public Rectangle(double height, double width) { 5 | super(height, width); 6 | } 7 | 8 | @Override 9 | double compute_area() { 10 | return height*width; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /solutions/nikhillund/Shape.java: -------------------------------------------------------------------------------- 1 | package solutions.nikhillund; 2 | 3 | public class Shape { 4 | double height; 5 | double width; 6 | 7 | public Shape(double height, double width) { 8 | this.height = height; 9 | this.width = width; 10 | } 11 | 12 | double compute_area() { 13 | return 0; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /solutions/nikhillund/Triangle.java: -------------------------------------------------------------------------------- 1 | package solutions.nikhillund; 2 | 3 | public class Triangle extends Shape { 4 | public Triangle(double height, double width) { 5 | super(height, width); 6 | } 7 | 8 | @Override 9 | double compute_area() { 10 | return (this.height * this.width)/2; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /solutions/nuthanc/Main.java: -------------------------------------------------------------------------------- 1 | package solutions.nuthanc; 2 | import java.util.Scanner; 3 | 4 | public class Main { 5 | public static void main(String[] args) { 6 | try (Scanner sc = new Scanner(System.in)) { 7 | System.out.println("Enter Shape: Triangle or Rectangle"); 8 | String input = sc.nextLine(); 9 | 10 | System.out.println("Enter Height"); 11 | double h = sc.nextDouble(); 12 | 13 | System.out.println("Enter Width"); 14 | double w = sc.nextDouble(); 15 | 16 | Shape shape; 17 | if (input.equals("Triangle")) { 18 | shape = new Triangle(h, w); 19 | } else { 20 | shape = new Rectangle(h, w); 21 | } 22 | 23 | printArea(shape); 24 | } 25 | } 26 | 27 | static void printArea(Shape shape) { 28 | System.out.println("Area is " + shape.compute_area()); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /solutions/nuthanc/Rectangle.java: -------------------------------------------------------------------------------- 1 | package solutions.nuthanc; 2 | 3 | public class Rectangle extends Shape { 4 | 5 | Rectangle(double height, double width) { 6 | super(height, width); 7 | } 8 | 9 | double compute_area() { 10 | double area = height * width; 11 | return area; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /solutions/nuthanc/Shape.java: -------------------------------------------------------------------------------- 1 | package solutions.nuthanc; 2 | 3 | public class Shape { 4 | double height, width; 5 | Shape(double height, double width) { 6 | this.height = height; 7 | this.width = width; 8 | } 9 | 10 | double compute_area() { 11 | return 0; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /solutions/nuthanc/Triangle.java: -------------------------------------------------------------------------------- 1 | package solutions.nuthanc; 2 | 3 | public class Triangle extends Shape { 4 | 5 | Triangle(double height, double width) { 6 | super(height, width); 7 | } 8 | 9 | double compute_area() { 10 | double area = height * width * 0.5; 11 | return area; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /solutions/okSanky/Main.java: -------------------------------------------------------------------------------- 1 | package solutions.okSanky; 2 | import java.util.Scanner; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) { 7 | // TODO Auto-generated method stub 8 | Scanner sc = new Scanner(System.in); 9 | System.out.println("Enter no. of test cases:-"); 10 | int t = sc.nextInt(); 11 | while(t > 0) { 12 | System.out.println("Enter the shape name like 'Triangle' or 'Rectangle':-"); 13 | String input = sc.next(); 14 | System.out.println("Enter height:"); 15 | double h = sc.nextDouble(); 16 | System.out.println("Enter width:"); 17 | double w = sc.nextDouble(); 18 | 19 | Shape shape; 20 | if (input == "Triangle") { 21 | shape = new Triangle(h, w); 22 | } else { 23 | shape = new Rectangle(h, w); 24 | } 25 | printArea(shape); 26 | t -=1; 27 | } 28 | return; 29 | } 30 | 31 | public static void printArea(Shape s) { 32 | System.out.println("Area is:- "+ s.compute_area()); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /solutions/okSanky/Rectangle.java: -------------------------------------------------------------------------------- 1 | package solutions.okSanky; 2 | 3 | public class Rectangle extends Shape { 4 | 5 | public Rectangle(double height, double width) { 6 | super(height, width); 7 | } 8 | 9 | protected double compute_area() { 10 | return this.height*this.width; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /solutions/okSanky/Shape.java: -------------------------------------------------------------------------------- 1 | package solutions.okSanky; 2 | 3 | public class Shape { 4 | protected double height; 5 | protected double width; 6 | 7 | public Shape(double height, double width) { 8 | this.height = height; 9 | this.width = width; 10 | } 11 | 12 | protected double compute_area() { 13 | return 0d; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /solutions/okSanky/Triangle.java: -------------------------------------------------------------------------------- 1 | package solutions.okSanky; 2 | 3 | public class Triangle extends Shape { 4 | 5 | public Triangle(double height, double width) { 6 | super(height, width); 7 | } 8 | 9 | protected double compute_area() { 10 | return (0.5*this.height*this.width); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /solutions/osoiri/Base.java: -------------------------------------------------------------------------------- 1 | package solutions.osoiri; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Base { 6 | 7 | static void printArea(Shape shape) { 8 | System.out.println("Area is " + shape.compute_area()); 9 | } 10 | 11 | public static void main(String[] args) { 12 | Scanner in = new Scanner(System.in); 13 | System.out.println("Shape Choice"); 14 | String input = in.next(); 15 | System.out.println("Enter area"); 16 | System.out.print("Height: "); 17 | double h = in.nextDouble(); 18 | System.out.print("Width: "); 19 | double w = in.nextDouble(); 20 | 21 | Shape shape; 22 | if ("Triangle".equals(input)) { 23 | shape = new Triangle(h, w); 24 | } else { 25 | shape = new Rectangle(h, w); 26 | } 27 | 28 | printArea(shape); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /solutions/osoiri/Rectangle.java: -------------------------------------------------------------------------------- 1 | package solutions.osoiri; 2 | 3 | public class Rectangle extends Shape{ 4 | 5 | Rectangle(double height, double width) { 6 | super(height, width); 7 | } 8 | 9 | @Override 10 | double compute_area() { 11 | return this.width*this.height; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /solutions/osoiri/Shape.java: -------------------------------------------------------------------------------- 1 | package solutions.osoiri; 2 | 3 | public class Shape { 4 | double height; 5 | double width; 6 | 7 | Shape() { 8 | this.height = 0; 9 | this.width = 0; 10 | } 11 | 12 | Shape(double height, double width) { 13 | this.height = height; 14 | this.width = width; 15 | } 16 | 17 | double compute_area() { 18 | return 0; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /solutions/osoiri/Triangle.java: -------------------------------------------------------------------------------- 1 | package solutions.osoiri; 2 | 3 | public class Triangle extends Shape{ 4 | 5 | Triangle(double height, double width) { 6 | super(height, width); 7 | } 8 | 9 | @Override 10 | double compute_area() { 11 | return 0.5*this.height*this.width; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /solutions/poojathalur/Main.java: -------------------------------------------------------------------------------- 1 | package solutions.poojathalur; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | System.out.println("Enter the shape - Rectangle or Triangle"); 8 | Scanner sc = new Scanner(System.in); 9 | String input = sc.next(); 10 | 11 | System.out.println("Enter the height of "+input); 12 | double h = sc.nextDouble(); 13 | 14 | System.out.println("Enter the width of "+input); 15 | double w = sc.nextDouble(); 16 | 17 | Shape shape; 18 | if (input.equalsIgnoreCase("Triangle")) { 19 | shape = new Triangle(h, w); 20 | } 21 | else { 22 | shape = new Rectangle(h, w); 23 | } 24 | 25 | printArea(shape); 26 | } 27 | 28 | static void printArea(Shape shape){ 29 | System.out.println(shape.compute_area()); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /solutions/poojathalur/Rectangle.java: -------------------------------------------------------------------------------- 1 | package solutions.poojathalur; 2 | 3 | public class Rectangle extends Shape{ 4 | 5 | Rectangle(double height, double width) { 6 | super(height, width); 7 | } 8 | 9 | double compute_area(){ 10 | return this.height*this.width; 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /solutions/poojathalur/Shape.java: -------------------------------------------------------------------------------- 1 | package solutions.poojathalur; 2 | 3 | public class Shape { 4 | double height; 5 | double width; 6 | 7 | Shape(double height, double width){ 8 | this.height = height; 9 | this.width = width; 10 | } 11 | 12 | //calculating area of shape 13 | double compute_area(){ 14 | return 0; 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /solutions/poojathalur/Triangle.java: -------------------------------------------------------------------------------- 1 | package solutions.poojathalur; 2 | 3 | public class Triangle extends Shape { 4 | 5 | Triangle(double height, double width) { 6 | super(height, width); 7 | } 8 | 9 | double compute_area(){ 10 | return this.height*this.width/2; 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /solutions/rahulgrover99/Main.java: -------------------------------------------------------------------------------- 1 | package solutions.rahulgrover99; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | System.out.println("Teach me coding senpai!"); 6 | } 7 | } -------------------------------------------------------------------------------- /solutions/rish8089/Client.java: -------------------------------------------------------------------------------- 1 | package solutions.rish8089; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | public class Client { 7 | enum ShapeNames{ 8 | Triangle,Rectangle; 9 | } 10 | public static void main(String[] args) throws IOException { 11 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 12 | String input = br.readLine(); 13 | ShapeNames sn; 14 | try { 15 | sn = ShapeNames.valueOf(input); 16 | }catch(IllegalArgumentException e){ 17 | System.out.println("Not a valid shape"); 18 | return; 19 | } 20 | String[] str = br.readLine().split(" "); 21 | double height = Double.parseDouble(str[0]); 22 | double width = Double.parseDouble(str[1]); 23 | Shape shape; 24 | if (ShapeNames.Triangle.equals(sn)) { 25 | shape = new Triangle(height, width); 26 | } else { 27 | shape = new Rectangle(height, width); 28 | } 29 | printArea(shape); 30 | } 31 | 32 | private static void printArea(Shape s) { 33 | System.out.println(s.compute_area()); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /solutions/rish8089/Rectangle.java: -------------------------------------------------------------------------------- 1 | package solutions.rish8089; 2 | 3 | public class Rectangle extends Shape { 4 | Rectangle(double height, double width){ 5 | super(height,width); 6 | } 7 | public double compute_area(){ 8 | return height*width; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /solutions/rish8089/Shape.java: -------------------------------------------------------------------------------- 1 | package solutions.rish8089; 2 | 3 | public class Shape { 4 | double height; 5 | double width; 6 | Shape(double height, double width){ 7 | this.height=height; 8 | this.width=width; 9 | } 10 | public double compute_area(){ 11 | return 0; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /solutions/rish8089/Triangle.java: -------------------------------------------------------------------------------- 1 | package solutions.rish8089; 2 | 3 | public class Triangle extends Shape { 4 | Triangle(double height, double width){ 5 | super(height,width); 6 | } 7 | public double compute_area(){ 8 | return 0.5*height*width; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /solutions/ritik2117/Main.java: -------------------------------------------------------------------------------- 1 | package ritik2117; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Main { 6 | 7 | private static void printArea(Shape shape) { 8 | System.out.println("Area is : " + shape.compute_area()); 9 | } 10 | public static void main(String[] args) { 11 | Shape shape; 12 | Scanner sc = new Scanner(System.in); 13 | System.out.println("Enter shape: "); 14 | String input = sc.nextLine(); 15 | System.out.println("Enter height: "); 16 | double height = sc.nextDouble(); 17 | System.out.println("Enter width: "); 18 | double width = sc.nextDouble(); 19 | sc.close(); 20 | if (input.equals("Triangle")) { 21 | shape = new Triangle(height, width); 22 | } 23 | else { 24 | shape = new Rectangle(height, width); 25 | } 26 | shape.compute_area(); 27 | printArea(shape); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /solutions/ritik2117/Rectangle.java: -------------------------------------------------------------------------------- 1 | package ritik2117; 2 | 3 | public class Rectangle extends Shape { 4 | 5 | Rectangle(double height, double width) { 6 | super(height, width); 7 | } 8 | 9 | @Override 10 | double compute_area() { 11 | return height * width; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /solutions/ritik2117/Shape.java: -------------------------------------------------------------------------------- 1 | package ritik2117; 2 | 3 | public class Shape { 4 | double height, width; 5 | 6 | Shape () { 7 | System.out.println("Deafult Constructor"); 8 | height = 0; 9 | width = 0; 10 | } 11 | 12 | Shape (double height, double width) { 13 | this.height = height; 14 | this.width = width; 15 | } 16 | 17 | double compute_area() { 18 | return 0d; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /solutions/ritik2117/Triangle.java: -------------------------------------------------------------------------------- 1 | package ritik2117; 2 | 3 | public class Triangle extends Shape { 4 | 5 | Triangle(double height, double width) { 6 | super(height, width); 7 | } 8 | 9 | @Override 10 | double compute_area() { 11 | return ((height * width)/2); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /solutions/samalprasant123/Rectangle.java: -------------------------------------------------------------------------------- 1 | package solutions.samalprasant123; 2 | 3 | public class Rectangle extends Shape { 4 | public Rectangle(double height, double width) { 5 | super(height, width); 6 | } 7 | 8 | @Override 9 | public double compute_area() { 10 | return getHeight() * getWidth(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /solutions/samalprasant123/Shape.java: -------------------------------------------------------------------------------- 1 | package solutions.samalprasant123; 2 | 3 | public class Shape { 4 | private double height; 5 | private double width; 6 | 7 | public Shape(double height, double width) { 8 | this.height = height; 9 | this.width = width; 10 | } 11 | 12 | public double getHeight() { 13 | return height; 14 | } 15 | 16 | public double getWidth() { 17 | return width; 18 | } 19 | 20 | public double compute_area() { 21 | return 0; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /solutions/samalprasant123/TestShapes.java: -------------------------------------------------------------------------------- 1 | package solutions.samalprasant123; 2 | 3 | import java.util.Scanner; 4 | 5 | public class TestShapes { 6 | public static void main(String[] args) { 7 | try (Scanner scanner = new Scanner(System.in)) { 8 | System.out.println("Enter shape(Triangle or Rectangle): "); 9 | String input = scanner.nextLine(); 10 | 11 | System.out.println("Enter height: "); 12 | double height = scanner.nextDouble(); 13 | 14 | System.out.println("Enter width: "); 15 | double width = scanner.nextDouble(); 16 | 17 | Shape shape; 18 | if (input.equals("Triangle")) { 19 | shape = new Triangle(height, width); 20 | } else { 21 | shape = new Rectangle(height, width); 22 | } 23 | 24 | printArea(shape); } 25 | } 26 | 27 | private static void printArea(Shape shape) { 28 | System.out.println("Area: " + shape.compute_area()); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /solutions/samalprasant123/Triangle.java: -------------------------------------------------------------------------------- 1 | package solutions.samalprasant123; 2 | 3 | public class Triangle extends Shape { 4 | public Triangle(double height, double width) { 5 | super(height, width); 6 | } 7 | 8 | @Override 9 | public double compute_area() { 10 | return getHeight() * getWidth() * 0.5; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /solutions/santoshjeergi/Client.java: -------------------------------------------------------------------------------- 1 | package solutions.santoshjeergi; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Client { 6 | public static void main(String[] args) { 7 | 8 | Scanner sc = new Scanner(System.in); 9 | System.out.println("Enter the shape : Triangle/Rectangle"); 10 | 11 | String input = sc.nextLine(); 12 | 13 | System.out.println("Enter height"); 14 | int hgt = sc.nextInt(); 15 | 16 | System.out.println("Enter width"); 17 | int width = sc.nextInt(); 18 | 19 | Shape s; 20 | if (input.equals("Triangle")) { 21 | s = new Triangle(hgt, width); 22 | } else { //if (input.equals("Rectangle")){ 23 | s = new Rectangle(width, hgt); 24 | } 25 | PrintArea(s); 26 | } 27 | 28 | 29 | 30 | private static void PrintArea( Shape s) { 31 | System.out.println(" s.Area : " + s.compute_area()); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /solutions/santoshjeergi/Rectangle.java: -------------------------------------------------------------------------------- 1 | package solutions.santoshjeergi; 2 | 3 | public class Rectangle extends Shape{ 4 | 5 | public Rectangle(double width, double height) { 6 | super(height, width); 7 | } 8 | 9 | @Override 10 | public double compute_area() { 11 | //return super.compute_area(); 12 | return getHeight() * getWidth() * 0.1; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /solutions/santoshjeergi/Shape.java: -------------------------------------------------------------------------------- 1 | package solutions.santoshjeergi; 2 | 3 | public class Shape { 4 | private double height; 5 | private double width; 6 | 7 | public Shape( double height, double width) { 8 | this.width = width; 9 | this.height = height; 10 | } 11 | public double compute_area() 12 | { 13 | return 0; //height * width; 14 | } 15 | 16 | public double getHeight() { 17 | return height; 18 | } 19 | 20 | public double getWidth() { 21 | return width; 22 | } 23 | } -------------------------------------------------------------------------------- /solutions/santoshjeergi/Triangle.java: -------------------------------------------------------------------------------- 1 | package solutions.santoshjeergi; 2 | 3 | 4 | public class Triangle extends Shape{ 5 | public Triangle(double height, double width) 6 | { 7 | super(height, width); 8 | } 9 | 10 | @Override 11 | public double compute_area() { 12 | //return super.compute_area(); 13 | return getHeight() * getWidth(); 14 | } 15 | } -------------------------------------------------------------------------------- /solutions/skamble89/Rectangle.java: -------------------------------------------------------------------------------- 1 | package solutions.skamble89; 2 | 3 | public class Rectangle extends Shape { 4 | public Rectangle(double height, double width) { 5 | super(height, width); 6 | } 7 | 8 | public double compute_area() { 9 | return this.height * this.width; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /solutions/skamble89/Shape.java: -------------------------------------------------------------------------------- 1 | package solutions.skamble89; 2 | 3 | public class Shape { 4 | public double height; 5 | public double width; 6 | 7 | public Shape(double height, double width) { 8 | this.height = height; 9 | this.width = width; 10 | } 11 | 12 | public double compute_area() { 13 | return 0d; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /solutions/skamble89/Test.java: -------------------------------------------------------------------------------- 1 | package solutions.skamble89; 2 | 3 | public class Test { 4 | public static void main(String[] args) { 5 | System.out.println( 6 | "For which shape would you like to compute area? Enter T/t for triangle or enter R/r for Rectangle"); 7 | 8 | String input = System.console().readLine(); 9 | System.out.println("Enter height"); 10 | Double h = Double.parseDouble(System.console().readLine()); 11 | System.out.println("Enter width"); 12 | Double w = Double.parseDouble(System.console().readLine()); 13 | 14 | Shape s; 15 | if (input.equalsIgnoreCase("T")) { 16 | s = new Triangle(h, w); 17 | } else { 18 | s = new Rectangle(h, w); 19 | } 20 | printArea(s); 21 | } 22 | 23 | private static void printArea(Shape s) { 24 | System.out.println(s.compute_area()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /solutions/skamble89/Triangle.java: -------------------------------------------------------------------------------- 1 | package solutions.skamble89; 2 | 3 | public class Triangle extends Shape { 4 | 5 | public Triangle(double height, double width) { 6 | super(height, width); 7 | } 8 | 9 | public double compute_area() { 10 | return 0.5 * this.height * this.width; 11 | } 12 | } 13 | --------------------------------------------------------------------------------