我有一个对象列表,其中包含名为Video
的类的电影名称:
Video v1 = new Video("The Shawshank Redemption", "Frank Darabont", "Drama", "9.2*", 49.99, true);
Video v2 = new Video("The Godfather", "Francis Ford Coppola", "Crime, Drama", "9.2*", 49.99, true);
Video v3 = new Video("The Godfather: Part II", "Francis Ford Coppola", "Crime, Drama", "9*", 48.99, true);
Video v4 = new Video("The Dark Knight", "Christopher Nolan", "Action, Crime, Drama", "9*", 48.99, false);
Video v5 = new Video("Pulp Fiction ", "Quentin Tarantino", "Crime, Thriller, Drama", "8.9*", 47.99, true);
我需要让用户输入名称或部分名称(包含至少3个字符),然后打印与用户输入相匹配的电影,我不确定如何以简单的方式进行操作
有什么想法吗? 这是我的完整代码
class Video
{
String name, director, type, rating;
Double price;
boolean borrowed;
String borrowedTrue;
Video(String n, String d, String t, String r, Double p, boolean b)
{
name = n;
director = d;
type = t;
rating = r;
price = p;
borrowed = b;
if(b == true)
{
borrowedTrue = "Yes";
}
else if(b == false)
{
borrowedTrue = "No";
}
}
public void display()
{
System.out.println("");
System.out.println("******************************");
System.out.println("");
System.out.println("Name: "+name);
System.out.println("Director: "+director);
System.out.println("Type: "+type);
System.out.println("Rating: "+rating);
System.out.println("Prince: "+price);
System.out.println("Borrowed: " +borrowedTrue);
System.out.println("");
System.out.println("******************************");
}
}
为类,我有这个对象
import java.util.*;
class TestVideo
{
static int counter = 0;
public static void main(String args[])
{
Video v1 = new Video("The Shawshank Redemption","Frank Darabont","Drama","9.2*",49.99,true);
Video v2 = new Video("The Godfather","Francis Ford Coppola","Crime,Drama","9.2*",49.99,true);
Video v3 = new Video("The Godfather: Part II","Francis Ford Coppola","Crime,Drama","9*",48.99,true);
Video v4 = new Video("The Dark Knight","Christopher Nolan","Action,Crime,Drama","9*",48.99,false);
Video v5 = new Video("Pulp Fiction ","Quentin Tarantino","Crime,Thriller,Drama","8.9*",47.99,true);
Video v6 = new Video("12 Angry Men","Sidney Lumet","Drama","8.9*",47.99,true);
Video v7 = new Video("The Good, the Bad and the Ugly","Sergio Leone","Western","8.9*",47.99,false);
Video v8 = new Video("Schindler's List","Steven Spielberg","Bography,History,Drama","8.9*",47.99,false);
Video v9 = new Video("The Lord of the Rings: The Return of the King","Peter Jackson"," Peter Jackson","8.9*",47.99,true);
Video v10 = new Video("Fight Club","David Fincher","Drama","8.9*",47.99,false);
Video v11 = new Video("The Lord of the Rings: The Fellowship of the Ring"," Peter Jackson"," Peter Jackson","8.8*",46.99,true);
Video v12 = new Video("Star Wars: Episode V - The Empire Strikes Back","Irvin Kershner","Action,Adventure,Fanstasy","8.8*",46.99,true);
Video v13 = new Video("Forrest Gump ","Robert Zemeckis","Romance,Drama","8.8*",46.99,false);
Video v14 = new Video("Inception","Christopher Nolan","Action,Mystery,Sci-Fi","8.8*",47.99,false);
Video v15 = new Video("The Lord of the Rings: The Two Towers","Peter Jackson","Adventure,Fantasy","8.8*",45.99,false);
Video v16 = new Video("Interstellar","Christopher Nolan","Adventure,Sci-Fi","8.8*",44.99,false);
Scanner user_input = new Scanner(System.in);
System.out.println("Please enter the name of the movie");
String matchName =user_input.next();
if (matchName.matchesIgnoreCase("[the]*"))
{
v1.display();
v2.display();
// i have to manually search for any videos which name contains 'the'
}
else
{
System.out.println("No match");
}
}
}
答案 0 :(得分:1)
如果您将视频实例存储在实现java.util.List
的类中,那么实现此目的的一个简单算法是搜索列表并将用户传递的搜索字符串与视频的名称字段进行比较。我会读取String方法的javadoc,比如`startsWith()。
例如,假设
// minimum functionality needed for example
public class Video{
private String name;
public String getName(){
return name;
}
public void printDetails(){
// some method that prints the movie details
}
}
你有你的清单,可能是一个封闭类的字段:
// Somewhere relevant in your code
List<Video> videos = new ArrayList<Video>();
// add your examples to the list
videos.add(v1);
videos.add(v2);
videos.add(v3);
videos.add(v4);
videos.add(v5);
此方法可以解决您的问题:
public void printNames(String query){
if(query.length() < 3){
// report an error, with an exception or print statement
System.out.println("Search query must be at least 3 characters.");
}
for(Video v : videos){
if(v.getName().startsWith(query)){
v.printDetails();
}
}
}
或者,我可能会建议使用StringBuilder
来构建返回String,然后打印它。或者,返回匹配视频列表并以其他方式操纵它们。