所以我有这段代码:
public class AccountService {
private Map<String, Account> accounts = DatabaseClass.getAccounts();
public AccountService(){
Account acc = new Account();
acc.setNickname("1");
acc.setPassword("2");
accounts.put("1", acc);
}
public List<Account> getAllAccounts(){
return new ArrayList<Account>(accounts.values());
}
public Account addAccount(String nickname, Account acc){
accounts.put(nickname, acc);
return acc;
}
public Account validateAccount(String nickname, String password){
Account acc = new Account(nickname,password);
if (accounts.containsValue(acc)){
acc.setNickname("true");
acc.setPassword("true");
}
return acc;
}
public class AccountResource {
AccountService accountservice = new AccountService();
@GET
public List<Account> getAllAccounts(){
return accountservice.getAllAccounts();
}
@POST
@Path("/{nickname}")
public Account addAccount(@PathParam("nickname") String nickname,Account acc){
return accountservice.addAccount(nickname,acc);
}
@GET
@Path("/{nickname}/{password}")
public Account validateAccount(@PathParam("nickname")String nickname, @PathParam("password") String password){
return accountservice.validateAccount(nickname , password);
}
public class DatabaseClass {
private static Map<String, Playlist> playlists = new HashMap<String, Playlist>();
private static Map<String, Account> accounts = new HashMap<String, Account>();
public static Map<String,Account> getAccounts (){
return accounts;
}
}
我试图查看地图(帐户)是否包含与acc
具有相同参数的对象。 constainsValue
的部分对我不起作用。有什么建议?
我已添加新对象以使用值1和2进行映射。
答案 0 :(得分:0)
我已经替换了这段代码:
public Account validateAccount(String nickname, String password){
Account acc = new Account(nickname,password);
if (accounts.containsValue(acc)){
acc.setNickname("true");
acc.setPassword("true");
}
return acc;
}
有了这个:
public Account validateAccount(String nickname, String password){
Account acc = new Account(nickname,password);
Account a= new Account();
if (accounts.containsKey(nickname)){
a = accounts.get(nickname);
if(acc.getNickname().equals(a.getNickname()) && acc.getPassword().equals(a.getPassword())){
acc.setNickname("true");
acc.setPassword("true");
}
}
return acc;
}
这解决了我的问题。感谢