diff --git a/ exercises/HelloWorldMain/.idea/.name b/ exercises/HelloWorldMain/.idea/.name new file mode 100644 index 0000000..e995cb6 --- /dev/null +++ b/ exercises/HelloWorldMain/.idea/.name @@ -0,0 +1 @@ +HelloWorldMain \ No newline at end of file diff --git a/ exercises/HelloWorldMain/.idea/compiler.xml b/ exercises/HelloWorldMain/.idea/compiler.xml new file mode 100644 index 0000000..a718c43 --- /dev/null +++ b/ exercises/HelloWorldMain/.idea/compiler.xml @@ -0,0 +1,30 @@ + + + + + + diff --git a/ exercises/HelloWorldMain/.idea/copyright/profiles_settings.xml b/ exercises/HelloWorldMain/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/ exercises/HelloWorldMain/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/ exercises/HelloWorldMain/.idea/encodings.xml b/ exercises/HelloWorldMain/.idea/encodings.xml new file mode 100644 index 0000000..e206d70 --- /dev/null +++ b/ exercises/HelloWorldMain/.idea/encodings.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/ exercises/HelloWorldMain/.idea/libraries/Maven__junit_junit_4_11.xml b/ exercises/HelloWorldMain/.idea/libraries/Maven__junit_junit_4_11.xml new file mode 100644 index 0000000..f33320d --- /dev/null +++ b/ exercises/HelloWorldMain/.idea/libraries/Maven__junit_junit_4_11.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/ exercises/HelloWorldMain/.idea/libraries/Maven__log4j_log4j_1_2_17.xml b/ exercises/HelloWorldMain/.idea/libraries/Maven__log4j_log4j_1_2_17.xml new file mode 100644 index 0000000..e383c1b --- /dev/null +++ b/ exercises/HelloWorldMain/.idea/libraries/Maven__log4j_log4j_1_2_17.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/ exercises/HelloWorldMain/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml b/ exercises/HelloWorldMain/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml new file mode 100644 index 0000000..f58bbc1 --- /dev/null +++ b/ exercises/HelloWorldMain/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/ exercises/HelloWorldMain/.idea/misc.xml b/ exercises/HelloWorldMain/.idea/misc.xml new file mode 100644 index 0000000..7a23553 --- /dev/null +++ b/ exercises/HelloWorldMain/.idea/misc.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + diff --git a/ exercises/HelloWorldMain/.idea/modules.xml b/ exercises/HelloWorldMain/.idea/modules.xml new file mode 100644 index 0000000..c7fabe5 --- /dev/null +++ b/ exercises/HelloWorldMain/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/ exercises/HelloWorldMain/.idea/scopes/scope_settings.xml b/ exercises/HelloWorldMain/.idea/scopes/scope_settings.xml new file mode 100644 index 0000000..922003b --- /dev/null +++ b/ exercises/HelloWorldMain/.idea/scopes/scope_settings.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/ exercises/HelloWorldMain/.idea/vcs.xml b/ exercises/HelloWorldMain/.idea/vcs.xml new file mode 100644 index 0000000..def6a6a --- /dev/null +++ b/ exercises/HelloWorldMain/.idea/vcs.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/ exercises/HelloWorldMain/.idea/workspace.xml b/ exercises/HelloWorldMain/.idea/workspace.xml new file mode 100644 index 0000000..20d839c --- /dev/null +++ b/ exercises/HelloWorldMain/.idea/workspace.xml @@ -0,0 +1,858 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + localhost + 5050 + + + + + + + + + + 1414620582002 + 1414620582002 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ exercises/HelloWorldMain/HelloWorldMain.iml b/ exercises/HelloWorldMain/HelloWorldMain.iml new file mode 100644 index 0000000..fbdf3c0 --- /dev/null +++ b/ exercises/HelloWorldMain/HelloWorldMain.iml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/ exercises/HelloWorldMain/logs.log b/ exercises/HelloWorldMain/logs.log new file mode 100644 index 0000000..6f3755d --- /dev/null +++ b/ exercises/HelloWorldMain/logs.log @@ -0,0 +1,9 @@ +DEBUG main ro.teamnet.hello.HelloWorld - DEBUG -> Enters in sayHello() method from HelloWorld +INFO main ro.teamnet.hello.HelloWorld - INFO -> Enters in returnHelloKey from HelloWorld +INFO main ro.teamnet.hello.HelloWorld - INFO -> Enters in returnHelloKey from HelloWorld +INFO main ro.teamnet.hello.HelloWorld - INFO -> Enters in returnHelloKey from HelloWorld +INFO main ro.teamnet.hello.HelloWorld - INFO -> Enters in returnHelloKey from HelloWorld +INFO main ro.teamnet.hello.HelloWorld - INFO -> Enters in returnHelloKey from HelloWorld +INFO main ro.teamnet.hello.HelloWorld - INFO -> Enters in returnHelloKey from HelloWorld +INFO main ro.teamnet.hello.HelloWorld - INFO -> Enters in returnHelloKey from HelloWorld +INFO main ro.teamnet.hello.HelloWorld - INFO -> Enters in returnHelloKey from HelloWorld diff --git a/ exercises/HelloWorldMain/pom.xml b/ exercises/HelloWorldMain/pom.xml new file mode 100644 index 0000000..7d4b0d3 --- /dev/null +++ b/ exercises/HelloWorldMain/pom.xml @@ -0,0 +1,41 @@ + + + 4.0.0 + + ro.teamnet.zerotohero + helloworld-main + 1.0 + A Maven project for displaying a Hello World Application + Hello World Project + + + log4j + log4j + 1.2.17 + + + junit + junit + 4.11 + + + + + + org.apache.maven.plugins + maven-source-plugin + + + attach-sources + + jar + + + + + + + + \ No newline at end of file diff --git a/ exercises/HelloWorldMain/src/main/java/ro/teamnet/hello/HelloWorld.java b/ exercises/HelloWorldMain/src/main/java/ro/teamnet/hello/HelloWorld.java new file mode 100644 index 0000000..f763930 --- /dev/null +++ b/ exercises/HelloWorldMain/src/main/java/ro/teamnet/hello/HelloWorld.java @@ -0,0 +1,32 @@ +package ro.teamnet.hello; + + +import org.apache.log4j.Logger; + +public class HelloWorld { + + static final Logger logger = Logger.getLogger(HelloWorld.class.getName()); + /** + * method for saying hello + * + */ + public void sayHello(){ + System.out.println("Hello World!"); + logger.debug("DEBUG -> Enters in sayHello() method from HelloWorld"); + logger.info("INFO -> Enters in returnHelloKey from HelloWorld"); + + } + + /** + * method for returning a key + * @return - The HelloWorld key + */ + public String returnHelloKey(){ + return "HelloKey"; + } + public static void main(String[] args) { + HelloWorld helloWorld = new HelloWorld(); + helloWorld.sayHello(); + } + +} diff --git a/ exercises/HelloWorldMain/src/main/resources/log4j.properties b/ exercises/HelloWorldMain/src/main/resources/log4j.properties new file mode 100644 index 0000000..23fe9fd --- /dev/null +++ b/ exercises/HelloWorldMain/src/main/resources/log4j.properties @@ -0,0 +1,17 @@ +log4j.rootLogger= info, stdout, R + +log4j.appender.stdout=org.apache.log4j.ConsoleAppender +log4j.appender.stdout.layout=org.apache.log4j.PatternLayout + +# Pattern to output the caller's file name and line number. +log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n + +log4j.appender.R=org.apache.log4j.RollingFileAppender +log4j.appender.R.File=logs.log + +log4j.appender.R.MaxFileSize=100KB +# Keep one backup file +log4j.appender.R.MaxBackupIndex=1 + +log4j.appender.R.layout=org.apache.log4j.PatternLayout +log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n diff --git a/ exercises/HelloWorldMain/src/test/java/ro/teamnet/hello/HelloWorldTest.java b/ exercises/HelloWorldMain/src/test/java/ro/teamnet/hello/HelloWorldTest.java new file mode 100644 index 0000000..34d1f98 --- /dev/null +++ b/ exercises/HelloWorldMain/src/test/java/ro/teamnet/hello/HelloWorldTest.java @@ -0,0 +1,21 @@ +package ro.teamnet.hello; + + +import org.junit.Test; + +public class HelloWorldTest { + + @Test + public void testSayHello(){ + HelloWorld helloWorld = new HelloWorld(); + helloWorld.sayHello(); + } + + @Test + public void testReturnHelloKey(){ + HelloWorld helloWorld = new HelloWorld(); + System.out.println("From Test: " + helloWorld.returnHelloKey()); + assert helloWorld.returnHelloKey().equals("HelloKey"); + } + +} diff --git a/ exercises/helloworld-extend/.idea/.name b/ exercises/helloworld-extend/.idea/.name new file mode 100644 index 0000000..b5341e8 --- /dev/null +++ b/ exercises/helloworld-extend/.idea/.name @@ -0,0 +1 @@ +helloworld-extend \ No newline at end of file diff --git a/ exercises/helloworld-extend/.idea/compiler.xml b/ exercises/helloworld-extend/.idea/compiler.xml new file mode 100644 index 0000000..2d836f0 --- /dev/null +++ b/ exercises/helloworld-extend/.idea/compiler.xml @@ -0,0 +1,30 @@ + + + + + + diff --git a/ exercises/helloworld-extend/.idea/copyright/profiles_settings.xml b/ exercises/helloworld-extend/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/ exercises/helloworld-extend/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/ exercises/helloworld-extend/.idea/encodings.xml b/ exercises/helloworld-extend/.idea/encodings.xml new file mode 100644 index 0000000..e206d70 --- /dev/null +++ b/ exercises/helloworld-extend/.idea/encodings.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/ exercises/helloworld-extend/.idea/libraries/Maven__junit_junit_4_11.xml b/ exercises/helloworld-extend/.idea/libraries/Maven__junit_junit_4_11.xml new file mode 100644 index 0000000..f33320d --- /dev/null +++ b/ exercises/helloworld-extend/.idea/libraries/Maven__junit_junit_4_11.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/ exercises/helloworld-extend/.idea/libraries/Maven__log4j_log4j_1_2_17.xml b/ exercises/helloworld-extend/.idea/libraries/Maven__log4j_log4j_1_2_17.xml new file mode 100644 index 0000000..e383c1b --- /dev/null +++ b/ exercises/helloworld-extend/.idea/libraries/Maven__log4j_log4j_1_2_17.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/ exercises/helloworld-extend/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml b/ exercises/helloworld-extend/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml new file mode 100644 index 0000000..f58bbc1 --- /dev/null +++ b/ exercises/helloworld-extend/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/ exercises/helloworld-extend/.idea/libraries/Maven__ro_teamnet_zerotohero_helloworld_main_1_0.xml b/ exercises/helloworld-extend/.idea/libraries/Maven__ro_teamnet_zerotohero_helloworld_main_1_0.xml new file mode 100644 index 0000000..f62c2b9 --- /dev/null +++ b/ exercises/helloworld-extend/.idea/libraries/Maven__ro_teamnet_zerotohero_helloworld_main_1_0.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/ exercises/helloworld-extend/.idea/misc.xml b/ exercises/helloworld-extend/.idea/misc.xml new file mode 100644 index 0000000..bd33217 --- /dev/null +++ b/ exercises/helloworld-extend/.idea/misc.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + diff --git a/ exercises/helloworld-extend/.idea/modules.xml b/ exercises/helloworld-extend/.idea/modules.xml new file mode 100644 index 0000000..e95e077 --- /dev/null +++ b/ exercises/helloworld-extend/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/ exercises/helloworld-extend/.idea/scopes/scope_settings.xml b/ exercises/helloworld-extend/.idea/scopes/scope_settings.xml new file mode 100644 index 0000000..922003b --- /dev/null +++ b/ exercises/helloworld-extend/.idea/scopes/scope_settings.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/ exercises/helloworld-extend/.idea/vcs.xml b/ exercises/helloworld-extend/.idea/vcs.xml new file mode 100644 index 0000000..def6a6a --- /dev/null +++ b/ exercises/helloworld-extend/.idea/vcs.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/ exercises/helloworld-extend/.idea/workspace.xml b/ exercises/helloworld-extend/.idea/workspace.xml new file mode 100644 index 0000000..7b0953c --- /dev/null +++ b/ exercises/helloworld-extend/.idea/workspace.xml @@ -0,0 +1,600 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + localhost + 5050 + + + + + + + 1414628346670 + 1414628346670 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ exercises/helloworld-extend/helloworld-extend.iml b/ exercises/helloworld-extend/helloworld-extend.iml new file mode 100644 index 0000000..ae98056 --- /dev/null +++ b/ exercises/helloworld-extend/helloworld-extend.iml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/ exercises/helloworld-extend/logs.log b/ exercises/helloworld-extend/logs.log new file mode 100644 index 0000000..a2d0be6 --- /dev/null +++ b/ exercises/helloworld-extend/logs.log @@ -0,0 +1 @@ +INFO main ro.teamnet.hello.HelloWorld - INFO -> Enters in returnHelloKey from HelloWorld diff --git a/ exercises/helloworld-extend/pom.xml b/ exercises/helloworld-extend/pom.xml new file mode 100644 index 0000000..499516d --- /dev/null +++ b/ exercises/helloworld-extend/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + + ro.teamnet.zerotohero + helloworld-extend + 1.0 + + + ro.teamnet.zerotohero + helloworld-main + 1.0 + + + \ No newline at end of file diff --git a/ exercises/helloworld-extend/src/main/java/ro/teamnet/hello2/HelloWorldExtend.java b/ exercises/helloworld-extend/src/main/java/ro/teamnet/hello2/HelloWorldExtend.java new file mode 100644 index 0000000..b98e50b --- /dev/null +++ b/ exercises/helloworld-extend/src/main/java/ro/teamnet/hello2/HelloWorldExtend.java @@ -0,0 +1,22 @@ +package ro.teamnet.hello2; + +import ro.teamnet.hello.*; + +public class HelloWorldExtend { + + public HelloWorldExtend() { + } + + public void extendSayHello(){ + HelloWorld helloWorld = new HelloWorld(); + helloWorld.sayHello(); + System.out.println("The new Hello World"); + } + + public static void main(String[] args) { + + + new HelloWorldExtend().extendSayHello(); + } + +} diff --git a/ exercises/zth-app/pom.xml b/ exercises/zth-app/pom.xml new file mode 100644 index 0000000..694ae18 --- /dev/null +++ b/ exercises/zth-app/pom.xml @@ -0,0 +1,23 @@ + + 4.0.0 + ro.teamnet.zth + zth-app + jar + 1.0-SNAPSHOT + zth-app + http://maven.apache.org + + + junit + junit + 3.8.1 + test + + + com.oracle + ojdbc6 + 11.2.0.3 + + + diff --git a/ exercises/zth-app/src/main/java/ro/teamnet/zth/App.java b/ exercises/zth-app/src/main/java/ro/teamnet/zth/App.java new file mode 100644 index 0000000..ffeff1d --- /dev/null +++ b/ exercises/zth-app/src/main/java/ro/teamnet/zth/App.java @@ -0,0 +1,59 @@ +package ro.teamnet.zth; + +import ro.teamnet.zth.dao.DepartmentDao; +import ro.teamnet.zth.dao.EmployeeDao; +import ro.teamnet.zth.dao.JobDao; +import ro.teamnet.zth.domain.Employee; +import ro.teamnet.zth.utils.DatabaseManager; + +import javax.xml.crypto.Data; +import java.sql.Connection; +import java.util.HashMap; + +/** + * Hello world! + * + */ +public class App +{ + private static final String username="ZTH_17"; + private static final String password="passw0rd"; + public static void main( String[] args ) + { + System.out.println( "Starting ZTH JDBC Tutorial!" ); + + Connection con = DatabaseManager.getConnection(username, password); + DatabaseManager.checkConnection(con); + + HashMap columns = new HashMap(); + + // columns.put("kk", "VARCHAR2(5)"); + columns.put("Id", "number(6,0)"); + columns.put("Name", "varchar2(50)"); + columns.put("Address", "nvarchar2(50)"); + + /* JobDao jobDao = new JobDao(); + System.out.println(jobDao.getJobById(con, "AD_PRES"));*/ + + /* DepartmentDao departmentDaoDao = new DepartmentDao(); + System.out.println(departmentDaoDao.getDepartmentById(con, new Long(50)));*/ + + EmployeeDao employeeDao = new EmployeeDao(); + System.out.println(employeeDao.getAllEmployees(con)); + + /* Employee employee = employeeDao.getEmployeeById(con, new Long(100)); + + Employee emp = employeeDao.getEmployeeById(con, new Long(100)); + employee.setId(new Long(1001)); + employee.setManager(emp); + employee.setEmail("e@yahoo.com"); + employeeDao.saveEmployee(employee, con);*/ + + /* Employee employee_ins = employeeDao.getEmployeeById(con, new Long(1001)); + employee_ins.setEmail("e12@yahoo.com"); + employeeDao.updateEmployee(employee_ins, con);*/ + Employee employee = employeeDao.getEmployeeById(con, new Long(1001)); + System.out.println(employee); + //employeeDao.deleteEmployee(employee, con); + } +} \ No newline at end of file diff --git a/ exercises/zth-app/src/main/java/ro/teamnet/zth/dao/DepartmentDao.java b/ exercises/zth-app/src/main/java/ro/teamnet/zth/dao/DepartmentDao.java new file mode 100644 index 0000000..f693a3c --- /dev/null +++ b/ exercises/zth-app/src/main/java/ro/teamnet/zth/dao/DepartmentDao.java @@ -0,0 +1,66 @@ +package ro.teamnet.zth.dao; + +//dao - data access object + +import ro.teamnet.zth.domain.Department; +import ro.teamnet.zth.utils.ResultSetToPojoConverter; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +public class DepartmentDao { + + public ArrayList getAllDepartments(Connection con) { + + String selectAllFromTableString = "Select department_id, department_name FROM Departments"; + PreparedStatement stmt = null; + + try { + stmt = con.prepareStatement(selectAllFromTableString); + + ResultSet rs = stmt.executeQuery(); + + return ResultSetToPojoConverter.convertToDepartment(rs); + } + catch(SQLException ex) { + ex.printStackTrace(); + } + finally { + try { + stmt.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return new ArrayList(); + } + + public Department getDepartmentById(Connection con, Long id) { + + String selectAllFromTableString = "Select department_id, department_name FROM Departments WHERE department_id = ?"; + PreparedStatement stmt = null; + try { + stmt = con.prepareStatement(selectAllFromTableString); + stmt.setLong(1,id); + ResultSet rs = stmt.executeQuery(); + + ArrayList departments = ResultSetToPojoConverter.convertToDepartment(rs); + + return departments.size() > 0 ? departments.get(0) : null; + } catch (SQLException e) { + e.printStackTrace(); + } + finally { + try { + stmt.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } +} diff --git a/ exercises/zth-app/src/main/java/ro/teamnet/zth/dao/EmployeeDao.java b/ exercises/zth-app/src/main/java/ro/teamnet/zth/dao/EmployeeDao.java new file mode 100644 index 0000000..8439168 --- /dev/null +++ b/ exercises/zth-app/src/main/java/ro/teamnet/zth/dao/EmployeeDao.java @@ -0,0 +1,198 @@ +package ro.teamnet.zth.dao; + + +import ro.teamnet.zth.domain.Employee; +import ro.teamnet.zth.utils.ResultSetToPojoConverter; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashMap; + +public class EmployeeDao { + + public ArrayList getAllEmployees(Connection con) { + + String selectAllFromTableString = "Select employee_id, first_name, last_name, email," + + " phone_number, hire_date, job_id, salary, commission_pct, manager_id, department_id FROM Employees"; + PreparedStatement stmt = null; + + + try { + stmt = con.prepareStatement(selectAllFromTableString); + stmt.setMaxRows(5); + ResultSet rs = stmt.executeQuery(); + + return ResultSetToPojoConverter.convertToEmployee(rs, con); + } + catch(SQLException ex) { + ex.printStackTrace(); + } + finally { + try { + stmt.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return new ArrayList(); + } + + public Employee getEmployeeById(Connection con, Long id) { + + String selectAllFromTableString = "Select employee_id, first_name, last_name, email," + + "phone_number, hire_date, job_id, salary, commission_pct, manager_id, department_id FROM Employees WHERE employee_id = ?"; + PreparedStatement stmt = null; + try { + stmt = con.prepareStatement(selectAllFromTableString); + stmt.setLong(1,id); + ResultSet rs = stmt.executeQuery(); + + ArrayList employees = ResultSetToPojoConverter.convertToEmployee(rs, con); + + return employees.size() > 0 ? employees.get(0) : null; + } catch (SQLException e) { + e.printStackTrace(); + } + finally { + try { + stmt.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + public void saveEmployee(Employee employee, Connection con) { + LinkedHashMap insertIntoTableEmployees = new LinkedHashMap(); + String tableName = "employees"; + insertIntoTableEmployees.put("employee_id", employee.getId().toString()); + insertIntoTableEmployees.put("first_name", employee.getFirstName()); + insertIntoTableEmployees.put("last_name", employee.getLastName()); + insertIntoTableEmployees.put("email", employee.getEmail()); + insertIntoTableEmployees.put("phone_number", employee.getPhoneNumber()); + insertIntoTableEmployees.put("hire_date", "TO_DATE('" + employee.getHireDate().toString() + "','yyyy-mm-dd')"); + insertIntoTableEmployees.put("job_id", employee.getJob().getId().toString()); + insertIntoTableEmployees.put("salary", employee.getSalary().toString()); + insertIntoTableEmployees.put("commission_pct", employee.getCommissionPoints().toString()); + insertIntoTableEmployees.put("manager_id", employee.getManager().getId().toString()); + insertIntoTableEmployees.put("department_id", employee.getDepartment().getId().toString()); + PreparedStatement stmt = null; + try { + + String createTableString = "INSERT INTO " + tableName + " ( "; + StringBuilder sqlStatement = new StringBuilder(); + sqlStatement.append(createTableString); + Integer valuesCount = insertIntoTableEmployees.keySet().size(); + for (String valueName : insertIntoTableEmployees.keySet()) { + valuesCount--; + String columnString = valueName + (valuesCount != 0 ? " , " : ")"); + sqlStatement.append(columnString); + } + valuesCount = insertIntoTableEmployees.keySet().size(); + sqlStatement.append(" VALUES ( '"); + for (String valueName : insertIntoTableEmployees.keySet()) { + valuesCount--; + String columnString; + if (valueName.equals("hire_date")) { + columnString = insertIntoTableEmployees.get(valueName) + (valuesCount != 0 ? " , '" : "')"); + }else if (valueName.equals("phone_number")) { + columnString = insertIntoTableEmployees.get(valueName) + (valuesCount != 0 ? "' , " : "')"); + }else + { + columnString = insertIntoTableEmployees.get(valueName) + (valuesCount != 0 ? "' , '" : "')"); + } + sqlStatement.append(columnString); + } + stmt = con.prepareStatement(sqlStatement.toString()); + // stmt.executeUpdate(sqlStatement.toString()); + stmt.execute(); + + System.out.println("Inserted into table " + tableName + "..."); + } catch (SQLException e) { + e.printStackTrace(); + } + finally { + try { + stmt.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + } + public void deleteEmployee(Employee employee, Connection con) { + PreparedStatement stmt = null; + String tableName = "employees"; + try { + + String deleteStatement = "DELETE FROM " + tableName + " WHERE employee_id = " + employee.getId(); + stmt = con.prepareStatement(deleteStatement); + // stmt.executeUpdate(deleteStatement); + stmt.execute(); + System.out.println("Deleted row from " + tableName + " from database..."); + } catch (SQLException e) { + e.printStackTrace(); + } + finally { + try { + stmt.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + + } + } + public void updateEmployee(Employee employee, Connection con) { + HashMap insertIntoTableEmployees = new HashMap(); + insertIntoTableEmployees.put("employee_id", employee.getId().toString()); + insertIntoTableEmployees.put("first_name", employee.getFirstName()); + insertIntoTableEmployees.put("last_name", employee.getLastName()); + insertIntoTableEmployees.put("email", employee.getEmail()); + insertIntoTableEmployees.put("phone_number", employee.getPhoneNumber()); + insertIntoTableEmployees.put("hire_date", "TO_DATE('" + employee.getHireDate().toString() + "','yyyy-mm-dd')"); + insertIntoTableEmployees.put("job_id", employee.getJob().getId().toString()); + insertIntoTableEmployees.put("salary", employee.getSalary().toString()); + insertIntoTableEmployees.put("commission_pct", employee.getCommissionPoints().toString()); + insertIntoTableEmployees.put("manager_id", employee.getManager().getId().toString()); + insertIntoTableEmployees.put("department_id", employee.getDepartment().getId().toString()); + String tableName = "employees"; + PreparedStatement stmt = null; + try { + // stmt = con.createStatement(); + String createTableString = "UPDATE " + tableName + " SET "; + StringBuilder sqlStatement = new StringBuilder(); + sqlStatement.append(createTableString); + Integer columnsCount = insertIntoTableEmployees.keySet().size(); + for (String columnName : insertIntoTableEmployees.keySet()) { + columnsCount--; + String columnString; + if (columnName.equals("hire_date")) { + columnString = columnName + " = " + insertIntoTableEmployees.get(columnName) + (columnsCount != 0 ? " , " : "' "); + } else { + columnString = columnName + " = '" + insertIntoTableEmployees.get(columnName) + (columnsCount != 0 ? "' , " : "' "); + } + sqlStatement.append(columnString); + } + sqlStatement.append("WHERE employee_id = " + employee.getId()); + //stmt.executeUpdate(sqlStatement.toString()); + stmt = con.prepareStatement(sqlStatement.toString()); + stmt.execute(); + System.out.println("Updated table " + tableName + " in database..."); + } catch (SQLException e) { + e.printStackTrace(); + } + finally { + try { + stmt.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + + } + } +} + diff --git a/ exercises/zth-app/src/main/java/ro/teamnet/zth/dao/JobDao.java b/ exercises/zth-app/src/main/java/ro/teamnet/zth/dao/JobDao.java new file mode 100644 index 0000000..acd2560 --- /dev/null +++ b/ exercises/zth-app/src/main/java/ro/teamnet/zth/dao/JobDao.java @@ -0,0 +1,61 @@ +package ro.teamnet.zth.dao; + +import ro.teamnet.zth.domain.Job; +import ro.teamnet.zth.utils.ResultSetToPojoConverter; + +import java.sql.*; +import java.util.ArrayList; + +public class JobDao { + + public ArrayList getAllJobs(Connection con) { + + String selectAllFromTableString = "Select job_id, job_title, min_salary, max_salary FROM Jobs"; + PreparedStatement stmt = null; + + try { + stmt = con.prepareStatement(selectAllFromTableString); + + ResultSet rs = stmt.executeQuery(); + + return ResultSetToPojoConverter.convertToJob(rs); + } + catch(SQLException ex) { + ex.printStackTrace(); + } + finally { + try { + stmt.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return new ArrayList(); + } + + public Job getJobById(Connection con, String id) { + + String selectAllFromTableString = "Select job_id, job_title, min_salary, max_salary FROM Jobs WHERE job_id = ?"; + PreparedStatement stmt = null; + try { + stmt = con.prepareStatement(selectAllFromTableString); + stmt.setString(1,id); + ResultSet rs = stmt.executeQuery(); + + ArrayList jobs = ResultSetToPojoConverter.convertToJob(rs); + + return jobs.size() > 0 ? jobs.get(0) : null; + } catch (SQLException e) { + e.printStackTrace(); + } + finally { + try { + stmt.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } +} diff --git a/ exercises/zth-app/src/main/java/ro/teamnet/zth/domain/Department.java b/ exercises/zth-app/src/main/java/ro/teamnet/zth/domain/Department.java new file mode 100644 index 0000000..65be390 --- /dev/null +++ b/ exercises/zth-app/src/main/java/ro/teamnet/zth/domain/Department.java @@ -0,0 +1,30 @@ +package ro.teamnet.zth.domain; + + +public class Department { + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getDepartmentName() { + return departmentName; + } + + public void setDepartmentName(String departmentName) { + this.departmentName = departmentName; + } + + private Long id; + private String departmentName; + + @Override + public String toString(){ + + return "[id: " + id + ", department_name: " + departmentName + "]\n"; + } +} diff --git a/ exercises/zth-app/src/main/java/ro/teamnet/zth/domain/Employee.java b/ exercises/zth-app/src/main/java/ro/teamnet/zth/domain/Employee.java new file mode 100644 index 0000000..59119db --- /dev/null +++ b/ exercises/zth-app/src/main/java/ro/teamnet/zth/domain/Employee.java @@ -0,0 +1,118 @@ +package ro.teamnet.zth.domain; + + +import java.util.Date; + +public class Employee { + + private Long id; + private String firstName; + private String lastName; + private Employee manager; + private String email; + private Date hireDate; + private Double salary; + private Double commissionPoints; + private String phoneNumber; + private Job job; + private Department department; + + public Department getDepartment() { + return department; + } + + public void setDepartment(Department department) { + this.department = department; + } + + public Job getJob() { + return job; + } + + public void setJob(Job job) { + this.job = job; + } + + public String getPhoneNumber() { + return phoneNumber; + } + + public void setPhoneNumber(String phoneNumber) { + this.phoneNumber = phoneNumber; + } + + public Double getCommissionPoints() { + return commissionPoints; + } + + public void setCommissionPoints(Double commissionPoints) { + this.commissionPoints = commissionPoints; + } + + public Double getSalary() { + return salary; + } + + public void setSalary(Double salary) { + this.salary = salary; + } + + public Date getHireDate() { + return hireDate; + } + + public void setHireDate(Date hireDate) { + this.hireDate = hireDate; + } + + public Employee getManager() { + return manager; + } + + public void setManager(Employee manager) { + this.manager = manager; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + + @Override + public String toString() { + + return "(id: " + id + ",firstName: " + firstName + ",lastName: " + lastName + ",email: " + email + + ",hireDate: " + hireDate+ ",salary: " + salary + ",phoneNumber: " + phoneNumber + + ",commissionPoints: " + commissionPoints+ ",manager: " + manager + ",job: " + job.toString() + +",department: " + department + ")\n"; + + } +} diff --git a/ exercises/zth-app/src/main/java/ro/teamnet/zth/domain/Job.java b/ exercises/zth-app/src/main/java/ro/teamnet/zth/domain/Job.java new file mode 100644 index 0000000..4ac2206 --- /dev/null +++ b/ exercises/zth-app/src/main/java/ro/teamnet/zth/domain/Job.java @@ -0,0 +1,54 @@ +package ro.teamnet.zth.domain; + + +public class Job { + + private String id; + private String jobTitle; + + public Job(){} + public Job(String id, String jobTitle, Double minSalary, Double maxSalary) { + this.id = new String(id); + this.jobTitle = new String(jobTitle); + this.minSalary = new Double(minSalary); + this.maxSalary = new Double(maxSalary); + } + public Double getMinSalary() { + return minSalary; + } + + public void setMinSalary(Double minSalary) { + this.minSalary = minSalary; + } + + public Double getMaxSalary() { + return maxSalary; + } + + public void setMaxSalary(Double maxSalary) { + this.maxSalary = maxSalary; + } + + public String getJobTitle() { + return jobTitle; + } + + public void setJobTitle(String jobTitle) { + this.jobTitle = jobTitle; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + private Double minSalary; + private Double maxSalary; + + public String toString() { + return "(job_id: " + id + ", job_name: " + jobTitle + ", minSalary: " + minSalary + ", maxSalary: " + maxSalary + ")"; + } +} diff --git a/ exercises/zth-app/src/main/java/ro/teamnet/zth/utils/DatabaseManager.java b/ exercises/zth-app/src/main/java/ro/teamnet/zth/utils/DatabaseManager.java new file mode 100644 index 0000000..afb2b94 --- /dev/null +++ b/ exercises/zth-app/src/main/java/ro/teamnet/zth/utils/DatabaseManager.java @@ -0,0 +1,110 @@ +package ro.teamnet.zth.utils; + +import java.sql.*; +import java.util.HashMap; + +public class DatabaseManager { + + public static Connection getConnection(String username, String password) { + + Connection con = null; + try { + + con = DriverManager.getConnection( + "jdbc:oracle:thin:@10.6.33.102:1521:orcl", + username, + password); + } + catch (SQLException e) { + e.printStackTrace(); + } + + return con; + } + + public static void checkConnection(Connection con) { + + Statement statement = null; + PreparedStatement stmt = null; + try { + // statement = con.createStatement(); + stmt = con.prepareStatement("SELECT SYSDATE FROM DUAL"); + // ResultSet res = statement.executeQuery("SELECT SYSDATE FROM DUAL"); + ResultSet res = stmt.executeQuery("SELECT SYSDATE FROM DUAL"); + if(res.next()) { + Date currentDate = res.getDate(1); + System.out.println("Current Date from Oracle is " + currentDate); + } + res.close(); + + } catch (SQLException e) { + e.printStackTrace(); + } + finally { + try { + // statement.close(); + stmt.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + } + public static void create(Connection con, String tableName, HashMap columnData) { + + Statement stmt = null; + PreparedStatement statement = null; + + try { + stmt = con.createStatement(); + String createTableString = "CREATE TABLE " + tableName + "("; + StringBuilder sqlStatement = new StringBuilder(); + sqlStatement.append(createTableString); + Integer columnsCount = columnData.keySet().size(); + + for(String columnName: columnData.keySet()) { + columnsCount--; + String columnString = columnName + " " + columnData.get(columnName) + (columnsCount != 0 ? "," : ")"); + sqlStatement.append(columnString); + } + System.out.println(sqlStatement.toString()); + stmt.executeUpdate(sqlStatement.toString()); + + System.out.println("Created table " + tableName + " in database ..."); + + } catch (SQLException e) { + e.printStackTrace(); + } + finally { + try { + stmt.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + } + + public static void drop(Connection con, String tableName) { + + Statement stmt = null; + + + try { + stmt = con.createStatement(); + String dropTableStatement = "DROP TABLE " + tableName; + stmt.executeUpdate(dropTableStatement); + + System.out.println("Dropped table " + tableName + " from database ..."); + } catch (SQLException e) { + e.printStackTrace(); + } + finally { + try { + stmt.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + + } +} diff --git a/ exercises/zth-app/src/main/java/ro/teamnet/zth/utils/ResultSetToPojoConverter.java b/ exercises/zth-app/src/main/java/ro/teamnet/zth/utils/ResultSetToPojoConverter.java new file mode 100644 index 0000000..652d952 --- /dev/null +++ b/ exercises/zth-app/src/main/java/ro/teamnet/zth/utils/ResultSetToPojoConverter.java @@ -0,0 +1,66 @@ +package ro.teamnet.zth.utils; + + +import ro.teamnet.zth.dao.DepartmentDao; +import ro.teamnet.zth.dao.EmployeeDao; +import ro.teamnet.zth.dao.JobDao; +import ro.teamnet.zth.domain.*; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +public class ResultSetToPojoConverter { + // employee_id, first_name, last_name, email," + + // " phone_number, hire_date, job_id, salary, commission_pct, manager_id, department_id + public static ArrayList convertToEmployee(ResultSet rs, Connection con) throws SQLException { + + ArrayList employees = new ArrayList(); + while(rs.next()) { + Employee emp = new Employee(); + emp.setId(rs.getLong("employee_id")); + emp.setFirstName(rs.getString("first_name")); + emp.setLastName(rs.getString("last_name")); + emp.setEmail(rs.getString("email")); + emp.setHireDate(rs.getDate("hire_date")); + emp.setSalary(rs.getDouble("salary")); + emp.setCommissionPoints(rs.getDouble("commission_pct")); + emp.setPhoneNumber(rs.getString("phone_number")); + + emp.setManager((new EmployeeDao()).getEmployeeById(con,rs.getLong("manager_id"))); + emp.setJob((new JobDao()).getJobById(con, rs.getString("job_id"))); + emp.setDepartment((new DepartmentDao()).getDepartmentById(con, rs.getLong("department_id"))); + employees.add(emp); + } + rs.close(); + return employees; + } + + public static ArrayList convertToJob(ResultSet rs) throws SQLException { + String job_id, job_title; + Double min_salary, max_salary; + ArrayList jobs = new ArrayList(); + while(rs.next()) { + job_id = rs.getString("job_id"); + job_title = rs.getString("job_title"); + min_salary = rs.getDouble("min_salary"); + max_salary = rs.getDouble("max_salary"); + jobs.add(new Job(job_id, job_title, min_salary, max_salary)); + } + rs.close(); + return jobs; + } + public static ArrayList convertToDepartment(ResultSet rs) throws SQLException { + + ArrayList departments = new ArrayList(); + while(rs.next()) { + Department dept = new Department(); + dept.setDepartmentName(rs.getString("department_name")); + dept.setId(rs.getLong("department_id")); + departments.add(dept); + } + rs.close(); + return departments; + } +} diff --git a/ exercises/zth-app/src/test/java/ro/teamnet/zth/AppTest.java b/ exercises/zth-app/src/test/java/ro/teamnet/zth/AppTest.java new file mode 100644 index 0000000..3f5a4ca --- /dev/null +++ b/ exercises/zth-app/src/test/java/ro/teamnet/zth/AppTest.java @@ -0,0 +1,38 @@ +package ro.teamnet.zth; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for simple App. + */ +public class AppTest + extends TestCase +{ + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Rigourous Test :-) + */ + public void testApp() + { + assertTrue( true ); + } +} diff --git a/ exercises/zth-app/zth-app.iml b/ exercises/zth-app/zth-app.iml new file mode 100644 index 0000000..76ade37 --- /dev/null +++ b/ exercises/zth-app/zth-app.iml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..523b702 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.idea +/workshops/Collections/collections-workshop-part2/warm-up2/.idea +/workshops/Collections/warm-up-workshop/warm-up/.idea +/workshops/SQL/SQL-Workshop.docx diff --git a/workshops/4.Threads_Serialization.pptx b/workshops/4.Threads_Serialization.pptx index 3c1517b..13887e3 100644 Binary files a/workshops/4.Threads_Serialization.pptx and b/workshops/4.Threads_Serialization.pptx differ diff --git a/workshops/Collections/collections-workshop-part2.zip b/workshops/Collections/collections-workshop-part2.zip deleted file mode 100644 index 3f28f28..0000000 Binary files a/workshops/Collections/collections-workshop-part2.zip and /dev/null differ diff --git a/workshops/Collections/collections-workshop-part2/warm-up2/pom.xml b/workshops/Collections/collections-workshop-part2/warm-up2/pom.xml new file mode 100644 index 0000000..9913300 --- /dev/null +++ b/workshops/Collections/collections-workshop-part2/warm-up2/pom.xml @@ -0,0 +1,19 @@ + + + 4.0.0 + + warm-up2 + warm-up2 + 1.0-SNAPSHOT + + + + junit + junit + 4.11 + + + + \ No newline at end of file diff --git a/workshops/Collections/collections-workshop-part2/warm-up2/src/main/java/exercise0/Exercise0.java b/workshops/Collections/collections-workshop-part2/warm-up2/src/main/java/exercise0/Exercise0.java new file mode 100644 index 0000000..b2f2b57 --- /dev/null +++ b/workshops/Collections/collections-workshop-part2/warm-up2/src/main/java/exercise0/Exercise0.java @@ -0,0 +1,44 @@ +package exercise0; + +import java.util.HashMap; +import java.util.Set; + +/** + * Created by Radu.Hoaghe on 10/29/2014. + * + * Exercise 0: Iterate over the keys of a Map using keySet method (this method returns a Set of all the map keys) + */ +public class Exercise0 { + + public Exercise0(){ + + } + + // TODO Exercise #0 a) iterate over a Map's keys using keySet method + public void iterateThroughMap(){ + + // TODO Exercise #0 b) Create a Map (HashMap) and add elements to it (using put() method) + + HashMap hashMap = new HashMap(); + + hashMap.put("mere", "Romania"); + hashMap.put("pere", "Italia"); + hashMap.put("gutui", "Romania"); + hashMap.put("kiwi", "Egipt"); + + // TODO Exercise #0 b) Don't forget to specify the types of the key and value when creating the Map + Set keys = hashMap.keySet(); + // TODO Exercise #0 c) Iterate over the Map using keySet() method and print all its elements + System.out.print("["); + for(String key : keys) + System.out.print(key + "=" + hashMap.get(key) + " "); + System.out.print("]"); + // TODO Exercise #0 c) The elements are printed like this: [key1=value1, key2=value2, ...] + + } + + public static void main(String[] args) { + Exercise0 exercise0 = new Exercise0(); + exercise0.iterateThroughMap(); + } +} diff --git a/workshops/Collections/collections-workshop-part2/warm-up2/src/main/java/exercise1/Exercise1.java b/workshops/Collections/collections-workshop-part2/warm-up2/src/main/java/exercise1/Exercise1.java new file mode 100644 index 0000000..9632b78 --- /dev/null +++ b/workshops/Collections/collections-workshop-part2/warm-up2/src/main/java/exercise1/Exercise1.java @@ -0,0 +1,75 @@ +package exercise1; + +import java.util.*; + +/** + * Created by Radu.Hoaghe on 10/29/2014. + * + * Exercise 1: You have a Map that holds in each element a country with its capital (countries Map) + * a) Find all the countries that start with 'R' character, iterating through the keys of the Map using + * keySet() method + * b) Find all the countries that start with 'R' character, iterating through the keys of the Map using + * Map.Entry, and convert all the characters of the countries found to lowercase + * c) Find the capital city with the longest name, iterating through the values of the Map (values() method) + * + */ +public class Exercise1 { + + // A map that holds some key-value pairs, the key represents the country and its value represents + // the capital city of the country + private Map countries; + + public Exercise1(Map countries){ + this.countries = countries; + } + + // TODO Exercise #1 a) You need to iterate over the map keys using a foreach loop (see Map.keySet()) + // TODO Exercise #1 a) and add the countries that start with 'R' character into the seekingCountries list + // TODO Exercise #1 a) hint: see String documentation + public List iteratingOverKeys(){ + + // The list of countries that start with the 'R' character + List seekingCountries = new ArrayList(); + Set keys = countries.keySet(); + + for( String key : keys) + if(key.startsWith("R")) + seekingCountries.add(key); + return seekingCountries; + } + + // TODO Exercise #1 b) You need to iterate over the map entries using a foreach loop (see Map.Entry) + // TODO Exercise #1 b) and convert to lowercase (hint: String documentation) all the countries that start with 'R' + // TODO Exercise #1 b) character into the seekingCountries list + public List iteratingOverEntries(){ + + // The list of countries that start with the 'R' character and will be converted to lowercase + List seekingCountries = new ArrayList(); + + for(Map.Entry entry : countries.entrySet()) + if (entry.getKey().startsWith("R")) + seekingCountries.add(entry.getKey().toLowerCase()); + + return seekingCountries; + } + + // TODO Exercise #1 c) You need to iterate over the map values using a foreach loop (see Map.values()) + // TODO Exercise #1 c) and find the capital city with the longest name + public String iteratingOverValues(){ + + // The country that has the capital city with the longest name + String seekingCapital = ""; + + + Collection values = countries.values(); + + int strlen = 0; + + for(String s : values) + if(s.length() > strlen) { + strlen = s.length(); + seekingCapital = s; + } + return seekingCapital; + } +} diff --git a/workshops/Collections/collections-workshop-part2/warm-up2/src/main/java/exercise2/Exercise2.java b/workshops/Collections/collections-workshop-part2/warm-up2/src/main/java/exercise2/Exercise2.java new file mode 100644 index 0000000..dd31fcb --- /dev/null +++ b/workshops/Collections/collections-workshop-part2/warm-up2/src/main/java/exercise2/Exercise2.java @@ -0,0 +1,66 @@ +package exercise2; + +import java.util.*; + +/** + * Created by Radu.Hoaghe on 10/30/2014. + * + * Exercise 2: Overriding equals() and hashCode() methods. + * Create a Set and a Map and add some elements to them before and after overriding equals() and hashCode() + * For this exercise you need to follow in order all the TODO steps. + */ +public class Exercise2 { + private List studentList; + private List cnps; + + public Exercise2(List studentList, List cnps){ + this.studentList = studentList; + this.cnps = cnps; + } + + public void addStudents(){ + + System.out.println("The list of students is: " + studentList); + + // TODO Exercise 2 a) Make a new Set, add the elements from the studentList into it + + HashSet hashSet = new HashSet(); + + for(Student s : studentList) + hashSet.add(s); + + // TODO and print them (hint: notice the duplicate elements) + System.out.println("studentList: " + studentList); + System.out.println("hashSet: " + hashSet); + // TODO (hint: in order to print the elements of Student class in a human readable way you should override + // TODO the toString() method from Student class + + // TODO Exercise 2 b) Remove all elements from the Set using clear() method and go to TODO Exercise 2 c) from Student class + + hashSet.clear(); + // TODO Exercise 2 e) Add the elements from studentList into the Set after you finished the TODO Exercise 2 c) + // TODO from Student class. What do you notice? + hashSet.addAll(studentList); + System.out.println("hashSet after override equals method " + hashSet); + // TODO Exercise 2 f) Remove all elements from the Set using clear() method and go to TODO Exercise 2 g) from Student class + hashSet.clear(); + // TODO Exercise 2 i) Add the elements from studentList into the Set after you finished the TODO Exercise 2 g) + // TODO from Student class. What do you notice this time? + hashSet.addAll(studentList); + System.out.println("hashSet after override hashCode method " + hashSet); + // TODO Exercise 2 j) Create a new Map so that it holds in the key all the Students from the + // TODO (Important !) Set you have just already created, and in the keys' value, the CNP of the Student found in cnps List + // TODO (suggestion: after you implement this, you could comment, firstly, the equals() method from Student class + // TODO and then the hashCode() method in order to see some differences). + + int index = 0; + HashMap hashMap = new HashMap(); + for(Student stud : hashSet) { + + hashMap.put(stud, cnps.get(index)); + index++; + } + + System.out.println(hashMap); + } +} diff --git a/workshops/Collections/collections-workshop-part2/warm-up2/src/main/java/exercise2/Student.java b/workshops/Collections/collections-workshop-part2/warm-up2/src/main/java/exercise2/Student.java new file mode 100644 index 0000000..45cfca1 --- /dev/null +++ b/workshops/Collections/collections-workshop-part2/warm-up2/src/main/java/exercise2/Student.java @@ -0,0 +1,76 @@ +package exercise2; + +/** + * Created by Radu.Hoaghe on 31.10.2014. + */ +public class Student { + private final String firstName; + private final String lastName; + private final Double averageGrade; + + public Student(String firstName, String lastName, Double averageGrade) { + this.firstName = firstName; + this.lastName = lastName; + this.averageGrade = averageGrade; + } + + public String getFirstName() { + return firstName; + } + + public String getLastName() { + return lastName; + } + + public Double getAverageGrade() { + return averageGrade; + } + + // TODO Exercise 2 c) Override the equals() method + @Override + public boolean equals(Object o) { + // TODO Exercise 2 c1) Check if the current instance is the same instance as the one from Object o + + if (this == o) return true; + // TODO Exercise 2 c2) Check if Object o is null + + if (o == null) + return false; + // TODO Exercise 2 c3) Cast the object into a Student variable + + Student stud = (Student) o; + // TODO Exercise 2 c3) Check if all the fields from Student class are equal to the ones from + + if (! stud.firstName.equals(this.firstName)) + return false; + + if (! stud.lastName.equals(this.lastName)) + return false; + + if (! stud.averageGrade.equals(this.averageGrade)) + return false; + // TODO Exercise 2 c3) the variable that you casted earlier (lastName, firstName, averageGrade) + return true; + // TODO Exercise 2 d) After you finished implementing equals method go to TODO Exercise 2 e) from Exercise2 class + } + + // TODO Exercise 2 g) Override the hashCode() method + // TODO Exercise 2 g) Hint: Don't forget to include in the hashCode result all the fields from + // TODO Exercise 2 g) the Student class + + @Override + public int hashCode() { + + int result = firstName.hashCode(); + result = result * 31 + lastName.hashCode(); + result = result * 31 + averageGrade.hashCode(); + // TODO Exercise 2 h) After you finished implementing hashCode go to TODO Exercise 2 i) from Exercise2 class + return result; + } + + // TODO You should override the toString() method in order to print the Student class' elements + @Override + public String toString() { + return "(" + firstName + " " + lastName + " " + averageGrade + ")" ; + } +} diff --git a/workshops/Collections/collections-workshop-part2/warm-up2/src/test/java/Exercise1Test.java b/workshops/Collections/collections-workshop-part2/warm-up2/src/test/java/Exercise1Test.java new file mode 100644 index 0000000..d33d4ff --- /dev/null +++ b/workshops/Collections/collections-workshop-part2/warm-up2/src/test/java/Exercise1Test.java @@ -0,0 +1,159 @@ +import exercise1.Exercise1; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Created by Radu.Hoaghe on 10/29/2014. + */ +public class Exercise1Test { + public static final String[][] data = { + // Africa + {"ALGERIA","Algiers"}, {"ANGOLA","Luanda"}, + {"BENIN","Porto-Novo"}, {"BOTSWANA","Gaberone"}, + {"BURKINA FASO","Ouagadougou"}, {"BURUNDI","Bujumbura"}, + {"CAMEROON","Yaounde"}, {"CAPE VERDE","Praia"}, + {"CENTRAL AFRICAN REPUBLIC","Bangui"}, + {"CHAD","N’djamena"}, {"COMOROS","Moroni"}, + {"CONGO","Brazzaville"}, {"DJIBOUTI","Dijibouti"}, + {"EGYPT","Cairo"}, {"EQUATORIAL GUINEA","Malabo"}, + {"ERITREA","Asmara"}, {"ETHIOPIA","Addis Ababa"}, + {"GABON","Libreville"}, {"THE GAMBIA","Banjul"}, + {"GHANA","Accra"}, {"GUINEA","Conakry"}, + {"BISSAU","Bissau"}, {"COTE D’IVOIRE (IVORY COAST)","Yamoussoukro"}, + {"KENYA","Nairobi"}, {"LESOTHO","Maseru"}, + {"LIBERIA","Monrovia"}, {"LIBYA","Tripoli"}, + {"MADAGASCAR","Antananarivo"}, {"MALAWI","Lilongwe"}, + {"MALI","Bamako"}, {"MAURITANIA","Nouakchott"}, + {"MAURITIUS","Port Louis"}, {"MOROCCO","Rabat"}, + {"MOZAMBIQUE","Maputo"}, {"NAMIBIA","Windhoek"}, + {"NIGER","Niamey"}, {"NIGERIA","Abuja"}, + {"RWANDA","Kigali"}, {"SAO TOME E PRINCIPE","Sao Tome"}, + {"SENEGAL","Dakar"}, {"SEYCHELLES","Victoria"}, + {"SIERRA LEONE","Freetown"}, {"SOMALIA","Mogadishu"}, + {"SOUTH AFRICA","Pretoria/Cape Town"}, + {"SUDAN","Khartoum"}, {"SWAZILAND","Mbabane"}, {"TANZANIA","Dodoma"}, + {"TOGO","Lome"}, {"TUNISIA","Tunis"}, + {"UGANDA","Kampala"},{"DEMOCRATIC REPUBLIC OF THE CONGO (ZAIRE)", "Kinshasa"}, + {"ZAMBIA","Lusaka"}, {"ZIMBABWE","Harare"}, + // Asia + {"AFGHANISTAN","Kabul"}, {"BAHRAIN","Manama"}, + {"BANGLADESH","Dhaka"}, {"BHUTAN","Thimphu"}, + {"BRUNEI","Bandar Seri Begawan"}, + {"CAMBODIA","Phnom Penh"}, + {"CHINA","Beijing"}, {"CYPRUS","Nicosia"}, + {"INDIA","New Delhi"}, {"INDONESIA","Jakarta"}, + {"IRAN","Tehran"}, {"IRAQ","Baghdad"}, + {"ISRAEL","Jerusalem"}, {"JAPAN","Tokyo"}, + {"JORDAN","Amman"}, {"KUWAIT","Kuwait City"}, + {"LAOS","Vientiane"}, {"LEBANON","Beirut"}, + {"MALAYSIA","Kuala Lumpur"}, {"THE MALDIVES","Male"}, + {"MONGOLIA","Ulan Bator"}, {"MYANMAR (BURMA)","Rangoon"}, + {"NEPAL","Katmandu"}, {"NORTH KOREA","P’yongyang"}, + {"OMAN","Muscat"}, {"PAKISTAN","Islamabad"}, + {"PHILIPPINES","Manila"}, {"QATAR","Doha"}, + {"SAUDI ARABIA","Riyadh"}, {"SINGAPORE","Singapore"}, + {"SOUTH KOREA","Seoul"}, {"SRI LANKA","Colombo"}, + {"SYRIA","Damascus"}, {"TAIWAN (REPUBLIC OF CHINA)","Taipei"}, + {"THAILAND","Bangkok"}, {"TURKEY","Ankara"}, + {"UNITED ARAB EMIRATES","Abu Dhabi"}, + {"VIETNAM","Hanoi"}, {"YEMEN","Sana’a"}, + // Australia and Oceania + {"AUSTRALIA","Canberra"}, {"FIJI","Suva"}, + {"KIRIBATI","Bairiki"},{"MARSHALL ISLANDS","Dalap-Uliga-Darrit"}, + {"MICRONESIA","Palikir"}, {"NAURU","Yaren"}, + {"NEW ZEALAND","Wellington"}, {"PALAU","Koror"}, + {"PAPUA NEW GUINEA","Port Moresby"}, + {"SOLOMON ISLANDS","Honaira"}, {"TONGA","Nuku’alofa"}, + {"TUVALU","Fongafale"}, {"VANUATU","Port-Vila"}, + {"WESTERN SAMOA","Apia"}, + // Eastern Europe and former USSR + {"ARMENIA","Yerevan"}, {"AZERBAIJAN","Baku"}, + {"BELARUS (BYELORUSSIA)","Minsk"}, + {"BULGARIA","Sofia"}, {"GEORGIA","Tbilisi"}, + {"KAZAKSTAN","Almaty"}, {"KYRGYZSTAN","Alma-Ata"}, + {"MOLDOVA","Chisinau"}, {"RUSSIA","Moscow"}, + {"TAJIKISTAN","Dushanbe"}, {"TURKMENISTAN","Ashkabad"}, + {"UKRAINE","Kyiv"}, {"UZBEKISTAN","Tashkent"}, + // Europe + {"ALBANIA","Tirana"}, {"ANDORRA","Andorra la Vella"}, + {"AUSTRIA","Vienna"}, {"BELGIUM","Brussels"}, + {"BOSNIA-HERZEGOVINA","Sarajevo"}, + {"CROATIA","Zagreb"}, {"CZECH REPUBLIC","Prague"}, + {"DENMARK","Copenhagen"}, {"ESTONIA","Tallinn"}, + {"FINLAND","Helsinki"}, {"FRANCE","Paris"}, + {"GERMANY","Berlin"}, {"GREECE","Athens"}, + {"HUNGARY","Budapest"}, {"ICELAND","Reykjavik"}, + {"IRELAND","Dublin"}, {"ITALY","Rome"}, + {"LATVIA","Riga"}, {"LIECHTENSTEIN","Vaduz"}, + {"LITHUANIA","Vilnius"}, {"LUXEMBOURG","Luxembourg"}, + {"MACEDONIA","Skopje"}, {"MALTA","Valletta"}, + {"MONACO","Monaco"}, {"MONTENEGRO","Podgorica"}, + {"THE NETHERLANDS","Amsterdam"}, {"NORWAY","Oslo"}, + {"POLAND","Warsaw"}, {"PORTUGAL","Lisbon"}, + {"ROMANIA","Bucharest"}, {"SAN MARINO","San Marino"}, + {"SERBIA","Belgrade"}, {"SLOVAKIA","Bratislava"}, + {"SLOVENIA","Ljuijana"}, {"SPAIN","Madrid"}, + {"SWEDEN","Stockholm"}, {"SWITZERLAND","Berne"}, + {"UNITED KINGDOM","London"}, {"VATICAN CITY","Vatican"}, + // North and Central America + {"ANTIGUA AND BARBUDA","Saint John’s"}, {"BAHAMAS","Nassau"}, + {"BARBADOS","Bridgetown"}, {"BELIZE","Belmopan"}, + {"CANADA","Ottawa"}, {"COSTA RICA","San Jose"}, + {"CUBA","Havana"}, {"DOMINICA","Roseau"}, + {"DOMINICAN REPUBLIC","Santo Domingo"}, + {"EL SALVADOR","San Salvador"}, {"GRENADA","Saint George’s"}, + {"GUATEMALA","Guatemala City"}, {"HAITI","Port-au-Prince"}, + {"HONDURAS","Tegucigalpa"}, {"JAMAICA","Kingston"}, + {"MEXICO","Mexico City"}, {"NICARAGUA","Managua"}, + {"PANAMA","Panama City"}, {"ST. KITTS-NEVIS","Basseterre"}, {"ST. LUCIA","Castries"}, + {"ST. VINCENT AND THE GRENADINES","Kingstown"}, + {"UNITED STATES OF AMERICA","Washington, D.C."}, + // South America + {"ARGENTINA","Buenos Aires"}, {"BOLIVIA","La Paz"}, + {"BRAZIL","Brasilia"}, {"CHILE","Santiago"}, + {"COLOMBIA","Bogota"}, {"ECUADOR","Quito"}, + {"GUYANA","Georgetown"}, {"PARAGUAY","Asuncion"}, + {"PERU","Lima"}, {"SURINAME","Paramaribo"}, + {"TRINIDAD AND TOBAGO","Port of Spain"}, + {"URUGUAY","Montevideo"}, {"VENEZUELA","Caracas"}, + }; + private Exercise1 exercise1; + private Map countries; + private String[] expected = {"russia", "romania", "rwanda"}; + private String[] expected2 = {"RUSSIA", "ROMANIA", "RWANDA"}; + private String expectedCapital = "Bandar Seri Begawan"; + + @Before + public void setUp() throws Exception { + countries = new HashMap(); + for(String[] countryAndCapital : data){ + countries.put(countryAndCapital[0], countryAndCapital[1]); + } + exercise1 = new Exercise1(countries); + + } + + @Test + public void testIteratingOverKeys() throws Exception { + List testResults = exercise1.iteratingOverKeys(); + Assert.assertEquals("Test Iterating Over Keys", Arrays.asList(expected2), testResults); + } + + @Test + public void testIteratingOverEntries() throws Exception { + List testResults = exercise1.iteratingOverEntries(); + Assert.assertEquals("Test Iterating Over Entries", Arrays.asList(expected), testResults); + } + + @Test + public void testIteratingOverValues() throws Exception { + String capital = exercise1.iteratingOverValues(); + Assert.assertEquals("Test Iterating Over Values", expectedCapital, capital); + } +} diff --git a/workshops/Collections/collections-workshop-part2/warm-up2/src/test/java/Exercise2Test.java b/workshops/Collections/collections-workshop-part2/warm-up2/src/test/java/Exercise2Test.java new file mode 100644 index 0000000..a1b00d7 --- /dev/null +++ b/workshops/Collections/collections-workshop-part2/warm-up2/src/test/java/Exercise2Test.java @@ -0,0 +1,73 @@ +import exercise2.Exercise2; +import exercise2.Student; +import junit.framework.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * Created by radu992 on 31.10.2014. + */ +public class Exercise2Test { + private Student[] students = {new Student("Popescu", "Mihai", 8.66), + new Student("Ionescu", "Ion", 7.92), + new Student("Popa", "Cristina", 9.34), + new Student("Barbu", "Mihai", 5.01), + new Student("Popescu", "Mihai", 8.66), + new Student("Serban", "Alin", 9.66), + new Student("Georgescu", "Andreea", 5.01), + new Student("Popa", "Cristina", 9.34), + new Student("Popescu", "Mihai", 8.66),}; + + private Long[] cnps = {1851021345131L, + 1920617149053L, + 1870505168646L, + 1870619152998L, + 1921204325416L, + 1931011351347L}; + + private List studentList; + private List cnpsList; + @Before + public void setUp() throws Exception { + studentList = new ArrayList(Arrays.asList(students)); + cnpsList = new ArrayList(Arrays.asList(cnps)); + + } + + @Test + public void testEqualsMethod() throws Exception { + new Exercise2(studentList, cnpsList).addStudents(); + Student student1 = studentList.get(0); // Mihai Popescu + Student student2 = studentList.get(4); // Mihai Popescu + Student student3 = studentList.get(8); // Mihai Popescu + Assert.assertEquals("Test equals method from student", true, student1.equals(student2)); + + Assert.assertEquals("Test reflexitivity on equals method", true, student1.equals(student1)); + + Assert.assertEquals("Test symmetry on equals method", student2.equals(student1), student1.equals(student2)); + + Assert.assertEquals("Test transitivity on equals method", student1.equals(student3), student1.equals(student2) + && student2.equals(student3)); + + Assert.assertEquals("Test null on equals method", false, student1.equals(null)); + } + + @Test + public void testHashCodeMethod() throws Exception { + Student student1 = studentList.get(0); // Mihai Popescu + Student student2 = studentList.get(4); // Mihai Popescu + Student student3 = studentList.get(1); + Student student4 = new Student("Mihai", "Popescu", 8.98); // Mihai Popescu with other averageGrade + + Assert.assertEquals("Test equal elements to have the same hashCode", student1.hashCode(), student2.hashCode()); + + Assert.assertNotSame("Test different elements to have different hashCode", student1.hashCode(), student3.hashCode()); + + Assert.assertNotSame("Test different elements that differ by one field to have different hashCode", student1.hashCode(), + student4.hashCode()); + } +} diff --git a/workshops/Collections/collections-workshop-part2/warm-up2/warm-up2.iml b/workshops/Collections/collections-workshop-part2/warm-up2/warm-up2.iml new file mode 100644 index 0000000..e1b5c99 --- /dev/null +++ b/workshops/Collections/collections-workshop-part2/warm-up2/warm-up2.iml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + diff --git a/workshops/Collections/collections-workshop.zip b/workshops/Collections/collections-workshop.zip deleted file mode 100644 index 742f23b..0000000 Binary files a/workshops/Collections/collections-workshop.zip and /dev/null differ diff --git a/workshops/Collections/warm-up-workshop/warm-up/pom.xml b/workshops/Collections/warm-up-workshop/warm-up/pom.xml new file mode 100644 index 0000000..2ccc729 --- /dev/null +++ b/workshops/Collections/warm-up-workshop/warm-up/pom.xml @@ -0,0 +1,19 @@ + + + 4.0.0 + + groupId + warm-up + 1.0-SNAPSHOT + + + + junit + junit + 4.11 + + + + \ No newline at end of file diff --git a/workshops/Collections/warm-up-workshop/warm-up/src/main/java/exercise/exercise0/Exercise0.java b/workshops/Collections/warm-up-workshop/warm-up/src/main/java/exercise/exercise0/Exercise0.java new file mode 100644 index 0000000..642a4e7 --- /dev/null +++ b/workshops/Collections/warm-up-workshop/warm-up/src/main/java/exercise/exercise0/Exercise0.java @@ -0,0 +1,55 @@ +package exercise.exercise0; + +/** + * Created by Radu.Hoaghe on 10/29/2014. + * + * Exercise 0: Create a List (ArrayList or LinkedList), add elements to it and print all of them using ListIterator + * for loop and foreach loop + * + */ +import java.util.*; +public class Exercise0 { + + public Exercise0(){ + + } + + public void iterateThroughList(){ + + // TODO Exercise #0 a) Create a list (ArrayList or LinkedList) and add elements to it + + ArrayList arrayList = new ArrayList(); + + arrayList.add(3); + arrayList.add(7); + arrayList.add(11); + + // TODO Exercise #0 a) Don't forget to specify the type of the list (Integer, String etc.) + + // TODO Exercise #0 b) Iterate through the list using ListIterator and print all its elements + + ListIterator it = arrayList.listIterator(); + + while(it.hasNext()) + System.out.print(it.next() + " "); + // TODO Exercise #0 c) Iterate through the list using for loop and print all its elements + + System.out.println(); + for(int i = 0; i < arrayList.size(); i++) + { + System.out.print((Integer)arrayList.get(i) + " "); + } + // TODO Exercise #0 d) Iterate through the list using foreach loop and print all its elements + System.out.println(); + for(Integer i : arrayList) + { + System.out.print(i + " "); + } + } + + public static void main(String[] args) { + // TODO Exercise #0 e) Create a new instance of Exercise0 class and call the iterateThroughList() method + + (new Exercise0()).iterateThroughList(); + } +} diff --git a/workshops/Collections/warm-up-workshop/warm-up/src/main/java/exercise/exercise1/Exercise1.java b/workshops/Collections/warm-up-workshop/warm-up/src/main/java/exercise/exercise1/Exercise1.java new file mode 100644 index 0000000..97d5eff --- /dev/null +++ b/workshops/Collections/warm-up-workshop/warm-up/src/main/java/exercise/exercise1/Exercise1.java @@ -0,0 +1,94 @@ +package exercise.exercise1; + +import java.util.ArrayList; +import java.util.List; +import java.util.ListIterator; + +/** + * Created by Radu.Hoaghe on 10/28/2014. + * + * Exercise 1: Compute the sum, the minimum and the maximum element from a given list (givenList) using three + * different ways to iterate over a List: + * a) ListIterator (implement it in the iterateUsingListIterator() method) + * b) for loop (implement it in the iterateUsingForLoop() method) + * c) foreach loop (implement it in the iterateUsingForEachLoop() method) + * + * In order to test your implementations you need to run the Exercise1Test from the test/java package + * (right-click on Exercise1Test class then click Run 'Exercise1Test' ) + */ +public class Exercise1{ + private List givenList; + + public Exercise1(List l) { + + this.givenList = l; + } + + // TODO Exercise #1 a) Compute sum and get the min and the max from list iterating through it using ListIterator + public List iterateUsingListIterator(){ + + // This List is used only for testing so you don't need to modify it + List testValues = new ArrayList(); + // TODO Exercise #1 a1) In order to pass the tests you need to name your variables sum, min and max or if + // TODO Exercise #1 a1) you want to name them differently you need to modify when you add them to testValues + + // Adding the results to the List in order to be tested + Integer sum =0, min = Integer.MAX_VALUE, max = Integer.MIN_VALUE, element; + ListIterator it = givenList.listIterator(); + while(it.hasNext()) { + + element = it.next(); + sum += element; + max = (element > max) ? element : max; + min = (element < min) ? element : min; + } + testValues.add(sum); + testValues.add(min); + testValues.add(max); + + return testValues; + } + + // TODO Exercise #0 b) Compute sum and get the min and the max from list iterating through it using for loop + public List iterateUsingForLoop(){ + + // This List is used only for testing so you don't need to modify it + List testValues = new ArrayList(); + + // Adding the results to the List in order to be tested + Integer sum = 0, min = Integer.MAX_VALUE, max = Integer.MIN_VALUE; + + for (int i = 0; i < givenList.size(); i++) { + + sum += (Integer) givenList.get(i); + max = ((Integer) givenList.get(i) > max) ? (Integer) givenList.get(i) : max; + min = ((Integer) givenList.get(i) < min) ? (Integer) givenList.get(i) : min; + } + testValues.add(sum); + testValues.add(min); + testValues.add(max); + + return testValues; + } + + // TODO Exercise #0 c) Compute sum and get the min and the max from list iterating through it using foreach loop + public List iterateUsingForEachLoop(){ + + // This List is used only for testing so you don't need to modify it + List testValues = new ArrayList(); + Integer sum = 0, min = Integer.MAX_VALUE, max = Integer.MIN_VALUE; + + for (Integer element : givenList) { + + sum += element; + max = (element > max) ? element : max; + min = (element < min) ? element : min; + } + // Adding the results to the List in order to be tested + testValues.add(sum); + testValues.add(min); + testValues.add(max); + + return testValues; + } +} diff --git a/workshops/Collections/warm-up-workshop/warm-up/src/main/java/exercise/exercise2/MyList.java b/workshops/Collections/warm-up-workshop/warm-up/src/main/java/exercise/exercise2/MyList.java new file mode 100644 index 0000000..3eab5f0 --- /dev/null +++ b/workshops/Collections/warm-up-workshop/warm-up/src/main/java/exercise/exercise2/MyList.java @@ -0,0 +1,127 @@ +package exercise.exercise2; + +import java.util.ArrayList; +import java.util.Collection; + +/** + * Created by Radu.Hoaghe on 28.10.2014. + * + * Exercise 2: Create a class that inherits ArrayList (class MyList). + * + * This list (MyList) should have the following functionality, besides the functionality + * that ArrayList already offers: it should retain in every moment how many different + * elements exist in the list. + * + * Examples: 1. If you have a List that contains: 7 5 3 2 4 1, there are 6 different elements. + * 2. If you have a List that contains: 5 6 1 2 5 6, there are 4 different elements. + * + * A variable that retains the number of different elements that exist in the list in + * every moment was already defined (differentElements). + * + * First of all, you will need to override the add methods so that every time a different + * element is added the counter will be updated. + * Hint : check out the List documentation to see the methods signatures. + * + * Secondly, you will also need to override the remove methods (Hint: Of course the List + * documentation) because the number of different elements in the list could change if + * the last element of its kind in the list is removed and by not overrriding it the + * counter will remain unchanged. + * + * Finally, you will need to override the clear method and create a getter method for the + * counter (in order to access it outside the class). + * + * In order to add/remove/clear the elements into/from the list you will need to use the + * add/remove/clear methods inherited from ArrayList. + * + * To test your implementation run the Exercise2Test class. + * + */ +public class MyList extends ArrayList { + + public int getDifferentElements() { + return differentElements; + } + + // A counter to hold the number of adds that were made on the list + private int differentElements; + + public MyList(){ + + super(); + differentElements = 0; + } + + // TODO Exercise #2 a) Override add() and addAll() methods so that the list should retain the number of + + + public boolean add(Integer element) { + if(!this.contains((Integer)element)) + differentElements++; + return super.add((Integer)element); + } + + @Override + public void add(int index, Integer e) { + + if(!this.contains((Integer)e)) + differentElements++; + super.add(index, (Integer)e); + } + + @Override + public boolean addAll(int index, Collection c) { + + for(Integer element: c) { + this.add(index, element); + index++; + } + + return true; + } + @Override + public boolean addAll(Collection c) { + + for(Integer element: c) { + if(!this.add(element)) + return false; + } + return true; + } + // TODO Exercise #2 a) different elements + + // TODO Exercise #2 b) Override the remove methods so that the number of different elements is updated when + + @Override + public boolean remove(Object o) { + + boolean res = super.remove(o); + if (res && !this.contains((Integer)o)) + differentElements--; + return res; + } + + @Override + public Integer remove(int index) { + + + Integer res = super.remove(index); + if (!this.contains(res)) + differentElements--; + return res; + } + + @Override + public void clear() { + super.clear(); + differentElements = 0; + } + + + // TODO Exercise #2 b) an element is removed + // TODO Exercise #2 b) hint: you need to update the number of different elements only when + // TODO Exercise #2 b) the element that needs to be removed is the last element of its kind in the list + + // TODO Exercise #2 c) Override the clear method and reset the number of different elements + + // TODO Exercise #2 d) Generate a getter method in order to get the counter value +} diff --git a/workshops/Collections/warm-up-workshop/warm-up/src/main/java/exercise/exercise3/Exercise3.java b/workshops/Collections/warm-up-workshop/warm-up/src/main/java/exercise/exercise3/Exercise3.java new file mode 100644 index 0000000..07dfebc --- /dev/null +++ b/workshops/Collections/warm-up-workshop/warm-up/src/main/java/exercise/exercise3/Exercise3.java @@ -0,0 +1,61 @@ +package exercise.exercise3; + +import java.util.*; + +/** + * Created by Radu.Hoaghe on 10/28/2014. + * + * Exercise 3: Fill three Set implementations that you know (Hint: they were described during + * the earlier presentation) with the List that is given to this class by + * its constructor. + * + * Check out the elements that the list mentioned above contains and then, add them + * to your three Sets. After this check out the elements of your Sets. What do you + * remark? What could be the reason? + * + * Finally, add to the one of the three Sets some elements + * that already exist in the Set (e.g add("that") and add("collection")) + * + * To run your implementation, run the Exercise3Test class. + */ +public class Exercise3 { + + // List containing some elements that need to be added into the Set + private List listToAdd; + + public Exercise3(List l) { + listToAdd = l; + } + + public void addElementsToSets(){ + + System.out.println("The elements that will be added to the Sets: "); + // TODO Exercise #3 a) Check the content of the elements you will add into the Set + + Set hashSet, linkedHashSet; + SortedSet treeSet; + System.out.print(listToAdd); + + hashSet = new HashSet(); + linkedHashSet = new LinkedHashSet(); + treeSet = new TreeSet(); + + + hashSet.addAll(listToAdd); + linkedHashSet.addAll(listToAdd); + treeSet.addAll(listToAdd); + + treeSet.add("duplicate"); + + System.out.println("\n hashSet " + hashSet); + System.out.println("linkedHashSet " + linkedHashSet); + System.out.println("treeSet " + treeSet); + + // TODO Exercise #3 b) add the elements from listToAdd to the Sets + + // TODO Exercise #3 c) Check the content of the Sets + + // TODO Exercise #3 d) Add to the TreeSet two elements that already exist in the Set + // TODO Exercise #3 d) and print again the TreeSet. What do you see? + } +} diff --git a/workshops/Collections/warm-up-workshop/warm-up/src/test/java/Exercise1Test.java b/workshops/Collections/warm-up-workshop/warm-up/src/test/java/Exercise1Test.java new file mode 100644 index 0000000..3f7cf75 --- /dev/null +++ b/workshops/Collections/warm-up-workshop/warm-up/src/test/java/Exercise1Test.java @@ -0,0 +1,53 @@ +import exercise.exercise1.Exercise1; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * Created by Radu.Hoaghe on 10/29/2014. + */ +public class Exercise1Test { + private Integer[] v = {15, 5, 34, 23, 7, 19, 20, 22, 78, 39, 8, 30, 27, 62, 28, 7}; + private List listToTest; + private List expected; + + private Exercise1 ex1; + + @Before + public void setUp() throws Exception { + listToTest = Arrays.asList(v); + ex1 = new Exercise1(listToTest); + expected = new ArrayList(); + expected.add(424); // sum + expected.add(5); // min + expected.add(78); // max + } + + @Test + public void testIterator() throws Exception { + List testResults = ex1.iterateUsingListIterator(); + Assert.assertEquals("Test sum with iterator", expected.get(0), testResults.get(0)); + Assert.assertEquals("Test min with iterator", expected.get(1), testResults.get(1)); + Assert.assertEquals("Test max with iterator", expected.get(2), testResults.get(2)); + } + + @Test + public void testFor() throws Exception { + List testResults = ex1.iterateUsingForLoop(); + Assert.assertEquals("Test sum with for", expected.get(0), testResults.get(0)); + Assert.assertEquals("Test min with for", expected.get(1), testResults.get(1)); + Assert.assertEquals("Test max with for", expected.get(2), testResults.get(2)); + } + + @Test + public void testForEach() throws Exception { + List testResults = ex1.iterateUsingForEachLoop(); + Assert.assertEquals("Test sum with foreach", expected.get(0), testResults.get(0)); + Assert.assertEquals("Test min with foreach", expected.get(1), testResults.get(1)); + Assert.assertEquals("Test max with foreach", expected.get(2), testResults.get(2)); + } +} diff --git a/workshops/Collections/warm-up-workshop/warm-up/src/test/java/Exercise2Test.java b/workshops/Collections/warm-up-workshop/warm-up/src/test/java/Exercise2Test.java new file mode 100644 index 0000000..fb0317b --- /dev/null +++ b/workshops/Collections/warm-up-workshop/warm-up/src/test/java/Exercise2Test.java @@ -0,0 +1,155 @@ +import exercise.exercise2.MyList; +import junit.framework.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; + + +public class Exercise2Test { + private MyList listToTest; + private int addExpectedTest1, addExpectedTest2; + private int insertExpectedTest1; + private int removeExpectedTest1, removeExpectedTest2; + private int addAllExpectedTest1, addAllExpectedTest2; + private Integer[] v = new Integer[5]; + + @Before + public void setUp() throws Exception { + listToTest = new MyList(); + addExpectedTest1 = 10; + addExpectedTest2 = 11; + insertExpectedTest1 = 13; + addAllExpectedTest1 = 10; + addAllExpectedTest2 = 13; + removeExpectedTest1 = 8; + removeExpectedTest2 = 8; + } + + @Test + public void testAdd() throws Exception { + listToTest.clear(); + Assert.assertEquals("Test clear method", 0, listToTest.getDifferentElements()); + + for(int i = 0; i < 10; i++) + listToTest.add(i); + + Assert.assertEquals("Test add method with all different elements", addExpectedTest1, listToTest.getDifferentElements()); + + listToTest.add(5); + listToTest.add(3); + listToTest.add(11); + listToTest.add(8); + + Assert.assertEquals("Test add method with some equal elements", addExpectedTest2, listToTest.getDifferentElements()); + } + + @Test + public void testAddAtIndex() throws Exception { + listToTest.clear(); + Assert.assertEquals("Test clear method", 0, listToTest.getDifferentElements()); + + + for(int i = 0; i < 10; i++) + listToTest.add(i); + + listToTest.add(8, 11); + listToTest.add(5, 12); + listToTest.add(3, 13); + + Assert.assertEquals("Test insert different elements", insertExpectedTest1, listToTest.getDifferentElements()); + + listToTest.add(7, 8); + listToTest.add(4, 4); + listToTest.add(5, 12); + + Assert.assertEquals("Test insert equal elements", insertExpectedTest1, listToTest.getDifferentElements()); + + } + + @Test + public void testAddAll() throws Exception { + listToTest.clear(); + Assert.assertEquals("Test clear method", 0, listToTest.getDifferentElements()); + + for(int i = 0; i < 10; i++) + listToTest.add(i); + + for(int i = 0; i < 5; i++) + v[i] = i; + + listToTest.addAll(Arrays.asList(v)); + Assert.assertEquals("Test addAll with all equal elements", addAllExpectedTest1, listToTest.getDifferentElements()); + + for(int i = 0; i < 5; i++) + v[i] = 5 * i; + + listToTest.addAll(Arrays.asList(v)); + Assert.assertEquals("Test addAll with all some different elements", addAllExpectedTest2, listToTest.getDifferentElements()); + + } + + @Test + public void testAddAllAtIndex() throws Exception { + listToTest.clear(); + Assert.assertEquals("Test clear method", 0, listToTest.getDifferentElements()); + + for(int i = 0; i < 10; i++) + listToTest.add(i); + + for(int i = 0; i < 5; i++) + v[i] = i; + + listToTest.addAll(5, Arrays.asList(v)); + Assert.assertEquals("Test addAll with all equal elements", addAllExpectedTest1, listToTest.getDifferentElements()); + + for(int i = 0; i < 5; i++) + v[i] = 5 * i; + + listToTest.addAll(10, Arrays.asList(v)); + Assert.assertEquals("Test addAll with some different elements", addAllExpectedTest2, listToTest.getDifferentElements()); + + } + + @Test + public void testRemove() throws Exception { + listToTest.clear(); + Assert.assertEquals("Test clear method", 0, listToTest.getDifferentElements()); + + for(int i = 0; i < 10; i++) + listToTest.add(i); + + listToTest.remove(5); + listToTest.remove(3); + + Assert.assertEquals("Test remove elements with 1 occurence", removeExpectedTest1, listToTest.getDifferentElements()); + + listToTest.add(7); + listToTest.add(7); + listToTest.remove(5); + + Assert.assertEquals("Test remove element with 3 occurences", removeExpectedTest2, listToTest.getDifferentElements()); + + } + + @Test + public void testRemoveObject() throws Exception { + listToTest.clear(); + Assert.assertEquals("Test clear method", 0, listToTest.getDifferentElements()); + + for(int i = 0; i < 10; i++) + listToTest.add(i); + + listToTest.remove((Integer) 5); + listToTest.remove((Integer) 3); + + Assert.assertEquals("Test remove elements with 1 occurence", removeExpectedTest1, listToTest.getDifferentElements()); + + listToTest.add(7); + listToTest.add(7); + listToTest.remove((Integer) 7); + + Assert.assertEquals("Test remove element with 3 occurences", removeExpectedTest2, listToTest.getDifferentElements()); + + } +} diff --git a/workshops/Collections/warm-up-workshop/warm-up/src/test/java/Exercise3Test.java b/workshops/Collections/warm-up-workshop/warm-up/src/test/java/Exercise3Test.java new file mode 100644 index 0000000..5fd7b7e --- /dev/null +++ b/workshops/Collections/warm-up-workshop/warm-up/src/test/java/Exercise3Test.java @@ -0,0 +1,25 @@ +import exercise.exercise3.Exercise3; +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; + +/** + * Created by Radu.Hoaghe on 10/29/2014. + */ +public class Exercise3Test { + private Exercise3 exercise3; + private String[] stringsToAddToSet = {"A", "collection", "that", "contains", "no", "duplicate", "elements", "."}; + + @Before + public void setUp() throws Exception { + exercise3 = new Exercise3(Arrays.asList(stringsToAddToSet)); + + } + + @Test + public void testExercise3() throws Exception { + exercise3.addElementsToSets(); + + } +} diff --git a/workshops/Collections/warm-up-workshop/warm-up/warm-up.iml b/workshops/Collections/warm-up-workshop/warm-up/warm-up.iml new file mode 100644 index 0000000..e1b5c99 --- /dev/null +++ b/workshops/Collections/warm-up-workshop/warm-up/warm-up.iml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + diff --git a/workshops/Collections/~$llections Workshop.docx b/workshops/Collections/~$llections Workshop.docx new file mode 100644 index 0000000..eaa1c6c Binary files /dev/null and b/workshops/Collections/~$llections Workshop.docx differ diff --git a/workshops/SQL/SQL-Workshop.docx b/workshops/SQL/SQL-Workshop.docx deleted file mode 100644 index ec496f9..0000000 Binary files a/workshops/SQL/SQL-Workshop.docx and /dev/null differ diff --git a/workshops/Workshop Maven.docx b/workshops/Workshop Maven.docx index 28caa99..1663454 100644 Binary files a/workshops/Workshop Maven.docx and b/workshops/Workshop Maven.docx differ diff --git a/workshops/~$rkshop part 1 lab.docx b/workshops/~$rkshop part 1 lab.docx new file mode 100644 index 0000000..eaa1c6c Binary files /dev/null and b/workshops/~$rkshop part 1 lab.docx differ