我有四个文件:
LibraryPatron.java
/** A class that maintains a shopping cart for an online store.
@author Frank M. Carrano
@version 4.0
*/ // Data fields are: Name, Library card number, Street, City, Zip
import java.util.*;
public class LibraryPatron
{
public static void main(String[] args)
{
Patron[] Patron =
{
new Patron("Name", "Library Card", "Address", "City", "Zip Code"),
new Patron("Name", "Library Card", "Address", "City", "Zip Code")};
LinkedInterface libraryPatronList = new LinkedBag();
Scanner input = new Scanner(System.in);
System.out.println("Enter name:");
if (libraryPatronList.contains(name))
{
System.out.println ("Patron is in system.");
String info = libraryPatronList.getData(name);
for (String field: info.split(","))
System.out.println(field);
}
else
System.out.println ("Patron is not in system.");
}
}
Patron.java
/** A class of items for sale.
@author Frank M. Carrano
@version 4.0
*/
public class Patron // Data fields are: Name, Library card number, Street, City, Zip
{
private String name;
private String libraryCard;
private String street;
private String city;
private String zip;
public Patron(String nameOfPatron, String libraryCardNumber, String streetName, String cityName, String zipName)
{
name = nameOfPatron;
libraryCard = libraryCardNumber;
street = streetName;
city = cityName;
zip = zipName;
} // end constructor
public String getName()
{
return name;
} // end getDescription
@Override
public boolean equals (Object patron2)
{
Patron tempPatron;
tempPatron = (Patron) patron2;
return (name.equals (tempPatron.getName()));
}
public String toString()
{
return name + "," + libraryCard + "," + street + "," + city + "," + zip;
} // end toString
} // end Item
LinkedBag.java
public final class LinkedBag implements LinkedInterface
{
Node firstNode;
int numberOfEntries;
// Simple constructor
public LinkedBag()
{
firstNode = null;
numberOfEntries = 0;
} // end default constructor
/** Locates a given entry within this bag.
@param anEntry The entry to be located.
@return A reference to the node if located, or null otherwise. */
private Node getReferenceTo (T anEntry)
{
Node currentNode = firstNode;
boolean found = false;
while (!found && currentNode != null)
{
if (anEntry.equals (currentNode.data))
found = true;
else
currentNode = currentNode.next;
}
return currentNode;
}
/** Tests whether this bag contains a given entry.
@param anEntry The entry to locate.
@return True if the bag contains anEntry, or false otherwise. */
public boolean contains(T anEntry)
{
Node currentNode = getReferenceTo (anEntry);
return !(currentNode == null);
} // end
/** Retrieves all entries that are in this bag.
@return A newly allocated array of all the entries in this bag. */
public T[] toArray()
{
@SuppressWarnings ("unchecked")
T[] result = (T[]) new Object[numberOfEntries];
int index = 0;
Node currentNode = firstNode;
while (currentNode != null)
{
result [index] = currentNode.data;
++index;
currentNode = currentNode.next;
}
return result;
}
private class Node
{
private T data;
private Node next;
// Two contructors.
private Node (T dataPortion)
{
this (dataPortion, null);
}
private Node (T dataPortion, Node nextNode)
{
data = dataPortion;
next = nextNode;
}
}
/** Returns information in node containing anEntry in String form
* @param anEntry The entry to be searched for.
* @return String value of data in node if found, or empty string otherwise */
public String getData(T anEntry)
{
String result = "";
Node currentNode = getReferenceTo (anEntry);
if (currentNode != null)
{
result = currentNode.data.toString();
}
return result;
} // end getData
} // end LinkedBag
LinkedInterface.java
/**
An interface that describes the operations of a bag of objects.
@author Frank M. Carrano
@version 4.0
*/
public interface LinkedInterface
{
public boolean contains(T name);
public String getData(T anEntry);
} // end BagInterface
我的问题是关于到目前为止我拥有的LibraryPatron.java代码。
我正在尝试获取用户的输入并浏览我创建的赞助人列表,以验证用户的输入是否属于我的链接列表,但是我不知道如何从String转换并使用我的方法需要。如果用户输入的内容是我提前创建的“链接列表”的一部分,它将返回用户的信息,包括其姓名,图书馆卡号,地址,城市和邮政编码。任何建议或指导将不胜感激,谢谢!
LinkedBag类没有添加东西的方法。您必须将代码添加到LinkedInterface和LinkedBag中。类似于
public void add(T anEntity)
用您创建的顾客来填写所链接的购物袋。
name
在LibraryPatron中是undefined。那将不得不变成这样Scanner input = new Scanner(System. in);
String name = input. nextLine();