我有四个文件:
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 代码。
我正在尝试获取用户的输入并扫描我创建的订阅者列表,以验证用户的输入是否是我的链接列表的一部分,但我不知道如何从字符串转换并使用这些方法我需要。如果用户的输入是我提前创建的链接列表的一部分,它将返回用户的信息,包括他们的姓名、借书证号、地址、城市和邮政编码。任何建议或指导将不胜感激,谢谢!
最佳答案
LinkedBag 类没有添加某些内容的方法。 您必须向 LinkedInterface 和 LinkedBag 添加代码才能执行此操作。类似于
public void add(T anEntity)
用您创建的订阅成员(member)填充 linkedBag。
LibraryPatron 中的 name
未定义。那必须改变成这样扫描仪输入 = new Scanner(System.in); 字符串名称 = 输入。 nextLine();
关于java - 不确定如何将用户的输入(字符串)转换为可接受的数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60501151/