【发布时间】:2019-10-15 22:29:52
【问题描述】:
对作业的解释: 仅使用 Collections Framework,编写一个允许用户买卖股票的程序。我不允许使用用户定义的类。
股票需要有一个 3 字符的字符串来表示股票名称,该股票的整数数量,以及每个该股票的出售价格。
如果用户从同一家公司购买了两支股票,则先买的股票先卖出。先进先出。
我试图创建一个带有字符串键的映射和一个带有嵌套 ArrayList 作为值的队列。我想将带有股票价格和股票数量的 ArrayList 推送到队列中。我认为 FIFO 队列非常适合跟踪和处理同一个股票名称下的大量购买。字符串键将是股票名称,其所有购买将按顺序记录在其下。
不幸的是,我似乎无法找到一种方法来实际进入队列,除非 a) 覆盖其中已经存在的任何内容,或者 b) 仅限于不能与股票名称绑定的队列的一个实例.
似乎我需要多个单独的队列实例,每个实例都在不同的字符串键下,但我不知道如何在没有用户定义的类的情况下正确实现。
您可以在提供的代码中看到其他使用嵌套集合的尝试。所有这些都受到以下事实的限制:我错过了有关收藏导航的某些内容,或者我对收藏的理解总体上存在缺陷。
public static void buy( Map<String, Queue< ArrayList<Integer> >> stockInfo, Queue< ArrayList<Integer> > stockQueue ) {
System.out.print("Please enter a set of three characters: ");
Scanner scanner = new Scanner(System.in);
var stockName = scanner.nextLine();
while (stockName.length() != STOCK_NAME_COUNT) {
System.out.printf("Invalid number of characters: %d \n", stockName.length());
System.out.print("Please enter a set of three characters: ");
stockName = scanner.nextLine();
}
System.out.print("Please enter number of stocks: ");
var stockNumber = scanner.nextInt();
while (stockNumber <= 0) {
System.out.printf("Invalid stock amount: %d \n", stockNumber);
System.out.print("Please enter number of stocks: ");
stockNumber = scanner.nextInt();
}
System.out.print("Please enter stock price: ");
var stockPrice = scanner.nextInt();
while (stockPrice <= 0) {
System.out.printf("Invalid price: %d \n", stockPrice);
System.out.print("Please enter stock price: ");
stockNumber = scanner.nextInt();
}
ArrayList<Integer>shares=new ArrayList<Integer>();
shares.add(stockNumber);
shares.add(stockPrice);
stockQueue.add(shares);
stockInfo.put(stockName, stockQueue);
}
...
public static void main(String[] args) {
//Set<String> stockNames = new HashSet<String>();
//Set<Queue< ArrayList<Integer> >> StockInfo = new HashSet<Queue< ArrayList<Integer> >>();
Map<String, Queue< ArrayList<Integer> >> stockInfo = new LinkedHashMap<String,Queue< ArrayList<Integer> >>();
Queue< ArrayList<Integer> > stockQueue = new LinkedList< ArrayList<Integer> >();
//Set<Queue< ArrayList<Integer> >> stockInfo = new HashSet<Queue< ArrayList<Integer> >> ();
//Queue< ArrayList<Integer> > stockInfo = new LinkedList< ArrayList<Integer> >();
var selection = menu("Choose option: ");
while (selection != 'E'){
if (selection == 'B'){
buy(stockInfo, stockQueue);
System.out.println(stockInfo);
} else {
//sale(stockInfo);
}
selection = menu("Choose option: ");
}
System.out.println("Goodbye!");
}
一个。购买(B 或 B)
b.销售(S 或 s)
c。退出(E 或 e)
选择选项:b
请输入一组三个字符:asd
请输入股票数量:120
请输入股价:10
{asd=[[120, 10]]}
一个。购买(B 或 B)
b.销售(S 或 s)
c。退出(E 或 e)
选择选项:b
请输入一组三个字符:asd
请输入股票数量:100
请输入股价:14
{asd=[[120, 10], [100, 14]]}
一个。购买(B 或 B)
b.销售(S 或 s)
c。退出(E 或 e)
选择选项:b
请输入一组三个字符:asd
请输入股票数量:120
请输入股价:10
{asd=[[120, 10], [100, 14], [120, 10]]}
一个。购买(B 或 B)
b.销售(S 或 s)
c。退出(E 或 e)
选择选项:b
请输入一组三个字符:qwe
请输入股票数量:50
请输入股价:13
{asd=[[120, 10], [100, 14], [120, 10], [50, 13]], qwe=[[120, 10], [100, 14], [120, 10], [50, 13]]}
一个。购买(B 或 B)
b.销售(S 或 s)
c。退出(E 或 e)
选择选项:e
再见!
我没有遇到语法错误,我不确定我所考虑的逻辑是否适用于我当前的工具集。
如果您查看我的输出,您会发现我的 ArrayLists 队列在每个 String 键上都发生了变化。我知道这是为什么,但是如果我在函数中声明一个新的队列实例,它将覆盖我在该键下已有的内容。
【问题讨论】:
标签: java collections