├── .gitignore
├── README.md
├── cab_booking_system
├── README.md
├── cab_booking_system.iml
└── src
│ ├── CabBookingSystemMainApp.java
│ ├── cab_manager
│ └── CabManager.java
│ ├── constants
│ └── CabBookingConstants.java
│ ├── entity
│ ├── AbstractAccount.java
│ ├── BaseCab.java
│ ├── Cab.java
│ ├── CabBookingRequest.java
│ ├── Driver.java
│ ├── Location.java
│ ├── Rider.java
│ └── RiderAccount.java
│ ├── enums
│ ├── AccountStatus.java
│ ├── CabBookingStatus.java
│ ├── CabStatus.java
│ └── RiderStatus.java
│ ├── rider_manager
│ └── RiderManager.java
│ └── service
│ ├── CabBookingService.java
│ ├── ICabAssignStrategy.java
│ └── SimpleCabAssignStrategy.java
├── deck-of-cards
├── README.md
└── src
│ └── com.deckofcards
│ ├── DeckOfCardMainApplication.java
│ ├── card
│ └── Cards.java
│ ├── deck
│ └── Deck.java
│ └── enums
│ └── Suite.java
├── food-ordering-system
├── README.md
├── food-ordering-system.iml
└── src
│ └── com.bonvivant
│ ├── BonVivantMainApp.java
│ ├── constants
│ ├── BonVivantConstants.java
│ └── RestaurantsConstants.java
│ ├── enums
│ ├── ItemStatus.java
│ ├── MenuType.java
│ ├── NotificationType.java
│ ├── OrderStatus.java
│ ├── PaymentStatus.java
│ └── RestaurantStatus.java
│ ├── exceptions
│ └── BonVivantException.java
│ ├── helper
│ └── Utils.java
│ ├── order
│ └── Order.java
│ ├── payments
│ ├── AbstractPayment.java
│ └── IPaymentStrategy.java
│ ├── restaurant
│ ├── FoodItem.java
│ ├── Menu.java
│ └── Restaurant.java
│ ├── service
│ ├── IRestaurantSelectionStratgey.java
│ ├── LowestPriceRestaurantStrategy.java
│ ├── RestaurantFinder.java
│ └── notification
│ │ └── AbstractNotification.java
│ └── system
│ ├── BonVivantRestaurantDriver.java
│ └── RestaurantManager.java
└── mychess.com
├── README.md
└── src
└── com.mychess.play
├── ChessGame.java
├── MyChessMainApp.java
├── board
└── Board.java
├── cell
└── Cell.java
├── constants
└── ChessConstants.java
├── enums
├── GameStatus.java
└── PieceColor.java
├── move
└── Moves.java
├── pieces
├── Bishop.java
├── King.java
├── Knight.java
├── Pawn.java
├── Piece.java
├── Queen.java
└── Rook.java
└── player
└── Player.java
/.gitignore:
--------------------------------------------------------------------------------
1 | # Compiled class file
2 | *.class
3 |
4 | # Log file
5 | *.log
6 |
7 | #editor dirs
8 | .vscode
9 | .idea
10 |
11 | # System files
12 | DS_Store
13 |
14 | # BlueJ files
15 | *.ctxt
16 |
17 | # Mobile Tools for Java (J2ME)
18 | .mtj.tmp/
19 |
20 | # Package Files #
21 | *.jar
22 | *.war
23 | *.nar
24 | *.ear
25 | *.zip
26 | *.tar.gz
27 | *.rar
28 |
29 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
30 | hs_err_pid*
31 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 | #### LLD implemented for following sample problems
3 | - [Deck of cards](https://github.com/imk13/low-level-design/blob/master/deck-of-cards)
4 | - [Mychess](https://github.com/imk13/low-level-design/blob/master/mychess.com)
5 | - [Cab booking system](https://github.com/imk13/low-level-design/blob/master/cab_booking_system)
6 | - [Food ordering system](https://github.com/imk13/low-level-design/blob/master/food-ordering-system)
7 | - [Simple Board Snake](https://github.com/imk13/play_snake)
8 | - [Battleship Game](https://github.com/imk13/battleship_game)
9 |
--------------------------------------------------------------------------------
/cab_booking_system/README.md:
--------------------------------------------------------------------------------
1 | # Cab booking system
2 |
--------------------------------------------------------------------------------
/cab_booking_system/cab_booking_system.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/cab_booking_system/src/CabBookingSystemMainApp.java:
--------------------------------------------------------------------------------
1 | import cab_manager.CabManager;
2 | import entity.Cab;
3 | import entity.CabBookingRequest;
4 | import entity.Driver;
5 | import entity.Location;
6 | import entity.Rider;
7 | import entity.RiderAccount;
8 | import java.sql.DriverAction;
9 | import rider_manager.RiderManager;
10 |
11 | public class CabBookingSystemMainApp {
12 | public static void main(String[] args) {
13 | System.out.println("Init bab booking system...");
14 | Location cabLocation1 = new Location(10, 10);
15 | Location cabLocation2 = new Location(100, 100);
16 | Location cabLocation3 = new Location(10, 5);
17 |
18 | Driver driver1 = new Driver("Suresh1");
19 | Driver driver2 = new Driver("Suresh2");
20 | Driver driver3 = new Driver("Suresh3");
21 |
22 | Cab cab1 = new Cab(driver1, "AB1234", cabLocation1);
23 |
24 | Cab cab2 = new Cab(driver2, "DE1234", cabLocation2);
25 |
26 | Cab cab3 = new Cab(driver3, "FG1234", cabLocation3);
27 |
28 | CabManager.getInstance().addCab(cab1);
29 | CabManager.getInstance().addCab(cab2);
30 | CabManager.getInstance().addCab(cab3);
31 |
32 | Location riderLocation = new Location(5, 5);
33 | RiderAccount riderAccount = new RiderAccount("Mukesh");
34 | Rider rider = new Rider(riderAccount, riderLocation);
35 |
36 | RiderManager.getInstance().addRider(rider);
37 |
38 | CabBookingRequest cabBookingRequest = RiderManager.getInstance().requestCab(rider, new Location(20, 20));
39 |
40 | System.out.println(cabBookingRequest);
41 |
42 | System.out.println(cab3.getStatus());
43 |
44 | System.out.println(rider.getStatus());
45 |
46 | System.out.println(RiderManager.getInstance().findRider("Mukesh").getStatus());
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/cab_booking_system/src/cab_manager/CabManager.java:
--------------------------------------------------------------------------------
1 | package cab_manager;
2 |
3 | import entity.Cab;
4 | import enums.CabStatus;
5 | import java.util.ArrayList;
6 | import java.util.HashMap;
7 |
8 | public class CabManager {
9 | private HashMap cabHashMap;
10 | private static CabManager instance = null;
11 | private CabManager(){
12 | cabHashMap = new HashMap<>();
13 | }
14 |
15 | public static CabManager getInstance() {
16 | if(instance == null){
17 | instance = new CabManager();
18 | }
19 | return instance;
20 | }
21 |
22 | public Cab findCab(String license_plate){
23 | if(cabHashMap.containsKey(license_plate)){
24 | return cabHashMap.get(license_plate);
25 | }
26 | return null;
27 | }
28 |
29 | public void addCab(Cab cab){
30 | if(!cabHashMap.containsKey(cab.getLicense_plate())){
31 | cabHashMap.put(cab.getLicense_plate(), cab);
32 | }
33 | }
34 |
35 | public void removeCab(Cab cab){
36 | if(cabHashMap.containsKey(cab.getLicense_plate())){
37 | cabHashMap.remove(cab.getLicense_plate());
38 | }
39 | }
40 |
41 | public ArrayList findAvailableCabs(){
42 | ArrayList cabs = new ArrayList<>();
43 | for(String lp : cabHashMap.keySet()){
44 | Cab cab1 = cabHashMap.get(lp);
45 | if(cab1.getStatus() == CabStatus.AVAILABLE){
46 | cabs.add(cab1);
47 | }
48 | }
49 | return cabs;
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/cab_booking_system/src/constants/CabBookingConstants.java:
--------------------------------------------------------------------------------
1 | package constants;
2 |
3 | public class CabBookingConstants {
4 | public static Double MAX_PICKUP_DISTANCE = 10.0;
5 | public static Double CAB_BASE_FARE = 10.0;
6 | public static Double CAB_PER_KM_CHARGE = 10.0;
7 | }
8 |
--------------------------------------------------------------------------------
/cab_booking_system/src/entity/AbstractAccount.java:
--------------------------------------------------------------------------------
1 | package entity;
2 |
3 | import enums.AccountStatus;
4 |
5 | public abstract class AbstractAccount {
6 | String name;
7 | String phone;
8 | String dob;
9 | String address;
10 | AccountStatus status;
11 |
12 | public AbstractAccount(String name){
13 | this.name = name;
14 | this.status = AccountStatus.ACTIVE;
15 | }
16 |
17 | public AbstractAccount(String name, String phone){
18 | this.name = name;
19 | this.status = AccountStatus.ACTIVE;
20 | this.phone = phone;
21 | }
22 |
23 | public AbstractAccount(String name, String phone, String dob){
24 | this.name = name;
25 | this.dob = dob;
26 | this.phone = phone;
27 | this.status = AccountStatus.ACTIVE;
28 | }
29 |
30 | public AbstractAccount(String name, String phone, String dob, String address){
31 | this.name = name;
32 | this.dob = dob;
33 | this.address = address;
34 | this.phone = phone;
35 | this.status = AccountStatus.ACTIVE;
36 | }
37 |
38 | public String getName() {
39 | return name;
40 | }
41 |
42 | public String getAddress() {
43 | return address;
44 | }
45 |
46 | public String getDob() {
47 | return dob;
48 | }
49 |
50 | public AccountStatus getStatus() {
51 | return status;
52 | }
53 |
54 | public String getPhone() {
55 | return phone;
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/cab_booking_system/src/entity/BaseCab.java:
--------------------------------------------------------------------------------
1 | package entity;
2 |
3 | import enums.CabStatus;
4 | import java.util.HashMap;
5 | import java.util.UUID;
6 |
7 | public class BaseCab {
8 | Driver driver;
9 | Location location;
10 | CabStatus status;
11 | String license_plate;
12 | String color;
13 |
14 | public BaseCab(Driver driver, String license_plate, Location location){
15 | this.driver = driver;
16 | this.license_plate = license_plate;
17 | this.color = "UNKNOWN";
18 | this.location = location;
19 | this.status = CabStatus.AVAILABLE;
20 | }
21 |
22 | public BaseCab(Driver driver, String license_plate, Location location, String color){
23 | this.driver = driver;
24 | this.license_plate = license_plate;
25 | this.color = color;
26 | this.location = location;
27 | this.status = CabStatus.AVAILABLE;
28 | }
29 |
30 | public CabStatus getStatus() {
31 | return status;
32 | }
33 |
34 | public Driver getDriver() {
35 | return driver;
36 | }
37 |
38 | public Location getLocation() {
39 | return location;
40 | }
41 |
42 | public String getLicense_plate() {
43 | return license_plate;
44 | }
45 |
46 | public String getColor() {
47 | return color;
48 | }
49 |
50 | public void setStatus(CabStatus status) {
51 | this.status = status;
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/cab_booking_system/src/entity/Cab.java:
--------------------------------------------------------------------------------
1 | package entity;
2 |
3 | import constants.CabBookingConstants;
4 | import java.util.HashMap;
5 |
6 | public class Cab extends BaseCab {
7 | Double baseFare;
8 | Double perKMFare;
9 | public Cab(Driver driver, String license_plate, Location location){
10 | super(driver, license_plate, location);
11 | this.baseFare = CabBookingConstants.CAB_BASE_FARE;
12 | this.perKMFare = CabBookingConstants.CAB_PER_KM_CHARGE;
13 | }
14 |
15 | public Cab(Driver driver, String license_plate, Location location, String color){
16 | super(driver, license_plate, location, color);
17 | this.baseFare = CabBookingConstants.CAB_BASE_FARE;
18 | this.perKMFare = CabBookingConstants.CAB_PER_KM_CHARGE;
19 | }
20 |
21 | public Double getBaseFare() {
22 | return baseFare;
23 | }
24 |
25 | public Double getPerKMFare() {
26 | return perKMFare;
27 | }
28 |
29 | public void setBaseFare(Double baseFare) {
30 | this.baseFare = baseFare;
31 | }
32 |
33 | public void setPerKMFare(Double perKMFare) {
34 | this.perKMFare = perKMFare;
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/cab_booking_system/src/entity/CabBookingRequest.java:
--------------------------------------------------------------------------------
1 | package entity;
2 |
3 | import enums.CabBookingStatus;
4 | import java.time.LocalDateTime;
5 | import java.util.UUID;
6 |
7 | public class CabBookingRequest {
8 | UUID id;
9 | Location from;
10 | Location to;
11 | Rider riderInfo;
12 | LocalDateTime date;
13 | CabBookingStatus status;
14 | String assignedTo;
15 |
16 | public CabBookingRequest(Location from, Location to, Rider rider) {
17 | this.id = UUID.randomUUID();
18 | this.from = from;
19 | this.to = to;
20 | this.riderInfo = rider;
21 | this.status = CabBookingStatus.PENDING;
22 | this.date = LocalDateTime.now();
23 | }
24 |
25 | public CabBookingStatus getStatus() {
26 | return status;
27 | }
28 |
29 | public Location getFrom() {
30 | return from;
31 | }
32 |
33 | public Location getTo() {
34 | return to;
35 | }
36 |
37 | public Rider getRiderInfo() {
38 | return riderInfo;
39 | }
40 |
41 | public LocalDateTime getDate() {
42 | return date;
43 | }
44 |
45 | public void setStatus(CabBookingStatus status) {
46 | this.status = status;
47 | }
48 |
49 | public void setTo(Location to) {
50 | this.to = to;
51 | }
52 |
53 | public UUID getId() {
54 | return id;
55 | }
56 |
57 | public String getAssignedTo() {
58 | return assignedTo;
59 | }
60 |
61 | public void setAssignedTo(String assignedTo) {
62 | this.assignedTo = assignedTo;
63 | }
64 |
65 | @Override
66 | public String toString(){
67 | return "[CabBookingRequest :" + " id=" + this.id.toString() + " from=" + this.from.toString()
68 | + " to=" + this.to.toString() + " Rider=" + this.riderInfo.getRiderAccount().getName()
69 | + " Date=" + this.date.toString() + " Status=" + this.status.toString() + " assignedTo=" + this.assignedTo +"]";
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/cab_booking_system/src/entity/Driver.java:
--------------------------------------------------------------------------------
1 | package entity;
2 |
3 | import java.util.HashMap;
4 | import java.util.UUID;
5 |
6 | public class Driver extends AbstractAccount {
7 | Double rating;
8 | HashMap cabBookingRequests;
9 |
10 | public Driver(String name) {
11 | super(name);
12 | this.rating = 3.0;
13 | cabBookingRequests = new HashMap<>();
14 | }
15 |
16 | public Driver(String name, String dob) {
17 | super(name, dob);
18 | }
19 |
20 | public Driver(String name, String dob, String address) {
21 | super(name, dob, address);
22 | }
23 |
24 | public void addBookingRequest(CabBookingRequest cabBookingRequest){
25 | if(!cabBookingRequests.containsKey(cabBookingRequest.getId())){
26 | cabBookingRequests.put(cabBookingRequest.getId(), cabBookingRequest);
27 | }
28 | }
29 |
30 | public void updateBookingRequest(CabBookingRequest cabBookingRequest){
31 | if(cabBookingRequests.containsKey(cabBookingRequest.getId())){
32 | cabBookingRequests.put(cabBookingRequest.getId(), cabBookingRequest);
33 | }
34 | }
35 |
36 | public CabBookingRequest findBookingRequest(UUID id){
37 | if(cabBookingRequests.containsKey(id)){
38 | return cabBookingRequests.get(id);
39 | }
40 | System.out.println("Cab booking request not found with id"+ id.toString());
41 | return null;
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/cab_booking_system/src/entity/Location.java:
--------------------------------------------------------------------------------
1 | package entity;
2 |
3 | public class Location {
4 | Integer x;
5 | Integer y;
6 |
7 | public Location(Integer x, Integer y) {
8 | this.x = x;
9 | this.y = y;
10 | }
11 |
12 | public Integer getX() {
13 | return x;
14 | }
15 |
16 | public Integer getY() {
17 | return y;
18 | }
19 |
20 | public void setX(Integer x) {
21 | this.x = x;
22 | }
23 |
24 | public void setY(Integer y) {
25 | this.y = y;
26 | }
27 |
28 | @Override
29 | public String toString(){
30 | return "[Location:" + " x=" + this.x + " y=" + this.y + "]";
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/cab_booking_system/src/entity/Rider.java:
--------------------------------------------------------------------------------
1 | package entity;
2 |
3 | import enums.CabBookingStatus;
4 | import enums.RiderStatus;
5 | import java.util.ArrayList;
6 | import java.util.HashMap;
7 | import java.util.UUID;
8 |
9 | public class Rider {
10 | RiderAccount riderAccount;
11 | RiderStatus status;
12 | Location location;
13 | Double rating;
14 | HashMap bookingRequests;
15 |
16 | public Rider(RiderAccount riderAccount, Location location){
17 | this.bookingRequests = new HashMap<>();
18 | this.status = RiderStatus.FREE;
19 | this.location = location;
20 | this.riderAccount = riderAccount;
21 | this.rating = 3.0;
22 | }
23 |
24 | public HashMap getBookingRequests() {
25 | return bookingRequests;
26 | }
27 |
28 | public Location getLocation() {
29 | return location;
30 | }
31 |
32 | public RiderAccount getRiderAccount() {
33 | return riderAccount;
34 | }
35 |
36 | public RiderStatus getStatus() {
37 | return status;
38 | }
39 |
40 | public void setStatus(RiderStatus status) {
41 | this.status = status;
42 | }
43 |
44 | public void setLocation(Location location) {
45 | this.location = location;
46 | }
47 |
48 | public void addBookingRequest(CabBookingRequest cabBookingRequest){
49 | if(!bookingRequests.containsKey(cabBookingRequest.getId())){
50 | bookingRequests.put(cabBookingRequest.getId(), cabBookingRequest);
51 | }
52 | }
53 |
54 | public void updateBookingRequest(CabBookingRequest cabBookingRequest){
55 | if(bookingRequests.containsKey(cabBookingRequest.getId())){
56 | bookingRequests.put(cabBookingRequest.getId(), cabBookingRequest);
57 | }
58 | }
59 |
60 | public CabBookingRequest findBookingRequest(UUID id){
61 | if(bookingRequests.containsKey(id)){
62 | return bookingRequests.get(id);
63 | }
64 | System.out.println("Cab booking request not found with id"+ id.toString());
65 | return null;
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/cab_booking_system/src/entity/RiderAccount.java:
--------------------------------------------------------------------------------
1 | package entity;
2 |
3 | public class RiderAccount extends AbstractAccount {
4 |
5 | public RiderAccount(String name) {
6 | super(name);
7 | }
8 |
9 | public RiderAccount(String name, String dob) {
10 | super(name, dob);
11 | }
12 |
13 | public RiderAccount(String name, String dob, String address) {
14 | super(name, dob, address);
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/cab_booking_system/src/enums/AccountStatus.java:
--------------------------------------------------------------------------------
1 | package enums;
2 |
3 | public enum AccountStatus {
4 | IN_ACTIVE,ACTIVE;
5 | }
6 |
--------------------------------------------------------------------------------
/cab_booking_system/src/enums/CabBookingStatus.java:
--------------------------------------------------------------------------------
1 | package enums;
2 |
3 | public enum CabBookingStatus {
4 | PENDING,BOOKED,COMPLETED,DENIED,CANCELLED;
5 | }
6 |
--------------------------------------------------------------------------------
/cab_booking_system/src/enums/CabStatus.java:
--------------------------------------------------------------------------------
1 | package enums;
2 |
3 | public enum CabStatus {
4 | NOT_AVAILABLE, AVAILABLE, BUSY;
5 | }
6 |
--------------------------------------------------------------------------------
/cab_booking_system/src/enums/RiderStatus.java:
--------------------------------------------------------------------------------
1 | package enums;
2 |
3 | public enum RiderStatus {
4 | FREE,IN_RIDE;
5 | }
6 |
--------------------------------------------------------------------------------
/cab_booking_system/src/rider_manager/RiderManager.java:
--------------------------------------------------------------------------------
1 | package rider_manager;
2 |
3 | import entity.CabBookingRequest;
4 | import entity.Location;
5 | import entity.Rider;
6 | import java.util.HashMap;
7 | import service.CabBookingService;
8 | import service.SimpleCabAssignStrategy;
9 |
10 | public class RiderManager {
11 | HashMap riderHashMap;
12 | private static RiderManager instance = null;
13 | private RiderManager(){
14 | riderHashMap = new HashMap<>();
15 | }
16 |
17 | public static RiderManager getInstance(){
18 | if(instance == null){
19 | instance = new RiderManager();
20 | }
21 | return instance;
22 | }
23 |
24 | public Rider findRider(String name) {
25 | if(riderHashMap.containsKey(name)){
26 | return riderHashMap.get(name);
27 | }
28 | return null;
29 | }
30 | public void addRider(Rider rider){
31 | if(!riderHashMap.containsKey(rider.getRiderAccount().getName())){
32 | riderHashMap.put(rider.getRiderAccount().getName(), rider);
33 | }
34 | }
35 |
36 | public void removeRider(Rider rider){
37 | if(riderHashMap.containsKey(rider.getRiderAccount().getName())){
38 | riderHashMap.remove(rider.getRiderAccount().getName());
39 | }
40 | }
41 |
42 | public CabBookingRequest requestCab(Rider rider, Location toLocation){
43 | CabBookingRequest cabBookingRequest = new CabBookingRequest(rider.getLocation(), toLocation, rider);
44 | new CabBookingService().useStrategy(new SimpleCabAssignStrategy()).assignCab(cabBookingRequest);
45 | return cabBookingRequest;
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/cab_booking_system/src/service/CabBookingService.java:
--------------------------------------------------------------------------------
1 | package service;
2 |
3 | import entity.Cab;
4 | import entity.CabBookingRequest;
5 | import enums.CabBookingStatus;
6 | import enums.CabStatus;
7 | import enums.RiderStatus;
8 |
9 | public class CabBookingService {
10 | ICabAssignStrategy cabAssignStrategy;
11 |
12 | public CabBookingService(){
13 | cabAssignStrategy = null;
14 | }
15 |
16 | public CabBookingService useStrategy(ICabAssignStrategy cabAssignStrategy){
17 | this.cabAssignStrategy = cabAssignStrategy;
18 | return this;
19 | }
20 |
21 | public void assignCab(CabBookingRequest cabBookingRequest){
22 | if(cabAssignStrategy == null){
23 | System.out.println("Cab strategy not defined!");
24 | return;
25 | }
26 | Cab foundCab = this.cabAssignStrategy.assignCab(cabBookingRequest);
27 | if(foundCab != null){
28 | foundCab.setStatus(CabStatus.BUSY);
29 | cabBookingRequest.setStatus(CabBookingStatus.BOOKED);
30 | cabBookingRequest.setAssignedTo(foundCab.getLicense_plate());
31 | cabBookingRequest.getRiderInfo().setStatus(RiderStatus.IN_RIDE);
32 | cabBookingRequest.getRiderInfo().addBookingRequest(cabBookingRequest);
33 | foundCab.getDriver().addBookingRequest(cabBookingRequest);
34 | }else{
35 | System.out.println("Cab not found in your location, please try after some time!");
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/cab_booking_system/src/service/ICabAssignStrategy.java:
--------------------------------------------------------------------------------
1 | package service;
2 |
3 | import entity.Cab;
4 | import entity.CabBookingRequest;
5 |
6 | public interface ICabAssignStrategy {
7 | Cab assignCab(CabBookingRequest cabBookingRequest);
8 | }
9 |
--------------------------------------------------------------------------------
/cab_booking_system/src/service/SimpleCabAssignStrategy.java:
--------------------------------------------------------------------------------
1 | package service;
2 |
3 | import cab_manager.CabManager;
4 | import constants.CabBookingConstants;
5 | import entity.Cab;
6 | import entity.CabBookingRequest;
7 | import entity.Location;
8 | import java.util.ArrayList;
9 | import org.omg.CORBA.INTERNAL;
10 |
11 | public class SimpleCabAssignStrategy implements ICabAssignStrategy {
12 |
13 | @Override
14 | public Cab assignCab(CabBookingRequest cabBookingRequest){
15 | ArrayList cabs = CabManager.getInstance().findAvailableCabs();
16 | Double minDistance = CabBookingConstants.MAX_PICKUP_DISTANCE;
17 | Cab foundCab = null;
18 | for(int i = 0; i < cabs.size(); i++) {
19 | // max pickup distance logic and return one cab;
20 | Double distance = calculateDistance(cabs.get(i).getLocation(), cabBookingRequest.getRiderInfo().getLocation());
21 | if(distance <= minDistance){
22 | minDistance = distance;
23 | foundCab = cabs.get(i);
24 | }
25 | }
26 | return foundCab;
27 | }
28 |
29 | private Integer getDiffSquare(Integer n1, Integer n2) {
30 | return (n1-n2) * (n1-n2);
31 | }
32 | private Double calculateDistance(Location p1, Location p2){
33 | return Math.sqrt(getDiffSquare(p1.getX(), p2.getX()) + getDiffSquare(p1.getY(), p2.getY()));
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/deck-of-cards/README.md:
--------------------------------------------------------------------------------
1 | # Deck of cards
2 |
--------------------------------------------------------------------------------
/deck-of-cards/src/com.deckofcards/DeckOfCardMainApplication.java:
--------------------------------------------------------------------------------
1 | import deck.Deck;
2 |
3 | public class DeckOfCardMainApplication {
4 |
5 | public static void main(String [] args ) {
6 | System.out.println("Hello world!");
7 | Deck deck = new Deck();
8 | System.out.printf("First Card Suite %s and %d value.\n", deck.getCardsDeck().get(0).getSuite().toString(), deck.getCardsDeck().get(0).getValue());
9 | }
10 | };
--------------------------------------------------------------------------------
/deck-of-cards/src/com.deckofcards/card/Cards.java:
--------------------------------------------------------------------------------
1 | package card;
2 |
3 | import enums.Suite;
4 |
5 | /**
6 | * Card class to represent particular card
7 | */
8 | public class Cards {
9 | int value;
10 | Suite suite;
11 |
12 | public Cards(int faceValue, Suite assignSuite){
13 | value = faceValue;
14 | suite = assignSuite;
15 | }
16 |
17 | public int getValue() {
18 | return value;
19 | }
20 |
21 | public Suite getSuite() {
22 | return suite;
23 | }
24 | }
--------------------------------------------------------------------------------
/deck-of-cards/src/com.deckofcards/deck/Deck.java:
--------------------------------------------------------------------------------
1 | package deck;
2 |
3 | import java.util.ArrayList;
4 | import card.Cards;
5 | import enums.Suite;
6 |
7 |
8 | public class Deck {
9 | private ArrayList cardsDeck = new ArrayList<>();
10 |
11 | public Deck() {
12 | for(int faceValue = 1; faceValue <= 13; faceValue++) {
13 | for(Suite suite: Suite.values()) {
14 | cardsDeck.add(new Cards(faceValue, suite));
15 | }
16 | }
17 | }
18 |
19 | /**
20 | * @return the cardsDeck
21 | */
22 | public ArrayList getCardsDeck() {
23 | return cardsDeck;
24 | }
25 | }
--------------------------------------------------------------------------------
/deck-of-cards/src/com.deckofcards/enums/Suite.java:
--------------------------------------------------------------------------------
1 | package enums;
2 |
3 | public enum Suite {
4 | DIAMOND,SPADE,CLUB,HEART;
5 | }
--------------------------------------------------------------------------------
/food-ordering-system/README.md:
--------------------------------------------------------------------------------
1 | # Food ordering system
2 |
--------------------------------------------------------------------------------
/food-ordering-system/food-ordering-system.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/food-ordering-system/src/com.bonvivant/BonVivantMainApp.java:
--------------------------------------------------------------------------------
1 | import java.util.HashMap;
2 | import java.util.Random;
3 | import enums.MenuType;
4 | import restaurant.Menu;
5 | import restaurant.Restaurant;
6 | import restaurant.FoodItem;
7 | import system.BonVivantRestaurantDriver;
8 | import system.RestaurantManager;
9 |
10 | public class BonVivantMainApp {
11 | public static void main(String [] args){
12 | System.out.println("Initializing Food Ordering System...");
13 | /**
14 | * Initiating restaurant driver
15 | */
16 | BonVivantRestaurantDriver bonVivantRestaurantDriver = BonVivantRestaurantDriver.getInstance();
17 |
18 | RestaurantManager restaurantManager = bonVivantRestaurantDriver.getRestaurantManager();
19 |
20 | try {
21 | Menu r1Menu = new Menu();
22 | r1Menu.addFoodItem(new FoodItem("BigFry", MenuType.VEG, 100.0));
23 | r1Menu.addFoodItem(new FoodItem("BigFish", MenuType.NONVEG, 200.0));
24 | r1Menu.addFoodItem(new FoodItem("BigFryFish", MenuType.NONVEG, 400.0));
25 |
26 | Restaurant restaurant1 = new Restaurant("HelloFoodie", getRandomCapacity(), r1Menu);
27 |
28 | restaurantManager.addRestaunrant(restaurant1);
29 |
30 | Restaurant restaurant2 = new Restaurant.Builder("FoodieFoody").setCapacity(getRandomCapacity()).build();
31 | Menu r2Menu = new Menu();
32 | r2Menu.addFoodItem(new FoodItem("BigFry", MenuType.VEG, 100.0));
33 | r2Menu.addFoodItem(new FoodItem("BigFish", MenuType.NONVEG, 200.0));
34 | restaurant2.setMenu(r2Menu);
35 | restaurantManager.addRestaunrant(restaurant2);
36 |
37 | Restaurant restaurant3 = new Restaurant.Builder("I_AM_HUNGRY").setCapacity(getRandomCapacity()).build();
38 | Menu r3Menu = new Menu();
39 | r3Menu.addFoodItem(new FoodItem("BigFry", MenuType.VEG, 100.0));
40 | r3Menu.addFoodItem(new FoodItem("BigFish", MenuType.NONVEG, 200.0));
41 | r3Menu.addFoodItem(new FoodItem("BigFryFish", MenuType.NONVEG, 300.0));
42 | restaurant3.setMenu(r3Menu);
43 | restaurantManager.addRestaunrant(restaurant3);
44 |
45 | System.out.println(restaurantManager.findRestaurant("HelloFoodie"));
46 | // Find restaurant by order items
47 |
48 | HashMap orderItems = new HashMap<>();
49 | orderItems.put("BigFish", 2);
50 | orderItems.put("BigFryFish", 2);
51 |
52 | System.out.println(restaurantManager.findBy(orderItems).getName());
53 | }catch (Exception ex){
54 | System.out.println(ex.getMessage());
55 | }
56 |
57 | }
58 |
59 | static Integer getRandomCapacity() {
60 | Integer MAX_CAPACITY = 10;
61 | return (new Random().nextInt(MAX_CAPACITY) + 1);
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/food-ordering-system/src/com.bonvivant/constants/BonVivantConstants.java:
--------------------------------------------------------------------------------
1 | package constants;
2 |
3 | public class BonVivantConstants {
4 | public static Integer DEFAULT_MAX_RESTAURANT_CAPACITY = 5;
5 | }
6 |
--------------------------------------------------------------------------------
/food-ordering-system/src/com.bonvivant/constants/RestaurantsConstants.java:
--------------------------------------------------------------------------------
1 | package constants;
2 |
3 | public class RestaurantsConstants {
4 | }
5 |
--------------------------------------------------------------------------------
/food-ordering-system/src/com.bonvivant/enums/ItemStatus.java:
--------------------------------------------------------------------------------
1 | package enums;
2 |
3 | public enum ItemStatus {
4 | OUT_OF_STOCK,AVAILABLE;
5 | }
6 |
--------------------------------------------------------------------------------
/food-ordering-system/src/com.bonvivant/enums/MenuType.java:
--------------------------------------------------------------------------------
1 | package enums;
2 | public enum MenuType {
3 | VEG,NONVEG,DESERT;
4 | }
5 |
--------------------------------------------------------------------------------
/food-ordering-system/src/com.bonvivant/enums/NotificationType.java:
--------------------------------------------------------------------------------
1 | package enums;
2 | public enum NotificationType {
3 | EMAIL,SMS,PUSH;
4 | }
5 |
--------------------------------------------------------------------------------
/food-ordering-system/src/com.bonvivant/enums/OrderStatus.java:
--------------------------------------------------------------------------------
1 | package enums;
2 |
3 | public enum OrderStatus {
4 | PENDING, ACCEPTED, PREPARING, DISPATCHED, ABORTED, CANCELLED;
5 | }
6 |
--------------------------------------------------------------------------------
/food-ordering-system/src/com.bonvivant/enums/PaymentStatus.java:
--------------------------------------------------------------------------------
1 | package enums;
2 |
3 | public enum PaymentStatus {
4 | PENDING, SUCCESS, FAILED, CANCELLED, REFUND;
5 | }
6 |
--------------------------------------------------------------------------------
/food-ordering-system/src/com.bonvivant/enums/RestaurantStatus.java:
--------------------------------------------------------------------------------
1 | package enums;
2 | public enum RestaurantStatus {
3 | CLOSED,OPEN;
4 | }
5 |
--------------------------------------------------------------------------------
/food-ordering-system/src/com.bonvivant/exceptions/BonVivantException.java:
--------------------------------------------------------------------------------
1 | package exceptions;
2 |
3 | public class BonVivantException extends Exception{
4 | private static final long serialVersionUID = 1L;
5 |
6 | public BonVivantException(String message){
7 | super(message);
8 | }
9 |
10 | public BonVivantException(String message, Throwable cause) {
11 | super(message, cause);
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/food-ordering-system/src/com.bonvivant/helper/Utils.java:
--------------------------------------------------------------------------------
1 | package helper;
2 |
3 | import java.util.UUID;
4 |
5 | public class Utils {
6 | public static UUID generateUUID() {
7 | return UUID.randomUUID();
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/food-ordering-system/src/com.bonvivant/order/Order.java:
--------------------------------------------------------------------------------
1 | package order;
2 |
3 | import java.util.ArrayList;
4 | import java.util.HashMap;
5 | import java.util.UUID;
6 | import enums.OrderStatus;
7 | import java.util.stream.Collectors;
8 | import restaurant.FoodItem;
9 | import restaurant.Restaurant;
10 |
11 | public class Order {
12 | UUID id;
13 | //item and quantity
14 | HashMap items;
15 | Double amount;
16 | OrderStatus status;
17 | UUID placedAt;
18 |
19 | public Order(HashMap orderItems, UUID restaurantUuid){
20 | this.id = UUID.randomUUID();
21 | this.items = orderItems;
22 | this.amount = calculateOrderAmount(orderItems);
23 | this.status = OrderStatus.PENDING;
24 | this.placedAt = restaurantUuid;
25 | }
26 |
27 | private Double calculateOrderAmount(HashMap orderItems){
28 | Double orderAmount = 0.0;
29 | for(FoodItem foodItem: orderItems.keySet()){
30 | // quantity * item price
31 | orderAmount += (orderItems.get(foodItem) * foodItem.getPrice());
32 | }
33 | return orderAmount;
34 | }
35 |
36 | /**
37 | * @return the amount
38 | */
39 | public Double getAmount() {
40 | return amount;
41 | }
42 |
43 | /**
44 | * @return the id
45 | */
46 | public UUID getId() {
47 | return id;
48 | }
49 |
50 | /**
51 | * @return the orderItems
52 | */
53 | public ArrayList getOrderItems() {
54 | return items.keySet().stream().collect(Collectors.toCollection(ArrayList::new));
55 | }
56 |
57 | public HashMap getItems() {
58 | return items;
59 | }
60 |
61 | /**
62 | * @return the placedAt
63 | */
64 | public UUID getPlacedAt() {
65 | return placedAt;
66 | }
67 |
68 | /**
69 | * @param placedAt the placedAt to set
70 | */
71 | public void setPlacedAt(UUID placedAt) {
72 | this.placedAt = placedAt;
73 | }
74 |
75 | /**
76 | * @return the status
77 | */
78 | public OrderStatus getStatus() {
79 | return status;
80 | }
81 |
82 | /**
83 | * @param status the status to set
84 | */
85 | public void setStatus(OrderStatus status) {
86 | this.status = status;
87 | }
88 | }
89 |
--------------------------------------------------------------------------------
/food-ordering-system/src/com.bonvivant/payments/AbstractPayment.java:
--------------------------------------------------------------------------------
1 | package payments;
2 |
3 | import order.Order;
4 |
5 | // TBD
6 | public class AbstractPayment {
7 | IPaymentStrategy paymentStrategy;
8 | Order order;
9 |
10 | public void doPayment(Order order) {
11 | paymentStrategy.pay(order);
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/food-ordering-system/src/com.bonvivant/payments/IPaymentStrategy.java:
--------------------------------------------------------------------------------
1 | package payments;
2 |
3 | import order.Order;
4 |
5 | public interface IPaymentStrategy {
6 | void pay(Order order);
7 | }
8 |
--------------------------------------------------------------------------------
/food-ordering-system/src/com.bonvivant/restaurant/FoodItem.java:
--------------------------------------------------------------------------------
1 | package restaurant;
2 |
3 | import enums.ItemStatus;
4 | import enums.MenuType;
5 | import enums.MenuType;
6 |
7 | public class FoodItem {
8 | private String name;
9 | private ItemStatus status;
10 | private MenuType category;
11 | private Double price;
12 |
13 | public FoodItem(String itemName, MenuType category, Double price){
14 | this.name = itemName;
15 | this.category = category;
16 | this.status = ItemStatus.AVAILABLE;
17 | this.price = price;
18 | }
19 |
20 | public FoodItem(String itemName, MenuType category, Double price,ItemStatus foodStatus){
21 | this.name = itemName;
22 | this.category = category;
23 | this.status = foodStatus;
24 | this.price = price;
25 | }
26 |
27 | /**
28 | * @return the name
29 | */
30 | public String getName() {
31 | return name;
32 | }
33 |
34 | /**
35 | * @param name the name to set
36 | */
37 | public void setName(String name) {
38 | this.name = name;
39 | }
40 |
41 | /**
42 | * @return the status
43 | */
44 | public ItemStatus getStatus() {
45 | return status;
46 | }
47 |
48 | /**
49 | * @param status the status to set
50 | */
51 | public void setStatus(ItemStatus status) {
52 | this.status = status;
53 | }
54 |
55 | /**
56 | * @return the category
57 | */
58 | public MenuType getCategory() {
59 | return category;
60 | }
61 |
62 | /**
63 | * @param category the category to set
64 | */
65 | public void setCategory(MenuType category) {
66 | this.category = category;
67 | }
68 |
69 | /**
70 | * @return the price
71 | */
72 | public Double getPrice() {
73 | return price;
74 | }
75 |
76 | /**
77 | * @param price the price to set
78 | */
79 | public void setPrice(Double price) {
80 | this.price = price;
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/food-ordering-system/src/com.bonvivant/restaurant/Menu.java:
--------------------------------------------------------------------------------
1 | package restaurant;
2 |
3 | import java.util.ArrayList;
4 | import java.util.HashMap;
5 | import enums.MenuType;
6 | import exceptions.BonVivantException;
7 | import java.util.List;
8 | import restaurant.FoodItem;
9 |
10 | public class Menu {
11 | private HashMap> menuMap;
12 |
13 | public Menu(){
14 | resetMenu();
15 | }
16 | /**
17 | * @return the List of all FoodItem
18 | */
19 | public List allItems() {
20 | ArrayList allFooddItems = new ArrayList<>();
21 | for(MenuType menuType: MenuType.values()){
22 | HashMap foodMenuMap = menuMap.get(menuType);
23 | allFooddItems.addAll(foodMenuMap.values());
24 | }
25 | return allFooddItems;
26 | }
27 |
28 | public void addFoodItem(FoodItem item){
29 | if(item.getCategory() == null){
30 | //throw new BonVivantException("FoodItem menu category is missing.");
31 | return;
32 | }
33 | if(menuMap.get(item.getCategory()) == null){
34 | menuMap.put(item.getCategory(), new HashMap<>());
35 | }
36 | menuMap.get(item.getCategory()).put(item.getName(), item);
37 | }
38 |
39 | public FoodItem findFoodItem(String itemName){
40 | for(MenuType menuType: MenuType.values()){
41 | HashMap foodMenuMap = menuMap.get(menuType);
42 | if(foodMenuMap != null && foodMenuMap.containsKey(itemName)){
43 | return foodMenuMap.get(itemName);
44 | }
45 | }
46 | return null;
47 | }
48 |
49 | public FoodItem findFoodItem(FoodItem foodItem){
50 | HashMap foodMenuMap = menuMap.get(foodItem.getCategory());
51 | if(foodMenuMap.containsKey(foodItem.getName())){
52 | return foodMenuMap.get(foodItem.getName());
53 | }
54 | return null;
55 | }
56 |
57 | public FoodItem updateFoodItem(FoodItem foodItem){
58 | HashMap foodMenuMap = menuMap.get(foodItem.getCategory());
59 | foodMenuMap.put(foodItem.getName(), foodItem);
60 | return foodItem;
61 | }
62 |
63 | public void removeItem(String itemName) throws BonVivantException {
64 | FoodItem delFoodItem = findFoodItem(itemName);
65 | if(delFoodItem == null){
66 | throw new BonVivantException("FoodItem does not exist.");
67 | }
68 | menuMap.get(delFoodItem.getCategory()).remove(delFoodItem.getName());
69 | }
70 |
71 | public void removeAll(){
72 | resetMenu();
73 | }
74 |
75 | private void resetMenu(){
76 | menuMap = new HashMap<>();
77 | }
78 |
79 | }
80 |
--------------------------------------------------------------------------------
/food-ordering-system/src/com.bonvivant/restaurant/Restaurant.java:
--------------------------------------------------------------------------------
1 | package restaurant;
2 |
3 | import constants.BonVivantConstants;
4 | import java.util.Deque;
5 | import java.util.HashMap;
6 | import java.util.LinkedList;
7 | import java.util.UUID;
8 |
9 | import enums.OrderStatus;
10 | import enums.RestaurantStatus;
11 | import exceptions.BonVivantException;
12 | import helper.Utils;
13 | import order.Order;
14 |
15 | public class Restaurant {
16 | private UUID id;
17 | private String name;
18 | private Menu menu;
19 | private RestaurantStatus status;
20 | private Integer capacity;
21 | private Deque orderProcessingQueue;
22 | private HashMap orderHistory;
23 |
24 | /**
25 | * Using Builder Pattern
26 | * @param builder
27 | */
28 | private Restaurant(Builder builder) {
29 | this.id = Utils.generateUUID();
30 | this.name = builder.name;
31 | this.menu = builder.menu;
32 | this.capacity = builder.capacity;
33 | this.status = builder.status;
34 | this.menu = builder.menu;
35 | this.orderHistory = new HashMap<>();
36 | this.orderProcessingQueue = new LinkedList<>();
37 | }
38 |
39 | /**
40 | * Builder class for creating Restaurant instance
41 | */
42 | public static class Builder{
43 | private String name;
44 | private Menu menu;
45 | private RestaurantStatus status;
46 | private Integer capacity;
47 |
48 | public Builder(String name) {
49 | this.name = name;
50 | this.status = RestaurantStatus.OPEN;
51 | this.capacity = BonVivantConstants.DEFAULT_MAX_RESTAURANT_CAPACITY;
52 | }
53 |
54 | public Builder setStatus(RestaurantStatus status){
55 | this.status = status;
56 | return this;
57 | }
58 |
59 | public Builder setCapacity(Integer capacity){
60 | this.capacity = capacity;
61 | return this;
62 | }
63 |
64 | public Builder setMenu(Menu menu){
65 | this.menu = menu;
66 | return this;
67 | }
68 |
69 |
70 | public Restaurant build(){
71 | return new Restaurant(this);
72 | }
73 | };
74 |
75 | public Restaurant(String name,Integer capacity, Menu restaurantMenu) {
76 | this.name = name;
77 | this.capacity = capacity;
78 | this.id = UUID.randomUUID();
79 | this.menu = restaurantMenu;
80 | this.status = RestaurantStatus.OPEN;
81 | this.orderHistory = new HashMap<>();
82 | this.orderProcessingQueue = new LinkedList<>();
83 | }
84 |
85 | /**
86 | * @return the menu
87 | */
88 | public Menu getMenu() {
89 | return menu;
90 | }
91 |
92 | /**
93 | * add food in the menu
94 | * @return void
95 | */
96 | public void addFoodItem(FoodItem foodItem) {
97 | if(menu.findFoodItem(foodItem) == null){
98 | menu.addFoodItem(foodItem);
99 | }
100 | }
101 |
102 | /**
103 | * remove food in the menu
104 | * @return void
105 | */
106 | public void removeFoodItem(FoodItem foodItem) {
107 | if(menu.findFoodItem(foodItem.getName()) != null){
108 | try {
109 | menu.removeItem(foodItem.getName());
110 | } catch (BonVivantException e) {
111 | e.printStackTrace();
112 | }
113 | }
114 | }
115 |
116 | public void updateFoodItem(FoodItem foodItem) {
117 | menu.updateFoodItem(foodItem);
118 | }
119 |
120 | /**
121 | * @return the id
122 | */
123 | public UUID getId() {
124 | return id;
125 | }
126 |
127 | /**
128 | * @return the name
129 | */
130 | public String getName() {
131 | return name;
132 | }
133 |
134 | /**
135 | * @return the status
136 | */
137 | public RestaurantStatus getStatus() {
138 | return status;
139 | }
140 |
141 | /**
142 | * @param capacity the capacity to set
143 | */
144 | public void setCapacity(Integer capacity) {
145 | this.capacity = capacity;
146 | }
147 |
148 | /**
149 | * @param menu the menu to set
150 | */
151 | public void setMenu(Menu menu) {
152 | this.menu = menu;
153 | }
154 |
155 | public Boolean canAcceptOrder(){
156 | if(orderProcessingQueue.size() < capacity){
157 | return true;
158 | }
159 | return false;
160 | }
161 |
162 | public void addOrders(Order order) throws BonVivantException {
163 | if(!canAcceptOrder()){
164 | throw new BonVivantException("Running at full capacity, Restaurant can not accept order. Try after some time.");
165 | }
166 | this.orderHistory.put(order.getId(), order);
167 | orderProcessingQueue.addLast(order);
168 | }
169 |
170 | public OrderStatus getOrderStatus(Order order) throws BonVivantException {
171 | if(!orderHistory.containsKey(order.getId())){
172 | throw new BonVivantException("Order is not present. Check order id.");
173 | }
174 | return orderHistory.get(order.getId()).getStatus();
175 | }
176 |
177 | private Order orderCompleted() {
178 | if(orderProcessingQueue.size() == 0){
179 | return null;
180 | }
181 | return orderProcessingQueue.pollFirst();
182 | }
183 |
184 | private Order updateOrderStatus(Order order, OrderStatus status){
185 | order.setStatus(status);
186 | return order;
187 | }
188 |
189 |
190 | }
191 |
--------------------------------------------------------------------------------
/food-ordering-system/src/com.bonvivant/service/IRestaurantSelectionStratgey.java:
--------------------------------------------------------------------------------
1 | package service;
2 |
3 | import restaurant.Restaurant;
4 |
5 | import java.util.HashMap;
6 |
7 | public interface IRestaurantSelectionStratgey {
8 | public Restaurant findBy(HashMap orderedItems);
9 | }
10 |
--------------------------------------------------------------------------------
/food-ordering-system/src/com.bonvivant/service/LowestPriceRestaurantStrategy.java:
--------------------------------------------------------------------------------
1 | package service;
2 |
3 | import enums.ItemStatus;
4 | import enums.RestaurantStatus;
5 | import java.util.ArrayList;
6 | import java.util.HashMap;
7 |
8 | import restaurant.FoodItem;
9 | import restaurant.Restaurant;
10 | import system.RestaurantManager;
11 |
12 | public class LowestPriceRestaurantStrategy implements IRestaurantSelectionStratgey {
13 |
14 | @Override
15 | public Restaurant findBy(HashMap orderedItems) {
16 | ArrayList restaurants= RestaurantManager.getInstance().getAvailableRestaurants();
17 | Restaurant orderFromRestaurant = null;
18 | Double minPrice = Double.MAX_VALUE;
19 | for(Restaurant restaurant: restaurants) {
20 | Boolean hasFoundAll = true;
21 | Double restaurantPrice = 0.0;
22 | for(String itemName: orderedItems.keySet()){
23 | FoodItem orderItem = restaurant.getMenu().findFoodItem(itemName);
24 | if(orderItem == null){
25 | hasFoundAll = false;
26 | break;
27 | }else if(orderItem.getStatus() != ItemStatus.AVAILABLE){
28 | hasFoundAll = false;
29 | break;
30 | }
31 | restaurantPrice += orderItem.getPrice() * orderedItems.get(orderItem.getName());
32 | }
33 | if(hasFoundAll && restaurantPrice < minPrice){
34 | minPrice = restaurantPrice;
35 | orderFromRestaurant = restaurant;
36 | }
37 | }
38 | return orderFromRestaurant;
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/food-ordering-system/src/com.bonvivant/service/RestaurantFinder.java:
--------------------------------------------------------------------------------
1 | package service;
2 |
3 | import restaurant.Restaurant;
4 |
5 | import java.util.HashMap;
6 |
7 | import exceptions.BonVivantException;
8 | import restaurant.FoodItem;
9 |
10 | public class RestaurantFinder {
11 | private IRestaurantSelectionStratgey _restaurantSelectionStratgey;
12 |
13 | public RestaurantFinder(){
14 | }
15 |
16 | public RestaurantFinder useStrategy(IRestaurantSelectionStratgey stratgey){
17 | _restaurantSelectionStratgey = stratgey;
18 | return this;
19 | }
20 |
21 | public Restaurant findRestaurant(HashMap orderedItems)
22 | throws BonVivantException {
23 | if(_restaurantSelectionStratgey == null){
24 | throw new BonVivantException("IRestaurantSelectionStratgey strategy is not set.");
25 | }
26 | return _restaurantSelectionStratgey.findBy(orderedItems);
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/food-ordering-system/src/com.bonvivant/service/notification/AbstractNotification.java:
--------------------------------------------------------------------------------
1 | package service.notification;
2 |
3 | public abstract class AbstractNotification {
4 | String content;
5 | Boolean status;
6 |
7 | public AbstractNotification(String content){
8 | this.content = content;
9 | this.status = false;
10 | }
11 | /**
12 | * @return the content
13 | */
14 | public String getContent() {
15 | return content;
16 | }
17 |
18 | /**
19 | * @param content the content to set
20 | */
21 | public void setContent(String content) {
22 | this.content = content;
23 | }
24 |
25 | /**
26 | * @return the status
27 | */
28 | public Boolean getStatus() {
29 | return status;
30 | }
31 |
32 | /**
33 | * @param status the status to set
34 | */
35 | public void setStatus(Boolean status) {
36 | this.status = status;
37 | }
38 | public abstract void send();
39 | }
40 |
--------------------------------------------------------------------------------
/food-ordering-system/src/com.bonvivant/system/BonVivantRestaurantDriver.java:
--------------------------------------------------------------------------------
1 | package system;
2 |
3 | /**
4 | * BonVivantRestaurantDriver is just a wrapper class for RestaurantManager
5 | * Can be removed this if not needed in future
6 | */
7 |
8 | public class BonVivantRestaurantDriver {
9 | private RestaurantManager restaurantManager = null;
10 |
11 | private static BonVivantRestaurantDriver instance = null;
12 |
13 | private BonVivantRestaurantDriver(){
14 | restaurantManager = RestaurantManager.getInstance();
15 | }
16 |
17 | public static BonVivantRestaurantDriver getInstance(){
18 | if(instance == null){
19 | instance = new BonVivantRestaurantDriver();
20 | }
21 | return instance;
22 | }
23 |
24 | /**
25 | * @return the restaurantManager
26 | */
27 |
28 | public RestaurantManager getRestaurantManager() {
29 | return restaurantManager;
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/food-ordering-system/src/com.bonvivant/system/RestaurantManager.java:
--------------------------------------------------------------------------------
1 | package system;
2 |
3 | import enums.RestaurantStatus;
4 | import java.util.ArrayList;
5 | import java.util.HashMap;
6 | import java.util.List;
7 |
8 | import exceptions.BonVivantException;
9 | import java.util.stream.Collectors;
10 | import order.Order;
11 | import restaurant.FoodItem;
12 | import restaurant.Restaurant;
13 | import service.RestaurantFinder;
14 | import service.LowestPriceRestaurantStrategy;
15 |
16 | public class RestaurantManager {
17 | private HashMap restaurantMap;
18 | private HashMap> restaurantOrdersHistory;
19 |
20 | private static RestaurantManager instance = null;
21 |
22 | private RestaurantManager(){
23 | this.restaurantMap = new HashMap<>();
24 | this.restaurantOrdersHistory = new HashMap<>();
25 | }
26 |
27 | public static RestaurantManager getInstance(){
28 | if(instance == null){
29 | instance = new RestaurantManager();
30 | }
31 | return instance;
32 | }
33 |
34 | public void addRestaunrant(Restaurant restaurant){
35 | if(restaurantMap.containsKey(restaurant.getName())){
36 | // throw new BonVivantException("Restaurant with this name already present. Copyright violation!");
37 | return;
38 | }
39 | restaurantMap.put(restaurant.getName(), restaurant);
40 | restaurantOrdersHistory.put(restaurant.getName(), new ArrayList());
41 | }
42 |
43 | public Restaurant findRestaurant(String name){
44 | if(restaurantMap.containsKey(name)){
45 | return restaurantMap.get(name);
46 | }
47 | return null;
48 | }
49 |
50 | public Restaurant findBy(HashMap orderedItems) throws BonVivantException {
51 | // TBD
52 | Restaurant restaurant = new RestaurantFinder()
53 | .useStrategy(new LowestPriceRestaurantStrategy())
54 | .findRestaurant(orderedItems);
55 | return restaurant;
56 | }
57 |
58 | public void removeRestaurant(Restaurant restaurant) throws BonVivantException {
59 | if(restaurantMap.containsKey(restaurant.getName())){
60 | restaurantMap.remove(restaurant.getName());
61 | restaurantOrdersHistory.remove(restaurant.getName());
62 | }
63 |
64 | throw new BonVivantException("Restaurnt not present with this name" + restaurant.getName());
65 | }
66 |
67 | public ArrayList getAvailableRestaurants() {
68 | return restaurantMap.values()
69 | .stream()
70 | .filter(x -> x.canAcceptOrder() && x.getStatus() == RestaurantStatus.OPEN)
71 | .collect(Collectors.toCollection(ArrayList::new));
72 | }
73 |
74 | public List getOrderHistory(Restaurant restaurant){
75 | if(restaurantMap.containsKey(restaurant.getName())){
76 | return restaurantOrdersHistory.get(restaurant.getName());
77 | }
78 | return null;
79 | }
80 |
81 | public void placeOrder(Restaurant restaurant, Order order) throws BonVivantException {
82 | order.setPlacedAt(restaurant.getId());
83 | if(restaurant.canAcceptOrder()){
84 | restaurant.addOrders(order);
85 | restaurantOrdersHistory.get(restaurant.getName()).add(order);
86 | }
87 | }
88 | }
89 |
--------------------------------------------------------------------------------
/mychess.com/README.md:
--------------------------------------------------------------------------------
1 | # Mychess
2 |
--------------------------------------------------------------------------------
/mychess.com/src/com.mychess.play/ChessGame.java:
--------------------------------------------------------------------------------
1 | import board.Board;
2 | import enums.GameStatus;
3 | import player.Player;
4 |
5 | public class ChessGame {
6 |
7 | Player whitePlayer;
8 | Player blackPlayer;
9 | Board gameBoard;
10 | GameStatus gameStatus;
11 |
12 | private ChessGame(Player p1wPlayer, Player p2bPlayer) {
13 | whitePlayer = p1wPlayer;
14 | blackPlayer = p2bPlayer;
15 | gameBoard = new Board();
16 | gameStatus = GameStatus.UNDECIDED;
17 | }
18 |
19 | private void initGame() throws InterruptedException {
20 | System.out.println("White Player start the game...");
21 | while(gameStatus == GameStatus.UNDECIDED){
22 |
23 | Thread.sleep(1000*6);
24 | System.out.println("Game is in progress...");
25 | }
26 | }
27 |
28 | public GameStatus getGameStatus(){
29 | return gameStatus;
30 | }
31 |
32 | public static void startGame(Player p1, Player p2) throws InterruptedException {
33 | ChessGame game = new ChessGame(p1, p2);
34 | game.initGame();
35 | }
36 | }
--------------------------------------------------------------------------------
/mychess.com/src/com.mychess.play/MyChessMainApp.java:
--------------------------------------------------------------------------------
1 | import player.Player;
2 |
3 | public class MyChessMainApp {
4 | public static void main(String[] args) {
5 | System.out.println("Initializing MyChess.com...");
6 | Player whitePlayer = new Player("Mike", true);
7 | Player blackPlayer = new Player("John", false);
8 | try {
9 | ChessGame.startGame(whitePlayer, blackPlayer);
10 | } catch (InterruptedException e) {
11 | // TODO Auto-generated catch block
12 | e.printStackTrace();
13 | }
14 | }
15 | }
--------------------------------------------------------------------------------
/mychess.com/src/com.mychess.play/board/Board.java:
--------------------------------------------------------------------------------
1 | package board;
2 |
3 | import cell.Cell;
4 | import enums.PieceColor;
5 | import pieces.Bishop;
6 | import pieces.King;
7 | import pieces.Knight;
8 | import pieces.Pawn;
9 | import pieces.Piece;
10 | import pieces.Queen;
11 | import pieces.Rook;
12 |
13 | public class Board {
14 | private Cell[][] board = null;
15 | Integer BOARD_SIZE = 8;
16 |
17 | public Board() {
18 | board = new Cell[BOARD_SIZE][BOARD_SIZE];
19 | resetBoard();
20 | }
21 |
22 | private void resetBoard(){
23 | Piece whitePiece = null;
24 | Piece blackPiece = null;
25 | for(Integer i = 0; i < BOARD_SIZE; i++){
26 | whitePiece = new Pawn(PieceColor.WHITE);
27 | blackPiece = new Pawn(PieceColor.BLACK);
28 |
29 | board[1][i] = new Cell(1, i, whitePiece);
30 | board[BOARD_SIZE-2][i] = new Cell(BOARD_SIZE-2, i, blackPiece);
31 | }
32 | Integer whiteCounter = 0;
33 | Integer blackCounter = 0;
34 | final Integer left = 0;
35 | final Integer right = BOARD_SIZE-1;
36 |
37 | board[left][whiteCounter] = new Cell(left, whiteCounter, new Rook(PieceColor.WHITE));
38 | board[left][right-whiteCounter] = new Cell(left,right-whiteCounter, new Rook(PieceColor.WHITE));
39 |
40 | board[right][blackCounter] = new Cell(right, blackCounter, new Rook(PieceColor.BLACK));
41 | board[right][right-blackCounter] = new Cell(right, right-blackCounter, new Rook(PieceColor.BLACK));
42 |
43 | whiteCounter++;
44 | board[left][whiteCounter] = new Cell(left, whiteCounter, new Knight(PieceColor.WHITE));
45 | board[left][right-whiteCounter] = new Cell(left, right- whiteCounter, new Knight(PieceColor.WHITE));
46 |
47 | blackCounter++;
48 | board[right][blackCounter] = new Cell(right, blackCounter,new Knight(PieceColor.BLACK));
49 | board[right][right-blackCounter] = new Cell(right, right-blackCounter,new Knight(PieceColor.BLACK));
50 |
51 | whiteCounter++;
52 | board[left][whiteCounter] = new Cell(left, whiteCounter, new Bishop(PieceColor.WHITE));
53 | board[left][right-whiteCounter] = new Cell(left, right-whiteCounter,new Bishop(PieceColor.WHITE));
54 |
55 | blackCounter++;
56 | board[right][blackCounter] = new Cell(right, blackCounter, new Bishop(PieceColor.BLACK));
57 | board[right][right-blackCounter] = new Cell(right, right-blackCounter, new Bishop(PieceColor.BLACK));
58 |
59 | whiteCounter++;
60 | board[left][whiteCounter] = new Cell(left, whiteCounter, new King(PieceColor.WHITE));
61 | board[left][right-whiteCounter] = new Cell(left, right-whiteCounter, new Queen(PieceColor.WHITE));
62 |
63 | blackCounter++;
64 | board[right][blackCounter] = new Cell(right, blackCounter, new King(PieceColor.BLACK));
65 | board[right][right-blackCounter] = new Cell(right, right-blackCounter, new Queen(PieceColor.BLACK));
66 | System.out.println("Board has been set.");
67 | }
68 | }
--------------------------------------------------------------------------------
/mychess.com/src/com.mychess.play/cell/Cell.java:
--------------------------------------------------------------------------------
1 | package cell;
2 |
3 | import pieces.Piece;
4 |
5 | public class Cell {
6 | Integer x;
7 | Integer y;
8 | Piece piece;
9 |
10 | public Cell(Integer x1, Integer y1, Piece newPiece){
11 | x = x1;
12 | y = y1;
13 | piece = newPiece;
14 | }
15 |
16 | public Piece getPiece() {
17 | return piece;
18 | }
19 |
20 | }
--------------------------------------------------------------------------------
/mychess.com/src/com.mychess.play/constants/ChessConstants.java:
--------------------------------------------------------------------------------
1 | package constants;
2 |
3 | public class ChessConstants {
4 | public static Integer BOARD_SIZE = 8;
5 | }
--------------------------------------------------------------------------------
/mychess.com/src/com.mychess.play/enums/GameStatus.java:
--------------------------------------------------------------------------------
1 | package enums;
2 |
3 | public enum GameStatus {
4 | CHECKMATE, DRAW, STALEMATE, ABORTED, UNDECIDED;
5 | }
--------------------------------------------------------------------------------
/mychess.com/src/com.mychess.play/enums/PieceColor.java:
--------------------------------------------------------------------------------
1 | package enums;
2 |
3 | public enum PieceColor {
4 | WHITE,BLACK;
5 | }
--------------------------------------------------------------------------------
/mychess.com/src/com.mychess.play/move/Moves.java:
--------------------------------------------------------------------------------
1 | package move;
2 |
3 | import java.util.ArrayList;
4 |
5 | import cell.Cell;
6 |
7 | public class Moves {
8 | private ArrayList moves = null;
9 |
10 | public Moves(){
11 | moves = new ArrayList<>();
12 | }
13 |
14 | public void addMove(Cell move){
15 | moves.add(move);
16 | }
17 |
18 | public ArrayList getMoves(){
19 | return moves;
20 | }
21 |
22 | }
--------------------------------------------------------------------------------
/mychess.com/src/com.mychess.play/pieces/Bishop.java:
--------------------------------------------------------------------------------
1 |
2 | package pieces;
3 |
4 | import cell.Cell;
5 | import enums.PieceColor;
6 |
7 | public class Bishop extends Piece{
8 |
9 | public Bishop(PieceColor pieceColor){
10 | super(pieceColor);
11 | }
12 |
13 | @Override
14 | public Boolean canMove(Cell from, Cell to) {
15 | System.out.println("Moving Bishop");
16 | return true;
17 | }
18 | };
--------------------------------------------------------------------------------
/mychess.com/src/com.mychess.play/pieces/King.java:
--------------------------------------------------------------------------------
1 | package pieces;
2 |
3 | import cell.Cell;
4 | import enums.PieceColor;
5 |
6 | public class King extends Piece {
7 | private Boolean isCastled;
8 |
9 | public King(PieceColor pieceColor){
10 | super(pieceColor);
11 | isCastled = false;
12 | }
13 |
14 | @Override
15 | public Boolean canMove(Cell from, Cell to){
16 | System.out.println("Moving King");
17 | return true;
18 | }
19 |
20 | public Boolean getIsCastled(){
21 | return isCastled;
22 | }
23 |
24 | public void setIsCastled() {
25 | isCastled = true;
26 | }
27 | }
--------------------------------------------------------------------------------
/mychess.com/src/com.mychess.play/pieces/Knight.java:
--------------------------------------------------------------------------------
1 | package pieces;
2 |
3 | import cell.Cell;
4 | import enums.PieceColor;
5 |
6 | public class Knight extends Piece {
7 | public Knight(PieceColor pieceColor){
8 | super(pieceColor);
9 | }
10 |
11 | @Override
12 | public Boolean canMove(Cell from, Cell to) {
13 | System.out.println("Moving Knight");
14 | return true;
15 | }
16 | }
--------------------------------------------------------------------------------
/mychess.com/src/com.mychess.play/pieces/Pawn.java:
--------------------------------------------------------------------------------
1 | package pieces;
2 |
3 | import cell.Cell;
4 | import enums.PieceColor;
5 |
6 | public class Pawn extends Piece{
7 | public Pawn(PieceColor pieceColor){
8 | super(pieceColor);
9 | }
10 |
11 | @Override
12 | public Boolean canMove(Cell from, Cell to) {
13 | System.out.println("Moving Pawn");
14 | return true;
15 | }
16 | }
--------------------------------------------------------------------------------
/mychess.com/src/com.mychess.play/pieces/Piece.java:
--------------------------------------------------------------------------------
1 | package pieces;
2 |
3 | import cell.Cell;
4 | import enums.PieceColor;
5 | public abstract class Piece {
6 | private Boolean isAlive = true;
7 | private PieceColor pieceColor;
8 |
9 | public Piece(PieceColor newPieceColor) {
10 | pieceColor = newPieceColor;
11 | }
12 | public abstract Boolean canMove(Cell from, Cell to);
13 | }
--------------------------------------------------------------------------------
/mychess.com/src/com.mychess.play/pieces/Queen.java:
--------------------------------------------------------------------------------
1 | package pieces;
2 |
3 | import cell.Cell;
4 | import enums.PieceColor;
5 |
6 | public class Queen extends Piece{
7 | public Queen(PieceColor pieceColor){
8 | super(pieceColor);
9 | }
10 |
11 | @Override
12 | public Boolean canMove(Cell from, Cell to) {
13 | System.out.println("Moving Queen");
14 | return true;
15 | }
16 | };
--------------------------------------------------------------------------------
/mychess.com/src/com.mychess.play/pieces/Rook.java:
--------------------------------------------------------------------------------
1 | package pieces;
2 |
3 | import cell.Cell;
4 | import enums.PieceColor;
5 |
6 | public class Rook extends Piece{
7 | private Boolean isCastled;
8 |
9 | public Rook(PieceColor pieceColor){
10 | super(pieceColor);
11 | isCastled = false;
12 | }
13 |
14 | @Override
15 | public Boolean canMove(Cell from, Cell to) {
16 | System.out.println("Moving Rook");
17 | return true;
18 | }
19 |
20 | public Boolean getIsCastled(){
21 | return isCastled;
22 | }
23 |
24 | public void setIsCastled() {
25 | isCastled = true;
26 | }
27 | }
--------------------------------------------------------------------------------
/mychess.com/src/com.mychess.play/player/Player.java:
--------------------------------------------------------------------------------
1 | package player;
2 |
3 | public class Player {
4 | private String name;
5 | private Boolean isWhite;
6 |
7 | public Player(String playerName, Boolean whiteFlag){
8 | name = playerName;
9 | isWhite = whiteFlag;
10 | }
11 |
12 | }
13 |
--------------------------------------------------------------------------------
| |