Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- abstract class User {
- protected String name;
- public User(final String name) {
- this.name = name;
- }
- public abstract double computeIncome(int mins);
- }
- class Subscriber extends User {
- private int level;
- public Subscriber(final String name, int level) {
- super(name);
- this.level = level;
- }
- @Override
- public double computeIncome(int mins) {
- return 1.5 * mins * level;
- }
- @Override
- public String toString() {
- return name + "-" + level;
- }
- }
- class Creator extends User {
- private Subscriber[] subs;
- private int len = 0;
- public Creator(final String name) {
- super(name);
- this.subs = new Subscriber[1];
- }
- public void add(final Subscriber sub) {
- if (len == subs.length) {
- Subscriber[] aux = subs;
- subs = new Subscriber[len * 2];
- for (int i = 0; i < len; i++) {
- subs[i] = aux[i];
- }
- }
- subs[len++] = sub;
- }
- @Override
- public double computeIncome(int mins) {
- double sum = 0;
- for (int i = 0; i < len; i++) {
- sum += subs[i].computeIncome(mins);
- }
- return sum;
- }
- public String toString() {
- String s = name;
- s += " - ";
- for (int i = 0; i < len; i++) {
- s += subs[i];
- }
- return s;
- }
- }
- class Platform {
- private User[] users;
- private int len = 0;
- private static final int MAX_LEN = 1000;
- public Platform() {
- users = new User[MAX_LEN];
- }
- public boolean addUser(final User user) {
- if (len == MAX_LEN) {
- return false;
- }
- users[len++] = user;
- return true;
- }
- public User getVIP(int mins) {
- User ans = null;
- double maxi = 0.0;
- for (int i = 0; i < len; i++) {
- double curr = users[i].computeIncome(mins);
- if (maxi < curr) {
- maxi = curr;
- ans = users[i];
- }
- }
- return ans;
- }
- }
- public class Main {
- public static void main(String[] args) {
- Creator c1 = new Creator("johnc1");
- Creator c2 = new Creator("johnc2");
- Subscriber sub = new Subscriber("john", 1);
- c1.add(sub);
- Platform p = new Platform();
- p.addUser(c1);
- p.addUser(c2);
- p.addUser(sub);
- System.out.println(p.getVIP(30));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement