Results 1 to 2 of 2
  1. #1
    Ben_Dover is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default Help using a method from another class

    So I've got three class files, each named: Saldo.java, Person.java and SaldoDemo.java

    So I've created an Object from the Person class in SaldoDemo, but how do I then use a method called "withdrawal" from Saldo on this Object?

    Person.java
    Java Code:
    package test;
    
    public class Person {
    
        private String name;
        private Saldo Saldot;
        private Saldo withdraw;
        
        public Person(String initialName, Saldo initialSaldo)
        {
            name = initialName;
            Saldot = initialSaldo;
        }
    
        public Person(Person original)
        {
            if (original == null)
            {
                System.out.println("Fatal error. 1");
                System.exit(0);
            }
    
            name = original.name;
        }
    
        public String toString()
        {
            return (name + " has " + Saldot + " euro.");
        }
    
        public boolean equals(Person otherPerson, Saldo otherSaldo)
        {
            if (otherPerson == null)
                return false;
            else
                return (name.equals(otherPerson.name));
        }
    
        public void setName(String newName)
        {
            name = newName;
        }
    }
    Saldo.java
    Java Code:
    package test;
    
    public class Saldo {
    
        private double Saldot = 0;
        private double withdraw;
        private double inSet;
    
        public Saldo(double Saldot)
        {
            System.out.println("Your Saldo is currently: " + Saldot + " euros.");
        }
    
        public void set(double newSaldo)
        {
            Saldot = newSaldo;
        }
    
        public void inSet(double inSet)
        {
            Saldot = Saldot + inSet;
            System.out.println("Registrated. 1");
        }
    
        public void withdrawal(double withdraw)
        {
            Saldot = Saldot - withdraw;
            System.out.println("Registrated. 2");
        }
    
        public void setSaldo(double Saldot)
        {
            this.Saldot = 0;
        }
    
        @Override
        public String toString()
        {
            return ("" + Saldot);
        }
    
        public void set(Person newName, double newSaldo)
        {
            name = newName;
            Saldot = newSaldo;
        }
    }
    SaldoDemo.java
    Java Code:
    package test;
    import java.util.Scanner;
    
    public class SaldoDemo {
        public static void main(String[] args) {
            String enterName;
            double enterSaldo, enterWithdraw;
    
            Scanner scan = new Scanner(System.in);
            System.out.println("Type in your name, please.");
            enterName = scan.nextLine();
            System.out.println("Type in how much money you have on your account.");
            enterSaldo = scan.nextDouble();
    
            Person Test =
                    new Person(enterName,
                        new Saldo(enterSaldo));
            System.out.println("The Person " + enterName + " has " + enterSaldo + " euro on his account.");
    
            enterWithdraw = scan.nextDouble();
    
            Test.withdrawal(enterWithdraw); // Problem here.
        }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You could either give Person a public getSaldo() method, and then call withdrawal on the Saldo object returned, or you could give Person a public withDrawFromSaldo(double amount) and then inside of this method call the private Saldo's withdrawal method. I favor the second solution as it only exposes what is necessary -- it does better at encapsulation.

Similar Threads

  1. Replies: 2
    Last Post: 05-01-2010, 08:59 AM
  2. Replies: 3
    Last Post: 02-09-2010, 05:22 AM
  3. Child-Class Calling a Method in a Parent-Class
    By Blah_ in forum New To Java
    Replies: 5
    Last Post: 09-29-2009, 02:48 AM
  4. Replies: 29
    Last Post: 09-25-2008, 07:55 PM
  5. Calling a method on original class from created class
    By kpedersen in forum Advanced Java
    Replies: 4
    Last Post: 08-20-2008, 12:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •