• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Java troubleshooting

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



So this is my code (sorry if incorrectly formatted im not sure how to)

the premise of the code is for me to fetch details about an item in order for a user to then bid on them. the bid must be an increment of atleast 10% but less than 20% of the start/current price
With my current code i get the flowing outputted

Please enter the id of the auction you would like to place a bid on?
1
Startprice0.0 //troubleshooting
0.0 //troubleshooting
Auction ID 1
Lower 0.0 //troubleshooting
upper 1.2 //troubleshooting
1: £20.0 £150.0 null item Name: Dog  Status: a Seller:  Dave

Please enter a value between £0.0 and £1.2

So my predicament is that while ive been able to index the stored items correctly using the auction id my startPrice is being set as 0 as is show with he fact the upperIncrement is set to 1.2

Any help


 
Marshal
Posts: 79929
396
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I only have enough time for a very quick and cursory look at your code, but let's start with the formatting:-
  • Line 2. { too far to the right. It should be vertically below the start of the method heading. Other code moved towards the left correspondingly.
  • Lines 3‑4 shouldn't be there at all.
  • Line 5: What does this mean? Are you using that local variable at all? Can you delete this line?
  • Lines 4 onwards. Don't use tabs; use spaces. You seem to be using Eclipse, so you can set an option to convert a tab to 4 spaces automatically.
  • Line 9: Don't write System.out.println(""); write System.out.println();
  • Line 10: Too long. Divide it after the first ( and indent the second half of the line two levels more than usual. Eclipse can probably do that automatically.
  • Line 12: At best confusing. Best deleted.
  • Line 14: Too long: divided similarly to that before. There is something suspicious about a constructor having so many parameters. It makes me think your class might be too complicated. But I am not definite about that. Status shouldn't start with a capital S.
  • Lines 15-17: Should be removed. Use a git repository or similar to record the old version in case you ever need to revert to it.
  • Lines 19-22: What do those lines do? Why are they there at all?
  • Line 30: The term increment doesn't describe what you are asking for. What you are asking for is latest bit PLUS minimum increment. Why is there a maximum increment which you can't bid more then?
  • Even after improving your formatting and removing much of the unnecessary code, your bid() method is still very long and would be better divided into several smaller methods. Validation of the current bid would be a good candidate. But I can't see why you are creating a new auction object inside such a method: that looks wrong. Even worse in the light of line 28.
    Why are you mixing integer and floating‑point arithmetic (lines 15‑16)? Don't use doubles for money: the best thing for that is BigInteger. See this old post of mine for two other useful links.Sorry to appear so negative, but thre is lots of scope for improving that code, so you might as well make the most of it.

    [edit]Please read the two links in the old post I quoted, not the rest of that thread.
     
    Sheriff
    Posts: 28321
    95
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You seem to be concerned by the fact that startPrice is zero. And your debugging code seems to confirm that fact.

    But on the other hand I don't see any code which sets that variable to anything. For that matter I don't even see any code which declares that variable. So the fact that it is zero is totally unsurprising to me. What reason do you have for expecting it to have some other value?
     
    shea terry
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:Welcome to the Ranch
    . . . .



    Much appreciated. The poor formatting is down to a frantic group effort and was going to attempt to tidy but thank you for the help with that.
    So the requirement is that the user must place a bid within 10 and 20% of the start/current bid. Awkward i know but that is the spec unfortunately.
    A lot of the code is for me trying to troubleshoot how to do it such as the sys.print(a.getAuctionID)
    The reason i have a new auction object is as i cannot use Auction.getAuctionID as it is called from a non static environment i have a picture of my original code which this has developed from which is a lot easier and simple to follow.
    code.png
    [Thumbnail for code.png]
     
    shea terry
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Paul Clapham wrote:You seem to be concerned by the fact that startPrice is zero. And your debugging code seems to confirm that fact.

    But on the other hand I don't see any code which sets that variable to anything. For that matter I don't even see any code which declares that variable. So the fact that it is zero is totally unsurprising to me. What reason do you have for expecting it to have some other value?



    So im trying to pull it from a linked list where it is defined as a constructor in the Account class
     
    Paul Clapham
    Sheriff
    Posts: 28321
    95
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    shea terry wrote:So im trying to pull it from a linked list where it is defined as a constructor in the Account class



    Looks like that isn't working, then. I see that you're passing it into the Account constructor but I don't see the code which computes the number which is passed in. You ought to look at that code.
     
    Campbell Ritchie
    Marshal
    Posts: 79929
    396
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Please only quote the parts of an old post you are replying to; I have deleted most of the quote.

    shea terry wrote:. . . Much appreciated.

    That's a pleasure

    The poor formatting is down to a frantic group effort and was going to attempt to tidy . . . .

    Famous last words. Make sure your code is as well formatted and in the best possible style as soon as you write it. It will be much quicker in the long run . . . and you won't forget to come back to it. Remember that Eclipse will do most of that automatically.

    . . .  i cannot use Auction.getAuctionID as it is called from a non static environment . . . .

    I don't like that error message, saying, “non‑static,” which misleads lots of beginners into making something static which oughtn't to be. Wherever your are calling that method from should most probably have static deleted.
     
    shea terry
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Ok so ive quickly reverted back to my original code that works when the variables are set as a static (I havent set them as static per the errors)
    the error is Cannot make a static reference to the non-static method getAuctionID() from the type Auction

    this is the entire account class so that you can grasp why i am trying to call from it (Needs formatting i think)


    If youd be able to shed any light i would very much appreciate it
     
    Campbell Ritchie
    Marshal
    Posts: 79929
    396
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    shea terry wrote:. . . my original code that works when the variables are set as a static . . .

    Only make something static iif you know why it should be static. If your original code works with something static, that may simply mean you haven't tested it with enough different auctions. By the way, selling one item isn't an auction; that is a lot.

    (Needs formatting i think) . . .

    I have spent lots of effort showing you how to format your code correctly.
     
    Paul Clapham
    Sheriff
    Posts: 28321
    95
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The code you posted earlier had Auctions.get(auctionID) in it, rather than Auction.get(auctionID). I speculate that the former uses a static method in the Auctions (not Auction) class which is supposed to return the Auction whose number is auctionID. At least, that would be a reasonable design, I don't know if it's in the actual design you're working on.

    If it is, then your code should include something like this:



    followed by



    followed by code which does things with that Auction.

    On the other hand you have things like this:



    which suggests that you're doing something like reading through a list of Auction objects to find the one you need to work with.

    Bottom line is that you are trying to write code based on a particular design, and you're asking for help with that code, but we know nothing about that design so we're just guessing about what you should do. I don't see any Auctions class, I don't see any list-scanning code, I do see a lot of undeclared variables...
     
    Paul Clapham
    Sheriff
    Posts: 28321
    95
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    ... or maybe Auctions is a variable referring to a list of Auction objects? Standard practice in Java is for class names to start with capital letters and for variable and method names to start with lower-case letters. So Auctions should be a class. However I see you have methods whose names start with capital letters, so we're left guessing about Auctions.
     
    shea terry
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Paul Clapham wrote:... or maybe Auctions is a variable referring to a list of Auction objects? Standard practice in Java is for class names to start with capital letters and for variable and method names to start with lower-case letters. So Auctions should be a class. However I see you have methods whose names start with capital letters, so we're left guessing about Auctions.


    So there is a linked list called Auctions

    There is a class called Auction which is in my reply before this
    the public method Auction holds constructors for the data types being held in the linked list, one of which is the AuctionID.
    I want to call the auctionID from the class Auction with the getAuctionID() method. However when i declare this it gives the error referring to not being able to compare static and non static values
     
    Paul Clapham
    Sheriff
    Posts: 28321
    95
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    shea terry wrote:So there is a linked list called Auctions



    Okay. Job number 1: change its name to auctions so you don't confuse people.

    And then you're going to need some code which goes through the list and finds an Auction object with the right auction ID. This is where you're going to call the getAuctionID method.

    the public method Auction holds constructors for the data types being held in the linked list, one of which is the AuctionID.



    Let's translate that so that you use the right terminology. First of all Auction is a public class. And second of all, that class only has one constructor. It's the one which starts "public Auction( ...".

    And yes, one of the variables of the Auction class is the auction ID. It's not held in the linked list, though. It's held in the Auction object. It's the Auction objects which are held in the linked list.

    And none of the variables of the Auction class should be public. They should all be private, and if you want to get their values from an Auction object then you call the getAuctionID or the other associated getSomething methods.

    It may be that by now I sound a bit cranky. Please don't take it that way. It's perfectly normal for a beginner to write code which is a mess to start out with. There's a whole lot of terminology which is not obvious and it takes a while to figure out how it all fits together.
     
    My honeysuckle is blooming this year! Now to fertilize this tiny ad:
    Gift giving made easy with the permaculture playing cards
    https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    reply
      Bookmark Topic Watch Topic
    • New Topic