SlideShare a Scribd company logo
1 of 6
Programação OrientadaaObjetos- JAVA
Enumeraçõese Composições
Exercício01
Ler os dados de um trabalhador com N contratos (N fornecido pelo usuário). Depois, solicitar
do usuário um mês e mostrar qual foi o salário do funcionário nesse mês, conforme exemplo
a seguir.
Enter department's name: Design
Enter worker data:
Name: Alex
Level: MID_LEVEL
Base salary: 1200.00
How many contracts to this worker? 3
Enter contract #1 data:
Date (DD/MM/YYYY): 20/08/2018
Value per hour: 50.00
Duration (hours): 20
Enter contract #2 data:
Date (DD/MM/YYYY): 13/06/2018
Value per hour: 30.00
Duration (hours): 18
Enter contract #3 data:
Date (DD/MM/YYYY): 25/08/2018
Value per hour: 80.00
Duration (hours): 10
Enter month and year to calculate income (MM/YYYY): 08/2018
Name: Alex
Department: Design
Income for 08/2018: 3000.00
Códigos do programa principal e das classes entites:
Program:
package apllication;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Scanner;
import entities.Department;
import entities.HourContract;
import entities.Worker;
import entities.enums.WorkLevel;
public class Program0 {
public static void main(String[] args) throws ParseException {
Locale.setDefault(Locale.US);
Scanner sc = new Scanner(System.in);
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
System.out.print("Enter department's name: ");
String departmentName = sc.nextLine();
System.out.println("Enter worker data:");
System.out.print("Name: ");
String workName = sc.nextLine();
System.out.print("Level: ");
String worklLevel = sc.nextLine();
System.out.print("Base salary: ");
double baseSalary = sc.nextDouble();
Worker worker = new Worker(workName, WorkLevel.valueOf(worklLevel),
baseSalary, new Department(departmentName));
System.out.print("How many contracts to this worker? ");
int n = sc.nextInt();
for(int i = 0; i < n; i++) {
System.out.println("Enter contract #" + (i + 1) + " dara:");
System.out.print("Date (DD/MM/YYYY): ");
Date contractDate = sdf.parse(sc.next());
System.out.print("Value per hour: ");
double valuePerHour = sc .nextDouble();
System.out.print("Duration (hours): ");
int hours = sc.nextInt();
HourContract contract = new HourContract(contractDate, valuePerHour,
hours);
worker.addContract(contract);
}
System.out.println();
System.out.print("Enter month and year to calculate income (MM/YYYY): ");
String monthAndYear = sc.next();
int month = Integer.parseInt(monthAndYear.substring(0, 2));
int year = Integer.parseInt(monthAndYear.substring(3));
System.out.println("Name: " + worker.getName());
System.out.println("Department: " + worker.getDepartment().getName());
System.out.println("Income for " + monthAndYear + ": " +
String.format("%.2f", worker.income(year, month)));
sc.close();
}
}
Department:
package entities;
public class Department {
private String name;
public Department() {
}
public Department(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
HourContract:
package entities;
import java.util.Date;
public class HourContract {
private Date date;
private Double valuePerHour;
private Integer hours;
public HourContract() {
}
public HourContract(Date date, Double valuePerHour, Integer hours) {
this.date = date;
this.valuePerHour = valuePerHour;
this.hours = hours;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public Double getValuePerHour() {
return valuePerHour;
}
public void setValuePerHour(Double valuePerHour) {
this.valuePerHour = valuePerHour;
}
public Integer getHours() {
return hours;
}
public void setHours(Integer hours) {
this.hours = hours;
}
public double totalValue() {
return valuePerHour * hours;
}
}
WorkLevel
package entities.enums;
public enum WorkLevel {
JUNIOR,
MID_LEVEL,
SENIOR;
}
Worker:
package entities;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import entities.enums.WorkLevel;
public class Worker {
private String name;
private WorkLevel level;
private Double baseSalary;
private Department department;
private List<HourContract> contracts = new ArrayList<>();
public Worker() {
}
public Worker(String name, WorkLevel level, Double baseSalary, Department
department) {
this.name = name;
this.level = level;
this.baseSalary = baseSalary;
this.department = department;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public WorkLevel getLevel() {
return level;
}
public void setLevel(WorkLevel level) {
this.level = level;
}
public Double getBaseSalary() {
return baseSalary;
}
public void setBaseSalary(Double baseSalary) {
this.baseSalary = baseSalary;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public List<HourContract> getContracts() {
return contracts;
}
public void addContract(HourContract contract) {
contracts.add(contract);
}
public void removeContract(HourContract contract) {
contracts.remove(contract);
}
public double income(int year, int month) {
double sum = baseSalary;
Calendar cal = Calendar.getInstance();
for(HourContract c: contracts) {
cal.setTime(c.getDate());
int c_year = cal.get(Calendar.YEAR);
int c_month = 1 + cal.get(Calendar.MONTH);
if(year == c_year && month == c_month) {
sum += c.totalValue();
}
}
return sum;
}
}

More Related Content

Similar to enum_comp_exercicio01.docx

Modify the Time classattached to be able to work with Date.pdf
Modify the Time classattached to be able to work with Date.pdfModify the Time classattached to be able to work with Date.pdf
Modify the Time classattached to be able to work with Date.pdf
aaseletronics2013
 
Working effectively with legacy code
Working effectively with legacy codeWorking effectively with legacy code
Working effectively with legacy code
ShriKant Vashishtha
 
Implementation Your program shall contain at least the follo.pdf
Implementation Your program shall contain at least the follo.pdfImplementation Your program shall contain at least the follo.pdf
Implementation Your program shall contain at least the follo.pdf
ADITIEYEWEAR
 
Visual studio 2008
Visual studio 2008Visual studio 2008
Visual studio 2008
Luis Enrique
 
INTERMIDIATE PROGRAMMINGCMPSC 122LAB 9 INHERITANCE AND POLYMO.docx
INTERMIDIATE PROGRAMMINGCMPSC 122LAB 9 INHERITANCE AND POLYMO.docxINTERMIDIATE PROGRAMMINGCMPSC 122LAB 9 INHERITANCE AND POLYMO.docx
INTERMIDIATE PROGRAMMINGCMPSC 122LAB 9 INHERITANCE AND POLYMO.docx
normanibarber20063
 

Similar to enum_comp_exercicio01.docx (20)

Synapse india dotnet development overloading operater part 4
Synapse india dotnet development overloading operater part 4Synapse india dotnet development overloading operater part 4
Synapse india dotnet development overloading operater part 4
 
Java Day-7
Java Day-7Java Day-7
Java Day-7
 
Java22_1670144363.pptx
Java22_1670144363.pptxJava22_1670144363.pptx
Java22_1670144363.pptx
 
Modify the Time classattached to be able to work with Date.pdf
Modify the Time classattached to be able to work with Date.pdfModify the Time classattached to be able to work with Date.pdf
Modify the Time classattached to be able to work with Date.pdf
 
Working effectively with legacy code
Working effectively with legacy codeWorking effectively with legacy code
Working effectively with legacy code
 
Data and time
Data and timeData and time
Data and time
 
Python datetime
Python datetimePython datetime
Python datetime
 
OOP program questions with answers
OOP program questions with answersOOP program questions with answers
OOP program questions with answers
 
Implementation Your program shall contain at least the follo.pdf
Implementation Your program shall contain at least the follo.pdfImplementation Your program shall contain at least the follo.pdf
Implementation Your program shall contain at least the follo.pdf
 
Capstone ms2
Capstone ms2Capstone ms2
Capstone ms2
 
11. Objects and Classes
11. Objects and Classes11. Objects and Classes
11. Objects and Classes
 
OOPS 22-23 (1).pptx
OOPS 22-23 (1).pptxOOPS 22-23 (1).pptx
OOPS 22-23 (1).pptx
 
Visual studio 2008
Visual studio 2008Visual studio 2008
Visual studio 2008
 
Creating a Facebook Clone - Part XXIX - Transcript.pdf
Creating a Facebook Clone - Part XXIX - Transcript.pdfCreating a Facebook Clone - Part XXIX - Transcript.pdf
Creating a Facebook Clone - Part XXIX - Transcript.pdf
 
Csphtp1 08
Csphtp1 08Csphtp1 08
Csphtp1 08
 
INTERMIDIATE PROGRAMMINGCMPSC 122LAB 9 INHERITANCE AND POLYMO.docx
INTERMIDIATE PROGRAMMINGCMPSC 122LAB 9 INHERITANCE AND POLYMO.docxINTERMIDIATE PROGRAMMINGCMPSC 122LAB 9 INHERITANCE AND POLYMO.docx
INTERMIDIATE PROGRAMMINGCMPSC 122LAB 9 INHERITANCE AND POLYMO.docx
 
Vedika C project.docx
Vedika C project.docxVedika C project.docx
Vedika C project.docx
 
Standardizing JavaScript Decorators in TC39 (Full Stack Fest 2019)
Standardizing JavaScript Decorators in TC39 (Full Stack Fest 2019)Standardizing JavaScript Decorators in TC39 (Full Stack Fest 2019)
Standardizing JavaScript Decorators in TC39 (Full Stack Fest 2019)
 
Xi practical file
Xi practical fileXi practical file
Xi practical file
 
Паразитируем на React-экосистеме (Angular 4+) / Алексей Охрименко (IPONWEB)
Паразитируем на React-экосистеме (Angular 4+) / Алексей Охрименко (IPONWEB)Паразитируем на React-экосистеме (Angular 4+) / Алексей Охрименко (IPONWEB)
Паразитируем на React-экосистеме (Angular 4+) / Алексей Охрименко (IPONWEB)
 

Recently uploaded

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Krashi Coaching
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Recently uploaded (20)

Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 

enum_comp_exercicio01.docx

  • 1. Programação OrientadaaObjetos- JAVA Enumeraçõese Composições Exercício01 Ler os dados de um trabalhador com N contratos (N fornecido pelo usuário). Depois, solicitar do usuário um mês e mostrar qual foi o salário do funcionário nesse mês, conforme exemplo a seguir.
  • 2. Enter department's name: Design Enter worker data: Name: Alex Level: MID_LEVEL Base salary: 1200.00 How many contracts to this worker? 3 Enter contract #1 data: Date (DD/MM/YYYY): 20/08/2018 Value per hour: 50.00 Duration (hours): 20 Enter contract #2 data: Date (DD/MM/YYYY): 13/06/2018 Value per hour: 30.00 Duration (hours): 18 Enter contract #3 data: Date (DD/MM/YYYY): 25/08/2018 Value per hour: 80.00 Duration (hours): 10 Enter month and year to calculate income (MM/YYYY): 08/2018 Name: Alex Department: Design Income for 08/2018: 3000.00 Códigos do programa principal e das classes entites: Program: package apllication; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import java.util.Scanner; import entities.Department; import entities.HourContract; import entities.Worker; import entities.enums.WorkLevel; public class Program0 { public static void main(String[] args) throws ParseException { Locale.setDefault(Locale.US); Scanner sc = new Scanner(System.in); SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); System.out.print("Enter department's name: "); String departmentName = sc.nextLine(); System.out.println("Enter worker data:"); System.out.print("Name: "); String workName = sc.nextLine(); System.out.print("Level: "); String worklLevel = sc.nextLine(); System.out.print("Base salary: "); double baseSalary = sc.nextDouble();
  • 3. Worker worker = new Worker(workName, WorkLevel.valueOf(worklLevel), baseSalary, new Department(departmentName)); System.out.print("How many contracts to this worker? "); int n = sc.nextInt(); for(int i = 0; i < n; i++) { System.out.println("Enter contract #" + (i + 1) + " dara:"); System.out.print("Date (DD/MM/YYYY): "); Date contractDate = sdf.parse(sc.next()); System.out.print("Value per hour: "); double valuePerHour = sc .nextDouble(); System.out.print("Duration (hours): "); int hours = sc.nextInt(); HourContract contract = new HourContract(contractDate, valuePerHour, hours); worker.addContract(contract); } System.out.println(); System.out.print("Enter month and year to calculate income (MM/YYYY): "); String monthAndYear = sc.next(); int month = Integer.parseInt(monthAndYear.substring(0, 2)); int year = Integer.parseInt(monthAndYear.substring(3)); System.out.println("Name: " + worker.getName()); System.out.println("Department: " + worker.getDepartment().getName()); System.out.println("Income for " + monthAndYear + ": " + String.format("%.2f", worker.income(year, month))); sc.close(); } } Department: package entities; public class Department { private String name; public Department() { } public Department(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } } HourContract:
  • 4. package entities; import java.util.Date; public class HourContract { private Date date; private Double valuePerHour; private Integer hours; public HourContract() { } public HourContract(Date date, Double valuePerHour, Integer hours) { this.date = date; this.valuePerHour = valuePerHour; this.hours = hours; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public Double getValuePerHour() { return valuePerHour; } public void setValuePerHour(Double valuePerHour) { this.valuePerHour = valuePerHour; } public Integer getHours() { return hours; } public void setHours(Integer hours) { this.hours = hours; } public double totalValue() { return valuePerHour * hours; } } WorkLevel package entities.enums; public enum WorkLevel { JUNIOR, MID_LEVEL, SENIOR; }
  • 5. Worker: package entities; import java.util.ArrayList; import java.util.Calendar; import java.util.List; import entities.enums.WorkLevel; public class Worker { private String name; private WorkLevel level; private Double baseSalary; private Department department; private List<HourContract> contracts = new ArrayList<>(); public Worker() { } public Worker(String name, WorkLevel level, Double baseSalary, Department department) { this.name = name; this.level = level; this.baseSalary = baseSalary; this.department = department; } public String getName() { return name; } public void setName(String name) { this.name = name; } public WorkLevel getLevel() { return level; } public void setLevel(WorkLevel level) { this.level = level; } public Double getBaseSalary() { return baseSalary; } public void setBaseSalary(Double baseSalary) { this.baseSalary = baseSalary; } public Department getDepartment() { return department; } public void setDepartment(Department department) { this.department = department; } public List<HourContract> getContracts() {
  • 6. return contracts; } public void addContract(HourContract contract) { contracts.add(contract); } public void removeContract(HourContract contract) { contracts.remove(contract); } public double income(int year, int month) { double sum = baseSalary; Calendar cal = Calendar.getInstance(); for(HourContract c: contracts) { cal.setTime(c.getDate()); int c_year = cal.get(Calendar.YEAR); int c_month = 1 + cal.get(Calendar.MONTH); if(year == c_year && month == c_month) { sum += c.totalValue(); } } return sum; } }