diff --git a/ReadmeResistencia.md b/ReadmeResistencia.md new file mode 100644 index 00000000..de5848de --- /dev/null +++ b/ReadmeResistencia.md @@ -0,0 +1,15 @@ +Los tutores del Bootcamp de Resistencia son: + +"Dario Santiago Vallejos" +"Gabriel Zimmermann" +"Mauro Daniel Perez" +"Nicolas Pogulanik" +"Justo Vargas" + +Site Manager + +"Juan Delfino" , + +Champion + +"Claudia Romero" , diff --git a/exercise_1/Circle.java b/exercise_1/Circle.java new file mode 100644 index 00000000..4fec49bb --- /dev/null +++ b/exercise_1/Circle.java @@ -0,0 +1,78 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +/** + * + * @author roberta + */ +public class Circle { // save as "Circle.java" + // private instance variable, not accessible from outside this class + + private double radius; + private String color; + + // 1st constructor, which sets both radius and color to default + public Circle() { + radius = 1.0; + color = "red"; + } + + // 2nd constructor with given radius, but color default + public Circle(double r) { + radius = r; + color = "red"; + } + + //1) 3rd constructor with the given radius and color + /** + * public Circle(double r, String c) { + * + * radius = r; color = c; + * + * } + */ + // A public method for retrieving the radius + public double getRadius() { + return radius; + } + + // A public method for computing the area of circle + public double getArea() { + return radius * radius * Math.PI; + } + + //2) Getter for instance variable color + public String getColor() { + return color; + } + + /** + * 4)Setter for instance variable radius public void setRadius(double r) { + * radius = r; } + * + * //4)// Setter for instance variable color public void setColor(String c){ + * color = c; } + */ + //5) Using the special keyword "this" + public void setRadius(double radius) { + this.radius = radius; + } + + public void setColor(String color) { + this.color = color; + } + + //5) Using the special keyword "this" + public Circle(double radius, String color) { + this.color = color; + this.radius = radius; + } + + public String toString() { + return "Circle: radius=" + radius + " color=" + color; + } + +} diff --git a/exercise_1/TestCircle.java b/exercise_1/TestCircle.java new file mode 100644 index 00000000..c9a659fe --- /dev/null +++ b/exercise_1/TestCircle.java @@ -0,0 +1,47 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +/** + * + * @author roberta + */ +public class TestCircle { // save as "TestCircle.java" + + public static void main(String[] args) { + // Declare and allocate an instance of class Circle called c1 + // with default radius and color + Circle c1 = new Circle(); + // Use the dot operator to invoke methods of instance c1. + System.out.println("The circle has radius of " + + c1.getRadius() + " and area of " + c1.getArea()); + + // Declare and allocate an instance of class circle called c2 + // with the given radius and default color + Circle c2 = new Circle(); + // Use the dot operator to invoke methods of instance c2. + System.out.println("The circle has radius of " + + c2.getRadius() + " and area of " + c2.getArea()); + + // 3) System.out.println(c1.radius); No, I can't because it has private access in Circle + // c1.radius=5.0; + + // 4) + Circle c3 = new Circle(); // construct an instance of Circle + c3.setRadius(5.0); // change radius + c3.setColor("green"); // change color + + /**5) toString + Circle c6 = new Circle(5.0); + System.out.println(c1.toString()); + + Circle c5 = new Circle(1.2); + System.out.println(c5.toString()); // explicit call + System.out.println(c5); // println() calls toString() implicitly, same as above + System.out.println("Operator '+' invokes toString() too: " + c2); // '+' invokes toString() too */ + + + } +} diff --git a/topic4-CRUD/userService/pom.xml b/topic4-CRUD/userService/pom.xml new file mode 100644 index 00000000..0b513a20 --- /dev/null +++ b/topic4-CRUD/userService/pom.xml @@ -0,0 +1,51 @@ + + + 4.0.0 + + org.springframework + gs-rest-service + 0.1.0 + + + org.springframework.boot + spring-boot-starter-parent + 1.1.10.RELEASE + + + + + org.springframework.boot + spring-boot-starter-web + + + junit + junit + 4.10 + test + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + + spring-releases + https://repo.spring.io/libs-release + + + + + spring-releases + https://repo.spring.io/libs-release + + + diff --git a/topic4-CRUD/userService/src/main/java/hello/Application.java b/topic4-CRUD/userService/src/main/java/hello/Application.java new file mode 100644 index 00000000..1f4b1db8 --- /dev/null +++ b/topic4-CRUD/userService/src/main/java/hello/Application.java @@ -0,0 +1,14 @@ +package hello; + +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.SpringApplication; +import org.springframework.context.annotation.ComponentScan; + +@ComponentScan +@EnableAutoConfiguration +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} diff --git a/topic4-CRUD/userService/src/main/java/hello/MySystemController.java b/topic4-CRUD/userService/src/main/java/hello/MySystemController.java new file mode 100644 index 00000000..25418f28 --- /dev/null +++ b/topic4-CRUD/userService/src/main/java/hello/MySystemController.java @@ -0,0 +1,122 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package hello; + +import java.util.ArrayList; +import java.util.List; + +/** + * + * @author roberta + */ +public class MySystemController { + + private List userList = new ArrayList(); + private static MySystemController system; + + public static MySystemController getInstance() { + if (system == null) { + system = new MySystemController(); + + } + return system; + } + + private MySystemController() { + } + + /** + * Creates a new user,if the user already exists displays an exception + * + * @param userName + * @param email + * @param password + * @param realName + * @throws UserAlreadyExistsException + */ + public void createUser(String userName, String email, String password, String realName) throws UserAlreadyExistsException { + + User user = new User(userName, email, password, realName); + if (!userList.contains(user)) { + userList.add(user); + } else { + throw new UserAlreadyExistsException("User already exists!"); + } + } + + /** + * Eliminate a user if exists and matches with the email inserted + * + * @param email + * + */ + public void deleteUser(String email) { + Integer indexOf = null; + for (User u : userList) { + if (u.getEmail().equals(email)) { + indexOf = userList.indexOf(u); + break; + } + } + if (indexOf != null) { + userList.remove(userList.get(indexOf)); + } + } + + /** + * Search the user by userName and return a list with the values. + * + * @param userName + * @return + */ + public List readUser(String userName) { + List resultList = new ArrayList(); + for (User u : userList) { + if (u.getUserName().contains(userName)) { + resultList.add(u); + } + } + return resultList; + + } + + /** + * Find a user by mail + * + * @param mail + * @return + */ + public User retrieveUser(String mail) { + + for (User u : userList) { + if (u.getEmail().equals(mail)) { + return u; + } + } + return null; + } + + /** + * Search for the email, and update userName | password | realName + * + * @param userName + * @param password + * @param email + * @param realName + */ + public void updateUser(String userName, String password, String email, String realName) { + + for (User u : userList) { + if (u.getEmail().equals(email)) { + u.setPassword(password); + u.setRealName(realName); + u.setUserName(userName); + break; + } + } + } + +} diff --git a/topic4-CRUD/userService/src/main/java/hello/Photo.java b/topic4-CRUD/userService/src/main/java/hello/Photo.java new file mode 100644 index 00000000..1418e06c --- /dev/null +++ b/topic4-CRUD/userService/src/main/java/hello/Photo.java @@ -0,0 +1,15 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package hello; + +/** + * + * @author roberta + */ +public class Photo { + +} diff --git a/topic4-CRUD/userService/src/main/java/hello/User.java b/topic4-CRUD/userService/src/main/java/hello/User.java new file mode 100644 index 00000000..3af15fa6 --- /dev/null +++ b/topic4-CRUD/userService/src/main/java/hello/User.java @@ -0,0 +1,83 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package hello; + +/** + * + * @author roberta + */ +public class User { + + private String userName; + private String email; + private String password; + private String realName; + + + public User(String userName, String email, String password, String realName) { + this.userName = userName; + this.email = email; + this.password = password; + this.realName = realName; + } + + public String getRealName() { + return realName; + } + + public void setRealName(String realName) { + this.realName = realName; + } + + public String getUserName() { + return userName; + } + + public String getEmail() { + return email; + } + + public String getPassword() { + return password; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public void setEmail(String email) { + this.email = email; + } + + public void setPassword(String password) { + this.password = password; + } + + @Override + public int hashCode() { + int hash = 3; + hash = 13 * hash + (this.email != null ? this.email.hashCode() : 0); + return hash; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final User other = (User) obj; + if ((this.email == null) ? (other.email != null) : !this.email.equals(other.email)) { + return false; + } + return true; + } + + + +} diff --git a/topic4-CRUD/userService/src/main/java/hello/UserAlreadyExistsException.java b/topic4-CRUD/userService/src/main/java/hello/UserAlreadyExistsException.java new file mode 100644 index 00000000..27b4afb5 --- /dev/null +++ b/topic4-CRUD/userService/src/main/java/hello/UserAlreadyExistsException.java @@ -0,0 +1,22 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package hello; + +/** + * + * @author roberta + */ +public class UserAlreadyExistsException extends Exception { + + public UserAlreadyExistsException() { + } + + public UserAlreadyExistsException(String string) { + super(string); + } + +} diff --git a/topic4-CRUD/userService/src/main/java/hello/WSApi.java b/topic4-CRUD/userService/src/main/java/hello/WSApi.java new file mode 100644 index 00000000..004f0269 --- /dev/null +++ b/topic4-CRUD/userService/src/main/java/hello/WSApi.java @@ -0,0 +1,44 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package hello; + +import java.util.logging.Level; +import java.util.logging.Logger; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +/** + * + * @author roberta + */ +@RestController +public class WSApi { + + @RequestMapping("/addUser") + public String addUser(@RequestParam(value = "userName") String userName, + @RequestParam(value = "email") String email, + @RequestParam(value = "password") String password, + @RequestParam(value = "realName") String realName) { + + try { + MySystemController.getInstance().createUser(userName, email, password, realName); + return "User created successfully!"; + } catch (UserAlreadyExistsException ex) { + Logger.getLogger(WSApi.class.getName()).log(Level.SEVERE, null, ex); + return ex.getMessage(); + } + + } + + @RequestMapping("/deleteUser") + public String deleteUser(@RequestParam(value = "userName") String userName) { + + MySystemController.getInstance().deleteUser(userName); + return "User eliminated successfully!"; + + } +} diff --git a/topic4-CRUD/userService/src/test/java/hello/MySystemControllerTest.java b/topic4-CRUD/userService/src/test/java/hello/MySystemControllerTest.java new file mode 100644 index 00000000..13984773 --- /dev/null +++ b/topic4-CRUD/userService/src/test/java/hello/MySystemControllerTest.java @@ -0,0 +1,112 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package hello; + +import java.util.List; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import static org.junit.Assert.*; +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * + * @author roberta + */ +public class MySystemControllerTest { + + private User jhon = new User("john", "jhon@gmail.com", "1234", "John Smith"); + private User mike = new User("mike", "mike@gmail.com", "1234", "Mike Gonzalez"); + private User linus = new User("linus", "linus@gmail.com", "1234", "Linus Torval"); + private User charles = new User("charles", "charles@gmail.com", "45687", "Charles Xavier"); + private MySystemController instance = MySystemController.getInstance(); + + public MySystemControllerTest() { + } + + @BeforeClass + public static void setUpClass() { + } + + @AfterClass + public static void tearDownClass() { + } + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + + /** + * Test of getInstance method, of class MySystemController. + */ + @org.junit.Test + public void testGetInstance() { + System.out.println("getInstance"); + MySystemController result = MySystemController.getInstance(); + assertNotNull(result); + + } + + /** + * Test of createUser method, of class MySystemController. + */ + @org.junit.Test + public void testCreateUser() throws Exception { + System.out.println("createUser"); + User user1 = new User("user1", "user1@hola.com", "1234", "Ana Lopez"); + String userName = "user1"; + String email = "user1@hola.com"; + String password = "1234"; + String realName = "Ana Perez"; + MySystemController instance = MySystemController.getInstance(); + instance.createUser(userName, email, password, realName); + List result = instance.readUser("user1"); + List expected = new ArrayList(); + expected.add(user1); + assertEquals(expected, result); + } + + /** + * Test of deleteUser method, of class MySystemController. + */ + @org.junit.Test + public void testDeleteUser() { + System.out.println("deleteUser"); + try { + instance.createUser(mike.getUserName(), mike.getEmail(), mike.getPassword(), mike.getRealName()); + } catch (UserAlreadyExistsException ex) { + Logger.getLogger(MySystemControllerTest.class.getName()).log(Level.SEVERE, null, ex); + fail(); + } + instance.deleteUser(mike.getEmail()); + assertNull(instance.retrieveUser(mike.getEmail())); + } + + /** + * Test of updateUser method, of class MySystemController. + */ + @org.junit.Test + public void testUpdateUser() { + try { + instance.createUser(mike.getUserName(), mike.getEmail(), mike.getPassword(), mike.getRealName()); + } catch (UserAlreadyExistsException ex) { + Logger.getLogger(MySystemControllerTest.class.getName()).log(Level.SEVERE, null, ex); + fail(); + } + instance.updateUser("mike1", mike.getPassword(), mike.getEmail(), mike.getRealName()); + User retrieveUser = instance.retrieveUser(mike.getEmail()); + assertEquals("mike1", retrieveUser.getUserName()); + } + +}