This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Help with Java donut exercise

 
Greenhorn
Posts: 24
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys

trying a java donut exercise

this is the question:

a donut shop sells the following donuts:

a. Plain donut £1.99
b. Sugar donut £2.50
c. Chocolate donut £2.99
d. Sprinkled donut £2.99

out put the menu

write a program that allows a customer to order donuts from the menu
they should specify the donut number (from the menu) and the required amount
customer can order as many types of donut as they wish and will calculate the cost of the
order

if the total is greater than £10.00 they get a 10% discount

here is my code thus far:

i have 2 classes

DonutShop



and Donut



The menu is working as expected, but is it in the right place?

I know objects of the donut need to be created, but where? in the menu after a b c etc?

don't really know where to go next, any help would be appreciated

thanks
 
Saloon Keeper
Posts: 6227
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • I would say that a Donut only has two attributes: name, and price.
  • Then, which Donut, the quantity, and the subtotal should be handled in a PurchaseItem class.
  • A list of Donuts should be available to create a menu from instead of hard coding a-e.
  • As a Donut is chosen from the menu and a quantity requested, then a new PurchaseItem should be created and added to the PurchaseItem list.
  • Separate methods should be created for prompting for menu choice and quantity.
  •  
    Marshal
    Posts: 14039
    234
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    There are at least a couple of ways you can write this: the non-OOP way or the OOP way.  In another thread you started, I gave you details about a development process that you said you were going to try out. Why don't you try it out with this problem? It's a good a place to start as any.
     
    Junilu Lacar
    Marshal
    Posts: 14039
    234
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Let's look at this code:

    There's quite a bit of duplication in this code and some mystery.

    First, the mystery. What's the purpose of line 9? What are you doing (or plan to do) with that Donut object?

    Second, the duplication. It may not seem obvious to you but the duplication I refer to is a duplicate idea, not character-for-character duplication. What's the idea? Well, Carey alludes to it in his reply. Here's a clue:

    See the duplication now? Duplication in code can be in the form of a basic idea. The idea you're duplicating is the idea of a Donut, its name, and its price. You've also manually displayed in a tediously repetitious way something that could have been done by a loop.
     
    Bartender
    Posts: 2371
    102
    Google Web Toolkit Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    An interesting add-on to this program should be that if a customer buys a Plain doughnut followed by a Sugar doughnut followed again by a Plain doughnut, it should internally register as 2 Plain doughnuts and 1 Sugar doughnut and show up in the final order confirmation as such. Of course you are only interested in the total and the 10% discount, but this would be a better User Experience.
    Next, this has been discussed a lot of times in coderanch: Do not using floats for currency.
     
    Junilu Lacar
    Marshal
    Posts: 14039
    234
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    salvin francis wrote:Next, this has been discussed a lot of times in coderanch: Do not using floats for currency.


    I know that's often pointed out but sometimes I think it's not something people need to worry about too much at that point. It would be helpful if we gave some explanation as to why instead of just giving a hard-and-fast, context-free rule like "don't use floats for currency".

    @OP, if you're interested or even mildly curious why, search for why you shouldn't use floating point numbers for money or just read this article: https://spin.atomicobject.com/2014/08/14/currency-rounding-errors/

    I don't know if that's a main focus of concern right now but just keep those points in mind in the future, if you have to do any kind of calculations that involve real money.
     
    salvin francis
    Bartender
    Posts: 2371
    102
    Google Web Toolkit Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Agreed, I should have given some context over that statement.
     
    Eric Flynn
    Greenhorn
    Posts: 24
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    ok didn't know that? ok so i read you must use Big Decimal, thats a class I take it? I will keep that in mind, though for these mickey mouse newb exercises i'm sure it isn't going to be a problem.

    here is my "script" for my program. I have expanded it a little. Cause I thought it might be worth doing it.

    Donut Shop Script:

    When the program starts the user is told "Welcome to donut seller software"

    The user is then presented with the main menu

    a. Plain donut  £1.99
    b. Sugar donut €2.50
    c. Chocolate donut €2.99
    d. Sprinkled donut €2.99
    e. Exit program

    The user is then asked to make a selection

    if the user selects a,b,c or d he is asked:
    How many (a,b,c or d)'s would you like to buy?
    (eg: How many plain donuts would you like to buy?) though im sure you knew what i meant

    if the user enters an invalid option, he/she is told and the menu is presented again

    After making the correct selection the user then enters the quantity to buy.
    The user is told to select a quantity between 1 and 20.

    if the user enters anything besides a number between 1 and 20 he is told that this is invalid, and is asked to enter the correct quantity again.

    On entering a correct quantity the user is presented with the main menu again,this continues unless the user enters e

    When the user enters e the following is displayed:

    the type of donut, the amount purchased, the total of those types of donuts purchased.
    (if 0 of a particular donut type is purchased, nothing is displayed)
    the total of all donuts purchased is then displayed
    if the total of all donuts purchased is over £10.00 then discount is displayed with the 10% removed from the total
    the final total minus any discount (if any) is printed
    have a nice day is printed
     
    Marshal
    Posts: 65782
    250
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Eric Flynn wrote:ok didn't know that . . .

    Unfortunately lots of other people don't know that either.

    use Big Decimal . . . for these mickey mouse newb exercises i'm sure it isn't going to be a problem. . . .

    You won't notice the problem but you might as well learn the correct way to calculate money from the word “go”.It is possible to reuse the two objects in lines 3‑4 because big decimal is immutable (at least in theory). Also have a look at this old post of mine.
     
    Junilu Lacar
    Marshal
    Posts: 14039
    234
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Eric Flynn wrote:here is my "script" for my program. I have expanded it a little. Cause I thought it might be worth doing it.
    ...


    Eric, that is a tremendous improvement from where you were heading. Awesome work in getting the big picture clearer in your mind and being able to express it in writing!

    But rather than me telling you what the next step would be, go back to that other thread and read the development process I described again. Then tell us what you think should be your next step, try to do it, and show us the result. Don't be afraid to fail / fall flat on your face. When babies learn to walk, falling flat on their faces is an important part of the process. But just as babies do when they fall, you just pick yourself up and try again. We're here to help.
     
    Junilu Lacar
    Marshal
    Posts: 14039
    234
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    One of my software development "mentors," Kent Beck, likes to say that code should tell a story. By writing that story out in plain English and making sure that there's an arc, a beginning, a middle, and an end, then you are laying out the groundwork and the framework for being able to tell that story as code.

    I have found this mental model—or metaphor, if you will—of what code really is very useful in my own work. I give all the credit back to people like Kent Beck, Martin Fowler, Ward Cunningham, and Robert Martin. I hope that in due course during your studies, your teachers will introduce you to the ideas that these people, whom I consider my mentors even though I've never met them personally (with the exception of Robert "Uncle Bob" Martin), have articulated in books, videos, and articles. Their ideas and examples have helped countless professional developers get better and be more productive. I think it would be a shame if you completed your studies without even hearing about them and the things they have to say about software development.
     
    Eric Flynn
    Greenhorn
    Posts: 24
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    very encouraging words,thank you all! will get right on it tomorrow.
     
    Junilu Lacar
    Marshal
    Posts: 14039
    234
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Eric Flynn wrote:I will keep that in mind, though for these mickey mouse newb exercises i'm sure it isn't going to be a problem.


    Good. I agree that float vs BigDecimal is not a biggie for this problem. Just knowing that it might be an issue when real money is involved is good enough for now. Besides, I doubt that was even part of the learning goals for this exercise.

    here is my "script" for my program.


    I think having a clear high-level idea of the story you want to tell before you start translating it to code is very important though. This is foundational to the problem solving process. I'm looking forward to seeing what you'll do next.
     
    Eric Flynn
    Greenhorn
    Posts: 24
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I'm guessing that I shouldn't even be looking at code...yet
    Is this now the time to identify classes members and methods? Or is there something else that needs to be done first?
     
    Campbell Ritchie
    Marshal
    Posts: 65782
    250
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Eric Flynn wrote:I'm guessing that I shouldn't even be looking at code...yet

    As a general rule of thumb, the more thinking and planning you do before you write any code the better.

    Is this now the time to identify classes members and methods? Or is there something else that needs to be done first?

    Careful about the w‍ord “members”, which has a specific meaning in Java®: methods, fields, and nested types.
    Otherwise, don't know. You have a good script, but go through it and make sure you have all the detail required. For example, how are you going to calculate the 10% discount? If I buy one doughnut of each type, will my bill of £10.46 permit me a discount of £1.05 or £1,04?
    Then change the whole thing to pseudo‑code. It will look like this:-Once you have that, you will hae something looking like code, but not quite. It should however be very easy to turn pseudo‑code intio real code.
     
    Campbell Ritchie
    Marshal
    Posts: 65782
    250
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    A few minutes ago, I wrote:. . . how are you going to calculate the 10% discount? . . .

    Carefully study different ways to calculate the discount, then, in the absence of any instructions to the contrary, choosd whichever you think will be easiest to implement
     
    Marshal
    Posts: 7178
    491
    Mac OS X VI Editor BSD Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:As a general rule of thumb, the more thinking and planning you do before you write any code the better.


    Is it still applicable in nowadays? Or I should say, if one practice TDD (=Test Driven Development), or even if not. As Junilu often says: "experiment your ideas in code", because code really tells the truth.

    Am I correct in saying, that logical exercises and algorithms are better solved on paper first, while the actual design of the system is better experimented with code, rather than planned upfront with charts/diagrams/etc..
     
    Junilu Lacar
    Marshal
    Posts: 14039
    234
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Eric Flynn wrote:I'm guessing that I shouldn't even be looking at code...yet
    Is this now the time to identify classes members and methods? Or is there something else that needs to be done first?


    Ah, it seems you have gone tentative on us now. Did you go back and re-read the development process I described in your other thread that you said you'd try?

    One of my biggest disappointments with academia is the seemingly lack of focus on testing as part of the development process. Testing, when done at the appropriate time (as code is written, not just after), puts your development process in a virtuous cycle. Testing after code has been written tends to put it in a vicious cycle, especially since testing after tends to be tainted by bias and done in a cursory and haphazard manner.

    Here's how Kent Beck described his process: "I would imagine some functionality and my next thought was 'How am I going to test that?' ... If you can't write a test for it then you have no business programming it."

    And here's what he said about tests: "When I'm writing tests I'm not just writing tests. I'm making API decisions, I'm making analysis decisions and the tests just happen to be the notation that I use to record those decisions."

    With that in mind, I'll give you three guesses on what I think you should do next...
     
    Campbell Ritchie
    Marshal
    Posts: 65782
    250
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Liutauras Vilda wrote:. . . Is it still applicable in nowadays? . . . if one practice TDD . . .

    Sorry, I thought I had replied to you earlier, Liutauras, but the message seems to have disappeared.

    Good point. I think you are right there. Once you have the plan, produce some code and test it, as Junilu says. Another thing I would do is try out little code blocks on JShell. It's much quicker than writing a whole class to test three lines of code. And if it doesn't work, you can destroy the evidence very simply

    the actual design of the system is better experimented with code, rather than planned upfront with charts/diagrams/etc..

    I was taught you have to use UML etc., but I think it is more accurate to say that UML doesn't assist coding at all. Anyway, as you hint, after a lot of coding (5 minutes), you will realise that the planned class design isn't working and will have to be changed.
     
    Junilu Lacar
    Marshal
    Posts: 14039
    234
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Liutauras Vilda wrote:

    Campbell Ritchie wrote:As a general rule of thumb, the more thinking and planning you do before you write any code the better.

    Is it still applicable in nowadays?


    I would say it's still true but only to some extent. You just don't want to overthink these things to the point of paralysis. The trick is to know where to draw the line between planning and doing. I tend to move quickly into experimenting as soon as I have even a semi-clear idea of what it is I want to achieve. I'd say that based on the "script" he wrote, Eric has a more than semi-clear idea of what he wants to achieve.
     
    Junilu Lacar
    Marshal
    Posts: 14039
    234
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Liu wrote:Or I should say, if one practice TDD (=Test Driven Development), or even if not. As Junilu often says: "experiment your ideas in code", because code really tells the truth.


    Generally, yes, the code does tell the truth. Or maybe better yet, it's the only thing you can count on to reveal the truth about the state of your understanding of the problem.
     
    Junilu Lacar
    Marshal
    Posts: 14039
    234
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Liu wrote:Am I correct in saying, that logical exercises and algorithms are better solved on paper first, while the actual design of the system is better experimented with code, rather than planned upfront with charts/diagrams/etc..


    Detailed design is better experimented with code. Jack Reeves argued that code is design therefore any other form of documentation that goes to the level of detail that can be translated directly into code is pretty much a duplicate effort and a waste of time. Planning out fields and methods is at that level of detail. That's why I never draw diagrams that show fields and scope of fields and such. That's just way too much detail to put in a non-executable and non-testable form of documentation. And they tend to be wrong anyway when done too early.

    Any more, if I create any kind of diagram when I'm developing, it will be high-level context diagrams or a rough estimation of the boundaries between major components of the system. And these will be drawn on a whiteboard or scrap paper with the assumption that they are probably wrong in some way and that they will soon change. Come to think of it, the diagram I have drawn the most often in the last few years is of Alistair Cockburn's hexagonal architecture. Any other diagram I may have drawn has now been flushed from memory, erased from the whiteboard, or tossed in the waste basket. That's how valuable I find diagrams anymore. Most of my tests, however, endure as code. At least the ones that represent successful experiments.
     
    Junilu Lacar
    Marshal
    Posts: 14039
    234
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:Another thing I would do is try out little code blocks on JShell. It's much quicker than writing a whole class to test three lines of code.


    Yes! More and more, I have been turning to JShell as a playground, a sandbox to get some quick-and-dirty learning done.
     
    Junilu Lacar
    Marshal
    Posts: 14039
    234
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:I was taught you have to use UML etc., but I think it is more accurate to say that UML doesn't assist coding at all. Anyway, as you hint, after a lot of coding (5 minutes), you will realise that the planned class design isn't working and will have to be changed.


    I taught myself UML only because Martin Fowler wrote a book about it. And Craig Larman also wrote a book about it that was the recommended study material for a now-defunct certification exam on OOAD issued by IBM, which of course I got. That was a few months of studying that I'll never get back.

    I find that the trick to creating useful diagrams is to make them such that they only capture ideas at a high level of abstraction, where any changes to details won't immediately invalidate them. The hexagonal architecture I mentioned earlier is at that level of abstraction. It's going to be valid regardless of the fields and methods you end up assigning to a class or the hierarchy of objects you design. I find that diagram very useful in framing conversations about course-grained details of the system with the development team. That's why it's the one diagram I draw the most often.
     
    Campbell Ritchie
    Marshal
    Posts: 65782
    250
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Junilu Lacar wrote:. . . non-testable form of documentation . . . tend to be wrong anyway when done too early. . . .

    Is that the same as my having to change the UML after 5 minutes? When I had to write UML for assignments, I got the code working and later let an Eclipse plug‑in produce the UML diagrams.

    And I see you like JShell as much as I do
     
    Junilu Lacar
    Marshal
    Posts: 14039
    234
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:

    Junilu Lacar wrote:. . . non-testable form of documentation . . . tend to be wrong anyway when done too early. . . .

    Is that the same as my having to change the UML after 5 minutes? When I had to write UML for assignments, I got the code working and later let an Eclipse plug‑in produce the UML diagrams.


    When you find you're having to change UML to reflect a change in code, you've discovered duplication and the need to eliminate it. Automated generation of UML after your code has stabilized is the most sensible use of time and UML there is, in my opinion.
     
    Campbell Ritchie
    Marshal
    Posts: 65782
    250
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Junilu Lacar wrote:. . . Automated generation of UML . . .

    It got me a grade A, too
     
    Eric Flynn
    Greenhorn
    Posts: 24
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This?

    "I start out with a small idea that I think will work. Then I write some test code to document what this idea might look like in code. Because there's no production code actually written, the "test code" isn't really test code: when I write it and the way I write it, it's really a specification of what you think is a good way to organize your objects. Once I think I have a good test, i.e. specification, I write the production code to make the test pass. Then I see if I can extend it further by adding some more functionality, again starting with a test to specify how I think the objects in the program should be organized and how they should behave around the new functionality.

    Throughout this process, you're always watching out for "code smells" -- things that make working with the code difficult. This is very important because you don't want to keep moving forward with code that's difficult to work with and extend. This is where refactoring comes in. It's a way to head off problems by reorganizing your existing code so that it's easier to work with going forward.

    In all my years of developing software, this is the process that has made the most sense to me."

    and this

    " I would ask myself questions about what somebody using the program might want to achieve. Then I'd ask questions about what kind of information they'd have to provide to the program to achieve that goal. I'd draw out a dialog between the user and the system, kind of like how people producing movies create storyboards so they have a better idea of how they're going to shoot scenes, what it's going look like to the viewer, how the scenes support the plot and form the arc of the story.

    When you think about all these high-level ideas first, it helps you see which low-level details are really important. Contrast that with the way you seem to be heading."

    is what i took away from that last post

    Then I write some test code to document what this idea might look like in code.?

    How? im sorry, im just not with you, i seem lost here

     
    Junilu Lacar
    Marshal
    Posts: 14039
    234
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Eric Flynn wrote:This?
    ...
    Then I write some test code to document what this idea might look like in code.?

    How? im sorry, im just not with you, i seem lost here


    That's Ok. This is good because it helps me get a feel for what gaps you'll need help filling. Sometimes it's hard for more experienced people to see where those gaps are and wrongly assume that the less experienced would be able to fill those gaps. To me it seems like common sense even though to you it makes no sense.

    So you have a pretty good outline of the story. Let's start laying it out in code. Forget about tests for now. That will come later.

    The first "scene" in your script is this: "When the program starts the user is told "Welcome to donut seller software"

    What does that look like in code? Write it out and show us.

    The second scene is "presenting the main menu". Again, write this out as code. Now, if this sounds like you're just going to end up with the same program you wrote before, stop for a minute. Remember my earlier comment about duplication in the statements that display the menu? Think about that when you're coding this section out.
     
    Eric Flynn
    Greenhorn
    Posts: 24
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ok I get what you saying about the menu display,that can be put in a loop, but that would have to go into a method
    callMainMenu or something so that the variables donutName and donutPrice can be fed into the method so that the loop can use these for each donut type

    also the welcome line,could that just be put into the main method? or do i need a method for that in the Donut class
    welcomeUser for example

    This is the problem I am having with design, i just cant work out what goes where and I know objects need to be made from classes, but where do specific responsibilities lie within which classes

    for example when objects are created with this code:

           

    is this done in the main method, or somewhere else, it all seems very confusing at this stage










    Staff note (Junilu Lacar):

    Please don't make drastic edits to your post so as to change the complete thought and render any replies to it nonsensical. I have reverted to your previous version because it will help others who share the same kind of doubts.

     
    Junilu Lacar
    Marshal
    Posts: 14039
    234
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This is what I meant when I said don't be afraid to fail. The idea that displaying the menu in a loop and it should be in its own method didn't come from nowhere, did it? It came from seeing something you did,p then realizing there's a better way. The process of moving from a poor way of coding something to a better way is called refactoring. I do it all the time. In fact, I'll often code something in a suboptimal way on purpose so I can see the path to a better way more clearly.

    There are many aspects of design you need to learn and apply. For now, just use these rules of thumb:

    1. The code should clearly express its intent. That is, it tells its story well.
    2. There is no duplication, like what your menu display had.

    The code you write doesn't have to follow these rules right away. Just write the code, then see if they violate these rules, then refactor to get the code in line. Let's do that greeting message first. Show us your code that might need some refactoring.
     
    Eric Flynn
    Greenhorn
    Posts: 24
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    I dont think the main menu can be in the donut class, but if its in the main class I have to instantiate an object to use it, i could make it static but i know this is not the way.
     
    Junilu Lacar
    Marshal
    Posts: 14039
    234
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Let's focus. Frame your mind first.

    1. You have an idea: The program should display a greeting
    2. You write the code
    3. You run the code and see that it does what you thought it should do.
    4. Make sure the code follows rules for good design.

    You have done 1 & 2. I assume you've done 3. You just haven't done 4.

    Now, think of the above process like this:
    1. Formulate a theory (by writing "the script")
    2. Set up an experiment in code (by writing it in Java)
    3. Run the experiment and see the results (by testing it)
    4. Refactor (by following the rules of good design)

    We'll do this over and over in small increments until we're done, with some refinement once you get a hang of this way of developing.
     
    Junilu Lacar
    Marshal
    Posts: 14039
    234
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Now, assuming you've run the code and saw that it displayed what you expected it to display, we can certainly try to refactor. What would be a better way to do this? Well, we always say around here that MainIsAPain (←that's a link), so how about this:


    You might think this too much, going from one line of code to seven lines of code to do basically the same thing. But this is the start of the process of functional decomposition. To stick with the movie production analogy, a script isn't just an endless series of lines, right? It might be divided up into major sections like the opening scene, scene 1, scene 2, etc. Moving statements out to their own small methods is like organizing sections of a script into scenes. This helps us tell a clearer story (rule #1).
     
    Junilu Lacar
    Marshal
    Posts: 14039
    234
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    One advantage of functional decomposition is that it helps you quickly find where something is being done in the code. Imagine all your code in a main() method that has 200 lines of code in it. Then say you want to change something, like say the greeting. It's much more intuitive to think "Well, I need to change the showGreeting() method" than "I need to change the main() method." What you've actually done there is given the statement System.out.println("Welcome to our Donut shop!"); a well-named intent: to show the greeting that users will see when they start the program.

    Now, think about the code that will display the menu, what method do you think that makes sense to put it in?
     
    Junilu Lacar
    Marshal
    Posts: 14039
    234
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If you keep doing this, the start() method becomes your "table of contents" or "story map" for everything that happens in the program. It becomes the place where you see the high-level flow of the program. From there, it should be easy to see what all the other parts of the program are, what they do to help tell the whole story.
     
    Eric Flynn
    Greenhorn
    Posts: 24
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    ok so i will apply that to the main menu

    but i still think i am missing a lot regarding the actual workings of oop as i still dont know where the menu should be placed
    it cant be in donut, because every object will then have a menu, which is obviously what i dont want

    i understand what you are saying and it does make a lot of sense, i think im missing that "bigger picture" of how things interact
    sorry if i cant convey it well to you.
     
    Campbell Ritchie
    Marshal
    Posts: 65782
    250
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Eric Flynn wrote:. . . I dont think the main menu can be in the donut class, but if its in the main class I have to instantiate an object to use it, i could make it static but i know this is not the way.

    That sentence suggests a few things to me:-
  • 1: Maybe you need another class whose methods will display that menu, or maybe you already have such a class. That sprang to mind in one second.
  • 2: Somebody has told you there is such a concept as a “main class”.
  • I don't go for the concept of a “main class” at all. I know that NetBeans by default calls the class with the main() method “Main”, but that is a different story. It makes me think there is a class which does all the work, but that sounds like a “God class”, which is a notorious anti‑pattern. It is almighty, does everything, and really turns all your programming into a mess.
    Maybe the shop classobject can display a menu. Maybe a customer comes to the shop and sets up a purchase and the purchase object displays the menu. Maybe something different. If you use a purchase object, you may have to allow for a purchase of no doughnuts at all. Maybe the customer says, “II actually wanted one with coffee icing.” This is different from when I was little, and doughnuts always had red jam in. Red was its flavour as well as its colour.
    As Junilu said, you have displayed a welcome greeting, which is like the sign on the shop door being turned from CLOSED to OPEN. Maybe a customer opens that door, walks up to the counter and asks, “What flavour doughnuts do you have today?” The assistant reads out the menu, but you will display it on screen: not much difference. Try it as an experiment; if you use JShell and it doesn't look good, you can turn your terminal off and completely destroy the evidence
     
    Junilu Lacar
    Marshal
    Posts: 14039
    234
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Eric Flynn wrote:but i still think i am missing a lot regarding the actual workings of oop as i still dont know where the menu should be placed
    it cant be in donut, because every object will then have a menu, which is obviously what i dont want


    Don't worry about that so much. In fact, you should embrace your confusion! That will make it all the more satisfying when you finally get it. Remember, the confusion you have is par for the course. In fact, with you realizing that a Donut shouldn't be responsible for displaying a menu, I think you may already be a stroke or two under compared to most other students we've seen come through here.
     
    Carey Brown
    Saloon Keeper
    Posts: 6227
    58
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Eric Flynn wrote:

    This is a step in the right direction but individual Donut variables will not facilitate looping, which is what you'll want for the menu display. If we assume that for now that the available list of Donuts for sale is not going to change, then it can be implemented as a DonutShop constant.
    Note that 'price' would ideally be a BigDecimal but for your exercise a double would suffice. Please don't use floats though, they are mostly used to interface with legacy code and most modern Java code uses doubles instead.
     
    See ya later boys, I think I'm in love. Oh wait, she's just a tiny ad:
    Java file APIs (DOC, XLS, PDF, and many more)
    https://products.aspose.com/total/java
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!