Java 更改此代码中的评级

Java 更改此代码中的评级,java,Java,在另一节课中,我使用设置评级更改这些歌曲的评级,但是我不确定我需要对此代码做什么才能永久更改评级。提前谢谢 import java.util.*; public class LibraryData { static String playCount() { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools

在另一节课中,我使用
设置评级
更改这些歌曲的评级,但是我不确定我需要对此代码做什么才能永久更改评级。提前谢谢

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
}
import java.util.*;
公共类库数据{
静态字符串播放计数(){
抛出新的UnsupportedOperationException(“尚未受支持”);//若要更改生成的方法体,请选择“工具”“模板”。
}
静态整数设定值(整数星型){
抛出新的UnsupportedOperationException(“尚未受支持”);//若要更改生成的方法体,请选择“工具”“模板”。
}
私有静态类项{
项(字符串n、字符串a、整数r){
name=n;
艺术家=a;
评级=r;
}
//实例变量
私有字符串名称;
私人弦乐演奏家;
私人内部评级;
私人int播放计数;
公共字符串toString(){
返回名称+“-”+艺术家;
}
}
//使用put插入键、值对的映射
//和get(key)来检索与键关联的值
//你不需要了解它是如何工作的!
私有静态映射库=新树映射();
静止的{
//如果你想有额外的图书馆项目,把它们放在这里
//使用相同的样式-键应为2位字符串
图书馆。放置(“01”,新物品(“橱窗里的小狗多少钱”,“Zee-J”,3));
图书馆。放置(“02”,新项目(“异国情调”,“马拉多纳”,5));
图书馆。put(“03”,新项目(“我梦想一个白色的圣诞节”,“路德维希·范·贝多芬”,2));
图书馆.put("04",新项目"田园交响乐","Cayley Minnow",1);;
图书馆.put(“05”,新条目(“英国无政府状态”,“国王歌手”,0));
}
公共静态字符串listAll(){
字符串输出=”;
迭代器迭代器=library.keySet().Iterator();
while(iterator.hasNext()){
字符串键=(字符串)迭代器。下一步();
Item=library.get(键);
输出+=键+“”+item.name+“-”+item.artist+“\n”;
}
返回输出;
}
公共静态字符串getName(字符串键){
Item=library.get(键);
如果(项==null){
return null;//null表示没有该项
}否则{
返回item.name;
}
}
公共静态字符串getArtister(字符串键){
Item=library.get(键);
如果(项==null){
return null;//null表示没有该项
}否则{
返回项目。艺术家;
}
}
公共静态整型getRating(字符串键){
Item=library.get(键);
如果(项==null){
return-1;//数量为负数表示没有该项
}否则{
退货项目.评级;
}
}
公共静态无效设置等级(字符串键,整数等级){
Item=library.get(键);
如果(项!=null){
项目评级=评级;
}
}
公共静态int getPlayCount(字符串键){
Item=library.get(键);
如果(项==null){
return-1;//数量为负数表示没有该项
}否则{
返回item.playCount;
}
}
公共静态void incrementPlayCount(字符串键){
Item=library.get(键);
如果(项!=null){
item.playCount+=1;
}
}
公共静态无效关闭(){
//此静态版本不执行任何操作。
//在使用数据库时,编写一条语句以关闭数据库
}

}

在项目中,您应该编写以下方法:

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

您还应该将实例变量更改为静态变量,方法是将它们称为“public static”,而不仅仅是“public”。

如果我不将项设置为静态,我的代码将变得不可用(出现许多错误)。为什么库数据中的
都是静态的?