Hi. I'm new to this forum and to programming in general. This is my first programming class and it is in JAVA. I have been doing alright in the class, but am really struggling with this weeks program assignment. If you could help me out at all I would really appreciate it!! Thank you so much.

This is the problem I have to solve.

Unit 8 - Bank Program - Looping

Instructions
Using the algorithm that you created in the last unit.
Write the Java program will solve the following problem definition.
Click on the View/Complete link to submit the program for grading.

A bank in your town updates its customer's accounts at the end of each month.
The bank offers two types of accounts, savings and checking.
Every customer must maintain a minimum balance.
If a customer's balance falls below the minimum,
there is a service fee of $10.00 for a savings account and $25.00 for checking accounts.
If the balance at the end of the month is at least the minimum balance, the account receives interest as follows:

  • Savings accounts receive 4% interest
  • Checking accounts with balances over $5,000 more than the minimum balance receive 3% interest; otherwise the interest is 1.5%

Minimum balance for savings accounts - 500.00
Minimum balance for checking accounts - 25.00

The data will be read from a data file that contains the following information:
account number, customer name, type of account, current balance

Record Layout of the file:
Account Number First Name Last Name AccountType Balance

The data file is attached to this assignment. The filename is bank.dat.

Please Note: The data files will be read and written to the current directory.
Use a relative pathnames for the files. The pathname to the input file
should be "bank.dat" and the pathname to the output file "bank.out".
If you use an absolute pathname like this: "c:/My Documents/ciss-110/bank.dat"
I will not be able to run your program because I do not have access to your hard drive.

This is what I have at the moment.

import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import java.awt.Font;
import java.io.*;
import java.util.*;
import java.lang.*;

public class bankprogramLoop
{
    public static void main(String[]args)
    throws FileNotFoundException
    {

//scanner infile
Scanner inFile = new Scanner(new FileReader("bank.dat"));
//save to file bank.out
PrintWriter outFile = new PrintWriter("bank.out");
    //constants
    double checkingMinimumBalance = 25.00;
    double checkingServiceFee = 25.00;
    double checkingInterestRateMin = 0.015;
    double checkingInterestRateMax = 0.03;
    double hihgerInterestBalance = checkingMinimumBalance + 5000;
    double savingsMinimumBalance = 500.00;
    double savingsServiceFee = 10.00;
    double savingsInterestRate = 0.04;

    //variables
    int accountNumber;
    String firstName;
    String lastName;
    double accountBalance;
    double checkingInterest;
    double savingsInterest;
    char accountType;
    double serviceFee;
    double interest;



    while (inFile.hasNext())
        {
            accountNumber = inFile.nextInt();
            firstName = inFile.next();
            lastName = inFile.next();
            accountType = inFile.next().charAt(0);
            accountBalance = inFile.nextDouble();

switch (accountType)
    {
    case 'c':
    case 'C':
    serviceFee = checkingServiceFee;
        if      (accountBalance < checkingMinimumBalance)
                        {

            interest = accountBalance * checkingInterestRateMin;
            accountBalance = (accountBalance - serviceFee) + interest;
                        }
    else if (accountBalance >= (checkingMinimumBalance+hihgerInterestBalance))
                        {
        interest = accountBalance * checkingInterestRateMax;
        accountBalance = (accountBalance - serviceFee) + interest;
                        }
                    else
                        {
            interest = accountBalance * checkingInterestRateMin;
            accountBalance = (accountBalance - serviceFee) + interest;
                        }

            case 's':
            case 'S':
            serviceFee = savingsServiceFee;
                    if (accountBalance < savingsMinimumBalance)
                        {

            interest = accountBalance*savingsInterestRate;
                accountBalance = (accountBalance - serviceFee) + interest;
                        }
                    else
                        {
                    interest = accountBalance * savingsInterestRate;
                accountBalance = (accountBalance - serviceFee) + interest;
        }
            default: System.out.println("***INVALID ACCOUNT TYPE***");
            }
    //write accountNumber, firstName, lastName, accountType, accountBalance to output file
outFile.printf("%9.2f %20s %20s %20s %9.2f", accountNumber, firstName, lastName, accountType, accountBalance);
}
//close files
inFile.close();
outFile.close();
}
}

Dani AI

Generated

A short expert note for anyone who comes here later: the symptoms described by (confusing output and the program appearing to “loop”) usually come from a handful of simple mistakes when processing tokenized input and applying per-account rules. was right — print the tokens as you read them to confirm the input stream — and the two most common logic bugs to check next are (1) switch fall‑through (missing break) and (2) incorrect condition/threshold math or applying interest when the balance is below the minimum.

A compact, safe pattern (using if/else to avoid accidental fall‑through) is shown below. It demonstrates the correct order: check for below‑minimum and apply the fee only, otherwise compute and add interest. The names and formatting here are intentionally different from the original post so the idea is clear without repeating the original code verbatim.

char t = Character.toUpperCase(accountTypeToken.charAt(0));
double chkMin = 25.0, savMin = 500.0;
double fee = (t == 'C') ? 25.0 : 10.0;
double highThreshold = chkMin + 5000.0;

if (t == 'C') {
    if (balance < chkMin) {
        balance -= fee;                      // below minimum: fee only
    } else {
        double rate = (balance > highThreshold) ? 0.03 : 0.015;
        balance += balance * rate;
    }
} else if (t == 'S') {
    if (balance < savMin) balance -= fee;
    else balance += balance * 0.04;
} else {
    // handle invalid type
}

Extra tips: use try-with-resources for files; validate tokens with hasNextInt/hasNextDouble or read line-by-line and split; prefer BigDecimal (or store cents as long) for money if exact cents matter; format output with correct printf specifiers (for example %d %s %s %c %.2f%n). Finally, remember to include break in switch blocks if you choose switch syntax to avoid silent fall‑through. — marking the thread solved is the right final step after confirming behavior.

Recommended Answers

All 5 Replies

What do you mean its looping??

Try displaying the values that you read from the file after every read

System.out.println(accountNumber);

and see if it reads all your data and to verify if your program is indeed looping.

In the bank.dat file there are four lines, each for a seperate account. two are savings and two are checking. The account type is specified by a C or S. the program should read a line, make the appropriate calculations, and save the file to bank.out

unless someone sees another issue with this, I believe the only thing I need to do is make the program read the char and apply it. So far I have not made that work...

this thread can close. I completed my assignment. Just rewrote everything and realized what I had been doing wrong. Got it working properly.

I think since you started the thread, you have to mark it solved.

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.