Advertisement
STANAANDREY

java oop lab test23

Dec 14th, 2023
740
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.50 KB | None | 0 0
  1.  
  2. abstract class User {
  3.     protected String name;
  4.  
  5.     public User(final String name) {
  6.         this.name = name;
  7.     }
  8.  
  9.     public abstract double computeIncome(int mins);
  10. }
  11.  
  12. class Subscriber extends User {
  13.     private int level;
  14.  
  15.     public Subscriber(final String name, int level) {
  16.         super(name);
  17.         this.level = level;
  18.     }
  19.  
  20.     @Override
  21.     public double computeIncome(int mins) {
  22.         return 1.5 * mins * level;
  23.     }
  24.  
  25.     @Override
  26.     public String toString() {
  27.         return name + "-" + level;
  28.     }
  29. }
  30.  
  31. class Creator extends User {
  32.  
  33.     private Subscriber[] subs;
  34.     private int len = 0;
  35.  
  36.     public Creator(final String name) {
  37.         super(name);
  38.         this.subs = new Subscriber[1];
  39.     }
  40.  
  41.     public void add(final Subscriber sub) {
  42.         if (len == subs.length) {
  43.             Subscriber[] aux = subs;
  44.             subs = new Subscriber[len * 2];
  45.             for (int i = 0; i < len; i++) {
  46.                 subs[i] = aux[i];
  47.             }
  48.         }
  49.         subs[len++] = sub;
  50.     }
  51.  
  52.     @Override
  53.     public double computeIncome(int mins) {
  54.         double sum = 0;
  55.         for (int i = 0; i < len; i++) {
  56.             sum += subs[i].computeIncome(mins);
  57.         }
  58.         return sum;
  59.     }
  60.  
  61.     public String toString() {
  62.         String s = name;
  63.         s += " - ";
  64.         for (int i = 0; i < len; i++) {
  65.             s += subs[i];
  66.         }
  67.         return s;
  68.     }
  69. }
  70.  
  71. class Platform {
  72.     private User[] users;
  73.     private int len = 0;
  74.     private static final int MAX_LEN  = 1000;
  75.  
  76.     public Platform() {
  77.         users = new User[MAX_LEN];
  78.     }
  79.  
  80.     public boolean addUser(final User user) {
  81.         if (len == MAX_LEN) {
  82.             return false;
  83.         }
  84.         users[len++] = user;
  85.         return true;
  86.     }
  87.  
  88.     public User getVIP(int mins) {
  89.         User ans = null;
  90.         double maxi = 0.0;
  91.         for (int i = 0; i < len; i++) {
  92.         double curr = users[i].computeIncome(mins);
  93.         if (maxi < curr) {
  94.             maxi = curr;
  95.             ans = users[i];
  96.         }
  97.         }
  98.         return ans;
  99.     }
  100. }
  101.  
  102. public class Main {
  103.     public static void main(String[] args) {
  104.         Creator c1 = new Creator("johnc1");
  105.         Creator c2 = new Creator("johnc2");
  106.         Subscriber sub = new Subscriber("john", 1);
  107.         c1.add(sub);
  108.         Platform p = new Platform();
  109.         p.addUser(c1);
  110.         p.addUser(c2);
  111.         p.addUser(sub);
  112.         System.out.println(p.getVIP(30));
  113.     }
  114. }
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement