2014-02-09 18 views
0

在另一個類im中,使用setRating來更改這些歌曲的評分,但是我不確定我需要對此代碼做些什麼才能夠永久更改評分。提前致謝。更改此代碼中的評分

import java.util.*; 

public class LibraryData { 

static String playCount() { 
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
} 

static int setRating(int stars) { 
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
} 

private static class Item { 

    Item(String n, String a, int r) { 
     name = n; 
     artist = a; 
     rating = r; 
    } 

    // instance variables 
    private String name; 
    private String artist; 
    private int rating; 
    private int playCount; 

    public String toString() { 
     return name + " - " + artist; 
    } 
} 

// with a Map you use put to insert a key, value pair 
// and get(key) to retrieve the value associated with a key 
// You don't need to understand how this works! 
private static Map<String, Item> library = new TreeMap<String, Item>(); 


static { 
    // if you want to have extra library items, put them in here 
    // use the same style - keys should be 2 digit Strings 
    library.put("01", new Item("How much is that doggy in the window", "Zee-J", 3)); 
    library.put("02", new Item("Exotic", "Maradonna", 5)); 
    library.put("03", new Item("I'm dreaming of a white Christmas", "Ludwig van Beethoven", 2)); 
    library.put("04", new Item("Pastoral Symphony", "Cayley Minnow", 1)); 
    library.put("05", new Item("Anarchy in the UK", "The Kings Singers", 0)); 
} 

public static String listAll() { 
    String output = ""; 
    Iterator iterator = library.keySet().iterator(); 
    while (iterator.hasNext()) { 
     String key = (String) iterator.next(); 
     Item item = library.get(key); 
     output += key + " " + item.name + " - " + item.artist + "\n"; 
    } 
    return output; 
} 

public static String getName(String key) { 
    Item item = library.get(key); 
    if (item == null) { 
     return null; // null means no such item 
    } else { 
     return item.name; 
    } 
} 

public static String getArtist(String key) { 
    Item item = library.get(key); 
    if (item == null) { 
     return null; // null means no such item 
    } else { 
     return item.artist; 
    } 
} 

public static int getRating(String key) { 
    Item item = library.get(key); 
    if (item == null) { 
     return -1; // negative quantity means no such item 
    } else { 
     return item.rating; 
    } 
} 

public static void setRating(String key, int rating) { 
    Item item = library.get(key); 
    if (item != null) { 
     item.rating = rating; 
    } 
} 

public static int getPlayCount(String key) { 
    Item item = library.get(key); 
    if (item == null) { 
     return -1; // negative quantity means no such item 
    } else { 
     return item.playCount; 
    } 
} 

public static void incrementPlayCount(String key) { 
    Item item = library.get(key); 
    if (item != null) { 
     item.playCount += 1; 
    } 
} 

public static void close() { 
    // Does nothing for this static version. 
    // Write a statement to close the database when you are using one 
} 

}

+0

爲什麼LibraryData中的所有'static'? –

回答

0

內部項目,你應該寫這個方法:

public static void setRating(int rating0) { 
    rating = rating0; 
} 

你也應該稱他們爲「公共靜態」,而不只是「公衆改變你的實例變量爲靜態變量「。

+0

如果我不使項目靜態我的代碼變得不可用(很多錯誤出現)。 – user3012997

+0

更改了包含該信息的答案。 –