This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Building Blockchain Apps and have Michael Yuan on-line!
See this thread for details.
Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization 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
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Trying to get user input

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to get user input in miniStatement method through getters and setters. This code runs and compiles but it doesn't print anything from miniStatement method.



Here is the output of the above code:-
https://drive.google.com/file/d/1kE7ElBuOItmXtMSfdDYhf21-G7yCcbyq/view?usp=drivesdk

What am I doing wrong here?  Can anybody throw some light on it?
 
Marshal
Posts: 68135
258
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can see other things:
  • Your indentation is inconsistent; you are changing from K&R indentation to Allman indentation and back surprisingly quickly. Inconsistent indentation will confuse you into misreading your own code. Also always precede { at the end of the line by whitespace.
  • Your cash() method is no use to the other code. It doesn't tell other code whether you have any money, and the hard‑coded amount may become inaccurate.
  •  
    Manish Pamnani
    Ranch Hand
    Posts: 57
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:I can see other things:

  • Your indentation is inconsistent; you are changing from K&R indentation to Allman indentation and back surprisingly quickly. Inconsistent indentation will confuse you into misreading your own code. Also always precede { at the end of the line by whitespace.
  • Your cash() method is no use to the other code. It doesn't tell other code whether you have any money, and the hard‑coded amount may become inaccurate.


  • Thanks for helping me out,  it seems my code had some minor errors to be taken care of.  Here's my code which I changed as per your advice:-



    Now this code seems to be running fine and gives me the desired output!

    And thanks for updating me with the knowledge of difference between K&R &  Allman indentation!
     
    Manish Pamnani
    Ranch Hand
    Posts: 57
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carey Brown wrote:I don't see where you are calling miniStatement().



    Thanks for helping me out!
     
    Carey Brown
    Saloon Keeper
    Posts: 6948
    65
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    FYI - Java convention
    If you have a field called "userName" then the getter would be called "getUserName()" and the setter "setUserName()". Note the upper case "U".
     
    Manish Pamnani
    Ranch Hand
    Posts: 57
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I want to make a ATM application which prints username and withdraws money from bank.  Basically it performs all the performs all the operations a bank ATM does!



    But when I try to print username in miniStatement method but it gives me following output:-
    https://drive.google.com/file/d/1qeoHfi6Y6gOt7bQA7nU34KAxJYVA4imK/view?usp=drivesdk

    Basically it does not prints the username,  but it should.
    So what am I missing here?
     
    Carey Brown
    Saloon Keeper
    Posts: 6948
    65
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Look at every place your variable "three" is used. None of them are calling setuserName().
     
    Carey Brown
    Saloon Keeper
    Posts: 6948
    65
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You are creating three totally distinct ATM objects: one, two, and three. Setting a user name in one has no effect on two or three.
     
    Manish Pamnani
    Ranch Hand
    Posts: 57
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Ganesh Patekar wrote:Manish Pamnani,
    I have merged your newly created topic into this topic to maintain it's integrity. Please don't create multiple threads for the same problem. I hope that helps.



    Thanks for helping me out.  As I am newbie so it's a bit difficult to gain knowledge about small things.  So,  thanks for helping me out and merging the topic to maintain it's integrity!
     
    Manish Pamnani
    Ranch Hand
    Posts: 57
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carey Brown wrote:Look at every place your variable "three" is used. None of them are calling setuserName().



    Here's what I did after your suggestion :-



    It seems you were right,  when I was using the object "three"  I wasn't using it to call the method miniStatement()  and when I did that it worked probably fine.

    So,  another beginner question(I don't know whether I should ask it or not!)  if I want to create new users then everytime I have to call the method miniStatement with a new object?
     
    Carey Brown
    Saloon Keeper
    Posts: 6948
    65
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Not know what your exact requirements are, I have these thoughts:
    Your ATM class is more like an AtmAccount class.
    As such you can't have an account without a user name, a starting balance, and a pin number. So the constructor should have those three parameters in it so that these can't default. Without this constructor you end up doing this:
    Which is prone to errors. It would be better to write:
    Whether or not you call miniStatement() depends on what you want to see for output.

     
    Carey Brown
    Saloon Keeper
    Posts: 6948
    65
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You probably want to make an ATM.toString() method that returns a formatted String instead of printing it. Format it to return the equivalent of:
    Then your miniStatment() be simply
     
    Bartender
    Posts: 1248
    86
    Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Manish Pamnani wrote:if I want to create new users then everytime I have to call the method miniStatement with a new object?

    Not only method miniStatement() but also all other setter and getter methods too. When you create a new object which will have It's own separate fields like userName, userBalance etc. you have to assign values to these fields by invoking methods like setuserName(" userName"), setatmPin(...) etc  on each object referred by one, two and three reference variables.
    If you don't assign values to these fields of each objects referred by one, two and three then they will have their default values in Java like int = 0 and String will be Null


    In your last post your code looks like this in pictorial form, see If you can understand this by an illustration.
    Blueprint-of-ATM-class.jpg
    [Thumbnail for Blueprint-of-ATM-class.jpg]
    Blueprint of ATM class
    ATM-class-objects.jpg
    [Thumbnail for ATM-class-objects.jpg]
    ATM class's object with respective fields values
    Staff note (Ganesh Patekar):

    Please excuse my sloppy handwriting.

     
    Ganesh Patekar
    Bartender
    Posts: 1248
    86
    Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Manish Pamnani wrote:

    We know field userName of object referred by one is set to "Manish" so prints Manish but you haven't set any values to other fields of object referred by one hence If you try to print any other fields of this object by invoking getter method on reference variable one like both prints 0 since we haven't set any values to these variables so they have default value 0 because atmBalance and atmPin are of int type.

    Each object have their own fields so you have to set them by invoking appropriate setter methods, to get values invoke getter methods and other method too like miniStatement and cash too on every object you create.

    As Carey Brown suggested It's good to use constructor to initialize these fields and use toString method to print output.

    If you haven't understood yet please go through The Java™ Tutorials: What Is an Object and What Is a Class?  

     
    Ganesh Patekar
    Bartender
    Posts: 1248
    86
    Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Nitpicking:
  • Method name starts with a small letter followed by camel case hence setter method setatmPin(...) would be better setAtmPin(...). Worth reading Naming Conventions
  • Variable name though It's a method parameter name better have meaningful name, here you have parameter named a in method public void setatmPin(int a), could be something else like pin Or atmPin.

  • while setting value in setter method like above It's good habit to use this keyword which refers to the current object who invoked this method. When you have method as In above method we might think we assigned value to the field atmPin of object who invoked this method which is declared at class level but in reality we have assigned passed value again to the same parameter atmPin which hides field atmPin. To avoid such mistakes you can use this keyword.

  • I'm not convinced why there is an instance variable for ATM balance i.e. private int atmBalance; since It better be shared among all objects of ATM we create.

  • Suppose we have an ATM whose balance is 10000 at the beginning.
    This is what I would expect
    1. ATM object referred by one ( User one ) withdraws 2000 then ATM balance should be 10000 - 2000 = 8000
    2. ATM object referred by two ( User two ) withdraws 3000 then ATM balance should be 8000 - 3000 = 5000
    3. ATM object referred by three ( User three ) withdraws 500 then ATM balance should be 5000 - 500 = 4500

    don't you think so?
     
    Ganesh Patekar
    Bartender
    Posts: 1248
    86
    Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This is an example how you can use constructor to initialize those fields and override public String toString() method of Object class to return and print a string that "textually represents" this object i.e. an object of Test class.

    Output:
    Test code: 101 Test Name: Primary
    Test code: 102 Test Name: Secondary

    Go through The Java™ Tutorials: Providing Constructors for Your Classes to know more about constructors.
     
    Manish Pamnani
    Ranch Hand
    Posts: 57
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Ganesh Patekar wrote:

    Manish Pamnani wrote:

    We know field userName of object referred by one is set to "Manish" so prints Manish but you haven't set any values to other fields of object referred by one hence If you try to print any other fields of this object by invoking getter method on reference variable one like both prints 0 since we haven't set any values to these variables so they have default value 0 because atmBalance and atmPin are of int type.

    Each object have their own fields so you have to set them by invoking appropriate setter methods, to get values invoke getter methods and other method too like miniStatement and cash too on every object you create.

    As Carey Brown suggested It's good to use constructor to initialize these fields and use toString method to print output.

    If you haven't understood yet please go through The Java™ Tutorials: What Is an Object and What Is a Class?  



    Here is the code i updated as per your suggestions:


    1)I also assigned values to every field of the object.
    2)I also used this keyword to call the current object instead of using getters and setters(which made my life a lot easier)
    3)I changed the method names into camel case as per naming conventions.

    but i couldn't figure out a way to make the atmBalance static so that it could be shared among all objects of ATM we create?
     
    Manish Pamnani
    Ranch Hand
    Posts: 57
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Manish Pamnani wrote:. . . but i couldn't figure out a way to make the "atmBalance" variable static so that it could be shared among all objects of ATM we create?



    I tried the following methods to make atmBalance static :-
       a) Changed int AmtWithdrawn to int AmtWithdrawn = 20000;
       b) Changed int AmtWithdrawn to public int AmtWithdrawn
    but couldn't get the results.
     
    Campbell Ritchie
    Marshal
    Posts: 68135
    258
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Don't quote the whole of a previous post, and don't put your new text inside the quote tags. I have corrected that for you, and removed most of the quoted text which is visible in your previous post. Who wants to see the same thing twice?

    I don't think you have followed Ganesh's suggestions at all. He showed you how to make the fields private, which you haven't done. He didn't show anything static. Why would you want something to be static? Unless you have a good reason to make something static, I work on the assumption that it is a mistake. Another thing Ganesh did was to simplify the code. Rather than showing you six fields as you have, he showed you two fields. He also showed you how to initialise those fields via the constructor. Start by reducing your class to two fields. User name and user balance maybe. As Ganesh said, give your class a toString() method. Then you can write...which is much more object‑oriented than trying to get the values directly from the fields of the ATM object. Get that working, and once you are happy that is working, add another field. Add it to the ATM object, to its constructor, to its toString() method, and everywhere it is needed.

    Are you trying to work out the total amount of money in the machine and stop withdrawals when the machine is empty? Why do you have one class? People do not simply withdraw money from the ATM. They withdraw money from their accounts and the ATM gives them the same amount of cash. The account and the ATM are separate. If have £1000 in my account and the ATM contain £10000, then I cannot withdraw all that £10000, only £1000 or the maximum daily withdrawal. If there is only £50 in the ATM, even though the money is still mine and I am still entitled to spend it, the ATM won't give me more than £50 if I request £200; after that it won't let anybody else have any more money. It won't say, “You haven't got any money,” but, “Machine not in service.” That denotes the ATM being completely separate from my account. Maybe if I walk a quarter‑mile I can find a different ATM which has got money in and I can get my £200.
     
    Ganesh Patekar
    Bartender
    Posts: 1248
    86
    Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
  • Seems you've not gone through the naming convention link posted in previous post. Field's name starts with first letter in lowercase ... hence UserBalance; better be userBalance; etc.
  • It's not mandatory to name parameters similar to the fields, could be something else, although named different better use this keyword while using instance field.
  • As CR already pointed about private and toString ...
  • Avoid writing too long lines else at least break them. Please go through Dont Write Long Lines

  • Probably I misunderstood OP's question and assumed there is only one ATM machine where multiple users make transactions so advised having static int atmBalance; to share current ATM balance among objects.

    @CR
    Agreed It makes no sense to have only one class, better have separate class for ATM as well as for user ...  perhaps need to rectify application design but before that need to know what OP's exact requirements are ...
     
    God is a comedian playing for an audience that is afraid to laugh - Voltair. 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!