I'm just not quite getting the whole method thing. I get the general concept and even like, but applying it is another matter for me. Smaller programs, examples of simple method programs I get. But when it comes to 'methodizing' a larger program, I begin to have trouble. Case-in-point, below I'm attempting to add methods to a sequential program I wrote earlier. (The original is posted below the method-attempt.) If anyone cares to chime in, it would really be helpful to me.
import java.io.*; import java.util.*; import java.text.DecimalFormat; public class Bank3 { public static void main(String[] args) throws IOException { String firstLine; String secondLine; String thirdLine; String fourthLine; String tempLine; double fifthLine; double monthlyFee = 0; double totalBalance = 0; openRead(); outPut(); } public static void openRead() throws IOException { File file = new File("AcctInfo.txt"); Scanner inputFile = new Scanner(file); DecimalFormat formatter = new DecimalFormat("#0.00"); while (inputFile.hasNext()) { String firstLine = inputFile.nextLine(); while (!(firstLine.isEmpty())) { String secondLine = inputFile.nextLine(); String thirdLine = inputFile.nextLine(); String fourthLine = inputFile.nextLine();; String tempLine = inputFile.nextLine(); Double fifthLine = Double.parseDouble(tempLine); double monthlyFee = 0; double totalBalance = 0; System.out.println("Account #:\t\t" + firstLine); System.out.println("Account Type:\t\t" + secondLine); System.out.println("Customer Name:\t\t" + thirdLine); System.out.println("Customer Type:\t\t" + fourthLine); System.out.println("Balance:\t\t$" + fifthLine); if (secondLine.equalsIgnoreCase("savings")) { monthlyFee = 0.00; } else if (fourthLine.equalsIgnoreCase("value") && fifthLine < 1500) { monthlyFee = 5.00; } else if (fourthLine.equalsIgnoreCase("value") && fifthLine >= 1500) { monthlyFee = 0.00; } else if (fourthLine.equalsIgnoreCase("advantage") && fifthLine < 1000) { monthlyFee = 10.00; } else if (fourthLine.equalsIgnoreCase("advantage") && fifthLine >= 1000) { monthlyFee = 0.00; } else if (fourthLine.equalsIgnoreCase("premier") && fifthLine < 25000) { monthlyFee = 30.00; } else if (fourthLine.equalsIgnoreCase("premier") && fifthLine >= 25000) { monthlyFee = 0.00; return monthlyFee; } } }} public static void outPut(double monthlyFee, fifthLine)throws IOException { System.out.println("Monthly Fee:\t\t$" + formatter.format(monthlyFee)); System.out.println(""); totalBalance += fifthLine; firstLine = inputFile.nextLine(); System.out.println("The total amount deposited: $" + formatter.format(totalBalance)); inputFile.close(); } }
Original Code:
import java.util.*; import java.io.*; import java.text.DecimalFormat; public class Bank1 { public static void main(String[] args) throws IOException { String firstLine; String secondLine; String thirdLine; String fourthLine; String tempLine; double fifthLine; double monthlyFee = 0; double totalBalance = 0; File file = new File("AcctInfo.txt"); Scanner inputFile = new Scanner(file); DecimalFormat formatter = new DecimalFormat("#0.00"); while (inputFile.hasNext()) { firstLine = inputFile.nextLine(); while (!(firstLine.isEmpty())) { secondLine = inputFile.nextLine(); thirdLine = inputFile.nextLine(); fourthLine = inputFile.nextLine();; tempLine = inputFile.nextLine(); fifthLine = Double.parseDouble(tempLine); System.out.println("Account #:\t\t" + firstLine); System.out.println("Account Type:\t\t" + secondLine); System.out.println("Customer Name:\t\t" + thirdLine); System.out.println("Customer Type:\t\t" + fourthLine); System.out.println("Balance:\t\t$" + fifthLine); if (secondLine.equalsIgnoreCase("savings")) { monthlyFee = 0.00; } else if (fourthLine.equalsIgnoreCase("value") && fifthLine < 1500) { monthlyFee = 5.00; } else if (fourthLine.equalsIgnoreCase("value") && fifthLine >= 1500) { monthlyFee = 0.00; } else if (fourthLine.equalsIgnoreCase("advantage") && fifthLine < 1000) { monthlyFee = 10.00; } else if (fourthLine.equalsIgnoreCase("advantage") && fifthLine >= 1000) { monthlyFee = 0.00; } else if (fourthLine.equalsIgnoreCase("premier") && fifthLine < 25000) { monthlyFee = 30.00; } else if (fourthLine.equalsIgnoreCase("premier") && fifthLine >= 25000) { monthlyFee = 0.00; } System.out.println("Monthly Fee:\t\t$" + formatter.format(monthlyFee)); System.out.println(""); totalBalance += fifthLine; firstLine = inputFile.nextLine(); } } System.out.println("The total amount deposited: $" + formatter.format(totalBalance)); inputFile.close(); } }