Results 1 to 4 of 4
  1. #1
    r1b
    r1b is offline Member
    Join Date
    Jan 2012
    Posts
    31
    Rep Power
    0

    Default Accesser and mutator for class

    can any one suggest how I would create the accessor if I was to make the dateCreated variable private? I also need the date to datestamp and not be editable after been called.

    Java Code:
    import java.util.*;
    
    public class Account {
    
    
    private int id;
    private String name;
    private double balance;
    private int pinNumber;
    private boolean isActive;
    Date dateCreated = new Date();
    private double annualInterestRate;
    
    
    
    public Account (int a, String b ) {
    
    int id = a;
    String name = b ;
    double balance;
    int pinNumber = 1386;
    boolean isActive = false;
    Date dateCreated = new Date();
    double annualInterestRate = 4.4;
    
    }
    
    // no agr constructor
    public Account (){};
    
    
    
    protected void  setpinNumber (int input) {
    pinNumber = input; }
    
    
    protected int getpinNumber () {
    return pinNumber;
    }
    
    
    protected boolean testpinNumber ( int entry) {
    
    if ( pinNumber == entry  ) {
    isActive = true ;
    }
    else
    {
    System.out.println (" Please re-enter your PIN number");
    isActive = false;
    
    }
    return isActive;
    
    }
    
    
    protected int getid () {
    return id;}
    
    
    protected String getname () {
    return name;}
    
    
    protected void setBalance ( double c) {
    	balance = c;}
    
    protected double getbalance () {
    return balance;}
    
    protected boolean isActive () {
    return isActive;}
    
    protected void deposit( double amount )
    	{
    		balance += amount;
    	}
    
            public double withdraw( double amount )
    	{
                    // See if amount can be withdrawn
    		if (balance >= amount)
    		{
    			balance -= amount;
                            return amount;
    		}
    		else
                    // Withdrawal not allowed
                            return 0.0;
    	}
    
    	protected void setannualInterestRate( double rate) {
    
     		annualInterestRate = rate ;
    		}
    
    // int for the 12 months of the year
    
    public double m  = 12;
    
     // method to get the monthly interest rate
    
    	protected  double getmonthlyInterestRate() {
    	 return annualInterestRate / m ;
    
    	    	}
    
    	 protected void setDate (Date day){
    	 	dateCreated = day ;
    	 	}
    
    
    
    
    }
    
    class TestAccountClass {
    
    
    	public static void main (String [] args)
    
    	{
    
    
    // create the object
    
    Account test = new Account (1122 ,"Robert Smith");
    
    //set the annual interest rate
    
             test.setannualInterestRate (4.55);
    
    // set balance of test
            test.setBalance (20000.00);
    
            // Deposit money
    		test.deposit (3000.00);
    
                    // Print current balance
    		System.out.println ("Current balance " +
    			test.getbalance());
    
                    // Withdraw money
    		test.withdraw(2500.00);
    
                    // Print remaining balance
    		System.out.println ("Remaining balance " +
    			test.getbalance());
    
    		System.out.println ("your current monthly interest rate is " +
    			test.getmonthlyInterestRate () );
    
    
    			// Print date of account creation
    		System.out.println ("Your account was created on " +
    			test.dateCreated.toString());
    
    }}
    Last edited by pbrockway2; 01-05-2012 at 12:12 AM. Reason: code tags added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,573
    Rep Power
    12

    Default Re: Accesser and mutator for class

    Add a getDate() method which constructs and returns a new date. The point is you don't want the caller to get their hands on the actual Date instance which is part of the class.

    Date(long)

    -----

    It would help if you formatted code and used the "code" tags. Put [code] at the start of the code and [/code] at the end.

  3. #3
    r1b
    r1b is offline Member
    Join Date
    Jan 2012
    Posts
    31
    Rep Power
    0

    Default Re: Accesser and mutator for class

    Thanks, I'l be aware of the tags in future. I added the getDate () method and seems to work fine.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,573
    Rep Power
    12

Similar Threads

  1. Mutator methods
    By toterpunkt in forum New To Java
    Replies: 8
    Last Post: 02-24-2011, 09:17 AM
  2. Please help with mutator
    By ethemartian in forum New To Java
    Replies: 21
    Last Post: 02-06-2011, 02:57 AM
  3. Mutator method for a calculator
    By Krakatau7 in forum New To Java
    Replies: 15
    Last Post: 01-11-2011, 12:15 AM
  4. Accessor/Mutator Question
    By noble in forum New To Java
    Replies: 4
    Last Post: 02-02-2010, 05:21 AM
  5. mutator method
    By dirtycash in forum New To Java
    Replies: 7
    Last Post: 11-22-2007, 11:29 PM

Posting Permissions

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