好的,这是对我必须写的内容的解释:
getBreadInfo
- 将 bread.txt 读入一个数组列表(包含面包名称、$ 和价格),然后分配给一个数组 breadInfo[],然后将该数组返回给 SandwichApp 以显示面包菜单。getBread
- 类似于 getBreadInfo,除了它只包含面包名称,并返回另一个数组 bread[] 供 SandwichApp 确定用户选择了哪个面包,因为用户输入与面包相关的数字(索引 + 1),而不是面包姓名。getMapBreadPrice
- 与上述两个类似,不同之处在于它返回一个包含面包名称(键)和价格(值)对的哈希映射,以便 SandwichApp 找出用户选择的面包的价格。
这就是我写的。只是想知道这是否正确?
public class SandwichDB {
private ArrayList<String> breadsList = null;
public String[] getBreadInfo()
{
breadsList = new ArrayList<>();
try (BufferedReader in =
new BufferedReader(
new FileReader("bread.txt")))
{
String line = in.readLine();
while (line != null)
{
String[] elems = line.split("~");
breadsList.add(elems[0]+ " $" + elems[1]);
}
}
catch(IOException e)
{
System.out.println(e);
return null;
}
String[] breadInfo = breadsList.toArray(new String[]{});
return breadInfo;
}
public String[] getBread()
{
breadsList = new ArrayList<>();
try (BufferedReader in =
new BufferedReader(
new FileReader("bread.txt")))
{
String line = in.readLine();
while (line != null)
{
String[] elems = line.split("~");
breadsList.add(elems[0]);
}
}
catch(IOException e)
{
System.out.println(e);
return null;
}
String[] bread = breadsList.toArray(new String[]{});
return bread;
}
public HashMap<String, String> getMapBreadPrice()
{
HashMap<String, String> mapBreadPrice = new HashMap<>();
String line, elems[];
try
{
FileReader fr = new FileReader("bread.txt");
BufferedReader br = new BufferedReader(fr);
while ((line=br.readLine()) != null)
{
elems = line.split("~");
mapBreadPrice.put(elems[0], elems[1]);
}
}
catch(IOException e)
{
System.out.println(e);
return null;
}
return mapBreadPrice;
}
}