Using Super Class and Extend From It

故事扮演 提交于 2019-12-11 13:05:15

问题


I want to sort some team base on their points (win: 3 points, draw: 1 points and defeat: 0 points). I have an array which contains some teams' names and their results, I have extracted their scores and then sorted them descending. Here is the code I have used:

import java.util.*;
import java.util.Map.Entry;

public class SCAN{
public static void main(String[] args) {
        Map<String, Integer> points = new HashMap<>();
        String[] challenges = new String[]{
            "Team(A) 2 - 1 Team(C)",
            "Team(D) 3 - 1 Team(A)",
            "Team(B) 3 - 3 Team(D)",
            "Team(C) 0 - 4 Team(B)",
            "Team(B) 2 - 0 Team(A)"
        };
    calcualte(challenges, points);
    List<Entry<String, Integer>> entries = new ArrayList<Entry<String, Integer>>(
            points.entrySet());
    Collections.sort(entries, new Comparator<Entry<String, Integer>>() {
        public int compare(Entry<String, Integer> e1,
                Entry<String, Integer> e2) {
            return e2.getValue().compareTo(e1.getValue()); // Sorts
                                                            // descending.
        }
    });
    Map<String, Integer> orderedMap = new LinkedHashMap<String, Integer>();
    for (Entry<String, Integer> entry : entries) {
        orderedMap.put(entry.getKey(), entry.getValue());
    }
    for (Entry<String, Integer> element : entries) {
        System.out.println(element);
        }
}

private static void calcualte(String[] challenges, Map<String, Integer> points) {
    List<String> challengeList = new ArrayList<>();
    for (String str : challenges) {
        String[] bits = str.trim().split(" ");
        String firstTeam = bits[0];
        String lastTeam = bits[bits.length - 1];
        if (!challengeList.contains(firstTeam)) {
            challengeList.add(firstTeam);
        }
        if (!challengeList.contains(lastTeam)) {
            challengeList.add(lastTeam);
        }
        int firstScore = Integer.parseInt(bits[1]);
        int lastScore = Integer.parseInt(bits[3]);
        if (firstScore > lastScore) {
            insert(3, points, firstTeam);
            insert(0, points, lastTeam);
        } else if (firstScore < lastScore) {
            insert(3, points, lastTeam);
            insert(0, points, firstTeam);
        } else {
            insert(1, points, firstTeam);
            insert(1, points, lastTeam);
        }
        {

        }
    }

}

private static void insert(int i, Map<String, Integer> points, String team) {
    int score = points.containsKey(team) ? points.get(team) : 0;
    score += i;
    points.put(team, score);
}
}

I know this, but I should do it by using a superclass So I need a subclass which its name is Plays and should be extended from a super class which its name is Playclass . The Playclass should not be changed. This class needs three abstracted method which should be overridden, they are won(),drawn() and defeated(). The other method which should to be overridden, is Print() which shows each team's information, like bellow:

public abstract class Playclass {
    private String name;
    Playclass(String name){
        this.name=name;
    }
    public String getName() {
        return name;
    }
    public abstract void print();
    public abstract void won();
    public abstract void drawn();
    public abstract void defeated();

}

The main class name could be Playleage and it should contain the String[] challenges = new String[]{…} which I showed in the first code. I know how to sort them base on their scores (like the first code) but I don’t know how to use the classes which I said. May someone help me? thanks

来源:https://stackoverflow.com/questions/23678210/using-super-class-and-extend-from-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!