Skip to content
This repository was archived by the owner on Nov 18, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/com/Dryft/DAOs/DriverDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand All @@ -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"),
Expand Down
8 changes: 0 additions & 8 deletions src/com/Dryft/Main.java

This file was deleted.

110 changes: 110 additions & 0 deletions src/com/Dryft/MainThread.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.Dryft;

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 {

public void run() {
try {
Thread.sleep(300);
System.out.println("Starting Driver Redistribution");
Connection conn = DBConn.getConn();
PreparedStatement st = conn.prepareStatement("Select id, location, homeLocation, carNumber from drivers Where onRoad = (?) ;");
st.setBoolean(1, false);
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();
}
}
}

/**
* 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();
}
}
}
9 changes: 8 additions & 1 deletion src/com/Dryft/models/Driver.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,6 +41,10 @@ public Location getLocation() {
return location;
}

public Location getHomeLocation() {
return homeLocation;
}

public void setLocation(Location location) {
this.location = location;
}
Expand Down
65 changes: 33 additions & 32 deletions src/tools/PopulateDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down