Advertisement
STANAANDREY

poo lect7

Nov 19th, 2023 (edited)
1,003
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.42 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. class Member {
  4.     private String name;
  5.     private int age;
  6.  
  7.     public Member(String name, int age) {
  8.         this.name = name;
  9.         this.age = age;
  10.     }
  11.  
  12.     @Override
  13.     public String toString() {
  14.         return "{" + name + "}";
  15.     }
  16. }
  17.  
  18. interface Risky {
  19.     double getRisk();
  20. }
  21.  
  22. abstract class Project implements Risky {
  23.     private String title, objective;
  24.     private Member manager;
  25.     protected long funds;
  26.     protected Member[] members;
  27.     protected int len = 0;
  28.     public Project(String title, String objective, Member manager, long funds) {
  29.         this.title = title;
  30.         this.objective = objective;
  31.         this.manager = manager;
  32.         this.funds = funds;
  33.     }
  34.  
  35.     public abstract void addMember(Member member);
  36. }
  37.  
  38. class ComercialProject extends Project {
  39.     private String deadLine;
  40.     private static final int MAX_LEN = 15;
  41.     private long marketingFunds;
  42.     private long teamsNr;
  43.     public ComercialProject(
  44.             String title,
  45.             String objective,
  46.             Member manager,
  47.             long funds,
  48.             String deadLine,
  49.             long teamsNr
  50.     ) {
  51.         super(title, objective, manager, funds);
  52.         this.deadLine = deadLine;
  53.         this.marketingFunds = funds / 2;
  54.         this.teamsNr = teamsNr;
  55.         this.members = new Member[MAX_LEN];
  56.     }
  57.  
  58.     @Override
  59.     public double getRisk() {
  60.         return (double)3*teamsNr/len/funds - marketingFunds;
  61.     }
  62.  
  63.     @Override
  64.     public void addMember(Member member) {
  65.         if (len == MAX_LEN) {
  66.             return;
  67.         }
  68.         members[len++] = member;
  69.     }
  70.  
  71.     @Override
  72.     public String toString() {
  73.         return "ComercialProject{" +
  74.                 "deadLine='" + deadLine + '\'' +
  75.                 ", marketingFunds=" + marketingFunds +
  76.                 ", teamsNr=" + teamsNr +
  77.                 ", funds=" + funds +
  78.                 ", members=" + Arrays.toString(members) +
  79.                 ", len=" + len +
  80.                 '}';
  81.     }
  82. }
  83.  
  84. class MilitaryProject extends Project {
  85.     private String deadLine;
  86.     private static final int MAX_LEN = 15;
  87.     private String password;
  88.  
  89.     public MilitaryProject(
  90.             String title,
  91.             String objective,
  92.             Member manager,
  93.             String deadLine,
  94.             long funds,
  95.             String password
  96.     ) {
  97.         super(title, objective, manager, funds);
  98.         this.deadLine = deadLine;
  99.         this.password = password;
  100.         this.members = new Member[MAX_LEN];
  101.     }
  102.     @Override
  103.     public double getRisk() {
  104.         return (double)len/password.length()/funds;
  105.     }
  106.  
  107.     @Override
  108.     public void addMember(Member member) {
  109.         if (len == MAX_LEN) {
  110.             return;
  111.         }
  112.         members[len++] = member;
  113.     }
  114.  
  115.     @Override
  116.     public String toString() {
  117.         return "MilitaryProject{" +
  118.                 "deadLine='" + deadLine + '\'' +
  119.                 ", password='" + password + '\'' +
  120.                 ", funds=" + funds +
  121.                 ", members=" + Arrays.toString(members) +
  122.                 ", len=" + len +
  123.                 '}';
  124.     }
  125. }
  126.  
  127. class OpenSourceProject extends Project {
  128.  
  129.     private String mailingList;
  130.  
  131.     public OpenSourceProject(
  132.             String title,
  133.             String objective,
  134.             Member manager,
  135.             long funds,
  136.             String mailingList
  137.     ) {
  138.         super(title, objective, manager, funds);
  139.         this.mailingList = mailingList;
  140.         this.members = new Member[1];
  141.     }
  142.  
  143.     @Override
  144.     public double getRisk() {
  145.         return (double)len/funds;
  146.     }
  147.  
  148.     @Override
  149.     public void addMember(Member member) {
  150.         if (len == members.length) {
  151.             Member[] aux = members;
  152.             members = new Member[len * 2];
  153.             for (int i = 0; i < len; i++) {
  154.                 members[i] = aux[i];
  155.             }
  156.         }
  157.         members[len++] = member;
  158.     }
  159.  
  160.     @Override
  161.     public String toString() {
  162.         return "OpenSourceProject{" +
  163.                 "mailingList='" + mailingList + '\'' +
  164.                 ", funds=" + funds +
  165.                 ", members=" + Arrays.toString(members) +
  166.                 ", len=" + len +
  167.                 '}';
  168.     }
  169. }
  170.  
  171.  
  172. public class InvestmentCompany {
  173.     private Project[] projects;
  174.     private int len = 0;
  175.  
  176.     public InvestmentCompany() {
  177.         this.projects = new Project[1];
  178.     }
  179.  
  180.     public void addProject(Project project) {
  181.         if (len == projects.length) {
  182.             Project[] aux = projects;
  183.             projects = new Project[len * 2];
  184.             for (int i = 0; i < len; i++) {
  185.                 projects[i] = aux[i];
  186.             }
  187.         }
  188.         projects[len++] = project;
  189.     }
  190.  
  191.     public Project getBestInvestment() {
  192.         double min = Double.MAX_VALUE;
  193.         Project ans = null;
  194.         for (int i = 0; i < len; i++) {
  195.             double risk = projects[i].getRisk();
  196.             if (risk < min) {
  197.                 min = risk;
  198.                 ans = projects[i];
  199.             }
  200.         }
  201.         return ans;
  202.     }
  203.     public static void main(String[] args) {
  204.         MilitaryProject mp = new MilitaryProject(
  205.                 "title1",
  206.                 "obj1",
  207.                 new Member("john1", 30),
  208.                 "30-11-2023",
  209.                 100,
  210.                 "secret"
  211.         );
  212.         ComercialProject cp = new ComercialProject(
  213.              "title2",
  214.              "obj2",
  215.              new Member("john2", 40),
  216.              1000,
  217.              "20-11-2023", 2
  218.         );
  219.         OpenSourceProject osp = new OpenSourceProject(
  220.                 "title3",
  221.                 "objective3",
  222.                 new Member("john3", 33),
  223.                 200,
  224.                 "x@gmail.com"
  225.         );
  226.         Member a, b, c, d, e;
  227.         a = new Member("a", 19);
  228.         b = new Member("b", 21);
  229.         c = new Member("c", 22);
  230.         d = new Member("d", 23);
  231.         e = new Member("e", 21);
  232.         cp.addMember(a);
  233.         cp.addMember(b);
  234.         mp.addMember(a);
  235.         mp.addMember(d);
  236.         mp.addMember(c);
  237.         osp.addMember(e);
  238.         InvestmentCompany investmentCompany = new InvestmentCompany();
  239.         investmentCompany.addProject(mp);
  240.         investmentCompany.addProject(osp);
  241.         investmentCompany.addProject(cp);
  242.         System.out.println(investmentCompany.getBestInvestment());
  243.     }
  244. }
  245.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement