Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Week 4 Lab #35

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions .idea/.gitignore

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

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

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

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

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

23 changes: 23 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.ironhack</groupId>
<artifactId>untitled2</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>22</maven.compiler.source>
<maven.compiler.target>22</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
</dependencies>
</project>
17 changes: 17 additions & 0 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.ironhack;

//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {
public static void main(String[] args) {
//TIP Press <shortcut actionId="ShowIntentionActions"/> 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 <shortcut actionId="Debug"/> to start debugging your code. We have set one <icon src="AllIcons.Debugger.Db_set_breakpoint"/> breakpoint
// for you, but you can always add more by pressing <shortcut actionId="ToggleLineBreakpoint"/>.
System.out.println("i = " + i);
}
}
}
97 changes: 97 additions & 0 deletions src/main/java/com/Person.java
Original file line number Diff line number Diff line change
@@ -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);
}


}
55 changes: 55 additions & 0 deletions src/main/java/com/PersonList.java
Original file line number Diff line number Diff line change
@@ -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<Person> 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();
}

}



}
Empty file.
19 changes: 19 additions & 0 deletions src/test/java/com/ironhack/PersonTest.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
}