对分配的解释:只使用集合框架,编写一个允许用户买卖股票的程序。我不允许使用用户定义的类。
股票需要有一个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!");
}a.购买(B或b)
b.销售(S或s)
c.出口(E或e)
选择:B
请输入一组三个字符: asd
请输入股票数量: 120
请输入股票价格: 10
{asd=[120,10]}
a.购买(B或b)
b.销售(S或s)
c.出口(E或e)
选择:B
请输入一组三个字符: asd
请输入股票数量: 100
请输入股票价格: 14
{asd=[120,10,100,14]}
a.购买(B或b)
b.销售(S或s)
c.出口(E或e)
选择:B
请输入一组三个字符: asd
请输入股票数量: 120
请输入股票价格: 10
{asd=[120,10,100,14,120,10]}
a.购买(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]}
a.购买(B或b)
b.销售(S或s)
c.出口(E或e)
选择:e
再见!
我没有语法错误,我不确定我想要的逻辑是否可以用我的当前工具集。
如果您查看我的输出,您可以看到我的ArrayLists队列在每个字符串键上都发生了变化。我知道为什么会这样,但是如果我在函数中声明一个队列的新实例,它将覆盖在该键下的内容。
发布于 2019-10-15 23:33:03
在您的buy()方法中,您总是要覆盖映射中的队列。您需要首先尝试检索现有队列(否则生成一个新队列),然后在必要时对其进行修改:
ArrayList<Integer>shares=new ArrayList<Integer>();
shares.add(stockNumber);
shares.add(stockPrice);
// this retrieves the existing queue, or creates and stores a new one if there wasn't one already
Queue<ArrayList<Integer>> stockQueue = stockInfo.getOrDefault(stockName, new LinkedList<ArrayList<Integer>>());
stockQueue.add(shares);然后您可以删除这一行:stockInfo.put(stockName, stockQueue);,因为getOrDefault()方法将新创建的队列放置在映射中。
您需要从stockQueue方法签名中删除buy()参数。
https://stackoverflow.com/questions/58403396
复制相似问题