Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- class Member {
- private String name;
- private int age;
- public Member(String name, int age) {
- this.name = name;
- this.age = age;
- }
- @Override
- public String toString() {
- return "{" + name + "}";
- }
- }
- interface Risky {
- double getRisk();
- }
- abstract class Project implements Risky {
- private String title, objective;
- private Member manager;
- protected long funds;
- protected Member[] members;
- protected int len = 0;
- public Project(String title, String objective, Member manager, long funds) {
- this.title = title;
- this.objective = objective;
- this.manager = manager;
- this.funds = funds;
- }
- public abstract void addMember(Member member);
- }
- class ComercialProject extends Project {
- private String deadLine;
- private static final int MAX_LEN = 15;
- private long marketingFunds;
- private long teamsNr;
- public ComercialProject(
- String title,
- String objective,
- Member manager,
- long funds,
- String deadLine,
- long teamsNr
- ) {
- super(title, objective, manager, funds);
- this.deadLine = deadLine;
- this.marketingFunds = funds / 2;
- this.teamsNr = teamsNr;
- this.members = new Member[MAX_LEN];
- }
- @Override
- public double getRisk() {
- return (double)3*teamsNr/len/funds - marketingFunds;
- }
- @Override
- public void addMember(Member member) {
- if (len == MAX_LEN) {
- return;
- }
- members[len++] = member;
- }
- @Override
- public String toString() {
- return "ComercialProject{" +
- "deadLine='" + deadLine + '\'' +
- ", marketingFunds=" + marketingFunds +
- ", teamsNr=" + teamsNr +
- ", funds=" + funds +
- ", members=" + Arrays.toString(members) +
- ", len=" + len +
- '}';
- }
- }
- class MilitaryProject extends Project {
- private String deadLine;
- private static final int MAX_LEN = 15;
- private String password;
- public MilitaryProject(
- String title,
- String objective,
- Member manager,
- String deadLine,
- long funds,
- String password
- ) {
- super(title, objective, manager, funds);
- this.deadLine = deadLine;
- this.password = password;
- this.members = new Member[MAX_LEN];
- }
- @Override
- public double getRisk() {
- return (double)len/password.length()/funds;
- }
- @Override
- public void addMember(Member member) {
- if (len == MAX_LEN) {
- return;
- }
- members[len++] = member;
- }
- @Override
- public String toString() {
- return "MilitaryProject{" +
- "deadLine='" + deadLine + '\'' +
- ", password='" + password + '\'' +
- ", funds=" + funds +
- ", members=" + Arrays.toString(members) +
- ", len=" + len +
- '}';
- }
- }
- class OpenSourceProject extends Project {
- private String mailingList;
- public OpenSourceProject(
- String title,
- String objective,
- Member manager,
- long funds,
- String mailingList
- ) {
- super(title, objective, manager, funds);
- this.mailingList = mailingList;
- this.members = new Member[1];
- }
- @Override
- public double getRisk() {
- return (double)len/funds;
- }
- @Override
- public void addMember(Member member) {
- if (len == members.length) {
- Member[] aux = members;
- members = new Member[len * 2];
- for (int i = 0; i < len; i++) {
- members[i] = aux[i];
- }
- }
- members[len++] = member;
- }
- @Override
- public String toString() {
- return "OpenSourceProject{" +
- "mailingList='" + mailingList + '\'' +
- ", funds=" + funds +
- ", members=" + Arrays.toString(members) +
- ", len=" + len +
- '}';
- }
- }
- public class InvestmentCompany {
- private Project[] projects;
- private int len = 0;
- public InvestmentCompany() {
- this.projects = new Project[1];
- }
- public void addProject(Project project) {
- if (len == projects.length) {
- Project[] aux = projects;
- projects = new Project[len * 2];
- for (int i = 0; i < len; i++) {
- projects[i] = aux[i];
- }
- }
- projects[len++] = project;
- }
- public Project getBestInvestment() {
- double min = Double.MAX_VALUE;
- Project ans = null;
- for (int i = 0; i < len; i++) {
- double risk = projects[i].getRisk();
- if (risk < min) {
- min = risk;
- ans = projects[i];
- }
- }
- return ans;
- }
- public static void main(String[] args) {
- MilitaryProject mp = new MilitaryProject(
- "title1",
- "obj1",
- new Member("john1", 30),
- "30-11-2023",
- 100,
- "secret"
- );
- ComercialProject cp = new ComercialProject(
- "title2",
- "obj2",
- new Member("john2", 40),
- 1000,
- "20-11-2023", 2
- );
- OpenSourceProject osp = new OpenSourceProject(
- "title3",
- "objective3",
- new Member("john3", 33),
- 200,
- "x@gmail.com"
- );
- Member a, b, c, d, e;
- a = new Member("a", 19);
- b = new Member("b", 21);
- c = new Member("c", 22);
- d = new Member("d", 23);
- e = new Member("e", 21);
- cp.addMember(a);
- cp.addMember(b);
- mp.addMember(a);
- mp.addMember(d);
- mp.addMember(c);
- osp.addMember(e);
- InvestmentCompany investmentCompany = new InvestmentCompany();
- investmentCompany.addProject(mp);
- investmentCompany.addProject(osp);
- investmentCompany.addProject(cp);
- System.out.println(investmentCompany.getBestInvestment());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement