我写的程序是一个拍卖应用程序,到目前为止我所做的是两个构造函数创建了两个ArrayList
,它们是绘画和 bid 列表。 Painting
列表包含绘画 ID 及其名称。相反,Bid
列表包含绘画名称和每个名称的出价金额。
我真正需要的是提示一条消息,在该消息之后用户必须引入其用户名,此时显示绘画名称和最高出价。例如,草莓绘画的最高金额为 1000000 。到目前为止,我已设法显示绘画名称,但不是最高出价。
如果你能帮助我了解从这一点开始从哪里开始,我将非常感激。这已经两天了,我仍然无法找到问题的解决方案。以下是一些代码块:
绘画
的ArrayList构造函数private ArrayList<Painting> Painting;
{
Painting = new ArrayList<Painting>();
}
public void add(int id, String name,String artist,String minibidprice)
{
Painting.add (new Painting(id,name,artist,minibidprice));
}
public void listAll ()
{
for (Painting p : Painting)
{
System.out.println(p);
}
}
出价列表的ArrayList构造函数
private ArrayList<Bids>Bids;
{
Bids = new ArrayList<Bids>();
}
public void add(int id, String bname,String pname,int value)
{
Bids.add (new Bids(id,bname,pname,value));
}
public void listAll2 ()
{
for (Bids b : Bids)
{
System.out.println(b);
}
}
以下是这些ArrayList
个对象的元素。第一个是绘画,下一个是出价。
Bidlists p = new Bidlists();
p.add(1440,"Water-Lillies","Claude Monet","$10,000.00");
p.add(1441,"Flowers in a Blue Vase ","Paul Cezanne","$20,000.00");
p.add(1442,"The Anglers","Henri Rousseau ","$50,000.00");
p.add(1443,"Wild Potato Dreaming","Emily Kngwarreye","$45,000.00");
p.add(1446,"Argenteuil","Claude Monet ","$100,000.00");
p.add(1447,"Luncheon on the Grass","Paul Cezanne","$50,000.00");
p.add(1448,"Antonia","Amadeo Modigliani ","$30,000.00");
p.add(1450,"The Young Apprentice","Amadeo Modigliani","$20,000.00");
p.add(1451,"Woman with Mandolin","Henri Matisse","$15,000.00");
p.add(1457,"Strawberries","Pierre-Auguste Renoir $","$30,000.00");
p.add(1458,"Alhalker Suite","Emily Kngwarreye ","$40,000.00");
Bid lists bid = new Bid lists();
bid.add(1,"Bill Chivers","Wild Potato Dreaming",100000000);
bid.add(2,"Ric Herbert","Strawberries",11000000);
bid.add(2,"Simon","Strawberries",32500000);
bid.add(4,"Bill Chivers","Luncheon on the Grass",90000000);
bid.add(5,"Simon ","Wild Potato Dreaming",110000000);
bid.add(6,"Bill Chivers ","Strawberries",25000000);
答案 0 :(得分:0)
您通常会使用Map
,因为它旨在允许某些数据与给定密钥相关联(即通过查找)。在这种情况下,您可以将绘画ID映射到最高出价(或者替换为所有出价列表,您按排序顺序维护,因此可以轻松选择最高出价。)
如果由于某种原因必须使用列表,那么您仍然可以通过迭代出价列表来实现您的要求,依次检查每个元素。当您找到与您的绘画ID相关的出价时,您可以考虑它,否则忽略其他绘画的出价。但请注意,与使用Map
相比,这在执行速度方面效率较低,并且还涉及更多手动代码(因此更容易出错)。
在选择最高出价方面,如果您给Bid
课程一个&#34;自然顺序&#34;这将有助于很多。 (根据出价规模)。这可以通过实现Comparable
来完成 - 一旦完成,集合框架就可以为您处理排序实现。但是,您必须将价格实际存储为数字而不是字符串。无论如何,这是一个好主意,因为字符串完全不透明。
一个简单的例子:
// Make Bids comparable
public class Bid implements Comparable<Bid> {
// fields as before, though we change the amount:
private final BigDecimal amount;
// Constructor as before - except you'll need to change the class of the amount
// now we add the compareTo method
public int compareTo(Bid other) {
// This says that Bids are ordered in the opposite way that their amounts
// are ordered, i.e. highest first
return -amount.compareTo(other.amount);
}
}
// Now in your main class you'll need to store the bids for each painting, rather
// than just a list:
private Map<Integer, List<Bid>> bidsByPainting = new HashMap<>();
// Change the method that adds bids so it looks a bit like this:
void add(Bid bid) {
List<Bid> existing = bidsByPainting.get(bid.getPaintingId());
if (existing == null) {
existing = new ArrayList<>();
bidsByPainting.put(bid.getPaintingId(), existing);
}
existing.add(bid);
// Sort the bids so that the highest is always first.
// (Note that this only works because we made Bid implement Comparable)
Collections.sort(existing);
}
// Now, at any time, you can get the highest bid for a painting by calling:
bidsByPainting.get(idYouCareAbout).get(0);
答案 1 :(得分:0)
在您的课程中,介绍一种名为
的方法public Painting getPainting(String paintId)
如果你想开发一种有效的方法,那么我会说gor for map with key as id和value as painting object你可以说
map.get(paintId)
获取Paiting对象。