-1

Got a simple auction program running, only problem is that if a user is removed before auction is closed his/hers bids are supposed to be removed. I dont have it 100% down yet but I think I am on the right path.

The bids have to be arrays and right now it is kinda removed or just moved maybe. This was a the error earlier.

Top Bid:[Wow 400 kr, Boy 311 kr, Man 33 kr, Dude 2 kr]

command>remove user

Name>wow

Wow has been removed from registry

command>list auctions

Auction # 1: Item. Top Bid:[Boy 311 kr, Man 33 kr, Exception in thread "main" java.lang.NullPointerException

public void removeBid(String name) {

    for(int a = 0;a < bidIndex; a++) {
        if(bids[a].getUser().getName().equals(name)) {
            bids[a]=null;
            bidIndex--;
            break;
        }

    }
    sortBids();



public void sortBids() {
    if(bidIndex > 1) {
        for(int a = 0; a < bidIndex -1; a++) {

            for(int b = a + 1; b < bidIndex; b++) {

                if(bids[a] == null || bids[a].getBid() < bids[b].getBid()) {
                    Bid temp = bids[a];
                    bids[a] = bids[b];
                    bids[b] = temp;
                }
            }
  • 1
    Related: https://stackoverflow.com/questions/642897/removing-an-element-from-an-array-java – jarmod Jan 17 '19 at 16:04
  • If you decrease bidIndex by 1 and removed a value between a and bidIndex you will be removing something from inside the list, not the top ones, the for in sortBids will ignore the last element since b < bidIndex that was a real value. – Marcos Vasconcelos Jan 17 '19 at 16:07

2 Answers2

0

Arrays cannot change size once initialized. If you create an array new String[10]; it will forever have 10 items (which are null by default). Setting an index to null doesn't change this.

String[] items = new String[] {"String1", "String2", "String3"};
items[1] = null;

This arrays would now look like [String1, null, String3]. If you need to change arrays as much as it seems, you're better off using a List or Map.

I would suggest using a HashMap if you want to easily link one object to another. In this case it looks like you'd be linking a String (name) to the Bid object.

Map<String, Bid> bids = new HashMap<String, Bid>();

Bid bid1 = new Bid(/*...*/);
Bid bid2 = new Bid(/*...*/);

// Add bids to the map

bids.put("Wow", bid1);
bids.put("Boy", bid2);

// Get any of these objects

Bid retrievedBid = bids.get("Wow");

// Or remove them

bids.remove("Wow");

HashMaps are similar in concept to associative arrays from other languages, where you have a key -> value relationship. Each key is unique, but the value can repeat.

They can also be converted to arrays if the final result you need is an array.

Bid[] bidsArray = new Bid[0];
bidsArray = bids.values().toArray(bidsArray);
Magnus Bull
  • 1,027
  • 1
  • 10
  • 21
0

One way you can achieve this is to convert the array to a list and then remove the bid using Java streams and convert back.

    List<Bid> bidsList = Arrays.asList(bids);
    bidsList = bidsList.stream()
            .filter(n -> !n.getUser().getName().equals(name))
            .collect(Collectors.toList());

    bids = bidsList.toArray(new Bid[bidsList.size()]);
Dan Wahl
  • 108
  • 1
  • 8