2014-12-05 28 views
-1

我的问题在于第五个功能。我没有得到如何解决它。有人能帮助我吗?Java程序使用列表,设置,数据库(板球管理系统)

  1. 从数据库填充并存储在列表中。 List<Player>populate() throws ClassNotFoundException, SQLException;
  2. 根据运行对列表进行排序(最高优先)。 void sortRuns(List<Player> list);
  3. 根据出道日期对列表进行排序。 void sortByDebut(List<Player> list);
  4. 计算每个玩家的平均值并返回一个具有场平均值的列表。退货清单应按平均值排序(最高优先)。 List<Player>sortByAverage(List<Player>list);
  5. 返回运行次数少于10000的玩家组。例如,如果您将3传递给该函数,它应该只会随机​​返回3个玩家,并且每次应该生成不同的集合 。 Set<Player> randomSet(List<Player> list,int n);

豆类类是:

public class Player{ 
    int capid; 
    String playerName; 
    Country country; 
    int matches; 
    int runs; 
    Date Debut; 
    int notOut; 
    float average; 
} 

国家为ENUM:

public enum Country { 
    India,Australia,SouthAfrica,NewZeland,England,SriLanka,WestIndies 
} 

Above Solved 4 functions are: 

public class DataManagerImpl implements DataManager 
{ 
DBConnectionImpl dm=new DBConnectionImpl(); 
Connection conn; 

@Override 
public List<Player> populate() throws ClassNotFoundException, SQLException { 

    List<Player> list=new ArrayList<Player>(); 
    conn=dm.getConnection(); 
    Statement st=conn.createStatement(); 
    ResultSet rs=st.executeQuery("Select * from stats"); 
    while(rs.next()) 
    { 
     Player p=new  Player(rs.getInt(1),rs.getString(2),Country.valueOf(rs.getString(3)),rs.getInt(4),rs.getInt (5),rs.getDate(6),rs.getInt(7));             
     list.add(p); 
    } 
    return list; 
} 


@Override 
public void sortRuns(List<Player> list) { 
    // TODO Auto-generated method stub 
    Collections.sort(list,new Comparator<Player>(){ 

     @Override 
     public int compare(Player o1, Player o2) { 
      // TODO Auto-generated method stub 
      return Integer.compare(o2.getRuns(),o1.getRuns()); 
     } 

    }); 

} 

@Override 
public void sortByDebut(List<Player> list) { 
    // TODO Auto-generated method stub 
    Collections.sort(list,new Comparator<Player>(){ 

     @Override 
     public int compare(Player o1, Player o2) { 
      // TODO Auto-generated method stub 
      return o1.getDebut().compareTo(o2.getDebut()); 
     } 

    }); 
} 

@Override 
public List<Player> sortByAverage(List<Player> list) { 

    List<Player> plist=new ArrayList<Player>(); 
    float average; 
    for(Player p:list) 
    { 
     int runs=p.getRuns(); 
     int matchs=p.getMatches(); 
     average=(runs/matchs); 
     p.setAverage(average); 
     plist.add(p); 

    } 
    Collections.sort(plist,new Comparator<Player>(){ 

     @Override 
     public int compare(Player o1, Player o2) { 
      // TODO Auto-generated method stub 
      return Double.compare(o2.getAverage(),o1.getAverage()); 
     } 

    }); 
    // TODO Auto-generated method stub 
    return plist; 
} 
+0

我编辑了你的问题,以便阅读。现在,您可能想编辑问题并添加自己的代码,以便其他人可以看到您如何解决其他4个问题以及您打算如何解决问题5.如果您需要帮助解决问题5,请尝试解释您的问题't'得到'。你不明白这个问题,或者你不知道如何解决功能中的一个特定问题(也许随机是麻烦..)? – GameDroids 2014-12-05 17:39:38

回答

0

因此,这里是我的理解这个问题。 这可能不会运行,但应该给你一个想法如何做到这一点。

Set<Player> randomSet(List<Player> list,int n){ 

    ArrayList<Player> possiblePlayers = new ArrayList<>(); // fist we create a list where we can put all players that have 10000 runs 

    for(Player player: list){    // we loop through the list 
     if(player.getRuns()> 10000){  // check each player if he had more than 10000 runs 
      possiblePlayers.add(player); // and if so, we add him to our list of possible results 
     } 
    } 

    // at this point we have a list of players that have more than 10000 runs and we need to randomly select a player and put him into the new Set 

    // so first we create a Set that we can return 
    Set<Player> resultSet = new HashSet<>(); // Set is abstract and needs an implemented class like HashSet. This may not be the most appropriate implementation for your case, but it will do for now. 

    // now we need to pick n random players from our list. So we actually generate n numbers that we use as indexes 

    Random random = new Random(); // first we instantiate our random generator 
    ArrayList<Integer> indexes = new ArrayList<>(); // we will use this list to store the generated indexes, so we can check that we don't pick the same index twice 


    while (n > 0) { 
     Integer randomIndex = random.nextInt(possiblePlayers.size()); // first we pick a number between 0 and the size of our possiblePlayers list 
     if (!indexes.contains(randomIndex)) { // then we check if that number is already in the indexes list 
      indexes.add(randomIndex); // if not, we add it 
      n--; // and decrement n; 
     } 
     // if the random number where already in the indexes list, the loop would simply continue and try again 
    } 

    // here we have now a lost of randomly generated (and unique) indexes 
    // now we get the players at that indexes and put them in the result set 

    for(Integer index: indexes){ 
     resultSet.add(possiblePlayers.get(index)); 
    } 

    // and finally return the set 
    return resultSet;   
} 

正如我以前说过,我不知道,如果这种方法没有错误运行。 (我没有测试它)。它确实给你一个解决方案。不能保证它是一个很好的解决方案,尽管:)正如你所看到的,有很多的列表和对象被创建,这样一个小任务。我发现很多潜力可以让它更有效率。

相关问题