gpt4 book ai didi

java - 我如何从我的代码中的照片中删除喜欢

转载 作者:行者123 更新时间:2023-11-28 20:48:30 25 4
gpt4 key购买 nike

我已经为一个照片上传网站编写了一个程序,用户可以在其中为照片点赞,我有一个测试脚本来点赞照片然后显示所有喜欢然后我想删除喜欢并现在显示所有喜欢。我该怎么做?

import java.util.*;

public class Likes {
private static Hashtable<Photo, TreeSet<User>> likes = new Hashtable<>();

//like photo
public static void like(Photo photo, User user) {
try {
likes.get(photo).add(user);
}
catch (NullPointerException e) {
likes.put(photo, new TreeSet<>());
likes.get(photo).add(user);
}
}



//unlike photo
public static void unlike(Photo photo, User user) {
try {
likes.get(photo.remove(user));
}
catch (NullPointerException e) {

}
}

//has liked photo
public boolean hasliked(Photo photo, User user) {
try {
return likes.get(photo).contains(user);
}
catch (NullPointerException e) {
return false;
}
}

//get all likes
public List<Photo> getAllLikes(User user){
// new empty list
List<Photo> likedphotos = new ArrayList<Photo>();

for (Photo photo : likes.keySet()) {
if(hasliked(photo, user)){
// add photo to list
likedphotos.add(photo);
}
// return that initial list
}

return likedphotos;

}



public String toString() {
return likes.toString();
}

public void get(Photo remove) {
}

public void remove(Photo photo1, User user1) {
}
}

测试:

import java.sql.SQLOutput;
import java.util.*;

public class TestDrive {

public static void main(String[] args) throws InputValidationException {



//create 2 photo object
Photo photo1 = new Photo();
Photo photo2 = new Photo();

//create 1 user object
User user1 = new User();

//create 1 like object
Likes likes1 = new Likes();


//set 2 photos
photo1.setimageURL("photo of user 2");
photo2.setimageURL("photo of user 1");

//print the 2 photos
System.out.println(photo1.getimageURL());
System.out.println(photo2.getimageURL());


//set 1 user
user1.setUserName("oopnoob");

//print the username
System.out.println(user1.getUserName());

//Like the 2 photos


Likes.like(photo1, user1);
Likes.like(photo2, user1);

//Test the hasliked method to see if user1 has liked both photos - both should return true

System.out.println("Should be true: " + likes1.hasliked(photo1, user1));
System.out.println("Should be true: " + likes1.hasliked(photo2, user1));


//Test the getAllLikes for user1. Should print 2 photos.

System.out.println("user1 liked: " + likes1.getAllLikes(user1));


//i dont know how to remove the like from the photo

//Test the unlike feature by unliking photo1

likes1.unlike(photo1, user1);


//Test the getAllLikes - should only print one photo

System.out.println("user1 liked: " + likes1.getAllLikes(user1));







}

}

最佳答案

基于你的这部分代码

   //unlike photo
public static void unlike(Photo photo, User user) {
try {
likes.get(photo.remove(user));
}
catch (NullPointerException e) {

}
}

您似乎是在删除照片而不是“赞”

代码应该是

    //unlike photo
public static void unlike(Photo photo, User user) {
try {
likes.get(photo).remove(user);
}
catch (NullPointerException e) {

}
}

注意:在类中捕获 NullPointerException 是一个非常糟糕的主意。如果您认为像 photo 或 user 这样的值可以为 null,最好在使用该值之前进行检查。捕获异常然后忽略它也不明智。感谢@Ghostcat 向我指出这一点。

关于java - 我如何从我的代码中的照片中删除喜欢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56362059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com