From c749bfc399e85bb8fed7ac0e1fe4a708fb8073a3 Mon Sep 17 00:00:00 2001 From: Aviral Agarwal Date: Fri, 15 Nov 2019 07:53:09 +0530 Subject: [PATCH 1/5] Add homeLocation Field to driver --- src/com/Dryft/DAOs/DriverDAO.java | 16 ++++++-- src/com/Dryft/models/Driver.java | 9 ++++- src/tools/PopulateDB.java | 65 ++++++++++++++++--------------- 3 files changed, 53 insertions(+), 37 deletions(-) diff --git a/src/com/Dryft/DAOs/DriverDAO.java b/src/com/Dryft/DAOs/DriverDAO.java index 52a2192..b36e37f 100644 --- a/src/com/Dryft/DAOs/DriverDAO.java +++ b/src/com/Dryft/DAOs/DriverDAO.java @@ -19,10 +19,17 @@ public static Driver getDriver(int id) throws SQLException { ResultSet result = st.executeQuery(); DBConn.closeConn(); if (result.next()) { - return new Driver(result.getInt("id"), result.getString("name"), - CarDAO.getCar(result.getString("CarNumber")), LocationDAO.getLocation(result.getString("location")), - result.getString("sex").charAt(0), result.getDouble("rating"), result.getInt("reviews"), - result.getBoolean("onRoad")); + return new Driver( + result.getInt("id"), + result.getString("name"), + CarDAO.getCar(result.getString("CarNumber")), + LocationDAO.getLocation(result.getString("location")), + LocationDAO.getLocation(result.getString("homeLocation")), + result.getString("sex").charAt(0), + result.getDouble("rating"), + result.getInt("reviews"), + result.getBoolean("onRoad") + ); } else { throw new IllegalArgumentException("Driver not found"); } @@ -47,6 +54,7 @@ public static Driver getBestDriver(Location origin, Car.CarType type) throws SQL result.getString("name"), CarDAO.getCar(result.getString("CarNumber")), LocationDAO.getLocation(result.getString("location")), + LocationDAO.getLocation(result.getString("homeLocation")), result.getString("sex").charAt(0), result.getDouble("rating"), result.getInt("reviews"), diff --git a/src/com/Dryft/models/Driver.java b/src/com/Dryft/models/Driver.java index 64f2565..f6a9e07 100644 --- a/src/com/Dryft/models/Driver.java +++ b/src/com/Dryft/models/Driver.java @@ -6,16 +6,19 @@ public class Driver { private final String name; private final Car car; private final char sex; + private final Location homeLocation; private Location location; private double rating; private int reviews; private boolean onRoad; - public Driver(int id, String name, Car car, Location location, char sex, double rating, int reviews, boolean onRoad) { + public Driver(int id, String name, Car car, Location location, Location homeLocation, char sex, double rating, + int reviews, boolean onRoad) { this.id = id; this.name = name; this.car = car; this.location = location; + this.homeLocation = homeLocation; this.sex = sex; this.rating = rating; this.reviews = reviews; @@ -38,6 +41,10 @@ public Location getLocation() { return location; } + public Location getHomeLocation() { + return homeLocation; + } + public void setLocation(Location location) { this.location = location; } diff --git a/src/tools/PopulateDB.java b/src/tools/PopulateDB.java index c1e929a..063e1e8 100644 --- a/src/tools/PopulateDB.java +++ b/src/tools/PopulateDB.java @@ -8,10 +8,10 @@ public class PopulateDB { private static void makeTables(Connection conn) { -// makeUserTable(conn); -// makeDriverTable(conn); -// makeCarTable(conn); -// makeLocationTable(conn); + makeUserTable(conn); + makeDriverTable(conn); + makeCarTable(conn); + makeLocationTable(conn); makeRideTable(conn); } @@ -37,18 +37,19 @@ private static void makeUserTable(Connection conn) { private static void makeDriverTable(Connection conn) { String deleteExistingTable = "DROP TABLE IF EXISTS drivers;"; - String createDriverTable = "CREATE TABLE drivers (" - + "id INT PRIMARY KEY," - + "name TEXT," - + "carNumber TEXT," - + "sex CHAR(1)," - + "location TEXT," - + "rating REAL," - + "reviews INT," - + "onRoad BOOLEAN," - + "FOREIGN KEY(carNumber) REFERENCES cars(licenseNumber)," - + "FOREIGN KEY(location) REFERENCES locations(name)" - + ");"; + String createDriverTable = "CREATE TABLE drivers (" + + "id INT PRIMARY KEY AUTOINCREMENT," + + "name TEXT," + + "carNumber TEXT," + + "sex CHAR(1)," + + "location TEXT," + + "homeLocation TEXT," + + "rating REAL," + + "reviews INT," + + "onRoad BOOLEAN," + + "FOREIGN KEY(carNumber) REFERENCES cars(licenseNumber)," + + "FOREIGN KEY(location) REFERENCES locations(name)" + + ");"; Statement stmt = null; try { stmt = conn.createStatement(); @@ -97,22 +98,22 @@ private static void makeLocationTable(Connection conn) { private static void makeRideTable(Connection conn) { String deleteExistingTable = "DROP TABLE IF EXISTS rides;"; - String createRideTable = "CREATE TABLE rides (" - + "id INT PRIMARY KEY," - + "userEmail TEXT ," - + "driver INT," - + "source TEXT," - + "destination TEXT," - + "startTime DATETIME," - + "duration INT," - + "distance INT," - + "driverTime INT," - + "cost INT," - + "FOREIGN KEY(userEmail) REFERENCES users(email)," - + "FOREIGN KEY(driver) REFERENCES drivers(id)," - + "FOREIGN KEY(source) REFERENCES locations(name)," - + "FOREIGN KEY(destination) REFERENCES locations(name)" - + ");"; + String createRideTable = "CREATE TABLE rides (" + + "id INT PRIMARY KEY AUTOINCREMENT," + + "userEmail TEXT ," + + "driver INT," + + "source TEXT," + + "destination TEXT," + + "startTime DATETIME," + + "duration INT," + + "distance INT," + + "driverTime INT," + + "cost INT," + + "FOREIGN KEY(userEmail) REFERENCES users(email)," + + "FOREIGN KEY(driver) REFERENCES drivers(id)," + + "FOREIGN KEY(source) REFERENCES locations(name)," + + "FOREIGN KEY(destination) REFERENCES locations(name)" + + ");"; Statement stmt = null; try { stmt = conn.createStatement(); From 7bb1c284b2d67579145b6e399181db2c8d8b8c82 Mon Sep 17 00:00:00 2001 From: Aviral Agarwal Date: Fri, 15 Nov 2019 09:49:13 +0530 Subject: [PATCH 2/5] Add driver distribution thread --- src/com/Dryft/Main.java | 8 ----- src/com/Dryft/MainThread.java | 59 +++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 8 deletions(-) delete mode 100644 src/com/Dryft/Main.java create mode 100644 src/com/Dryft/MainThread.java diff --git a/src/com/Dryft/Main.java b/src/com/Dryft/Main.java deleted file mode 100644 index 25d81ea..0000000 --- a/src/com/Dryft/Main.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.Dryft; - -public class Main { - - public static void main(String[] args) { - System.out.println("Hello World"); - } -} diff --git a/src/com/Dryft/MainThread.java b/src/com/Dryft/MainThread.java new file mode 100644 index 0000000..1255ae4 --- /dev/null +++ b/src/com/Dryft/MainThread.java @@ -0,0 +1,59 @@ +package com.Dryft; + +import com.Dryft.utils.DBConn; +import com.Dryft.gui.SignIn; + +import java.lang.Thread; +import java.io.IOException; +import java.io.File; +import java.sql.Connection; +import java.sql.PreparedStatement; + +class DistributionThread extends Thread { + + public void run() { + try { + Thread.sleep(300); + System.out.println("Starting Driver Redistribution"); + Connection conn = DBConn.getConn(); + PreparedStatement st = conn.prepareStatement("UPDATE drivers set location = homeLocation ;"); + st.executeUpdate(); + DBConn.closeConn(); + } catch (Exception e) { + e.printStackTrace(); + } + } +} + +/** + * Launch the application. + */ + +public class MainThread { + public static boolean windowClosed; + public static void main(String[] args) { + boolean fileCreated=false; + String[] a={}; + SignIn.main(a); + try { + while(true){ + File file = new File("/tmp/running.txt"); + if (file.createNewFile()) { + fileCreated=true; + DistributionThread distribute = new DistributionThread(); + distribute.setDaemon(true); + distribute.start(); + } + if(windowClosed){ + break; + } + } + if(fileCreated){ + File file = new File("/tmp/running.txt"); + file.delete(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file From 2a175eba4cbdcc91f6e142bd37886cb30a4d0dcc Mon Sep 17 00:00:00 2001 From: Aviral Agarwal Date: Fri, 15 Nov 2019 12:02:26 +0530 Subject: [PATCH 3/5] Add new lind and check for driver on road --- src/com/Dryft/MainThread.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/com/Dryft/MainThread.java b/src/com/Dryft/MainThread.java index 1255ae4..f6501ca 100644 --- a/src/com/Dryft/MainThread.java +++ b/src/com/Dryft/MainThread.java @@ -16,7 +16,8 @@ public void run() { Thread.sleep(300); System.out.println("Starting Driver Redistribution"); Connection conn = DBConn.getConn(); - PreparedStatement st = conn.prepareStatement("UPDATE drivers set location = homeLocation ;"); + PreparedStatement st = conn.prepareStatement("UPDATE drivers set location = homeLocation Where onRoad = (?) ;"); + st.setBoolean(1, false); st.executeUpdate(); DBConn.closeConn(); } catch (Exception e) { @@ -56,4 +57,4 @@ public static void main(String[] args) { e.printStackTrace(); } } -} \ No newline at end of file +} From 6ec45b4764c93b68e35c4551050fd9cf41813ecd Mon Sep 17 00:00:00 2001 From: Aviral Agarwal Date: Fri, 15 Nov 2019 13:11:09 +0530 Subject: [PATCH 4/5] Refactor driver distribution --- src/com/Dryft/MainThread.java | 74 ++++++++++++++++++++++++++++++----- 1 file changed, 64 insertions(+), 10 deletions(-) diff --git a/src/com/Dryft/MainThread.java b/src/com/Dryft/MainThread.java index f6501ca..74fae2d 100644 --- a/src/com/Dryft/MainThread.java +++ b/src/com/Dryft/MainThread.java @@ -2,12 +2,54 @@ import com.Dryft.utils.DBConn; import com.Dryft.gui.SignIn; +import com.Dryft.models.Car; +import com.Dryft.models.Ride; +import com.Dryft.models.Location; +import com.Dryft.DAOs.LocationDAO; +import com.Dryft.DAOs.CarDAO; import java.lang.Thread; import java.io.IOException; import java.io.File; import java.sql.Connection; import java.sql.PreparedStatement; +import java.sql.ResultSet; + +class DriverThread extends Thread { + + private int id; + private Location origin; + private Location homeLocation; + private Car car; + + DriverThread(int id, Location origin, Location homeLocation, Car car) { + this.id = id; + this.origin = origin; + this.homeLocation = homeLocation; + this.car = car; + } + + public void run() { + try{ + Connection conn = DBConn.getConn(); + PreparedStatement st = conn.prepareStatement("Update drivers set onRoad = (?) where id = (?) ;"); + st.setBoolean(1, true); + st.setInt(2, id); + st.executeUpdate(); + int time = Ride.calculateDistance(origin, homeLocation) / car.getSpeed(); + Thread.sleep(time); + PreparedStatement stm = conn + .prepareStatement("Update drivers set onRoad = (?) and location = homeLocation where id = (?) ;"); + stm.setBoolean(1, false); + stm.setInt(2, id); + stm.executeUpdate(); + DBConn.closeConn(); + } + catch(Exception e){ + e.printStackTrace(); + } + } +} class DistributionThread extends Thread { @@ -16,14 +58,25 @@ public void run() { Thread.sleep(300); System.out.println("Starting Driver Redistribution"); Connection conn = DBConn.getConn(); - PreparedStatement st = conn.prepareStatement("UPDATE drivers set location = homeLocation Where onRoad = (?) ;"); + PreparedStatement st = conn.prepareStatement("Select id, location, homeLocation, carNumber from drivers Where onRoad = (?) ;"); st.setBoolean(1, false); - st.executeUpdate(); + ResultSet result = st.executeQuery(); + while (result.next()) { + DriverThread th = new DriverThread(result.getInt("id"), + LocationDAO.getLocation(result.getString("location")), + LocationDAO.getLocation(result.getString("homeLocation")), + CarDAO.getCar(result.getString("carNumber"))); + th.start(); + } DBConn.closeConn(); } catch (Exception e) { e.printStackTrace(); } } + + public static void sendDriverBack() { + + } } /** @@ -32,26 +85,27 @@ public void run() { public class MainThread { public static boolean windowClosed; + public static void main(String[] args) { - boolean fileCreated=false; - String[] a={}; + boolean fileCreated = false; + String[] a = {}; SignIn.main(a); try { - while(true){ + while (true) { File file = new File("/tmp/running.txt"); if (file.createNewFile()) { - fileCreated=true; + fileCreated = true; DistributionThread distribute = new DistributionThread(); distribute.setDaemon(true); distribute.start(); } - if(windowClosed){ + if (windowClosed) { break; - } + } } - if(fileCreated){ + if (fileCreated) { File file = new File("/tmp/running.txt"); - file.delete(); + file.delete(); } } catch (IOException e) { e.printStackTrace(); From 71510b61dfc28cf6880b28158cfa3035e4f18ae9 Mon Sep 17 00:00:00 2001 From: Aviral Agarwal Date: Fri, 15 Nov 2019 13:17:31 +0530 Subject: [PATCH 5/5] Remove irrelevant method --- src/com/Dryft/MainThread.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/com/Dryft/MainThread.java b/src/com/Dryft/MainThread.java index 74fae2d..d11ef3b 100644 --- a/src/com/Dryft/MainThread.java +++ b/src/com/Dryft/MainThread.java @@ -73,10 +73,6 @@ public void run() { e.printStackTrace(); } } - - public static void sendDriverBack() { - - } } /**