Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

all tests done and passing #5

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,14 @@
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>

<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
</dependencies>


</project>
87 changes: 87 additions & 0 deletions src/main/java/daos/CarDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package daos;

public class CarDTO implements DTO {
Integer carId;
String make;
String model;
Integer year;
String color;
String vin;

public CarDTO() {
}

public CarDTO(String make, String model, Integer year, String color, String vin) {
this.make = make;
this.model = model;
this.year = year;
this.color = color;
this.vin = vin;
}

public CarDTO(Integer carId, String make, String model, Integer year, String color, String vin) {
this.carId = carId;
this.make = make;
this.model = model;
this.year = year;
this.color = color;
this.vin = vin;
}

public Integer getCarId() {
return carId;
}

public void setCarId(Integer carId) {
this.carId = carId;
}

public String getMake() {
return make;
}

public void setMake(String make) {
this.make = make;
}

public String getModel() {
return model;
}

public void setModel(String model) {
this.model = model;
}

public Integer getYear() {
return year;
}

public void setYear(Integer year) {
this.year = year;
}

public String getColor() {
return color;
}

public void setColor(String color) {
this.color = color;
}

public String getVin() {
return vin;
}

public void setVin(String vin) {
this.vin = vin;
}

public Integer getId() {
return null;
}

@Override
public String toString() {
return String.format("Car ID: %d, Make: %s, Model: %s, Year: %d, Color: %s, VIN: %s", carId, make, model, year, color, vin);
}
}
33 changes: 33 additions & 0 deletions src/main/java/daos/ConnectToDB.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package daos;

import com.mysql.cj.jdbc.Driver;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectToDB {
public static final String URL = "jdbc:mysql://localhost:3306/DavesDB";
public static final String USER = "root";
public static final String PASS = "ZipCode5.2";

/**
* Get a connection to database
* @return Connection object
*/
public static Connection getConnection() {
try {
DriverManager.registerDriver(new Driver());
return DriverManager.getConnection(URL, USER, PASS);
} catch (SQLException ex) {
throw new RuntimeException("Error connecting to the database", ex);
}
}

/**
* Test Connection
*/
public static void main(String[] args) {
Connection connection = ConnectToDB.getConnection();
}
}
17 changes: 17 additions & 0 deletions src/main/java/daos/DAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package daos;

import java.util.List;

public interface DAO <T>{

T findById(Integer id);

List findAll();

String update (T dto);

String create (T dto);

Boolean delete (Integer id);

}
119 changes: 119 additions & 0 deletions src/main/java/daos/DAOConcrete.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package daos;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class DAOConcrete implements DAO <CarDTO> { //This class does CRUD operations

public CarDTO findById(Integer id) {
Connection connection = ConnectToDB.getConnection();

try {
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("Select * from car where carid=" + id);
if (rs.next()){
return extractUserFromResultSet(rs);
}
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}

public List findAll() {
Connection connection = ConnectToDB.getConnection();

try {
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("Select * from car");
List carsList = new ArrayList();

while (rs.next()){

CarDTO car = extractUserFromResultSet(rs);
carsList.add(car);
}
return carsList;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}

public String update(CarDTO dto) {
Connection connection = ConnectToDB.getConnection();

try {
PreparedStatement ps = connection.prepareStatement("update car set make=?, model=?, yearbuilt=?, color =?, vin=? where carid=?");

ps.setString(1, dto.getMake());
ps.setString(2, dto.getModel());
ps.setInt(3, dto.getYear());
ps.setString(4, dto.getColor());
ps.setString(5, dto.getVin());
ps.setInt(6, dto.getCarId());
int i = ps.executeUpdate();

if (i == 1) {
return dto.toString();
}
} catch (SQLException e) {
e.printStackTrace();
}
return "";
}

public String create (CarDTO dto) {
Connection connection = ConnectToDB.getConnection();

try {
PreparedStatement ps = connection.prepareStatement("Insert into car values(?, ?, ?, ?, ?, ?)");
ps.setInt(1, dto.getCarId());
ps.setString(2, dto.getMake());
ps.setString(3, dto.getModel());
ps.setInt(4, dto.getYear());
ps.setString(5, dto.getColor());
ps.setString(6, dto.getVin());
int i = ps.executeUpdate();

if (i == 1) {
return dto.toString();
//return dto;
}

} catch (SQLException e) {
e.printStackTrace();
}
return "";
}

public Boolean delete(Integer id) {
Connection connection = ConnectToDB.getConnection();

try{
Statement stmt = connection.createStatement();
int i = stmt.executeUpdate(("delete from car where carid=" + id));

if(i >= 1) {
return true;
}
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}

private CarDTO extractUserFromResultSet (ResultSet rs) throws SQLException {
CarDTO myCar = new CarDTO();

myCar.setCarId(rs.getInt("carid"));
myCar.setMake((rs.getString("make")));
myCar.setModel((rs.getString("model")));
myCar.setYear(rs.getInt("yearbuilt"));
myCar.setColor((rs.getString("color")));
myCar.setVin((rs.getString("vin")));

return myCar;
}
}
Empty file removed src/main/java/daos/DELETEME.txt
Empty file.
5 changes: 5 additions & 0 deletions src/main/java/daos/DTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package daos;

public interface DTO {
Integer getId ();
}
Empty file removed src/main/java/models/DELETEME.txt
Empty file.
57 changes: 57 additions & 0 deletions src/test/java/daos/CarDTOTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package daos;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class CarDTOTest {
DAOConcrete daoConcrete;
CarDTO car1, car2, car3;

@Before
public void setUp() throws Exception {
daoConcrete = new DAOConcrete();
car1 = new CarDTO(6, "Oldsmobile", "Cutlass",1987, "Black", "1987OC");
car2 = new CarDTO(5, "Pontiac", "Firebird", 1981, "Yellow", "1981PF");
car3 = new CarDTO(2, "Pontiac", "Lemans", 1969, "Gold", "1969PL");
}

@Test
public void testFindById () {
String expected = "Car ID: 6, Make: Oldsmobile, Model: Cutlass, Year: 1987, Color: Black, VIN: 1987OC";
String actual = daoConcrete.findById(6).toString();
Assert.assertEquals(expected, actual);
}

@Test
public void testFindAll () {
Integer expected = 6;
Integer actual = daoConcrete.findAll().size();
Assert.assertEquals(expected, actual);
}


@Test
public void testCreate () {
String expected = "Car ID: 7, Make: Toyota, Model: Echo, Year: 2001, Color: Green, VIN: 2001TE";
CarDTO carToCreate = new CarDTO(7, "Toyota", "Echo", 2001, "Green", "2001TE");
String actual = daoConcrete.create(carToCreate);
Assert.assertEquals(expected, actual);
}

@Test
public void testUpdate () {
String expected = "Car ID: 7, Make: Toyota, Model: Echo, Year: 2001, Color: Green Patina, VIN: 2001TE";
CarDTO carToModify = new CarDTO(7, "Toyota", "Echo", 2001, "Green Patina", "2001TE");
String actual = daoConcrete.update(carToModify);
Assert.assertEquals(expected, actual);
}

@Test
public void testDelete () {
Boolean expected = true;
Boolean actual = daoConcrete.delete(7);
Assert.assertEquals(expected, actual);
}

}
17 changes: 17 additions & 0 deletions src/test/java/daos/ConnectToDBTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package daos;

import org.junit.Assert;
import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;

public class ConnectToDBTest {


@Test
public void getConnection() throws SQLException {
Connection connect = ConnectToDB.getConnection();
Assert.assertFalse(connect.isClosed());
}
}
Empty file removed src/test/java/daos/DELETEME.txt
Empty file.