Results 1 to 5 of 5
  1. #1
    ostkanten is offline Member
    Join Date
    Jul 2012
    Posts
    37
    Rep Power
    0

    Default How do I "export" correct balance?

    I have four classes: Bank with menu and mainmethod, Customer, Account (supercklass) and SavingAccount(subclass). I have a withdrawal fee after the first withdrawel. It works but when I do several withdrawels the withdrawelFee isn´t subtracted from the startingBalance at the next withdrawel. I have tried a lot to get it work and just now my Account and SavingAccount code is as follows (shorted version). What do I have to do to get it work?

    Java Code:
    abstract class Account  {
    	
    	private static int accountCounter = 1001; 
    	protected int accountNumber=1000;
    	protected double balance; 
    	protected String pNumber;
    	protected double outputFee;
    	protected double outputFeeRate;
    	protected int numberOfWithdrawels;
    
    	public Account(){
    		accountNumber = accountCounter++;
    	}
    	
    			
    	public void withdrawAmount (int accountNumber, int amountOut, int antalUttag){
    	}
    	
    	public void countOutputFee(int accountNumber, int amountOut){
    	}
    	
    			
    	/**
    	 * Mutators
    	 */
    	public void setPNumber(String personNummer){
    		pNumber= personNummer;
    	}
    			
    	/**
    	 * Accessorss
    	 */
    	public String getPNumber(){
    		return pNumber;
    	}
    	
    	public int getAccountNumber(){
    		return accountNumber;
    	}
    	
    	public double getBalance(){
    		return balance;
    	}
    	
    	public double getOutputFee(){
    		return outputFee;
    	}
    	
    	public double getOutputFeeRate(){
    		return outputFeeRate;
    	}
    	
    }
    Java Code:
    public class SavingAccounts extends Account {
    
    	private double outputFeeRate=0.01;
    				
    
    	public SavingAccounts (){
    		super();
    	}
    
    	public SavingAccounts (int accountNumber, double startingBalance, String kontoTyp, double interestRate, double outputFeeRate){
    		super();
    		balance = startingBalance;
    	}
    	
    	public void depositAmount (int accountNumber, double amountIn){
    		balance += amountIn;
    	}
    
    	public void withdrawAmount (int accountNumber, int amountOut, int antalUttag){
    		if(amountOut + outputFee > balance){
    			System.out.println("Not enough money.");
    		}
    		else{
    			if(numberOfWothdrawels==0){
    				balance= balance - amountOut;
    				antalUttag = antalUttag +1;
    			}
    			else{
    				balance = balance - amountOut - outputFee;
    			}
    		}
    	}
    	
    	public void countOutputFee(int accountNumber, int amountOut){
    		outputFee = outputFeeRate*amountOut;
    	}
    	
    	/**
    	 * Mutators
    	 */
    		
    	public void setNumberOfWithdrawels(int antalUttag){
    		numberOfWithdrawels = numberOfWithdrawels+1;
    	}
    	
    	public void setBalance(double balance){
    		balance=balance - outputFee;
    	}
    	
    	public String toString(){
    		String info = "AccountNumber: " + accountNumber 
    				+"\nWithdrawelcost at >1 withdrawel/year(1%): " + outputFee
    				+ "\nBalance: " + (balance - outputFee + interest); 
    		return info;
    	}
    }
    Last edited by ostkanten; 09-07-2012 at 09:00 AM.

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,805
    Rep Power
    19

    Default Re: How do I "export" correct balance?

    Write a load of debug code in there (using println()) to see what the values of everything is.
    Then, if you still have a problem, show the new code here along with the output from a run that highlights the problem.

    Note that the above code does not compile:
    Java Code:
    if(numberOfWothdrawels==0){
    there is no 'numberOfWothdrawels' variable.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Nilla is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default Re: How do I "export" correct balance?

    I have put System.out.println("Balance " + balance); or in the Bank class System.out.println("Balance " account.getBalance());
    and the problem is that the balance not is reduced with the outputFee which I thought that

    Java Code:
    countOutputFee(accountNumber, amountOut);
    balance = balance - amountOut - outputFee;
    would fix in the SavingAccount class. In toString() I have written

    Java Code:
    public String toString(){
    	String info = "Balance: " + (balance - outputFee + interest); 
    	return info;
    }
    and it will give the right balance. But then the incoming balance in the next transaction will be wrong. I think I have some problems with set and get methods. My code is now:

    Java Code:
    abstract class Account  {
    	
    	private static int accountCounter = 1001; 
    	protected int accountNumber=1000;
    	protected double balance; 
    	protected double interest;
    	protected double interestRate;
    	protected String pNumber;
    	protected String kontoTyp; 
    	protected double outputFee;
    	protected double outputFeeRate;
    	protected int antalUttag;
    
    	public Account(){
    		accountNumber = accountCounter++;
    	}
    	
    	public void withdrawAmount (int accountNumber, int amountOut, int antalUttag){
    	}	
    		
    	public void countOutputFee(int accountNumber, int amountOut){
    		
    	}
    	
    	/**
    	 * Mutators
    	 */
    	public void setPNumber(String personNummer){
    		pNumber= personNummer;
    	}
    	
    	public void setBalance(double balance, double outputFee){
    		balance=balance - outputFee;
    	}
    	
    	public void setInterest(double ranta){
    		interest=ranta;
    	}
    	
    	/**
    	 * Accessors
    	 */
    	public String getPNumber(){
    		return pNumber;
    	}
    	
    	public int getAccountNumber(){
    		return accountNumber;
    	}
    	
    	public double getBalance(){
    		return balance;
    	}
    	
    	public double getInterest(){
    		return interest;
    	}
    	
    	public double getInterestRate(){
    		return interestRate;
    	}
    	
    	public double getDebtInterestRate(){
    		return interestRate;
    	}
    
    	public double getDebtInterest() {
    		return debtInterest;
    	}
    	
    	public String getKontoTyp(){
    		return kontoTyp;
    	}
    	
    	public double getOutputFee(){
    		return outputFee;
    	}
    	
    	public double getOutputFeeRate(){
    		return outputFeeRate;
    	}
    	
    	public int getAntalUttag(){
    		return antalUttag;
    	}
    }
    Java Code:
    public class SavingAccounts extends Account {
    
    	private double outputFeeRate=0.01;
    	private String kontoTyp = "sparkonto";
    	
    	public SavingAccounts (){
    		super();
    	}
    
    	public SavingAccounts (int accountNumber, double startingBalance, String kontoTyp){
    		super();
    		balance = startingBalance;
    		kontoTyp="sparkonto";
    	}
    	
    	public void depositAmount (int accountNumber, double amountIn){
    		balance += amountIn;
    	}
    
    	public void withdrawAmount (int accountNumber, int amountOut, int antalUttag){
    				
    		if(amountOut + outputFee > balance){
    			System.out.println("There is not enough money.");
    		}
    		else if(amountOut + outputFee < balance){
    			if(antalUttag==0){
    				balance= balance - amountOut;
    				antalUttag = antalUttag +1;
    			}
    			else{
    				countOutputFee(accountNumber, amountOut);
    				balance = balance - amountOut - outputFee;
    			}
    		}
    	}
    	
    	public void countInterest(int accountNumber, double balance, double interestRate){
    		interest = interestRate*balance;
    	}
    
    	public void countOutputFee(int accountNumber, int amountOut){
    		outputFee = outputFeeRate*amountOut;
    	}
    	
    	/**
    	 * Mutator-metod
    	 */
    	public void setKontoTyp(String kontoTyp){
    		kontoTyp = "sparkonto";
    	}
    	
    	public void setAntalUttag(int antalUttag){
    		antalUttag = antalUttag +1;
    	}
    	
    	public void setBalance(double balance){
    		balance = balance - outputFee + interest;
    	}
    	
    	/**
    	 * Accessormetod
    	 */
    	public String getKontoTyp(){
    		return kontoTyp;
    	}
    	
    	public double getBalance(){
    		return this.balance;
    	}
    	
    	/**
    	 * Presentationsmetod
    	 * @return info
    	 */
    	
    	public String toString(){
    		String info = "Sparkontonummer: " + accountNumber 
    				+"\nUttagsavgift vid >1 uttag per år (1%): " + outputFee
    				+ "\nUtgående saldo: " + (balance - outputFee + interest); 
    		return info;
    	}
    }
    This time I have not translated the variablenames so I hope it will be no problem to compile the code.

    In the Bank class the withdrawel method looks lite this:

    Java Code:
    public void withdrawMoney(String kundNummer, int kontoNummer){
    		System.out.println("Vilket belopp vill du ta ut: ");
    		int amountOut = scanner.nextInt();
    		scanner.nextLine();
    		for(Account konto: accounts){
    			if(kontoNummer==konto.accountNumber){
    				int antalUttag =0;
    				System.out.println("Balance in: " + (konto.getBalance()));	
    				if(konto.getKontoTyp().equals("sparkonto")){
    					konto.withdrawAmount(kontoNummer, amountOut, antalUttag);
    					System.out.println("Uttag: " + amountOut + " kronor");
    					System.out.println(konto.toString());
    					if(amountOut<=konto.getBalance()){
    						konto.countOutputFee(kontoNummer,  amountOut);
    						double uttag = amountOut + konto.getOutputFee();
    						if(uttag>konto.getBalance()){
    					        System.out.println("Not enough money.");
    						}
    						else{
    							System.out.println("Uttag: " + amountOut + " kronor");
    							System.out.println("Balance out: " + konto.getBalance());
    						}
    					}
    				}
    		
    				
    			}
    			else if (kontoNummer !=konto.accountNumber){
    				System.out.println("Valt konto existerar inte.");
    			}
    			}
    		}

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,805
    Rep Power
    19

    Default Re: How do I "export" correct balance?

    So put some debug in the code that is supposed to be setting that value correctly.
    That's where I would put it if I thought it wasn't working.
    Presumably it's the 'withdrawAmount' method going wrong?
    So print out everything that's going into there, and which path it takes through that method.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Nilla is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default Re: How do I "export" correct balance?

    I think I have solved this problem now.

Similar Threads

  1. Replies: 21
    Last Post: 04-04-2012, 07:01 PM
  2. Replies: 1
    Last Post: 02-01-2012, 08:37 PM
  3. Replies: 1
    Last Post: 10-03-2011, 03:42 AM
  4. Replies: 8
    Last Post: 03-01-2011, 05:44 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 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
  •