2014-12-05 42 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;   
} 

正如我以前說過,我不知道,如果這種方法沒有錯誤運行。 (我沒有測試它)。它確實給你一個解決方案。不能保證它是一個很好的解決方案,儘管:)正如你所看到的,有很多的列表和對象被創建,這樣一個小任務。我發現很多潛力可以讓它更有效率。