大家好,我目前正在创建一个程序,允许用户创建数组、搜索数组以及从数组中删除元素。查看 LibraryMenu
方法,在 switch 语句中创建数组的第一种情况工作正常,但是当我尝试编译时,其他情况会产生“找不到符号错误”。
我的问题是我希望搜索和删除函数引用第一个开关案例 - 创建库数组。任何帮助都会受到赞赏,即使它可能来自一个简单的错误。
import java.util.*;
public class EnterLibrary
{
public static void LibraryMenu()
{
java.util.Scanner scannerObject =new java.util.Scanner(System.in);
LibraryMenu Menu = new LibraryMenu();
Menu.displayMenu();
switch (scannerObject.nextInt() )
{
case '1':
{
System.out.println ("1 - Add Videos");
Library[] newLibrary;
newLibrary = createLibrary();
}
break;
case '2':
System.out.println ("2 - Search Videos");
searchLibrary(newLibrary);
break;
case '3':
{
System.out.println ("3 - Change Videos");
//Change video method TBA
}
break;
case '4':
System.out.println ("4 - Delete Videos");
deleteVideo(newLibrary);
break;
default:
System.out.println ("Unrecognized option - please select options 1-3 ");
break;
}
}
public static Library[] createLibrary()
{
Library[] videos = new Library[4];
java.util.Scanner scannerObject =new java.util.Scanner(System.in);
for (int i = 0; i < videos.length; i++)
{
//User enters values into set methods in Library class
System.out.print("Enter video number: " + (i+1) + "\n");
String number = scannerObject.nextLine();
System.out.print("Enter video title: " + (i+1) + "\n");
String title = scannerObject.nextLine();
System.out.print("Enter video publisher: " + (i+1) + "\n");
String publisher = scannerObject.nextLine();
System.out.print("Enter video duration: " + (i+1) + "\n");
String duration = scannerObject.nextLine();
System.out.print("Enter video date: " + (i+1) + "\n");
String date= scannerObject.nextLine();
System.out.print("VIDEO " + (i+1) + " ENTRY ADDED " + "\n \n");
//Initialize arrays
videos[i] = new Library ();
videos[i].setVideo( number, title, publisher, duration, date );
}
return videos;
}
public static void printVidLibrary( Library[] videos)
{
//Get methods to print results
System.out.print("\n======VIDEO CATALOGUE====== \n");
for (int i = 0; i < videos.length; i++)
{
System.out.print("Video number " + (i+1) + ": \n" + videos[i].getNumber() + "\n ");
System.out.print("Video title " + (i+1) + ": \n" + videos[i].getTitle() + "\n ");
System.out.print("Video publisher " + (i+1) + ": \n" + videos[i].getPublisher() + "\n ");
System.out.print("Video duration " + (i+1) + ": \n" + videos[i].getDuration() + "\n ");
System.out.print("Video date " + (i+1) + ": \n" + videos[i].getDate() + "\n ");
}
}
public static Library searchLibrary( Library[] videos)
{
//User enters values to setSearch
Library titleResult = new Library();
java.util.Scanner scannerObject =new java.util.Scanner(System.in);
for (int n = 0; n < videos.length; n++)
{
System.out.println("Search for video number:\n");
String newSearch = scannerObject.nextLine();
titleResult.getSearch( videos, newSearch);
if (!titleResult.equals(-1))
{
System.out.print("Match found!\n" + newSearch + "\n");
}
else if (titleResult.equals(-1))
{
System.out.print("Sorry, no matches found!\n");
}
}
return titleResult;
}
public static void deleteVideo( Library[] videos)
{
Library titleResult = new Library();
java.util.Scanner scannerObject =new java.util.Scanner(System.in);
for (int n = 0; n < videos.length; n++)
{
System.out.println("Search for video number:\n");
String deleteSearch = scannerObject.nextLine();
titleResult.deleteVideo(videos, deleteSearch);
System.out.print("Video deleted\n");
}
}
public static void main(String[] args)
{
Library[] newLibrary;
new LibraryMenu();
}
}
最佳答案
我认为这是一个糟糕的设计。您将太多的东西混合在一起:用户界面、逻辑、数据结构。
首先将 LibraryArray
与 LibraryMenu
隔离。您根本不应该在其中看到任何开关或输入或输出。
Java 是一种面向对象的语言。开始从对象的角度思考您的系统。我没有看到 Video
和 VideoCatalog
之类的类。如果您创建了这个系统,您会发现它更容易实现。
看起来你已经开始了:
package model;
public class Video {
private Long id;
private String title;
private String publisher;
private int durationSeconds;
private Date publicationDate;
// add ctors, getters, etc. Immutable? Could be...
// equals, hash code, toString
}
让您的 VideoCatalog 远离用户界面或 I/O:
package model;
public interface VideoCatalog {
List<Video> find();
List<Video> find(String title);
List<Video> find(Date startDate, Date endDate) ;
Long save(Video video);
void update(Video video);
void delete(Video video);
}
现在您可以拥有一个使用您想要的任何数据结构的实现:
package model;
public class VideoCatalogImpl implements VideoCatalog {
private Set<Video> videos;
// add implementations here.
}
关于Java:带有涉及数组的方法的 Switch 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10970290/