import da michele

This commit is contained in:
orange 2016-12-14 12:47:52 +01:00
parent 117eef00bc
commit 28900650a6
11 changed files with 540 additions and 0 deletions

View File

@ -0,0 +1,8 @@
package com.gmail.zurlo.michelef.util;
import java.text.SimpleDateFormat;
public class Constants {
public static final String DATE_FORMAT = "dd-MM-yyyy";
public static final SimpleDateFormat S_DATE_FORMAT = new SimpleDateFormat(DATE_FORMAT);
}

View File

@ -0,0 +1,12 @@
package com.gmail.zurlo.michelef.util;
public class IllegalDateException extends RuntimeException {
public IllegalDateException() {
}
public IllegalDateException(String msg) {
super(msg);
}
}

View File

@ -0,0 +1,12 @@
package com.gmail.zurlo.michelef.util;
public class IllegalPercentageException extends RuntimeException {
public IllegalPercentageException() {
}
public IllegalPercentageException(String msg) {
super(msg);
}
}

View File

@ -0,0 +1,12 @@
package com.gmail.zurlo.michelef.util;
public class IncorrectLabelException extends RuntimeException {
public IncorrectLabelException() {
}
public IncorrectLabelException(String msg) {
super(msg);
}
}

View File

@ -0,0 +1,77 @@
package com.gmail.zurlo.michelef.classi;
import java.io.PrintStream;
import java.util.Date;
import java.util.Scanner;
import com.gmail.zurlo.michelef.util.IllegalPercentageException;
public class IndustrialProject extends Project {
public IndustrialProject(String name, String description, double budget, Date start, Date end, String company,
double fundedPercentage) {
super(name, description, budget, start, end);
this.company = company;
this.fundedPercentage = fundedPercentage;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public double getFundedPercentage() {
return fundedPercentage;
}
public void setFundedPercentage(double fundedPercentage) {
this.fundedPercentage = fundedPercentage;
}
public static IndustrialProject read(Scanner sc) {
String company;
double fundingPercentage;
Project p = Project.read(sc);
if (!sc.hasNextLine()) {
return null;
}
company = sc.nextLine();
if (!sc.hasNextLine()) {
return null;
}
try {
fundingPercentage = Double.parseDouble(sc.nextLine());
if (fundingPercentage < 0 || fundingPercentage > 100) {
throw new IllegalPercentageException("Percentage value is out of bounds!");
}
} catch (NumberFormatException e) {
System.err.println("An exception occurred while parsing funding percentage, project " + p.getName());
System.err.println("Funding percentage will be assigned default value..");
fundingPercentage = 0;
} catch (IllegalPercentageException e) {
System.err.println("An exception occurred while parsing funding percentage, project " + p.getName());
System.err.println(e.getMessage());
System.err.println("Funding percentage will be assigned default value..");
fundingPercentage = 0;
}
return new IndustrialProject(p.getName(), p.getDescription(), p.getBudget(), p.getStart(), p.getEnd(), company,
fundingPercentage);
}
public void print(PrintStream ps) {
super.print(ps);
ps.println(company);
ps.println(fundedPercentage);
}
private String company;
private double fundedPercentage;
}

View File

@ -0,0 +1,134 @@
package com.gmail.zurlo.michelef.classi;
import java.io.PrintStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import com.gmail.zurlo.michelef.util.Constants;
import com.gmail.zurlo.michelef.util.IllegalDateException;
public class Project {
public Project(String name, String description, double budget, Date start, Date end) {
this.name = name;
this.description = description;
this.budget = budget;
this.start = start;
this.end = end;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public double getBudget() {
return budget;
}
public void setBudget(double budget) {
this.budget = budget;
}
public Date getStart() {
return start;
}
public void setStart(Date start) {
this.start = start;
}
public Date getEnd() {
return end;
}
public void setEnd(Date end) {
this.end = end;
}
public static Project read(Scanner sc) {
String name, description;
double budget;
Date start;
Date end;
SimpleDateFormat df = Constants.S_DATE_FORMAT;
if (!sc.hasNextLine()) {
return null;
}
name = sc.nextLine();
if (!sc.hasNextLine()) {
return null;
}
description = sc.nextLine();
if (!sc.hasNextLine()) {
return null;
}
try {
budget = Double.parseDouble(sc.nextLine());
} catch (NumberFormatException e) {
System.err.println("An exception occurred while parsing budget, project " + name);
System.err.println("Budget will be assigned default value...");
budget = 0;
}
try {
start = df.parse(sc.nextLine());
} catch (ParseException e) {
System.err.println("An exception occurred while parsing start date, project " + name);
System.err.println("Start date will be assigned default system date...");
start = new Date();
}
try {
end = df.parse(sc.nextLine());
if (end.before(start)) {
throw new IllegalDateException("End date is before start date!");
}
} catch (ParseException e) {
System.err.println("An exception occurred while parsing end date, project " + name);
System.err.println("End date will be assigned default system date...");
end = new Date();
} catch (IllegalDateException e) {
System.err.println("An exception occurred while parsing end date, project " + name);
System.err.println(e.getMessage());
System.err.println("End date will be assigned default system date...");
end = new Date();
}
return new Project(name, description, budget, start, end);
}
public void print(PrintStream ps) {
SimpleDateFormat df = Constants.S_DATE_FORMAT;
ps.println(name);
ps.println(description);
ps.println(budget);
ps.println(df.format(start));
ps.println(df.format(end));
}
private String name;
private String description;
private double budget;
private Date start;
private Date end;
}

View File

@ -0,0 +1,52 @@
package com.gmail.zurlo.michelef.scenari;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import com.gmail.zurlo.michelef.classi.IndustrialProject;
import com.gmail.zurlo.michelef.classi.Project;
import com.gmail.zurlo.michelef.classi.ResearchProject;
import com.gmail.zurlo.michelef.util.IncorrectLabelException;
public class ProjectTester {
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(new File("Projects.txt"));
Project project = null;
String type = sc.nextLine();
//type = sc.nextLine();
while (type != null){
try {
if(!type.equals("Ricerca") && !type.equals("Industria")) {
throw new IncorrectLabelException("Label is not \"Industria\" or \"Ricerca\"!");
}
if(type.equals("Ricerca")) {
project = ResearchProject.read(sc);
} else if(type.equals("Industria")) {
project = IndustrialProject.read(sc);
}
if(project != null) {
project.print(System.out);
}
}
catch(IncorrectLabelException e) {
System.err.println("An exception occurred while reading project label \"" + type + "\"");
System.err.println(e.getMessage());
System.err.println("All next line different from Industria or Ricerca will be skipped...");
while(!type.equals("Ricerca") && !type.equals("Industria") && sc.hasNextLine()) {
type = sc.nextLine();
}
if(!sc.hasNextLine()) {
type = null;
}
}
}
}
}

View File

@ -0,0 +1,24 @@
Ricerca
Eureka
Definizione metodi di analisi
3000000
10-04-2004
10-04-2007
Comunità europea
IV programma quadro
Industria
L2eL
Evoluzione di processi di business
1000000
11-11-2002
10-11-2004
EDS
20
Ricerca
CdC
Creazione di uninfrastruttura di ricerca
5000000
01-03-2003
28-02-2006
Regione Campania
Centri di Competenza

View File

@ -0,0 +1,127 @@
package com.gmail.zurlo.michelef.classi;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;
import com.gmail.zurlo.michelef.util.IncorrectLabelException;
public class ResearchCenter {
public ResearchCenter(Scanner sc) {
projects = new ArrayList<Project>();
Project p = readProject(sc);
while (p != null) {
projects.add(p);
p = readProject(sc);
}
}
private ResearchCenter(ArrayList<Project> projects) {
this.projects = projects;
}
private Project readProject(Scanner sc) {
Project project = null;
if (!sc.hasNextLine()) {
return null;
}
String type = sc.nextLine();
while (type != null) {
try {
if (!type.equals("Ricerca") && !type.equals("Industria")) {
throw new IncorrectLabelException("Label is not \"Industria\" or \"Ricerca\"!");
}
if (type.equals("Ricerca")) {
project = ResearchProject.read(sc);
} else if (type.equals("Industria")) {
project = IndustrialProject.read(sc);
}
return project;
} catch (IncorrectLabelException e) {
System.err.println("An exception occurred while reading project label \"" + type + "\"");
System.err.println(e.getMessage());
System.err.println("All next line different from Industria or Ricerca will be skipped...");
while (!type.equals("Ricerca") && !type.equals("Industria") && sc.hasNextLine()) {
type = sc.nextLine();
}
if (!sc.hasNextLine()) {
type = null;
}
}
}
return null;
}
public ResearchCenter filterByPreviousDate(Date date) {
ArrayList<Project> projectsFilter = new ArrayList<Project>();
for (Project p : projects) {
if (p.getEnd().before(date)) {
projectsFilter.add(p);
}
}
return new ResearchCenter(projectsFilter);
}
public ResearchCenter filterByNextDate(Date date) {
ArrayList<Project> projectsFilter = new ArrayList<Project>();
for (Project p : projects) {
if (p.getStart().after(date)) {
projectsFilter.add(p);
}
}
return new ResearchCenter(projectsFilter);
}
public void sortByName() {
int i, j = 0;
boolean done = false;
while (!done && j < projects.size() - 1) {
done = true;
for (i = 0; i < projects.size() - j - 1; i++) {
if (projects.get(i).getName().compareTo(projects.get(i + 1).getName()) > 0) {
swap(i, i + 1);
done = false;
}
}
j++;
}
}
private void swap(int i, int j) {
Project temp = projects.get(i);
projects.set(i, projects.get(j));
projects.set(j, temp);
}
public void print(PrintStream ps) {
for (Project p : projects) {
if (p instanceof ResearchProject) {
ps.println("Ricerca");
} else {
ps.println("Industria");
}
p.print(ps);
}
}
private ArrayList<Project> projects;
}

View File

@ -0,0 +1,22 @@
package com.gmail.zurlo.michelef.scenari;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import com.gmail.zurlo.michelef.classi.ResearchCenter;
public class ResearchCenterTester {
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(new File("Projects.txt"));
ResearchCenter rc = new ResearchCenter(sc);
rc.print(System.out);
System.out.println("*****");
rc.sortByName();
rc.print(System.out);
}
}

View File

@ -0,0 +1,60 @@
package com.gmail.zurlo.michelef.classi;
import java.io.PrintStream;
import java.util.Date;
import java.util.Scanner;
public class ResearchProject extends Project {
public ResearchProject(String name, String description, double budget, Date start, Date end, String fundingBody,
String fundingLaw) {
super(name, description, budget, start, end);
this.fundingBody = fundingBody;
this.fundingLaw = fundingLaw;
}
public String getFundingBody() {
return fundingBody;
}
public void setFundingBody(String fundingBody) {
this.fundingBody = fundingBody;
}
public String getFundingLaw() {
return fundingLaw;
}
public void setFundingLaw(String fundingLaw) {
this.fundingLaw = fundingLaw;
}
public static ResearchProject read(Scanner sc) {
String fundingBody, fundingLaw;
Project p = Project.read(sc);
if (!sc.hasNextLine()) {
return null;
}
fundingBody = sc.nextLine();
if (!sc.hasNextLine()) {
return null;
}
fundingLaw = sc.nextLine();
return new ResearchProject(p.getName(), p.getDescription(), p.getBudget(), p.getStart(), p.getEnd(),
fundingBody, fundingLaw);
}
public void print(PrintStream ps) {
super.print(ps);
ps.println(fundingBody);
ps.println(fundingLaw);
}
private String fundingBody;
private String fundingLaw;
}