7 Replies - 300 Views - Last Post: 13 February 2012 - 09:17 AM Rate Topic: -----

Topic Sponsor:

#1 teamsilvamma  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 109
  • Joined: 17-January 12

Help with a Savings Account Program

Posted 07 February 2012 - 08:24 PM

Hi guys,

The code is working just fine so far but it needs to add up the totals from both accounts and I can't figure it out for the life of me. Here is my codes


Driver

/********************************************************************
 * LuisSilvaProg4.java
 * Luis Silva
 * 
 * This is the driver program that outputs objects from the 
 * SavingsAccount.java class
 *********************************************************************/
public class LuisSilvaProg4 {
	
	public static void main( String args[] ){

		
//Recalling information from both objects Saver1 and saver2		
	SavingsAccount saver1 = new SavingsAccount(10002, 2000.00);  
    SavingsAccount saver2 = new SavingsAccount(10003, 3000.00);  
    SavingsAccount.newInterestRate( 0.05 );  
      
    System.out.println( "\nMonthly balances for one year with 0.05 annual interest:\n" );
//Table heading    
    System.out.printf( "%10s%10s%10s%10s%10s\n", "Month", "Account#", "Balance", "Account#", "Balance" );
 	System.out.printf( "%10s%10s%10s%10s%10s\n", "-----", "--------", "-------", "--------", "-------" );  
 	System.out.printf( "%10s%10s%10s%10s%10s\n", "0",saver1.getACCOUNT_NUMBER(), saver1.toString(), 
 			saver2.getACCOUNT_NUMBER(), saver2.toString());  

//for loop to output a year long interest increments 	
    for ( int month = 1; month <= 12; month++ ){

 		switch (month){
 			case 1:System.out.printf( "\n");
 			break;
 			case 2:System.out.printf( "\n");
 			break;
 			case 3:System.out.printf( "\n");
 			break;
 			case 4:System.out.printf( "\n");
 			break;
 			case 5:System.out.printf( "\n");
 			break;
 			case 6:System.out.printf( "\n");
 			break;
 			case 7:System.out.printf( "\n");
 			break;
 			case 8:System.out.printf( "\n");
 			break;
 			case 9:System.out.printf( "\n");
 			break;
 			case 10:System.out.printf( "\n");
 			break;
 			case 11:System.out.printf( "\n");
	 		break;
 			case 12:System.out.printf( "\n");
 			break;
 		}//end switch  

//This outputs the incremented amounts monthly 		
 		String monthLabel = String.format( "%d", month );
 		saver1.addMonthlyInterest(); saver2.addMonthlyInterest();
 		
 		System.out.printf( "%10s%10s%10s%10s%10s\n", monthLabel, 
 		saver1.getACCOUNT_NUMBER(), saver1.toString(), saver2.getACCOUNT_NUMBER(), 
 		saver2.toString());
 		
     }//end for  
   
// After the last month’s printout, compute and display the total of both balances.  
      
    System.out.printf( "%10s%10s\n", "\nFinal balance of both accounts combined:", "######");
      
      
	}//end main      
}//LuisSilvaProg4  





And here is my class program

/**************************************************************
 * SavingsAccount.java
 * Luis Silva
 * 
 * Class file to calculate 5 percent interest in a bank accnt
 **************************************************************/
public class SavingsAccount {
	
	private static double annualInterestRate = 0;  
    int ACCOUNT_NUMBER; // instance constant  
    double Balance = 0.0; // instance variable 
      
//This sets the interest rate to 5%	
    public static void newInterestRate( double interestRate ){
  		annualInterestRate = .05;
  	}//end newInterestRate
    
//Two-parameter constructor for instance constant & instance variable  
    public SavingsAccount(int account, double balance) {  
        this.ACCOUNT_NUMBER = account;  
        this.Balance = balance;  
    }//end constructor 
        
//Accessors for the instance constant and instance variables    
    public int getACCOUNT_NUMBER(){  
        return ACCOUNT_NUMBER;   
    }//end accessor1  
        
    public double getBalance(){  
        return Balance;   
    }//end accessor 2  

    
//formula for the monthly interest rate      
    public void addMonthlyInterest () {  
    	Balance += (Balance  * annualInterestRate / 12);  
    }//end addMonthlyInterest
    
          
//This method makes the format to be printed            
    public String toString(){
		return String.format( "$%.2f", Balance );
    }//end toString
  
}//end class



Thanks fellas or gals ;)

Is This A Good Question/Topic? 0
  • +

Replies To: Help with a Savings Account Program

#2 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1644
  • View blog
  • Posts: 4,126
  • Joined: 14-March 10

Re: Help with a Savings Account Program

Posted 07 February 2012 - 08:31 PM

It is just simple, your account class has getBalance() method, use it to get balance from both account and just add them, something like:
double total = saver1.getBalance() + saver2.getBalance();


Also, what is going on here?????
for ( int month = 1; month <= 12; month++ ){

 		switch (month){
 			case 1:System.out.printf( "\n");
 			break;
 			case 2:System.out.printf( "\n");
 			break;
 			case 3:System.out.printf( "\n");
 			break;
 			case 4:System.out.printf( "\n");
 			break;
 			case 5:System.out.printf( "\n");
 			break;
 			case 6:System.out.printf( "\n");
 			break;
 			case 7:System.out.printf( "\n");
 			break;
 			case 8:System.out.printf( "\n");
 			break;
 			case 9:System.out.printf( "\n");
 			break;
 			case 10:System.out.printf( "\n");
 			break;
 			case 11:System.out.printf( "\n");
	 		break;
 			case 12:System.out.printf( "\n");
 			break;
 		}//end switch 

Is this serious?
Was This Post Helpful? 1
  • +
  • -

#3 illuss  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 49
  • Joined: 14-April 11

Re: Help with a Savings Account Program

Posted 07 February 2012 - 08:34 PM

you can create another method,call it totalBalanceoOnBoth, in your SavingAccounts class that take 4 parameters
public double totalBalanceoOnBoth(int account1, double balanceOfAcc1, int account2, double balanceOfAcc2) {
return balanceOfAcc1 + balanceOfAcc2;
}


This post has been edited by illuss: 07 February 2012 - 08:35 PM

Was This Post Helpful? 0
  • +
  • -

#4 teamsilvamma  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 109
  • Joined: 17-January 12

Re: Help with a Savings Account Program

Posted 07 February 2012 - 09:19 PM

View Postsmohd, on 07 February 2012 - 08:31 PM, said:

It is just simple, your account class has getBalance() method, use it to get balance from both account and just add them, something like:
double total = saver1.getBalance() + saver2.getBalance();


Also, what is going on here?????
for ( int month = 1; month <= 12; month++ ){

 		switch (month){
 			case 1:System.out.printf( "\n");
 			break;
 			case 2:System.out.printf( "\n");
 			break;
 			case 3:System.out.printf( "\n");
 			break;
 			case 4:System.out.printf( "\n");
 			break;
 			case 5:System.out.printf( "\n");
 			break;
 			case 6:System.out.printf( "\n");
 			break;
 			case 7:System.out.printf( "\n");
 			break;
 			case 8:System.out.printf( "\n");
 			break;
 			case 9:System.out.printf( "\n");
 			break;
 			case 10:System.out.printf( "\n");
 			break;
 			case 11:System.out.printf( "\n");
	 		break;
 			case 12:System.out.printf( "\n");
 			break;
 		}//end switch 

Is this serious?




smohd,

Thanks, I can't believe it. It's always the easiest things that escapes me.

The switch statement is to make the 12 month compound into a table \n draws spaces . DO you have a better idea?
Was This Post Helpful? 0
  • +
  • -

#5 Sheph  Icon User is online

  • D.I.C Addict
  • member icon

Reputation: 294
  • View blog
  • Posts: 777
  • Joined: 12-October 11

Re: Help with a Savings Account Program

Posted 07 February 2012 - 09:32 PM

Your decision tree, has the same result for all 12 months. The switch statement accomplishes the same exact thing for all possible cases and so it really accomplishes nothing. You might as well just put System.out.println(); and replace the whole switch statement, unless you are planning to add more stuff.
Was This Post Helpful? 1
  • +
  • -

#6 teamsilvamma  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 109
  • Joined: 17-January 12

Re: Help with a Savings Account Program

Posted 07 February 2012 - 09:38 PM

View PostSheph, on 07 February 2012 - 09:32 PM, said:

Your decision tree, has the same result for all 12 months. The switch statement accomplishes the same exact thing for all possible cases and so it really accomplishes nothing. You might as well just put System.out.println(); and replace the whole switch statement, unless you are planning to add more stuff.



You know what??? I just took it off and the strangest thing happened!.....It works just fine! HA HA HA...Yeah, I was thinking too much I think. I was planning on formatting other stuff in there but I started to go a different route and then since it was working, I thought that it was needed.

Thanks. It works perfectly now
Was This Post Helpful? 0
  • +
  • -

#7 mikeyh  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 08-February 12

Re: Help with a Savings Account Program

Posted 08 February 2012 - 02:02 PM

Is there a specific reason you chose to go with this to format?

//This method makes the format to be printed           
public String toString(){
    return String.format( "$%.2f", Balance );
}//end toString



You can get rid of that all together in the class by adding these two things to the main:

import java.text.*;	//<-------ADD THIS

public class LuisSilvaProg4{

	public static void main(String args[]){
		SavingsAccount saver1 = new SavingsAccount(10002, 2000);
    	        SavingsAccount saver2 = new SavingsAccount(10003, 3000);
         	SavingsAccount.newInterestRate(0.05);
		
		DecimalFormat df = new DecimalFormat("#.00"); //<----ADD THIS



Then just replace all the:
saver#.toString()



With:
df.format(saver#.getBalance())



Like this:
saver1.addMonthlyInterest(); saver2.addMonthlyInterest();
    System.out.printf( "%10s%10s%10s%10s%10s\n", monthLabel,
       saver1.getACCOUNT_NUMBER(), df.format(saver1.getblance()), saver2.getACCOUNT_NUMBER(),
          df.format(saver2.getBalance());



Your way works but just a thought...
Was This Post Helpful? 0
  • +
  • -

#8 teamsilvamma  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 109
  • Joined: 17-January 12

Re: Help with a Savings Account Program

Posted 13 February 2012 - 09:17 AM

View Postmikeyh, on 08 February 2012 - 02:02 PM, said:

Is there a specific reason you chose to go with this to format?

//This method makes the format to be printed           
public String toString(){
    return String.format( "$%.2f", Balance );
}//end toString



You can get rid of that all together in the class by adding these two things to the main:

import java.text.*;	//<-------ADD THIS

public class LuisSilvaProg4{

	public static void main(String args[]){
		SavingsAccount saver1 = new SavingsAccount(10002, 2000);
    	        SavingsAccount saver2 = new SavingsAccount(10003, 3000);
         	SavingsAccount.newInterestRate(0.05);
		
		DecimalFormat df = new DecimalFormat("#.00"); //<----ADD THIS



Then just replace all the:
saver#.toString()



With:
df.format(saver#.getBalance())



Like this:
saver1.addMonthlyInterest(); saver2.addMonthlyInterest();
    System.out.printf( "%10s%10s%10s%10s%10s\n", monthLabel,
       saver1.getACCOUNT_NUMBER(), df.format(saver1.getblance()), saver2.getACCOUNT_NUMBER(),
          df.format(saver2.getBalance());



Your way works but just a thought...



That is actually interesting. I'll save the stubs for future reference. Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1