Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Several questions about my code

  1. #1
    Junior Member
    Join Date
    May 2013
    Location
    Tilburg
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question Several questions about my code

    Hello,

    The comments in the code are my specified questions about the code. Please help me with finding the proper solution. I searched on the web, but I did not find the right solution for my problem. I couldn't find the right way to implement the information I found on the internet. So that's why I need some people who have more experience with Java who are willing to help me.

    Could you help me please?

    This is my code (with comments/questions):

     
    import java.util.Scanner;
    import java.util.ArrayList;
     
    class Bank{
      double balance;
      //Account account;
      ArrayList<Account>account = new ArrayList<Account>();
      CommandReader commandReader;
     
      Bank(){
        commandReader = new CommandReader();
      }
     
      void run(){
        commandReader.run();  // The commandReader is a class which already should be fine
      }
     
      void enroll(String name){
        if(account.equals(account)){  //Why is this always true? I doesn't want to create two of the same accounts. In the commandReader the input is enroll Name
          System.out.println("already enrolled");
        } else {
          account.add(account); //This does also not work properly! when enroll Name is typed in by the user, there should be enrolled an account with name = Name and balance = 0
        }
      }
     
      void print(String name){
        if(account.equals(account)){
          System.out.println("Account of "+name);
          System.out.println("  balance: "+balance);
        } else {
          System.out.println("no such name "+name);
        }
      }
     
      void printAll(){
        if(account.equals(account)){
          System.out.println(account); //What I want to print here are all the accounts and they should be printed the same as the method above prints the accounts
        }
      }
     
      public static void main(String[] args){
          new Bank().run();
      }
    }
     
    class Account{
      double balance;
      String name;
     
      Account(){
        bank = new Bank(); //Is this necessary?
      }
     
      void user(String name, double balance){
        this.name = name;
        this.balance = balance;  //I think I don't understand it yet. The name and balance is what I want to store of the accounts
      }
    }


  2. #2
    Member jbarke12's Avatar
    Join Date
    May 2013
    Location
    Charleston SC
    Posts
    31
    Thanks
    0
    Thanked 10 Times in 9 Posts

    Default Re: Several questions about my code

    I answered the questions in the code
     
    import java.util.Scanner;
    import java.util.ArrayList;
     
    class Bank{
      double balance;
      //Account account;
      ArrayList<Account>account = new ArrayList<Account>();
      CommandReader commandReader;
     
      Bank(){
        commandReader = new CommandReader();
      }
     
      void run(){
        commandReader.run();  // The commandReader is a class which already should be fine
      }
     
      void enroll(String name){
        if(account.equals(account)){  //Why is this always true? I doesn't want to create two of the same accounts. In the commandReader the input is enroll Name\
    //this is always true because you haven't defined a .equals in your Account Object so it is using the default equals method.  Which is never equal until you pass the same object in.
          System.out.println("already enrolled");
        } else {
          account.add(account); //This does also not work properly! when enroll Name is typed in by the user, there should be enrolled an account with name = Name and balance = 0
        }
      }
     
      void print(String name){
        if(account.equals(account)){
          System.out.println("Account of "+name);
          System.out.println("  balance: "+balance);
        } else {
          System.out.println("no such name "+name);
        }
      }
     
      void printAll(){
        if(account.equals(account)){
          System.out.println(account); //What I want to print here are all the accounts and they should be printed the same as the method above prints the accounts You need to define a toString //method in account to print the information out that you want. 
        }
      }
     
      public static void main(String[] args){
          new Bank().run();
      }
    }
     
    class Account{
      double balance;
      String name;
     
      Account(){
        bank = new Bank(); //Is this necessary?  I would probably pass a bank into the account class to associate the correct bank with the account.  Acount( Bank b){ bank= b;}
      }
     
      void user(String name, double balance){
        this.name = name;
        this.balance = balance;  //I think I don't understand it yet. The name and balance is what I want to store of the accounts
      }
    }

  3. #3
    Junior Member
    Join Date
    May 2013
    Location
    Tilburg
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Several questions about my code

    I don't understand you. I'm a beginner in Java, so I'm not familiair with everything. How do I define the accounts.equals in my Account class? And do you know why my accound.add(account); doesn't work?

    My whole code with some modifications:

    import java.util.Scanner;
    import java.util.ArrayList;
     
    class Bank{
      double balance;
      //Account account;
      ArrayList<Account>account = new ArrayList<Account>();
      CommandReader commandReader;
     
      Bank(){
        commandReader = new CommandReader();
      }
     
      void run(){
        commandReader.run();
      }
     
      void enroll(String name){
        balance = 0;
        /*if(account.equals(account)){
          System.out.println("already enrolled");
        } else {*/
          account.add(user);
        //}
      }
     
      void deposit(String name, int amount){
        if(account.equals(account)){
          balance = balance + amount;
        } else {
          System.out.println("no such name "+name);
        }
      }
     
      void withdraw(String name, int amount){
        if(account.contains(name)){
          balance = balance - amount;
        } else {
          System.out.println("no such name "+name);
        }
      }
     
      void print(String name){
        if(account.equals(account)){
          System.out.println("Account of "+name);
          System.out.println("  balance: "+balance);
        } else {
          System.out.println("no such name "+name);
        }
      }
     
      void printAll(){
        /*if(account.contains()){
          System.out.println(account);
        }*/
        System.out.println(account.toString());
      }
     
      void printRed(){
        if (balance < 0){
          System.out.println(account.toString());
        } else {
          System.out.print("no negative balances");
        }
      }
     
      void interest (double rate){
     
      }
     
      public static void main(String[] args){
          new Bank().run();
      }
    }
     
    class CommandReader{
        Bank bank;
        Account account;
     
        /*
        CommandReader(Bank bank) {
            this.bank = bank;
        }*/
     
        /*CommandReader() {
            bank = new Bank();
        }*/
     
        // @pre: input complies to specified format
        void run(){
            Scanner scanner;
            String command;
            String name;
            int amount;
            scanner = new Scanner(System.in);
            bank = new Bank();
            do{
                command = scanner.next().toLowerCase();
                if (command.equals("enroll")) {
                    name = scanner.next();
                    bank.enroll(name);
                } else if (command.equals("deposit")) {
                    name = scanner.next();
                    amount = scanner.nextInt();
                    bank.deposit(name, amount);
                } else if (command.equals("withdraw")) {
                    name = scanner.next();
                    amount = scanner.nextInt();
                    bank.withdraw(name, amount);
                } else if (command.equals("print")) {
                    name = scanner.next();
                    bank.print(name);
                } else if (command.equals("printall")) {
                    bank.printAll();
                } else if (command.equals("printred")) {
                    bank.printRed();
                } else if (command.equals("interest")) {
                    double rate = scanner.nextDouble();
                    bank.interest(rate);
                } else if (command.equals("stop")) { 
                    // do nothing
                } else {
                    System.out.println("unknown command");
                    // skip rest of line
                    scanner.nextLine();
                }
            } while (! command.equals("stop"));
        }
    }
     
    class Account{
      String name;
      double balance;
     
      Account(Bank b){
        bank = b;
      }
     
      void user(String name, double balance){
        this.name = name;
        this.balance = balance;
      }
    }

  4. #4
    Member jbarke12's Avatar
    Join Date
    May 2013
    Location
    Charleston SC
    Posts
    31
    Thanks
    0
    Thanked 10 Times in 9 Posts

    Default Re: Several questions about my code

    class Account{
      double balance;
      String name;
      Bank bank;
     
      Account(){ //default constructor
        this("", 0.0, null); 
       // bank = new Bank(); //Is this necessary?
      }
    //fully specified constructor
      Account(String name, double balance, Bank b){
       this.name=name;
       this.balance=balance;
       this.bank= b;
      }
     
      void user(String name, double balance){
        this.name = name;
        this.balance = balance;
      }
     //I would normally define getters here and set all my variables to private.  If you do this You will need in isEquals to use a.getName().equals(name); instead of //a.name.equals(name);  this is an example of a getter
    // public String getName(){
    //return name;
    // }
    @Override // This is a string representation of your object
      public String toString(){
      return "Name: " + name+ " balance: " + balance;
    }
    @Override   // This is how you define an isEqual for this particular class.  Instead of it comparing if it is the same instance of a object it will compare the three things in the object.
     
    public boolean equals(Account a){
     if(a.name.equals(name) && a.bank.equals(bank) && a.balance==balance){
     return true;
    } 
     return false;
    }
    }

    This is where I would start with your code for account

  5. #5
    Junior Member
    Join Date
    May 2013
    Location
    Tilburg
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Several questions about my code

    Thank you for your clear explanation! But I have still 1 question, how do I add a new account with a name which is entered by the user and an initial balance of 0? I want to do that in the enroll method in the class Bank.

    This is what I have right now:

    void enroll(String name){
       account.add(account);    //This should add new items to the ArrayList which should be stored stored in class Bank, for a clearer view: see the code I uploaded before
    }

Similar Threads

  1. List of my Java3D Questions, and Proguard questions
    By Zachary1234 in forum Java SE APIs
    Replies: 0
    Last Post: November 16th, 2012, 08:40 PM
  2. Few questions
    By CSUTD in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 11th, 2011, 09:23 PM
  3. Many questions
    By SharpT in forum What's Wrong With My Code?
    Replies: 11
    Last Post: January 18th, 2011, 08:56 PM
  4. A few questions
    By adenverd in forum Java Theory & Questions
    Replies: 3
    Last Post: May 26th, 2010, 03:34 AM
  5. [SOLVED] Some serious questions,
    By Time in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 17th, 2010, 02:52 AM