2014-02-09 3 views
0

setRating을 사용하여 다른 등급의 학생은이 노래의 등급을 변경하지만 영구적으로 등급을 변경할 수 있으려면이 코드를 어떻게해야하는지 잘 모르겠습니다. 미리 감사드립니다.이 코드의 등급을 변경하십시오.

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

또한 대신 "공공의"공공 정적 "을 호출하여 정적 변수로 인스턴스 변수를 변경해야합니다, 당신은이 방법을 작성해야 항목 내부

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에서 모두 '정적'인 이유는 무엇입니까? –

답변

0

. "

+0

항목을 정적으로 만들지 않으면 내 코드가 사용 불능이됩니다 (많은 오류가 나옵니다). – user3012997

+0

변경된 답변은 해당 정보를 통합합니다. –