• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why is the total sale price not calculating/printing out as 0.0?  RSS feed

 
Ada Bradburry
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So it was almost working before (only printing half of what it should have and printing out half the number of total sales it should have) so then I made the totalSales variable static and it's printing everything it should be, but now the total sales won't calculate at all! I'm sure it's something really simple, but I've hit a mental road block. Any suggestions?

What I wrote:


What's given:
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
setTotalSales() isn't called anywhere so the it is never incremented at stays at 0.
 
Peter Schalk
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed, setTotalSales should be called before getTotalSales because getTotalSales is dependent on it's result. setTotalSales needs the phonePrice so you should add a getPhonePrice() method as well.

If you insert this on line 41 of ConcertPromoter:

public double getPhonePrice()
{
return phonePrice;
}

and this on line 41 of ConcertPromoterDriver:

johnMayer.setTotalSales(johnMayer.getTixSold(), johnMayer.getPhonePrice());

and this on line 95 of ConcertPromoterDriver:

m5.setTotalSales(m5.getTixSold(), m5.getPhonePrice());

everything will work fine.
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Peter Schalk

Actually, now I have had a few minutes to look at the code, I think that method should not be called. In fact, I think it ought not even to be there.
I can see a problem that the object cannot be guaranteed to be in a consistent state. You need to have the concept that a class has an invariant, and all methods must maintain that invariant. If they fail to maintain the invariant, they are incorrect. The constructor should initialise the new object into a consistent state, where the invariant is established. Your invariant might demand that the on‑the‑day price be greater than the advance price, or that a positive number of tickets be available. That ain't happening, because there isn't a written constructor. All those values are 0 before line 08, and the band's name reads null. Total sales is one thing which ought to be 0 when the object is new.
Yes, I know you are setting those values later, but that is not how objects should be constructed. The setPrice method is for alterations, like this:-
Special: 18th October only. All John Mayer tickets for $49.99!!

Solution: add a constructor which takes those values. You can verify that the values establish the invariants in the constructor (and use similar verification in the set methods).

Now, how do you work out the total sales prices? The concert promoter will do that. He will receive money for each ticket sold, and count it. The tax people are not going to phone him and say, “You sold 12 John Mayer tickets for $79.99, so you owe us $xy.zz tax.” Not at all. So the adding of total price should be inside the concert object. Whenever you sell a ticket, you are going to get money for it, so adding the money is part of the process of selling the ticket. Maybe the sellTickets method will do that, maybe it is better to call a (private) method which adds the price, from the sellTicket method. Just as you calculate tickets still available in the concert class, calculate the total sales money there.

Why is the total sales variable static? That is usually a mistake.
Why have you got two booleans which you never use? You are setting one but never reading it, and you never refer to the other at all. And are you duplicating information there? Can you have values of those two which are mutually incompatible?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!