9 Replies - 379 Views - Last Post: 19 March 2012 - 08:01 AM Rate Topic: -----

#1 limozine111  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-March 12

Can't not go back to create new account

Posted 15 March 2012 - 06:28 PM

I have two class, account and accountTest. For some reasons, in my AccountTest class, I use array to search name and if name exists, they will show me where its stored if not they will ask if I want to add new account. So here is my code. How do I go back to the beginning after the name is found(Im thinking about making a loop), and how do I return the total number of accounts.

Account Class

public class Account {
	Person per = new Person();
	Person person;
	String accountName;
	double accountBalance;
	private String accountNumber;
	private static int numAccounts;


public Account(String accountName, double accountBalance, String accountNumber){
this.accountName = accountName;
this.accountBalance = 0;
this.accountNumber = per.getId();
numAccounts++;}

public Account(){
this.accountName = "";
this.accountBalance = 0;
this.accountNumber = per.getId();
;}


public String getaccountName() {
	return accountName;}
	
public int getnumAccounts() {
	return numAccounts;}


public double getaccountbalance(){
	return accountBalance;}


public void withdrawal(double amount) {
	accountBalance -=  amount;}
	
	
//deposit method
public void deposit(double amount){
	accountBalance += amount;}

//inquiry
	public double inquiry(){
	return inquiry();}



public String toString(){
	return "Name: " + accountName +"\nBalance: " + accountBalance;
} 
}




-----------------------------------------------------------------

AccountTest class

import javax.swing.JOptionPane;

public class AccountTest {
	final static int SIZE = 10;
	private static Account[] accountList = new Account[SIZE];

	public static void main(String[] args) {
	
	int count1 = 0;
 			
			while(count1 == 0){
			
			Person per = new Person();
			String names = JOptionPane.showInputDialog("Enter Name");
			double balance = Double.parseDouble(JOptionPane.showInputDialog("Enter Balance"));
			Account ac = new Account(names, balance, per.getId());
			accountList[ac.getnumAccounts() - 1] = ac;
			Account activeAC = new Account();
			
			String name = JOptionPane.showInputDialog("Enter name to search ");
			// search by name
			int foundIndex = findAccount(name);
			if (foundIndex < 0) {JOptionPane.showConfirmDialog(null,"Account was not found. Do you want to create new account?");}
				// if yes go back to the beginning (creating account)
			 else {activeAC = accountList[foundIndex];JOptionPane.showMessageDialog(null, name+ " found at " + foundIndex);}

			
		
							

				// deposit, withdraw.... options
				// Ask for checking or saving
			
			String depositorchecking = JOptionPane.showInputDialog("Checking or Saving?"+ "\nEnter 1 for checking. Enter 2 for saving");
				
if (depositorchecking.equalsIgnoreCase("1")) {
	

				// deposit-----------------------------------
					String amt = JOptionPane.showInputDialog("Enter amount to deposit");
					double amount = Double.parseDouble(amt);
					double dbalance = balance + amount;
					if (amount < 0) {JOptionPane.showMessageDialog(null,"Wrong Input (negative amount)");} 
					else {JOptionPane.showMessageDialog(null, "$" + amount+ " has been deposited"+ "\nYour balance now is: " + "$ " + dbalance);}
					

					// withdraw--------------------------------------
					String amt1 = JOptionPane.showInputDialog("Enter amount to withdraw");
					double amount1 = Double.parseDouble(amt1);
					double wbalance = dbalance - amount1;
					if (amount1 > dbalance) {JOptionPane.showMessageDialog(null,"Insufficient funds");}
					else if (amount1 < 0) {JOptionPane.showMessageDialog(null, "Invalid Input");} 
					else {JOptionPane.showMessageDialog(null, "$" + amount1+ " has been withdrawn"+ "\nYour balance now is: " + "$ " + wbalance);}}
					
				

else if (depositorchecking.equalsIgnoreCase("2")) {
										
					// deposit-----------------------------------
					String amt = JOptionPane.showInputDialog("Enter amount to deposit");
					double amount = Double.parseDouble(amt);
					double dbalance = balance + amount;
					if (amount > 0) {JOptionPane.showMessageDialog(null, "$" + amount+ " has been deposited"+ "\nYour balance now is: " + dbalance);} 
					else {JOptionPane.showMessageDialog(null, "Wrong Input");}}

else JOptionPane.showMessageDialog(null, "Wrong Input");
			count1 = JOptionPane.showConfirmDialog(null, "Do you want to continue");}
	JOptionPane.showMessageDialog(null, "Goodbye");}
				
				

	private static int findAccount(String name) {
		// TODO Auto-generated method stub
		int i = 0;
		while (i < accountList[0].getnumAccounts()) {
			if (name.equals(accountList[i].getaccountName())) {
				return i;
			}
			i++;
		}
		return -1;
		
	} 
	
	
}


	



Is This A Good Question/Topic? 0
  • +

Replies To: Can't not go back to create new account

#2 wrightm96  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 43
  • Joined: 06-March 12

Re: Can't not go back to create new account

Posted 15 March 2012 - 06:44 PM

You have a problem with your handling of the number of accounts. Each time you create an Account, it creates a completely different variable which stores the number of accounts. You should instead move the number of accounts to accountTest, and every time an account is made, increment it by one. Then it would be easy to return.

What do you mean go back to the beginning?
Was This Post Helpful? 0
  • +
  • -

#3 limozine111  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-March 12

Re: Can't not go back to create new account

Posted 15 March 2012 - 06:51 PM

 // search by name
22
            int foundIndex = findAccount(name);
23
            if (foundIndex < 0) {JOptionPane.showConfirmDialog(null,"Account was not found. Do you want to create new account?");}
24
                // if yes go back to the beginning (creating account)
25
             else {activeAC = accountList[foundIndex];JOptionPane.showMessageDialog(null, name+ " found at " + foundIndex);}


in the above code in AccountTest, I want to create a question to ask if I want to go back after I finish searching, adding name, or name is found or I want to use that account and that balance to continue do the transaction deposit or withdraw

View Postwrightm96, on 15 March 2012 - 06:44 PM, said:

You have a problem with your handling of the number of accounts. Each time you create an Account, it creates a completely different variable which stores the number of accounts. You should instead move the number of accounts to accountTest, and every time an account is made, increment it by one. Then it would be easy to return.

What do you mean go back to the beginning?


how do I move number of accounts to accountTest
Was This Post Helpful? 0
  • +
  • -

#4 wrightm96  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 43
  • Joined: 06-March 12

Re: Can't not go back to create new account

Posted 15 March 2012 - 06:59 PM

View Postlimozine111, on 15 March 2012 - 06:51 PM, said:

how do I move number of accounts to accountTest


Move line 7 in Account to AccountTest. Then put statements to increment the value of numAccounts in AccountTest.

You could just add a while loop along with another JOptionPane to go back:

boolean stop = false;
		
		while (!stop)
		{
			//all your code for making/searching/transactions goes here.
			
			//then one more JOptionPane to check if they want to continue
			int cont = JOptionPane.showConfirmDialog(null, "Continue?", "", JOptionPane.YES_NO_OPTION);
			
			if (cont == JOptionPane.YES_OPTION)
			{
				
			}
			else
			{
				stop = true;
			}
		}


Was This Post Helpful? 0
  • +
  • -

#5 limozine111  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-March 12

Re: Can't not go back to create new account

Posted 15 March 2012 - 07:31 PM

here is my new code

import javax.swing.JOptionPane;

public class AccountTest {
	
	final static int SIZE = 10;
	private static Account[] accountList = new Account[SIZE];
	private static int numAccounts;

	public static void main(String[] args) {
	int count1 = 0;
	numAccounts++;
		
	boolean stop = false;
	
	while (!stop)
	{
		//all your code for making/searching/transactions goes here.
		
	
			while(count1 == 0){
			
			Person per = new Person();
			String accountName = JOptionPane.showInputDialog("Enter Name");
			double balance = Double.parseDouble(JOptionPane.showInputDialog("Enter Balance"));
			Account ac = new Account(accountName, balance, per.getId());
			accountList[ac.getnumAccounts() - 1] = ac;
			Account activeAC = new Account();
			
			String name = JOptionPane.showInputDialog("Enter name to search ");
			// search by name
			int foundIndex = findAccount(name);
			if (foundIndex < 0) {JOptionPane.showConfirmDialog(null,"Account was not found. Do you want to create new account?");}
				// if yes go back to the beginning (creating account)
			 else {activeAC = accountList[foundIndex];JOptionPane.showMessageDialog(null, name+ " found at " + foundIndex);}

			
		
							

				// deposit, withdraw.... options
				// Ask for checking or saving
			
			String depositorchecking = JOptionPane.showInputDialog("Checking or Saving?"+ "\nEnter 1 for checking. Enter 2 for saving");
				
if (depositorchecking.equalsIgnoreCase("1")) {
	

				// deposit-----------------------------------
					String amt = JOptionPane.showInputDialog("Enter amount to deposit");
					double amount = Double.parseDouble(amt);
					double dbalance = balance + amount;
					if (amount < 0) {JOptionPane.showMessageDialog(null,"Wrong Input (negative amount)");} 
					else {JOptionPane.showMessageDialog(null, "$" + amount+ " has been deposited"+ "\nYour balance now is: " + "$ " + dbalance);}
					

					// withdraw--------------------------------------
					String amt1 = JOptionPane.showInputDialog("Enter amount to withdraw");
					double amount1 = Double.parseDouble(amt1);
					double wbalance = dbalance - amount1;
					if (amount1 > dbalance) {JOptionPane.showMessageDialog(null,"Insufficient funds");}
					else if (amount1 < 0) {JOptionPane.showMessageDialog(null, "Invalid Input");} 
					else {JOptionPane.showMessageDialog(null, "$" + amount1+ " has been withdrawn"+ "\nYour balance now is: " + "$ " + wbalance);}}
					
				

else if (depositorchecking.equalsIgnoreCase("2")) {
										
					// deposit-----------------------------------
					String amt = JOptionPane.showInputDialog("Enter amount to deposit");
					double amount = Double.parseDouble(amt);
					double dbalance = balance + amount;
					if (amount > 0) {JOptionPane.showMessageDialog(null, "$" + amount+ " has been deposited"+ "\nYour balance now is: " + dbalance);} 
					else {JOptionPane.showMessageDialog(null, "Wrong Input");}}

else JOptionPane.showMessageDialog(null, "Wrong Input");
			count1 = JOptionPane.showConfirmDialog(null, "Do you want to continue");}
	JOptionPane.showMessageDialog(null, "Goodbye");}}
				
				

	private static int findAccount(String name) {
		// TODO Auto-generated method stub
		int i = 0;
		while (i < accountList[0].getnumAccounts()) {
			if (name.equals(accountList[i].getaccountName())) {
				return i;
			}
			i++;
		}
		return -1;
		
	}

	//then one more JOptionPane to check if they want to continue
	int cont = JOptionPane.showConfirmDialog(null, "Continue?", "", JOptionPane.YES_NO_OPTION);  //<---error right here
			
			if (cont == JOptionPane.YES_OPTION)
			{
				
			}
			else
			{
				boolean stop = true;
			}
		}}

	




I still could not run it. Maybe I got error due to the loop but remember the transaction I used is connected with the balance in the searching area so if I make the terminate loop after searching and before transaction, it will not recognize the balance in deposit and withdraw
Was This Post Helpful? 0
  • +
  • -

#6 limozine111  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-March 12

Re: Can't not go back to create new account

Posted 15 March 2012 - 07:40 PM

can you just copy my both codes Account and AccountTest and paste it in ur eclipse software and try to run it ? I know its hard for me to explain because english is not my first language and sometime people get confused to what I say.
Was This Post Helpful? 0
  • +
  • -

#7 wrightm96  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 43
  • Joined: 06-March 12

Re: Can't not go back to create new account

Posted 16 March 2012 - 09:01 AM

Would love to. Would you please post your Person class first?
Was This Post Helpful? 0
  • +
  • -

#8 limozine111  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-March 12

Re: Can't not go back to create new account

Posted 16 March 2012 - 09:37 AM

View Postwrightm96, on 16 March 2012 - 09:01 AM, said:

Would love to. Would you please post your Person class first?


Okay, here is my Person Class. I think I did it wrong with the inquiry in my Account Class when returning but im not sure. can you also check that for me ?

Person Class
import java.text.DecimalFormat;

import java.util.Random;

public class Person {

	private String firstname;

	private String lastname;

	private String middlename;

	private String gender;

	private String id;

	private static int nopopulation;

	/**
	 * 
	 * right click-> source -> generate constructor using field*
	 */

	public Person() {

		this.firstname = "";

		this.lastname = "";

		this.middlename = "";

		this.gender = "";

		this.id = "";

	}

	public Person(String firstname, String lastname, String middlename,

	String gender) {

		super();

		this.firstname = firstname;

		this.lastname = lastname;

		this.middlename = middlename;

		this.gender = gender;

		this.id = generateID();

	}

	/**
	 * 
	 * source -> generate setter and getter
	 */

	public String getFirstname() {

		return firstname;

	}

	public void setFirstname(String firstname) {

		this.firstname = firstname;

	}

	public String getLastname() {

		return lastname;

	}

	public void setLastname(String lastname) {

		this.lastname = lastname;

	}

	public String getMiddlename() {

		return middlename;

	}

	public void setMiddlename(String middlename) {

		this.middlename = middlename;

	}

	public String getGender() {

		return gender;

	}

	public void setGender(String gender) {

		this.gender = gender;

	}

	public String getId() {

		return id;

	}

	public void setId(String id) {

		this.id = id;

	}

	public static int getNopopulation() {

		return nopopulation;

	}

	public static void setNopopulation(int nopopulation) {

		Person.nopopulation = nopopulation;

	}

	@Override
	public String toString() {

		return "Person [firstname=" + firstname + ", lastname=" + lastname

		+ ", middlename=" + middlename + ", gender=" + gender + ", id="

		+ id + "]";

	}

	private String generateID() {

		{

			DecimalFormat df = new DecimalFormat("00000000000000");

			String id;

			Random rand = new Random();

			id = df.format(Math.abs(rand.nextLong())).substring(0, 14);

			return id;

		}

	}

}

Was This Post Helpful? 0
  • +
  • -

#9 limozine111  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-March 12

Re: Can't not go back to create new account

Posted 16 March 2012 - 10:53 AM

Here is the requirement for my project: http://pastebin.com/nQfhhXJL

Here is a part i want to add in my code
Person per = new Person();
	Account ac = new Account("Mike", 500, per.getId());
	JOptionPane.showMessageDialog(null, ac.toString());
	String mike = JOptionPane.showInputDialog(null, "Enter amount to deposit");
	double add = Double.parseDouble(mike);
	ac.deposit(add);
	JOptionPane.showMessageDialog(null, "$ "+ add + " has been deposited");
	String mike1 = JOptionPane.showInputDialog(null, "Enter amount to withdraw");
	double addm = Double.parseDouble(mike1);
	ac.withdrawal(addm);
	JOptionPane.showMessageDialog(null, "$ "+ addm + " has been deposited");
	JOptionPane.showMessageDialog(null, "Total balance for account Mike is: " + ac.inquiry());


	JOptionPane.showMessageDialog(null,"The account balance now is: " +"$" +ac.inquiry());
	
	Account ac2 = new Account("Susan", 100, per.getId());
	JOptionPane.showMessageDialog(null, ac2.toString());
	String susan = JOptionPane.showInputDialog(null, "Enter amount to withdraw");
	double add1 = Double.parseDouble(susan);
          //what should I code here so that if the amount you want to withdraw is greater than the balance, JOptionPane will show insufficient funds, else process the withdrawal ?
	ac2.withdrawal(add1);  
	JOptionPane.showMessageDialog(null, "$"+ add1 + " has been withdrawn");
	JOptionPane.showMessageDialog(null,"The account balance now is: " +"$" +ac2.inquiry());}

Was This Post Helpful? 0
  • +
  • -

#10 wrightm96  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 43
  • Joined: 06-March 12

Re: Can't not go back to create new account

Posted 19 March 2012 - 08:01 AM

All the code runs correctly for me.

What seems to be the problem?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1