From a1093d4d68d07f17a9f3668d61b32ee1d2b816a3 Mon Sep 17 00:00:00 2001 From: Manfredas Date: Sat, 1 Jun 2024 10:04:08 +0200 Subject: [PATCH 1/2] Week 4 Lab --- .gitignore | 38 ++++++++ .idea/.gitignore | 8 ++ .idea/misc.xml | 6 ++ .idea/vcs.xml | 6 ++ pom.xml | 23 +++++ src/main/java/Main.java | 2 + src/main/java/com/ironhack/Main.java | 17 ++++ src/main/java/com/ironhack/Person.java | 97 +++++++++++++++++++ src/main/java/com/ironhack/PersonList.java | 55 +++++++++++ .../java/com/ironhack/PersonListTest.java | 0 src/test/java/com/ironhack/PersonTest.java | 19 ++++ 11 files changed, 271 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/misc.xml create mode 100644 .idea/vcs.xml create mode 100644 pom.xml create mode 100644 src/main/java/Main.java create mode 100644 src/main/java/com/ironhack/Main.java create mode 100644 src/main/java/com/ironhack/Person.java create mode 100644 src/main/java/com/ironhack/PersonList.java create mode 100644 src/test/java/com/ironhack/PersonListTest.java create mode 100644 src/test/java/com/ironhack/PersonTest.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..1b2d693 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..9279a81 --- /dev/null +++ b/pom.xml @@ -0,0 +1,23 @@ + + + 4.0.0 + + com.ironhack + untitled2 + 1.0-SNAPSHOT + + + 22 + 22 + UTF-8 + + + + junit + junit + 4.13.2 + + + \ No newline at end of file diff --git a/src/main/java/Main.java b/src/main/java/Main.java new file mode 100644 index 0000000..7046417 --- /dev/null +++ b/src/main/java/Main.java @@ -0,0 +1,2 @@ +public class Main { +} diff --git a/src/main/java/com/ironhack/Main.java b/src/main/java/com/ironhack/Main.java new file mode 100644 index 0000000..dfb64c1 --- /dev/null +++ b/src/main/java/com/ironhack/Main.java @@ -0,0 +1,17 @@ +package com.ironhack; + +//TIP To Run code, press or +// click the icon in the gutter. +public class Main { + public static void main(String[] args) { + //TIP Press with your caret at the highlighted text + // to see how IntelliJ IDEA suggests fixing it. + System.out.printf("Hello and welcome!"); + + for (int i = 1; i <= 5; i++) { + //TIP Press to start debugging your code. We have set one breakpoint + // for you, but you can always add more by pressing . + System.out.println("i = " + i); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/ironhack/Person.java b/src/main/java/com/ironhack/Person.java new file mode 100644 index 0000000..dc932d5 --- /dev/null +++ b/src/main/java/com/ironhack/Person.java @@ -0,0 +1,97 @@ +package com.ironhack; +//Create a Person class that has the following properties: +// +//id: an integer +//name: a string formatted as "firstName lastName" +//age: an integer +//occupation: a stringCreate a Person class that has the following properties: +// +//id: an integer +//name: a string formatted as "firstName lastName" +//age: an integer +//occupation: a string + +import java.util.Objects; + +public class Person { + private int id; + private String firstName; + private String lastName; + private int age; + private String occupation; + private static int nextId = 1; + //A constructor that takes in an integer id, a string name, + // an integer age and a string occupation as arguments and sets their respective properties. + + public Person(int id, String name, int age, String occupation) { + this.id = nextId++; + this.firstName = name; + this.lastName = name; + this.age = age; + this.occupation = occupation; + + } + //A setAge method that takes in an integer age and sets the age property, + // but throws an error if age is less than 0. + public int getAge() { + return age; + } + + public void setAge ( int age){ + this.age = age; + if(age<=0){ + throw new RuntimeException("Age can not be less than 0"); + } + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName){ + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = nextId; + } + + public static int getNextId() { + return nextId; + } + + public static void setNextId(int nextId) { + Person.nextId = nextId; + } + + public String getOccupation () { + return occupation; + } + + public void setOccupation (String occupation){ + this.occupation = occupation; + } + //An equals method that takes in a Person object and returns true if their properties are the same, + // excluding the id property. + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Person person = (Person) o; + return age == person.age && Objects.equals(firstName, person.firstName) && Objects.equals(occupation, person.occupation); + } + + +} \ No newline at end of file diff --git a/src/main/java/com/ironhack/PersonList.java b/src/main/java/com/ironhack/PersonList.java new file mode 100644 index 0000000..6011039 --- /dev/null +++ b/src/main/java/com/ironhack/PersonList.java @@ -0,0 +1,55 @@ +package com.ironhack; + +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.List; + +//Create a PersonsList class that holds a list of Person objects. +public class PersonList { + private List people; + //Create a findByName method that takes in a string name and returns the Person object with a name that matches exactly. The name parameter should be formatted as "firstName lastName". + // This method should throw an exception if the name parameter is not properly formatted. + + + public Person findByName(String name) { + String[] nameParts = name.split(" "); + if (nameParts.length != 2) { + throw new RuntimeException("Name should formatted as 'firstName lastName'"); + } + String firstName = nameParts[0]; + String lastName = nameParts[1]; + for (Person person : people) { + if (firstName.equals(person.getFirstName()) && lastName.equals(person.getLastName())) { + return person; + } + } + return null; + } + //Create a clone method that takes in a Person object and returns + // a new Person object with the same properties, except with a new id. + public Person clone (){ + try{ + Person clonesdPerson = (Person) super.clone(); + clonesdPerson.setId(Person.getNextId()); + }catch (CloneNotSupportedException e) { + throw new RuntimeException(); + } + return clone(); + + + } + //Create a method that takes in a Person object as a parameter and uses the toString method to write the Person information to a file. + // This method should handle any errors as necessary. + public void writePersonToFile(Person person, String fileName) { + try(PrintWriter out = new PrintWriter(new FileWriter(fileName))) { + out.println(person.toString()); + }catch (IOException e) { + e.printStackTrace(); + } + + } + + + +} \ No newline at end of file diff --git a/src/test/java/com/ironhack/PersonListTest.java b/src/test/java/com/ironhack/PersonListTest.java new file mode 100644 index 0000000..e69de29 diff --git a/src/test/java/com/ironhack/PersonTest.java b/src/test/java/com/ironhack/PersonTest.java new file mode 100644 index 0000000..fd40186 --- /dev/null +++ b/src/test/java/com/ironhack/PersonTest.java @@ -0,0 +1,19 @@ +package com.ironhack; + +import junit.framework.TestCase; +import org.junit.Test; + +public class PersonTest extends TestCase { + + @Test + + public void testSetAgeThrowsErrorForNegativeAge() { + Person person = new Person(1, "James",18, "Welder"); + try { + person.setAge(-5); + fail("Expected an IllegalArgumentException to be thrown"); + } catch (IllegalArgumentException e) { + assertEquals("Age cannot be negative", e.getMessage()); + } + } +} From 4f3631b95a420199d007a7118bd5480f4c3fd5fb Mon Sep 17 00:00:00 2001 From: Manfredas Date: Sat, 1 Jun 2024 10:08:40 +0200 Subject: [PATCH 2/2] Week 4 Lab --- src/main/java/Main.java | 17 ++++++++++++++++- src/main/java/com/{ironhack => }/Person.java | 0 .../java/com/{ironhack => }/PersonList.java | 0 src/main/java/com/ironhack/Main.java | 17 ----------------- 4 files changed, 16 insertions(+), 18 deletions(-) rename src/main/java/com/{ironhack => }/Person.java (100%) rename src/main/java/com/{ironhack => }/PersonList.java (100%) delete mode 100644 src/main/java/com/ironhack/Main.java diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 7046417..dfb64c1 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,2 +1,17 @@ +package com.ironhack; + +//TIP To Run code, press or +// click the icon in the gutter. public class Main { -} + public static void main(String[] args) { + //TIP Press with your caret at the highlighted text + // to see how IntelliJ IDEA suggests fixing it. + System.out.printf("Hello and welcome!"); + + for (int i = 1; i <= 5; i++) { + //TIP Press to start debugging your code. We have set one breakpoint + // for you, but you can always add more by pressing . + System.out.println("i = " + i); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/ironhack/Person.java b/src/main/java/com/Person.java similarity index 100% rename from src/main/java/com/ironhack/Person.java rename to src/main/java/com/Person.java diff --git a/src/main/java/com/ironhack/PersonList.java b/src/main/java/com/PersonList.java similarity index 100% rename from src/main/java/com/ironhack/PersonList.java rename to src/main/java/com/PersonList.java diff --git a/src/main/java/com/ironhack/Main.java b/src/main/java/com/ironhack/Main.java deleted file mode 100644 index dfb64c1..0000000 --- a/src/main/java/com/ironhack/Main.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.ironhack; - -//TIP To Run code, press or -// click the icon in the gutter. -public class Main { - public static void main(String[] args) { - //TIP Press with your caret at the highlighted text - // to see how IntelliJ IDEA suggests fixing it. - System.out.printf("Hello and welcome!"); - - for (int i = 1; i <= 5; i++) { - //TIP Press to start debugging your code. We have set one breakpoint - // for you, but you can always add more by pressing . - System.out.println("i = " + i); - } - } -} \ No newline at end of file