所以我需要使用具有属性名称,评级,导演,年份和毛泽东的java创建电影数据库。该程序还必须添加电影,按标题删除电影,按标题排序电影,按评级排序电影,按Box Office Gross排序电影,由导演显示电影,打印到数据库文件(您可以定义自己的协议)和从数据库文件中读取。我遇到ArrayList <Movie>
的错误,它说它无法解析为某种类型,因此每当调用Movie时都会给我错误。
这是给出错误的块
ArrayList<Movie> movieDB = new ArrayList<Movie>();
int choice;
Scanner input = new Scanner(System.in);
String mvname, director, fileName;
int year;
double rating, gross;
这是我到目前为止的代码
import java.awt.List;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class MovieDatabase
{
public static void main(String[]args) throws Exception
{
ArrayList<Movie> movieDB = new ArrayList<Movie>();
int choice;
Scanner input=new Scanner(System.in);
String mvname,director,fileName;
int year;
double rating,gross;
while(true)
{
//menu
System.out.println("1.Add a movie\n2.Remove a moive\n3.Sort movie by Title\n4.Sort movie by Rating");
System.out.println("5.Sort movie by Box office Gross\n6.Show movies by a director");
System.out.print("7.Print to a database file\n8.Read from a database file\n9.exit: ");
choice=input.nextInt();
switch(choice)
{
case 1: System.out.println("Enter movie name: ");
mvname=input.next();
System.out.println("Enter year: ");
year=input.nextInt();
System.out.println("Enter Director name: ");
director=input.next();
System.out.println("Enter movie rating: ");
rating=input.nextDouble();
System.out.println("Enter Box Office Gross: ");
gross=input.nextDouble();
//add new moive object ot movie data base
movieDB.add(new Movie(mvname,year,rating,director,gross));
break;
case 2: System.out.println("Enter movie title: ");
mvname=input.next();
//remove specified movie object by title
for(Movie m:movieDB)
{
if(mvname.equals(m.getMovie_name()))
{
movieDB.remove(m);
}
}
break;
case 3: //Sort the movie data records by movie title
Collections.sort(movieDB,Movie.titleComparator);
System.out.println("After sorting by title");
for(Movie m:movieDB)
{
System.out.println(m.toString());
}
break;
case 4: //Sort the movie data records by movie rating
Collections.sort(movieDB,Movie.rateComparator);
System.out.println("After sorting by rating");
for(Movie m:movieDB)
{
System.out.println(m.toString());
}
break;
case 5: //Sort the movie data records by movie gross
Collections.sort(movieDB,Movie.grossComparator);
System.out.println("After sorting by gross");
for(Movie m:movieDB)
{
System.out.println(m.toString());
}
break;
case 6: //display movies by director name
System.out.println("Enter Director name: ");
director=input.next();
for(Movie m:movieDB)
{
if(director.equals(m.getMovie_Director()))
{
System.out.println(m.toString());
}
}
break;
case 7: //print movie data records to a text file
System.out.println("Enter File name: ");
fileName=input.next();
FileWriter fWriter = null;
BufferedWriter writer = null;
fWriter = new FileWriter(fileName+".txt",true);
writer = new BufferedWriter(fWriter);
for(Movie m:movieDB)
{
writer.write(m.toString());
System.out.println(m.toString());
}
break;
case 8:
//read movie data records from a text file
System.out.println("Enter File name: ");
fileName=input.next();
BufferedReader br = new BufferedReader(new FileReader(fileName+".txt"));
String line;
while((line = br.readLine()) != null)
{
System.out.println(line);
String lines[]=line.split(" ");
mvname=lines[0];
year=Integer.parseInt(lines[1]);
rating=Double.parseDouble(lines[2]);
director=lines[3];
gross=Double.parseDouble(lines[4]);
movieDB.add(new Movie(mvname,year,rating,director,gross));
}
br.close();
break;
case 9:System.exit(1);
default: System.out.println("Wrong Choice!");
}
}
}
}
答案 0 :(得分:0)
您错误拼写了下面的课程名称电影
ArrayList<Movie> movieDB = new ArrayList<Moive>();
尝试此操作并告知我们是否仍然因错误而失败。