-1

I'm setting up an eAuction system and I want to make a list for each buyer of the auctions which they have won. However, I cannot seem to pass a string from one class to a linked list in another. I've had a look through the site about passing string from one class to another but none of them have worked.

I've tried passing it simply using the .add method but it keeps giving me suggestions that don't work.

This is my Auction class

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.LinkedList;
import java.util.List;

public class Auction {
    private int auctionID;
    private boolean blocked = false;
    private double startPrice, reservePrice;
    private LocalDateTime closeDate;
    private Item item;
    private double UP, LOW;
    private double currentBid;
    private List<Bid> bids = new LinkedList<>();
    private Status status;
    private User seller;
    private boolean itemFound;
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd MM yyyy");
    private double bidAmount;
    private User highestBidder;

    public Auction(double startPrice, double reservePrice, double currentBid, User highestBidder,
            LocalDateTime closeDate, Item item, User seller, Status status) throws Exception {
        if (closeDate.isBefore(LocalDateTime.now().plusDays(7))) {
            this.startPrice = startPrice;
            this.reservePrice = reservePrice;
            this.closeDate = closeDate;
            this.item = item;
            this.highestBidder = highestBidder;
            this.currentBid = currentBid;
            this.seller = seller;
            UP = currentBid * 0.20;
            LOW = currentBid * 0.10;
        } else {
            throw new Exception("CloseDate error: " + closeDate.format(formatter));
        }
    }

    public void auctionVictory() {
        if (status == Status.CLOSED && currentBid >= reservePrice) {
            User winner = getHighestBidder();
            Item winnerItem = getItem();
            winnerItem.getItemName(itemName);
            winner.victories.add(itemName);
        }
    }
}

My Item Class:

public class Item {
    private String itemName;

    public Item(String itemName, User seller) {
        setDescription(itemName);
    }

    public boolean checkItemSearch(String itemDescription) {
        if (itemName.equals(itemDescription)) {
            return true;
        }
        return false;
    }

    @Override
    public String toString() {
        return getItemName(itemName);
    }

    public void setDescription(String itemName) {
        this.itemName = itemName;
    }

    public String getItemName(String itemName) {
        return itemName;
    }
}

My Buyer Class:

public final class Buyer extends User {
    public static List<String> victories = new LinkedList<String>();

    public void Victory() {
    }

    public Buyer(String username, String password) {
        super(username, password);
    }
}

I expect the method 'auctionVictory' to get the User with the highest bid and get the Item that was up for auction if the current bid is higher than the reserve price.

Once I get the Item and Buyer I want to call the getItemName method from the Item class and get the string name for this item. After it gets this I want to add this string to the 'victories' list in the Buyer class.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Feng
  • 1
  • 1
  • 2
  • 1
    you have static field `victories` in `Buyer` class change `winner.victories.add(itemName);` to `Buyer.victories.add(itemName);` – Nonika Apr 13 '19 at 14:31
  • Additionally, because neither your `item` object and your `auctionVictory` void are static, you can simply use it directly i.e. `item = new Item(arguments);`. If you want to get a `Buyer` object, you have to MAKE it. – FailingCoder Apr 13 '19 at 14:38

1 Answers1

1

Your victories list is a static variable which means it's shared between every instance of the Buyer class, and accessing it through the instance variable "winner" doesn't make sense.

I'd assume you want your victories to be specific to the user, so you should remove the static modifier.

SchoolJava101
  • 609
  • 1
  • 5
  • 8
  • Yeah id like it to be specific to a user. However in my main class I have a method for printing out the victories list and it says that I need to change the victories list to static. – Feng Apr 13 '19 at 14:55
  • This is a http://xyproblem.info. You asked about adding a string result to another class but now you've got a error (https://stackoverflow.com/questions/3039306/how-can-i-create-a-new-class-instance-from-a-class-within-a-static-class) that shouldn't exist given the code. – FailingCoder Apr 13 '19 at 14:58