diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..7bc07ec --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,10 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Environment-dependent path to Maven home directory +/mavenHomeManager.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/lab-java-standard-input-and-classes.iml b/.idea/lab-java-standard-input-and-classes.iml new file mode 100644 index 0000000..b107a2d --- /dev/null +++ b/.idea/lab-java-standard-input-and-classes.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..6f29fee --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..398d0f7 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ 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/Lab/Employee.java b/Lab/Employee.java new file mode 100644 index 0000000..8422697 --- /dev/null +++ b/Lab/Employee.java @@ -0,0 +1,58 @@ +package Lab; + +public class Employee { + private String name; + private String email; + private int age; + private double salary; + + + public Employee(String name, String email, int age, double salary) { + this.name = name; + this.email = email; + this.age = age; + this.salary = salary; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public double getSalary() { + return salary; + } + + public void setSalary(double salary) { + this.salary = salary; + } + + @Override + public String toString() { + return "Employee{" + + "name='" + name + '\'' + + ", email='" + email + '\'' + + ", age=" + age + + ", salary=" + salary + + '}'; + } +} diff --git a/Lab/Intern.java b/Lab/Intern.java new file mode 100644 index 0000000..7070b39 --- /dev/null +++ b/Lab/Intern.java @@ -0,0 +1,23 @@ +package Lab; + +public class Intern extends Employee{ + public static final double SALARY_LIMIT=20000.0; + + public Intern(String name, String email, int age, double salary) { + super(name, email, age, validateSalary(salary)); + } + + private static double validateSalary(double salary) { + if (salary > SALARY_LIMIT) { + throw new IllegalArgumentException("El salario del becario no puede superar los " + SALARY_LIMIT); + } + return salary; + } + + @Override + public void setSalary(double salary) { + if (salary > SALARY_LIMIT) { + throw new IllegalArgumentException("El alario del becario no puede superar los "+SALARY_LIMIT); + } + } +} diff --git a/Lab/Main.java b/Lab/Main.java new file mode 100644 index 0000000..f60f557 --- /dev/null +++ b/Lab/Main.java @@ -0,0 +1,37 @@ +package Lab; + +import javax.sound.midi.Soundbank; +import java.io.BufferedWriter; +import java.io.FileWriter; +import java.util.ArrayList; +import java.util.List; + +public class Main { + public static void main(String[] args) { + List employees = new ArrayList<>(); + + employees.add(new Employee("Luis Pérez", "luis@example.com", 30, 35000)); + employees.add(new Employee("Ana Gómez", "ana@example.com", 28, 40000)); + employees.add(new Intern("Carlos Ruiz", "carlos@example.com", 21, 18000)); + employees.add(new Employee("Sofía López", "sofia@example.com", 35, 50000)); + employees.add(new Intern("Lucía Díaz", "lucia@example.com", 22, 20000)); + employees.add(new Employee("Miguel Torres", "miguel@example.com", 40, 45000)); + employees.add(new Employee("Paula Moreno", "paula@example.com", 33, 38000)); + employees.add(new Intern("Jorge Ramírez", "jorge@example.com", 23, 19500)); + employees.add(new Employee("Elena Sánchez", "elena@example.com", 29, 41000)); + employees.add(new Intern("David Castro", "david@example.com", 24, 19000)); + + try(BufferedWriter writer=new BufferedWriter(new FileWriter("employee.txt"))) { + for(Employee e:employees){ + writer.write(e.toString()); + writer.newLine(); + } + System.out.println("archivo Employee creado Correctamente"); + }catch (Exception e){ + System.err.println("Error al escribir el archivo "+e.getMessage()); + } + + + + } +} diff --git a/employee.txt b/employee.txt new file mode 100644 index 0000000..19f0991 --- /dev/null +++ b/employee.txt @@ -0,0 +1,10 @@ +Employee{name='Luis Pérez', email='luis@example.com', age=30, salary=35000.0} +Employee{name='Ana Gómez', email='ana@example.com', age=28, salary=40000.0} +Employee{name='Carlos Ruiz', email='carlos@example.com', age=21, salary=18000.0} +Employee{name='Sofía López', email='sofia@example.com', age=35, salary=50000.0} +Employee{name='Lucía Díaz', email='lucia@example.com', age=22, salary=20000.0} +Employee{name='Miguel Torres', email='miguel@example.com', age=40, salary=45000.0} +Employee{name='Paula Moreno', email='paula@example.com', age=33, salary=38000.0} +Employee{name='Jorge Ramírez', email='jorge@example.com', age=23, salary=19500.0} +Employee{name='Elena Sánchez', email='elena@example.com', age=29, salary=41000.0} +Employee{name='David Castro', email='david@example.com', age=24, salary=19000.0} diff --git a/out/production/lab-java-standard-input-and-classes/.idea/.gitignore b/out/production/lab-java-standard-input-and-classes/.idea/.gitignore new file mode 100644 index 0000000..7bc07ec --- /dev/null +++ b/out/production/lab-java-standard-input-and-classes/.idea/.gitignore @@ -0,0 +1,10 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Environment-dependent path to Maven home directory +/mavenHomeManager.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/out/production/lab-java-standard-input-and-classes/.idea/lab-java-standard-input-and-classes.iml b/out/production/lab-java-standard-input-and-classes/.idea/lab-java-standard-input-and-classes.iml new file mode 100644 index 0000000..b107a2d --- /dev/null +++ b/out/production/lab-java-standard-input-and-classes/.idea/lab-java-standard-input-and-classes.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/out/production/lab-java-standard-input-and-classes/.idea/misc.xml b/out/production/lab-java-standard-input-and-classes/.idea/misc.xml new file mode 100644 index 0000000..6f29fee --- /dev/null +++ b/out/production/lab-java-standard-input-and-classes/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/out/production/lab-java-standard-input-and-classes/.idea/modules.xml b/out/production/lab-java-standard-input-and-classes/.idea/modules.xml new file mode 100644 index 0000000..398d0f7 --- /dev/null +++ b/out/production/lab-java-standard-input-and-classes/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/out/production/lab-java-standard-input-and-classes/.idea/vcs.xml b/out/production/lab-java-standard-input-and-classes/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/out/production/lab-java-standard-input-and-classes/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/out/production/lab-java-standard-input-and-classes/Lab/Employee.class b/out/production/lab-java-standard-input-and-classes/Lab/Employee.class new file mode 100644 index 0000000..0f992be Binary files /dev/null and b/out/production/lab-java-standard-input-and-classes/Lab/Employee.class differ diff --git a/out/production/lab-java-standard-input-and-classes/Lab/Intern.class b/out/production/lab-java-standard-input-and-classes/Lab/Intern.class new file mode 100644 index 0000000..c446b49 Binary files /dev/null and b/out/production/lab-java-standard-input-and-classes/Lab/Intern.class differ diff --git a/out/production/lab-java-standard-input-and-classes/Lab/Main.class b/out/production/lab-java-standard-input-and-classes/Lab/Main.class new file mode 100644 index 0000000..62cf28e Binary files /dev/null and b/out/production/lab-java-standard-input-and-classes/Lab/Main.class differ diff --git a/out/production/lab-java-standard-input-and-classes/README.md b/out/production/lab-java-standard-input-and-classes/README.md new file mode 100644 index 0000000..b6d794a --- /dev/null +++ b/out/production/lab-java-standard-input-and-classes/README.md @@ -0,0 +1,185 @@ +![logo_ironhack_blue 7](https://user-images.githubusercontent.com/23629340/40541063-a07a0a8a-601a-11e8-91b5-2f13e4e6b441.png) + +# LAB Java | Standard Input and Classes + +## Introduction + +We have just learned how to use input/output in the console and files, so let's practice a bit more. + +
+ +## Requirements + +1. Fork this repo. +2. Clone this repo. +3. Add your instructor and the class graders as collaborators to your repository. If you are unsure who your class graders are, ask your instructor or refer to the day 1 slide deck. +4. In the repository, create a Java project and add the code for the following prompts. + +## Submission + +Once you finish the assignment, submit a URL link to your repository or your pull request in the field below. + +
+ +## Instructions + +### Employee and Intern Classes + +1. Create an `Employee` class to represent an employee of a company. The class should have `name`, `email`, `age`, `salary` properties and appropriate getters and setters. +2. Create an `Intern` class that extends from `Employee`. The `Intern` class should have a salary limit of 20000 (constant). +3. Implement validation in the `Intern` class to ensure that an intern is not created (or salary updated) with a salary that exceeds the maximum allowed value. +4. Write a program that creates 10 `Employee` objects and prints all of their properties (name, email, age, salary) to a file named `employees.txt`. + +
+ +### Tips + +- Be sure to consider the relationship between the `Employee` and `Intern` classes when designing your solution. +- Think about the different properties and behaviors that are applicable to both `Employee` and `Intern` objects and consider how you can use inheritance to avoid duplication of code. +- Use appropriate access modifiers (e.g. `private`, `protected`, `public`) to control the visibility of your class properties and methods. +- Make sure to handle edge cases and handle them appropriately (e.g. what happens if an `Intern` object is created with a salary that exceeds the maximum allowed value?). + +
+ +## FAQs + +
+ +
+ I am stuck and don't know how to solve the problem or where to start. What should I do? + +
+ + If you are stuck in your code and don't know how to solve the problem or where to start, you should take a step back and try to form a clear, straight forward question about the specific issue you are facing. The process you will go through while trying to define this question, will help you narrow down the problem and come up with potential solutions. + + For example, are you facing a problem because you don't understand the concept or are you receiving an error message that you don't know how to fix? It is usually helpful to try to state the problem as clearly as possible, including any error messages you are receiving. This can help you communicate the issue to others and potentially get help from classmates or online resources. + + Once you have a clear understanding of the problem, you should be able to start working toward the solution. + +
+ +
+ +
+ How do I create a Maven project in IntelliJ? + +
+ + To create a Maven project in IntelliJ, you can follow these steps: + + 1. Open IntelliJ IDEA and click the "Create New Project" button. + 2. In the "New Project" dialog, select "Maven" as the build system. + 3. Specify the name of the project. + 4. In the "Project Location" section, specify a location where you want to save your project. + 5. Select the "Create Git repository" checkbox in order to initialize the git repository upon creation of the project. + 6. Click the "Create" button to create the Maven project. + + +
+ +
+ +
+ What is inheritance in Java and how do I extend a class to another class? + +
+ + Inheritance in Java is a mechanism that allows you to create a new class (the child class) based on an existing class (the parent class). The child class inherits the properties and methods of the parent class, which means that you can reuse the code and add new functionality to the child class. + + To extend a class to another class, you use the `extends` keyword in the child class declaration. The parent class is specified after the `extends` keyword. For example: + + ```java + class ParentClass { + int x; + + void display() { + System.out.println("This is the ParentClass"); + } + } + + class ChildClass extends ParentClass { + int y; + + void display() { + System.out.println("This is the ChildClass"); + } + } + ``` + + In this example, `ChildClass` extends `ParentClass`, so it inherits the `x` variable and the `display()` method. You can also add new properties and methods to the child class, such as the y variable and the `display()` method override in the child class. + + It's important to note that the child class can access all the properties and methods of the parent class that have public or protected visibility. Properties and methods with private visibility are not inherited by the child class. + +
+ +
+ +
+ What is the difference between using "PrintWriter" and "FileWriter" in Java and how can they be used together? + +
+ + `PrintWriter` and `FileWriter` are two classes in Java that are used to write data to a file. However, they serve different purposes and have different features. + + `FileWriter` is a basic class in Java that provides simple file output functions. You can use it to write text to a file, but it does not provide any formatting options, such as controlling the number of characters per line or specifying the line separator. + + `PrintWriter`, on the other hand, provides advanced printing functions, including formatting options and automatic line flushing. It also provides a more user-friendly API for writing to a file compared to `FileWriter`. + + Here's how you can use `FileWriter` and `PrintWriter` together in a program: + + ```java + import java.io.*; + + public class WriteToFile { + public static void main(String[] args) { + try { + FileWriter fw = new FileWriter("output.txt"); + PrintWriter pw = new PrintWriter(fw); + pw.println("This is a line written using PrintWriter"); + pw.println("Another line written using PrintWriter"); + pw.close(); + fw.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + ``` + + In this example, `FileWriter` is used to open a file named `output.txt` and `PrintWriter` is used to write lines of text to the file. The `println` method in `PrintWriter` automatically adds a line separator after each line of text, making it easier to write formatted text to a file. + + It's important to note that you need to close both `FileWriter` and `PrintWriter` when you are done writing to the file, as shown in the code above. + +
+ +
+ +
+ I am unable to push changes to my repository. What should I do? + +
+ + If you are unable to push changes to your repository, here are a few steps that you can follow: + + 1. Check your internet connection: Ensure that your internet connection is stable and working. + 1. Verify your repository URL: Make sure that you are using the correct repository URL to push your changes. + 2. Check Git credentials: Ensure that your Git credentials are up-to-date and correct. You can check your credentials using the following command: + + ```bash + git config --list + ``` + + 4. Update your local repository: Before pushing changes, make sure that your local repository is up-to-date with the remote repository. You can update your local repository using the following command: + + ```bash + git fetch origin + ``` + + 5. Check for conflicts: If there are any conflicts between your local repository and the remote repository, resolve them before pushing changes. + 6. Push changes: Once you have resolved any conflicts and updated your local repository, you can try pushing changes again using the following command: + + ```bash + git push origin + ``` + +