Results 1 to 1 of 1
  1. #1
    felito is offline Member
    Join Date
    Nov 2010
    Posts
    48
    Rep Power
    0

    Default change value of arraylist

    Hi guys

    i have a problem that's making me go crazy

    So, i have a script that doing a search in an arraylist and i want, if a account exists, the user have the possibility to deposit or withdraw in respective account.

    But i have a problem with statics references, !

    Java Code:
    package sistema;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Scanner;
    
    public class Bank {
    
    	static List <Account> lista = new ArrayList<Account>();
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		showAll();
    		deposit1();
    	}
    
    
    	public static void deposit1() {
    		// TODO Auto-generated method stub
    		Scanner sc = new Scanner(System.in);    
    		System.out.println("Digite o nome da conta:");    
    		String find = sc.nextLine();
    
    		Account<?> ac = findByName(find);
    		
    		if(ac == null) {
    			System.out.println("Account does not exist");
    		} else {
    			System.out.println("Found account! " + ac);
    			Scanner sc1 = new Scanner(System.in);    
    			System.out.println("Digite a quantia a levantar:"); 
    			Double scan = sc1.nextDouble();
    			Account.withdraw(scan);
    
    		}
    	}
    
    	private static Account<?> findByName(String name) {
    		// TODO Auto-generated method stub
    		for(Account<?> ac :lista) {
    			if(ac.getAccountName().equals(name)) {
    				return ac;
    			}
    		}
    		return null;
    	}
    
    
    
    
    	public static void showAll() {
    		// TODO Auto-generated method stub
    
    		CurrentAccount conta1 = new CurrentAccount("Alberto Carlos", 1052);
    		CurrentAccount conta2 = new CurrentAccount("Pedro Fonseca", 30);
    		CurrentAccount conta3 = new CurrentAccount("Ricardo Vitor", 1534);
    		CurrentAccount conta4 = new CurrentAccount("João Lopes", 3135);
    
    		AccountLongTerm conta11 = new AccountLongTerm("Jacinto Alves", 54502);
    		AccountLongTerm conta12 = new AccountLongTerm("Ana Anabela", 30);
    		AccountLongTerm conta13 = new AccountLongTerm("Carlos Brás", 1234);
    		AccountLongTerm conta14 = new AccountLongTerm("José Fonseca", 545);
    
    		AccountP conta21 = new AccountP("Manuel Jose", 2200);
    		AccountP conta22 = new AccountP("Carla Costa", 3050);
    		AccountP conta23 = new AccountP("Francisco José", 12214);
    		AccountP conta24 = new AccountP("Alberto Teixeira", 31415);
    
    
    		Bank Bank1 = new Bank (); 
    
    		Bank1.addAccount(conta1);
    		Bank1.addAccount(conta2);
    		Bank1.addAccount(conta3);
    		Bank1.addAccount(conta4);		
    		Bank1.addAccount(conta11);
    		Bank1.addAccount(conta12);
    		Bank1.addAccount(conta13);
    		Bank1.addAccount(conta14);
    		Bank1.addAccount(conta21);
    		Bank1.addAccount(conta22);
    		Bank1.addAccount(conta23);
    		Bank1.addAccount(conta24);
    
    		Collections.sort(Bank1.lista);
    
    		System.out.printf("Bank Accounts:" + "%n");
    		Iterator<Account> itr = Bank1.lista.iterator();
    		while (itr.hasNext()) {
    			Account<?>  element = itr.next();
    			System.out.printf(element + " " + "%n");
    		}
    		System.out.println();
    
    
    
    	}
    
    
    
    	public void addAccount(Account<?> conta) {
    		lista.add (conta);
    
    	};
    
    }

    currentAccount (for example), is the same more or less to the others accounts

    Java Code:
    package sistema;
    
    class CurrentAccount extends Account<Object> {
    
    	public CurrentAccount(String accountN, double bal) {
    		super(accountN, bal);
    		// TODO Auto-generated constructor stub
    		rate = 0.01;
    		resultLong();
    	}
    
    	public String toString() {
    		return "Conta à ordem: " + getAccountName() + ", saldo " + balanceMoney.format(getBalance())  + "?" +  ", Juros: " + decimals.format(resultLong) + "?";
    
    	}
    
    	@Override
    	public void deposit(double amount) {
    		// TODO Auto-generated method stub
    
    	}
    
    	@Override
    	public double withdraw(double amount) {
    		// TODO Auto-generated method stub
    		if (getBalance()-amount <0){
    			System.out.println("You don' have suficient founds!");
    		}
    		else {
    			setBalance(getBalance()-amount);
    		}
    
    		return getBalance();
    	}
    
    }
    superclass account

    Java Code:
    package sistema;
    
    import java.text.DecimalFormat;
    
    abstract class Account<T> implements java.lang.Comparable<T> {
    
    	private String accountName;
    	private double balance;
    	protected double resultLong;
    	protected double rate;
    
    	public Account (String accountN, double bal) {
    		// TODO Auto-generated constructor stub
    		setAccountName(accountN);
    		setBalance(bal);
    	}
    
    	public int compareTo(Object o) {
    		// TODO Auto-generated method stub
    
    		if (this.getBalance() > ((Account<?>) o).getBalance()) {
    			return -1;
    		}
    		if (this.getBalance() < ((Account<?>) o).getBalance()) {
    			return 1;
    		}
    		return 0;
    	}
    
    
    	DecimalFormat balanceMoney = new DecimalFormat( " 0" ); 
    	DecimalFormat decimals = new DecimalFormat( " 0.00 " );  
    
    	public double resultLong() {
    		return resultLong = balance*rate;
    	}
    
    	public void setBalance(double balance) {
    		this.balance = balance;
    	}
    
    
    	public double getBalance() {
    		return balance;
    	}
    
    
    	public void setAccountName(String accountName) {
    		this.accountName = accountName;
    	}
    
    
    	public String getAccountName() {
    		return accountName;
    	}
    
    	public abstract void deposit(double amount);
    	public abstract double withdraw (double amount);
    
    }
    thanks for help
    Last edited by felito; 12-12-2010 at 07:51 PM.

Similar Threads

  1. Replies: 7
    Last Post: 11-22-2010, 01:26 PM
  2. Replies: 2
    Last Post: 11-16-2010, 05:51 PM
  3. Creating an ArrayList from an ArrayList
    By Klahking in forum New To Java
    Replies: 17
    Last Post: 09-09-2010, 03:34 PM
  4. Need to change an ArrayList to an Array
    By trojansc82 in forum New To Java
    Replies: 9
    Last Post: 07-25-2010, 08:26 PM
  5. Replies: 2
    Last Post: 04-21-2008, 11:43 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
  •