Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Bank Account

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Location
    Goulds, FL
    Posts
    7
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Bank Account

    Hello! I'm having some difficulty with my bank account project. I'm supposed to create a menu where the user can create a new account, withdraw, deposit, view their balance, and exit. There's issues with the account creation. Can someone please help me fix this problem?

    Here's my necessitated class below: BankAccount, TestBankAccount, SavingsAccount, CurrentAccount, and Bank

     /*---------------------------------------------------
     
     Name: Christian Bethel
     
     Student ID: T19170213
     
     COP 2800 - Java Programming 
     
     Fall 2014 - T Th 6:00PM - 9:20PM
     
     Project 3
     
     Plagiarism Statement
     
     I certify that this assignment is my own work and that I
     have not copied in part or whole or otherwise plagiarized 
     the work of other students and/or persons.
     
    ----------------------------------------------------------*/
     
    package BankAccount;
     
    import java.util.Date;
    import java.util.Random;
     
    //Project 3
    public class BankAccount
     
    {
    	protected static int accountID;
    	protected static double accountBalance;
    	private static double annualInterestRate;
    	public static Date dateCreated = new Date();
    	static String name = new String();
    	private static String password = new String();
    	Random RandomNumber = new Random();
     
    	BankAccount() {
    		accountID = 1000 + (int) (Math.random() * 9000);
    		String myAccountID = accountID + "";
    	}
     
    	BankAccount(double AccountBalance) {
    		accountID = (int) (Math.random() * 9000);
    		String myAccountID = accountID + "";
    		accountBalance = AccountBalance;
    	}
     
    	public static void setAccountBalance(double newAccountBalance) {
    		accountBalance = newAccountBalance;
    	}
     
    	public static double getAccountBalance() {
    		return accountBalance;
    	}
     
    	static void withdraw(double withdrawAmount) {
    		if (accountBalance == 0 && withdrawAmount > accountBalance)
    			System.out.println("Insufficient Funds!!!" + accountBalance);
    		if (accountBalance == 0 ^ withdrawAmount > accountBalance)
    			System.out
    					.println("Hey! You can't take out more than you already have!\n"
    							+ accountBalance);
    		accountBalance -= withdrawAmount;
    	}
     
    	static double deposit(double depositAmount) {
    		return accountBalance += depositAmount;
    	}
     
    	public static void setAnnualInterestRate(double yourAnnualInterestRate) {
    		annualInterestRate = yourAnnualInterestRate;
    	}
     
    	public double getAnnualInterestRate() {
    		return annualInterestRate;
    	}
     
    	public static int getAccountID() {
    		return accountID;
    	}
     
    	public static Date getDateCreated() {
    		return dateCreated;
    	}
     
    	static double getMonthlyInterestRate() {
    		return annualInterestRate / 12;
    	}
     
    	public static String getName() {
    		return name;
    	}
     
    	public static void setName(String myName) {
    		name = myName;
    	}
     
    	static void setPassword() {
    		Random idGenerator = new Random(System.currentTimeMillis());
    		char[] letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
    		String password = "";
     
    		for (int i = 1; i <= 4; i++)
    			password += letters[idGenerator.nextInt(27)];
    	}
     
    	public static String getPassword() {
    		return password;
    	}
    }
     
    /*---------------------------------------------------
     
     Name: Christian Bethel
     
     Student ID: T19170213
     
     COP 2800 - Java Programming 
     
     Fall 2014 - T Th 6:00PM - 9:20PM
     
     Project 3
     
     Plagiarism Statement
     
     I certify that this assignment is my own work and that I
     have not copied in part or whole or otherwise plagiarized 
     the work of other students and/or persons.
     
    ----------------------------------------------------------*/
     
    package BankAccount;
     
    import java.util.Scanner;
     
    //Project 3
    public class TestBankAccount {
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
     
    		System.out.println("-Main Menu-\n" + "1.Create New Account"
    				+ "   2.Deposit" + "   3.Withdraw\n" + "4.Display Balance"
    				+ "   5.Open/Close an Account" + "   6.Exit\n"
    				+ "What would you like to do? ");
     
    		int Choice = input.nextInt();
     
    		switch (Choice) {
    		case 1:
    			System.out.println("Please enter your name: ");
    			String Name = input.nextLine();
    			BankAccount.setName(Name);
    			BankAccount.setPassword();
    			System.out.println("Your Credentials\n" + "Name: "
    					+ BankAccount.getName() + "\n" + "Password: "
    					+ BankAccount.getPassword());
    			BankAccount regularAccount = new BankAccount();
    			Bank.bankAccounts.add(regularAccount);
    			TestBankAccount.main(null);
    			break;
    		case 2:
    			System.out.println("Enter the amount you wish to deposit: ");
    			double DepositAmount = input.nextDouble();
    			BankAccount.deposit(DepositAmount);
    			System.out.println("Balance: " + "$"
    					+ BankAccount.getAccountBalance() + "0");
    			TestBankAccount.main(null);
    			break;
     
    		case 3:
    			System.out.println("Enter the amount you wish to withdraw: ");
    			double WithdrawAmount = input.nextDouble();
    			BankAccount.withdraw(WithdrawAmount);
    			System.out.println("Balance: " + "$"
    					+ BankAccount.getAccountBalance() + "0");
    			TestBankAccount.main(null);
    			break;
     
    		case 4:
    			System.out.println("Balance: " + "$"
    					+ BankAccount.getAccountBalance() + "0" + "\n"
    					+ "Monthly Interest Rate: "
    					+ BankAccount.getMonthlyInterestRate() * 100 + "%" + "\n"
    					+ "Created On: " + BankAccount.getDateCreated() + "\n"
    					+ "AccountID: " + BankAccount.getAccountID());
    			TestBankAccount.main(null);
    			break;
     
    		case 5:
    			System.out.println("Would you like to open or close an account?\n"
    					+ "1.Open\n" + "2.Close\n");
    			int accountOption = input.nextInt();
    			if (accountOption == 1)
    				Bank.openAccount();
    			else if (accountOption == 2)
    				Bank.closeAccount();
    			else
    				System.out.println("Invalid. Try again!");
    			TestBankAccount.main(null);
    			break;
    		case 6:
    			System.out.println("Do you want to exit?\n" + "1. Yes\n" + "2. No");
    			int ExitChoice = input.nextInt();
     
    			switch (ExitChoice) {
    			case 1:
    				System.exit(0);
    				break;
    			case 2:
    				TestBankAccount.main(null);
    				break;
    			default:
    				System.out.print("Ivnalid. Try Again!");
    				break;
    			}
    		}
    	}
    }
     
    /*---------------------------------------------------
     
     Name: Christian Bethel
     
     Student ID: T19170213
     
     COP 2800 - Java Programming 
     
     Fall 2014 - T Th 6:00PM - 9:20PM
     
     Project 3
     
     Plagiarism Statement
     
     I certify that this assignment is my own work and that I
     have not copied in part or whole or otherwise plagiarized 
     the work of other students and/or persons.
     
    ----------------------------------------------------------*/
     
    package BankAccount;
     
    import java.util.Date;
    import java.util.Random;
     
    public class SavingsAccount extends BankAccount {
    	protected static double interest;
    	private static int accountID;
    	protected static double accountBalance;
    	private static double annualInterestRate;
    	static Date dateCreated = new Date();
    	private static String name = new String();
    	private static String password = new String();
    	Random RandomNumber = new Random();
     
    	SavingsAccount() {
    		super();
    		accountID = (int) (Math.random() * 10000);
    		String myAccountID = accountID + "";
    	}
     
    	SavingsAccount(double accountBalance) {
    		super();
    		accountID = (int) (Math.random() * 10000);
    		String myAccountID = accountID + "";
    	}
     
    	public static void setAccountBalance(double newAccountBalance) {
    		accountBalance = newAccountBalance;
    	}
     
    	public static void addInterest(double interest) {
    		BankAccount.accountBalance += (interest * 12);
    	}
     
    	public static double getAccountBalance() {
    		return accountBalance;
    	}
     
    	static void withdraw(double withdrawAmount) {
    		if (accountBalance == 0)
    			System.out.println("Insufficient Funds!!!");
    		if (withdrawAmount > accountBalance)
    			System.out
    					.println("Hey! You can't take out more than you already have!");
    		accountBalance -= withdrawAmount;
    	}
     
    	static double deposit(double depositAmount) {
    		return accountBalance += depositAmount;
    	}
     
    	public static void setAnnualInterestRate(double yourAnnualInterestRate) {
    		annualInterestRate = yourAnnualInterestRate;
    	}
     
    	@Override
    	public double getAnnualInterestRate() {
    		return annualInterestRate;
    	}
     
    	public static int getAccountID() {
    		return accountID;
    	}
     
    	public static Date getDateCreated() {
    		return dateCreated;
    	}
     
    	static double getMonthlyInterestRate() {
    		return annualInterestRate / 12;
    	}
     
    	public static String getName() {
    		return name;
    	}
     
    	public static void setName(String name) {
    	}
     
    	static String generatePassword() {
    		Random idGenerator = new Random(System.currentTimeMillis());
    		char[] letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
    		String password = "";
     
    		for (int i = 1; i <= 4; i++)
    			password += letters[idGenerator.nextInt(27)];
    		return password;
    	}
     
    	public static String getPassword() {
    		return password;
    	}
     
    }
     
    /*---------------------------------------------------
     
     Name: Christian Bethel
     
     Student ID: T19170213
     
     COP 2800 - Java Programming 
     
     Fall 2014 - T Th 6:00PM - 9:20PM
     
     Project 3
     
     Plagiarism Statement
     
     I certify that this assignment is my own work and that I
     have not copied in part or whole or otherwise plagiarized 
     the work of other students and/or persons.
     
    ----------------------------------------------------------*/
     
    package BankAccount;
     
    import java.util.Date;
    import java.util.Random;
     
    public class CurrentAccount extends BankAccount {
    	private static int accountID;
    	protected static double accountBalance;
    	private static double annualInterestRate;
    	protected static double overdraftLimit = 500.0;
    	static Date dateCreated = new Date();
    	private static String name = new String();
    	private static String password = new String();
    	Random RandomNumber = new Random();
     
    	CurrentAccount() {
    		super();
    		accountID = (int) (Math.random() * 10000);
    		String myAccountID = accountID + "";
    	}
     
    	CurrentAccount(double accountBalance) {
    		super();
    		accountID = (int) (Math.random() * 10000);
    		String myAccountID = accountID + "";
    	}
     
    	public static void setAccountBalance(double newAccountBalance) {
    		accountBalance = newAccountBalance;
    	}
     
    	public static double getAccountBalance() {
    		return accountBalance;
    	}
     
    	public static void withdraw(double withdrawAmount) {
    		if (accountBalance == 0)
    			System.out.println("Insufficient Funds!!!");
    		if (withdrawAmount > accountBalance)
    			System.out
    					.println("Hey! You can't take out more than you already have!");
    		accountBalance -= withdrawAmount;
    	}
     
    	public static double deposit(double depositAmount) {
    		return accountBalance += depositAmount;
    	}
     
    	public static void setAnnualInterestRate(double yourAnnualInterestRate) {
    		annualInterestRate = yourAnnualInterestRate;
    	}
     
    	@Override
    	public double getAnnualInterestRate() {
    		return annualInterestRate;
    	}
     
    	public static int getAccountID() {
    		return accountID;
    	}
     
    	public static Date getDateCreated() {
    		return dateCreated;
    	}
     
    	public static double getMonthlyInterestRate() {
    		return annualInterestRate / 12;
    	}
     
    	public static String getName() {
    		return name;
    	}
     
    	public static void setName(String name) {
    	}
     
    	public static String generatePassword() {
    		Random idGenerator = new Random(System.currentTimeMillis());
    		char[] letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
    		String password = "";
     
    		for (int i = 1; i <= 4; i++)
    			password += letters[idGenerator.nextInt(27)];
    		return password;
    	}
     
    	public static String getPassword() {
    		return password;
    	}
    }
     
    /*---------------------------------------------------
     
     Name: Christian Bethel
     
     Student ID: T19170213
     
     COP 2800 - Java Programming 
     
     Fall 2014 - T Th 6:00PM - 9:20PM
     
     Project 3
     
     Plagiarism Statement
     
     I certify that this assignment is my own work and that I
     have not copied in part or whole or otherwise plagiarized 
     the work of other students and/or persons.
     
    ----------------------------------------------------------*/
     
    package BankAccount;
     
    import java.util.ArrayList;
    import java.util.Scanner;
     
    public class Bank {
    	static ArrayList<Object> bankAccounts = new ArrayList<>(3);
     
    	public static void update() {
    		for (int i = 0; i < bankAccounts.size(); i++) {
    			if (bankAccounts.get(i) instanceof SavingsAccount)
    				SavingsAccount.addInterest(SavingsAccount.interest);
    			else if (bankAccounts.get(i) instanceof CurrentAccount)
    				System.out.print("You have reached your overdraft limit!");
    		}
    	}
     
    	public static void openAccount() {
    		Scanner input = new Scanner(System.in);
    		System.out.println("What type of account would you like to open?\n"
    				+ "1.Regular Account" + "   2.Savings Account\n"
    				+ "3.Current Account" + "   4.Update Accounts");
    		int choice = input.nextInt();
     
    		switch (choice) {
    		case 1:
    			BankAccount newBankAccount = new BankAccount(
    					BankAccount.accountBalance);
    			bankAccounts.add(newBankAccount);
    			break;
    		case 2:
    			SavingsAccount newSavingsAccount = new SavingsAccount(
    					SavingsAccount.accountBalance);
    			bankAccounts.add(newSavingsAccount);
    			break;
    		case 3:
    			CurrentAccount newCurrentAccount = new CurrentAccount(
    					CurrentAccount.accountBalance);
    			bankAccounts.add(newCurrentAccount);
    			break;
    		case 4:
    			Bank.update();
    			break;
    		default:
    			System.out.println("Invalid. Try again!");
    			System.exit(0);
    			break;
    		}
     
    	}
     
    	public static void closeAccount() {
    		Scanner input = new Scanner(System.in);
    		System.out.println("What is your name?");
    		String myName = input.nextLine();
    		System.out.println("What is your ID?");
    		int ID = input.nextInt();
    		if (ID == BankAccount.accountID && myName.equals(BankAccount.name))
    			bankAccounts.remove(bankAccounts.size() - 1);
    		else
    			System.exit(0);
    	}
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Bank Account

    There's issues with the account creation.
    What issue? Post the error message, describe the issue, post a sample run. Give us something to understand what you're trying to fix.

  3. #3
    Junior Member
    Join Date
    Sep 2014
    Location
    Goulds, FL
    Posts
    7
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Bank Account

    OK, for the account, I have to enter my name. The program then generates a random 4 digit ID number and a random 4-letter string which serves as a password. Every time I do so, The program either 1.)only displays the name, 2.)terminates, 3.)throws an Input Mismatch or Array Out of Bounds Exception, or 4.)loops infinitely back to the main menu, skipping the block entirely.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Bank Account

    Add print statements to the code to determine which methods are being run, what their inputs and outputs are and to ultimately determine why the logic flow you think the program should follow isn't being followed.

    If you wrote all of this code before doing any testing, you're doing it wrong. You should have written and tested each menu choice before writing the next. Keep the amount of untested code as small as possible, make small changes, and test the changes to ensure the desired results have been achieved.

Similar Threads

  1. bank account
    By rutchel alferez in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 9th, 2013, 08:31 AM
  2. Bank Account HELP!!!! PLEASE.
    By metaleddie13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 16th, 2011, 08:06 PM
  3. Please help - Bank account application
    By brandonssss in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 2nd, 2010, 03:10 PM
  4. Bank account GUI using swing
    By AlanM595 in forum AWT / Java Swing
    Replies: 5
    Last Post: April 2nd, 2009, 04:39 AM
  5. How to delete records from a Random-Access File?
    By keith in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: March 31st, 2009, 11:40 AM