2014-02-09 27 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

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