What is the best way to do the following? :

I'm writing a test program for a family of classes that simulate bank accounts, and I need the test class to take a series of characters from the command line (the letters 'c,' 's' and 'm', specifically, in any given order and in any amount) and it needs to create a bank account for each letter. Checking account for each 'c' present, Savings account for each 's,' and Money Market account for each 'm.' The creation of each type of account is not my problem. Mine is much simpler. What command should I use to check the letters if I put them into a String or a char array? I have that part of the main method set up as 'if' and 'else if' statements nested in a for loop. I've tried a couple different things, such as trying to compare each letter when put into a String, using charAt(). With that, I've gotten a couple different errors, and the same with trying to compare each element in a char array made up of the letters from the command line. I just need to know the right command that will compare each character, and give me a boolean value if it matches one of those three letters.

Thanks!

Seems like charAt() would work. Can you paste the code? Also, how are you looping through the string? There must be a reason why it's not working, so maybe you can show us the code to see what's going on.

Here's the code I'm using. This version compiles, but gives me a NullPointerException error on line 79, where it reads:

System.out.println( "Account Type: " + bankAccounts[i].toString() );

That line is supposed to print out what type of account that is, using a method within the constructor class.

This leads me to believe that the "accounts" aren't even being created. Also, I put in a couple lines that print out the starting balance after the accounts are supposed to be created. After creating 3 accounts, the starting balance should be set to 1700. Its still set to 500 (The initial value) when I run the program.

I have to submit this tonight, so I'm sending the incomplete version here, but any help would still be appreciated. :)

public class TestBankAccount {
    
    //String to store the arguments from the command line
    public static char[] accounts;
    //Stores how many accounts are entered on the command line
    public static int howManyAccts = 0;
    //Opening balance of first account
    public static int balance = 500;
    // Increment value for each additional account
    public static final int INCREMENT = 400;
    // Array to store the savings, checking and money market accounts
    public static BankAccount[] bankAccounts;
    
    /**
     * Main method, receives arguments from the command line and tests
     * the hierarchy of the BankAccount class family.
     * 
     * @param args the command line arguments
     *
     */
    
    public static void main( String args[] ) {
	// Checks for Command Line Arguments
	if&#40; args.length < 1 &#41; &#123;
	    System.err.println&#40;"Usage&#58; java TestBankAccount AccountTypeList"&#41;;
	&#125;
	else &#123;
	    accounts =new char&#91;args.length&#93;;
	    bankAccounts = new BankAccount&#91;args.length&#93;;
	    // Creates an account for each letter on the command line
	    for&#40; int i = 0; i < args.length; i++ &#41; &#123;
		// Savings Accounts
		if&#40; accounts&#91;i&#93; == 's' &#41; &#123;
		    bankAccounts&#91;i&#93; = new SavingsAccount&#40; balance &#41;;
		    balance += INCREMENT;
		&#125; 
		// Checking Accounts
		else if&#40; accounts&#91;i&#93; == 'c' &#41; &#123;
		    bankAccounts&#91;i&#93; = new CheckingAccount&#40; balance &#41;;
		    balance += INCREMENT;
		&#125; 
		// Money Market Accounts
		else if&#40; accounts&#91;i&#93; == 'm' &#41; &#123;
		    bankAccounts&#91;i&#93; = new MoneyMarket&#40; balance &#41;;
		    balance += INCREMENT;
		&#125;
	    &#125;
    
	    // Prints the quarterly statement &#40;last three months&#41;
	    for&#40; int x = 1; x <= 3; x++ &#41; &#123;
		System.out.println&#40; "Month " + x + "&#58;" &#41;;
		System.out.println&#40;&#41;;
		for&#40; int i = 0; i < args.length; i++ &#41; &#123;
		    System.out.println&#40; "Account Type&#58; " +
					bankAccounts&#91;i&#93;.toString&#40;&#41; &#41;;
		    bankAccounts&#91;i&#93;.earnInterest&#40;&#41;;
		    bankAccounts&#91;i&#93;.printStatement&#40;&#41;;
		    bankAccounts&#91;i&#93;.newMonth&#40;&#41;;
		    System.out.println&#40;&#41;;
		&#125;
		System.out.println&#40;&#41;;
	    &#125;
	&#125;
    &#125;
&#125;

It looks like you never actually store the command line arguments anywhere. You start comparing accounts == 's' in your first if statement before the elements in the accounts array have been initialized.

try adding this first thing inside your main loop:


accounts = new char&#91;args.length&#93;;
for&#40;int i=0; i<args.length; i++&#41; &#123;
  accounts&#91;i&#93; = args&#91;i&#93;.charAt&#40;0&#41;;
&#125;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.