////////////////////////////////////////////////////////////////////////////////// // ICS 3M1 - Mrs. Shanks // Patrick Paskaris // October 2, 2007 // Selection Assignment 3 // // This program will calculate a $120 000 mortgage being paid off in $24 000 chunks // at 10.75% interest per year, and output the previous outstanding balance, the // year-end interest, the principal paid and the year-end outstanding balance. ////////////////////////////////////////////////////////////////////////////////// // Variables Used: // addedSpaces - String of spaces for alignment // alignString - String of spaces & original string for alignment // amountLeft - The outstanding amount // COLUMN_MARGIN - Length of MARGIN // COLUMN_WIDTH - Length of UNDERLINE_STR // completionDecimal - Used to check for decimals // completionLen - Length of the completion string // DECIMAL - '.' // FILE_NAME - Name of the input file // HALF_MARGIN - Spaces for half-margin // HEADER_COUNT - Number of headers // HEADER1 - Title of column 1 // HEADER2 - Title of column 2 // HEADER3 - Title of column 3 // HEADER4 - Title of column 4 // HEADER5 - Title of column 5 // HEADER6 - Title of column 6 // interest - The interest on the outstanding amount // INTEREST_RATE - Interest rate on mortgage // INTEREST_STR - Output string for the interest rate // MARGIN - Spaces for margin // MORTGAGE_AMOUNT - Initial cost of mortgage // outCompleted - The completed string // outMortgageAmount - Output string for the initial cost of mortgage // outZero - Output string for "0.00" // overPaid - Difference of amountLeft and paid when amountLeft is less than paid // paid - The amount paid each year on the outstanding amount // PAY_AMOUNT - Amount paid each year // ROUNDER - 100 // spacesDiff - Amount of spaces needed for alignment // UNDERLINE_STR - Underline for headers // underlineLine - Combined underlines // year - Year counter ////////////////////////////////////////////////////////////////////////////////// class Selections3 { public static void main( String args[] ) { final double MORTGAGE_AMOUNT = 120000, PAY_AMOUNT = 24000, INTEREST_RATE = .1075; final String INTEREST_STR = ( INTEREST_RATE * 100 ) + "%"; double amountLeft = MORTGAGE_AMOUNT, interest = 0, paid = 0, overPaid = 0; int year = 0; String outZero = "$ 0.00", outMortgageAmount = ""; // For table output final String UNDERLINE_STR = "____________________", MARGIN = " ", HALF_MARGIN = " "; final String HEADER1 = "Year", HEADER2 = "Previous Outstanding", HEADER3 = "Interest", HEADER4 = "Principal", HEADER5 = "Outstanding"; final int HEADER_COUNT = 5; final int COLUMN_MARGIN = MARGIN.length( ), COLUMN_WIDTH = UNDERLINE_STR.length( ); String underlineLine = ""; int spacesDiff = 0; String addedSpaces = "", alignString = ""; // Code for .00 completion final int ROUNDER = 100; final char DECIMAL = '.'; final int DECIMAL_POS = 2; char completionDecimal = '.'; int completionLen = 0; String outCompleted = ""; // Header 1 alignString = HEADER1; // Layout is key with a table, so this code determines the // proper amount of spaces to insert before the text and // after it to ensure everything lines up. spacesDiff = ((COLUMN_WIDTH + COLUMN_MARGIN) / 2) - (alignString.length() / 2); addedSpaces = ""; for ( int spaceAdd = 0; spaceAdd <= spacesDiff; spaceAdd++ ) { addedSpaces += " "; } alignString = addedSpaces + alignString; System.out.print( alignString ); spacesDiff = ( COLUMN_WIDTH + COLUMN_MARGIN ) - alignString.length( ); addedSpaces = ""; for ( int spaceAdd = 0; spaceAdd <= spacesDiff; spaceAdd++ ) { addedSpaces += " "; } alignString = addedSpaces; System.out.print( alignString ); // Header 2 alignString = HEADER2; // Code for centering spacesDiff = ((COLUMN_WIDTH + COLUMN_MARGIN) / 2) - (alignString.length() / 2); addedSpaces = ""; for ( int spaceAdd = 0; spaceAdd <= spacesDiff; spaceAdd++ ) { addedSpaces += " "; } alignString = addedSpaces + alignString; System.out.print( alignString ); spacesDiff = ( COLUMN_WIDTH + COLUMN_MARGIN ) - alignString.length( ); addedSpaces = ""; for ( int spaceAdd = 0; spaceAdd <= spacesDiff; spaceAdd++ ) { addedSpaces += " "; } alignString = addedSpaces; System.out.print( alignString ); // Header 3 alignString = HEADER3; // Code for centering spacesDiff = ((COLUMN_WIDTH + COLUMN_MARGIN) / 2) - (alignString.length() / 2); addedSpaces = ""; for ( int spaceAdd = 0; spaceAdd <= spacesDiff; spaceAdd++ ) { addedSpaces += " "; } alignString = addedSpaces + alignString; System.out.print( alignString ); spacesDiff = ( COLUMN_WIDTH + COLUMN_MARGIN ) - alignString.length( ) - 2; addedSpaces = ""; for ( int spaceAdd = 0; spaceAdd <= spacesDiff; spaceAdd++ ) { addedSpaces += " "; } alignString = addedSpaces; System.out.print( alignString ); // Header 4 alignString = HEADER4; // Code for centering spacesDiff = ((COLUMN_WIDTH + COLUMN_MARGIN) / 2) - (alignString.length() / 2); addedSpaces = ""; for ( int spaceAdd = 0; spaceAdd <= spacesDiff; spaceAdd++ ) { addedSpaces += " "; } alignString = addedSpaces + alignString; System.out.print( alignString ); spacesDiff = ( COLUMN_WIDTH + COLUMN_MARGIN ) - alignString.length( ); addedSpaces = ""; for ( int spaceAdd = 0; spaceAdd <= spacesDiff; spaceAdd++ ) { addedSpaces += " "; } alignString = addedSpaces; System.out.print( alignString ); // Header 4 alignString = HEADER5; // Code for centering spacesDiff = ((COLUMN_WIDTH + COLUMN_MARGIN) / 2) - (alignString.length() / 2); addedSpaces = ""; for ( int spaceAdd = 0; spaceAdd <= spacesDiff; spaceAdd++ ) { addedSpaces += " "; } alignString = addedSpaces + alignString; System.out.print( alignString ); spacesDiff = ( COLUMN_WIDTH + COLUMN_MARGIN ) - alignString.length( ); addedSpaces = ""; for ( int spaceAdd = 0; spaceAdd <= spacesDiff; spaceAdd++ ) { addedSpaces += " "; } alignString = addedSpaces; System.out.print( alignString ); System.out.print( "\n" ); // Dynamic underlining based on number of columns for ( int underline = 0; underline < HEADER_COUNT; underline++ ) { underlineLine += MARGIN + UNDERLINE_STR; } System.out.println( underlineLine ); while ( amountLeft > 0 ) { // Increase the year year++; // Year is centered alignString = year + ""; spacesDiff = ((COLUMN_WIDTH + COLUMN_MARGIN) / 2) - (alignString.length() / 2); addedSpaces = ""; for ( int spaceAdd = 0; spaceAdd <= spacesDiff; spaceAdd++ ) { addedSpaces += " "; } alignString = addedSpaces + alignString; System.out.print( alignString ); spacesDiff = (COLUMN_WIDTH + COLUMN_MARGIN) - alignString.length() + HALF_MARGIN.length(); addedSpaces = ""; for ( int spaceAdd = 0; spaceAdd <= spacesDiff; spaceAdd++ ) { addedSpaces += " "; } alignString = addedSpaces; System.out.print( alignString ); // Previous Outstanding is right-aligned and .00 completed // .00 completion is the method of adding the final 0 on numbers // which do not have them. Checking the second last character // in the string will determine if a 0 has to be added. outCompleted = "$ " + amountLeft + ""; completionLen = outCompleted.length( ); completionDecimal = outCompleted.charAt( completionLen - DECIMAL_POS ); if ( completionDecimal == '.' ) { outCompleted += "0"; } alignString = outCompleted; spacesDiff = ( COLUMN_WIDTH ) - alignString.length( ); addedSpaces = ""; for ( int spaceAdd = 0; spaceAdd <= spacesDiff; spaceAdd++ ) { addedSpaces += " "; } alignString = addedSpaces + alignString; System.out.print( alignString ); spacesDiff = (COLUMN_WIDTH + (COLUMN_MARGIN / 2)) - alignString.length() + 1; addedSpaces = ""; for ( int spaceAdd = 0; spaceAdd <= spacesDiff; spaceAdd++ ) { addedSpaces += " "; } alignString = addedSpaces; System.out.print( alignString ); // Calculate the interest, oustanding and amount paid interest = amountLeft * INTEREST_RATE; paid = PAY_AMOUNT - interest; if ( paid < 0 ) { amountLeft += -paid; paid = 0; } amountLeft -= paid; // Round the interest, oustanding and amount paid interest = Math.round( interest * ROUNDER ); interest /= ROUNDER; amountLeft = Math.round( amountLeft * ROUNDER ); amountLeft /= ROUNDER; paid = Math.round( paid * ROUNDER ); paid /= ROUNDER; // Interest is right-aligned and .00 completed outCompleted = "$ " + interest + ""; completionLen = outCompleted.length( ); completionDecimal = outCompleted.charAt( completionLen - DECIMAL_POS ); if ( completionDecimal == '.' ) { outCompleted += "0"; } alignString = outCompleted; spacesDiff = ( COLUMN_WIDTH ) - alignString.length( ); addedSpaces = ""; for ( int spaceAdd = 0; spaceAdd <= spacesDiff; spaceAdd++ ) { addedSpaces += " "; } alignString = addedSpaces + alignString; System.out.print( alignString ); spacesDiff = (COLUMN_WIDTH + (COLUMN_MARGIN / 2)) - alignString.length() + 1; addedSpaces = ""; for ( int spaceAdd = 0; spaceAdd <= spacesDiff; spaceAdd++ ) { addedSpaces += " "; } alignString = addedSpaces; System.out.print( alignString ); if ( amountLeft > 0 ) { // PayAmount and AmountLeft are right-aligned and .00 completed outCompleted = "$ " + paid + ""; completionLen = outCompleted.length( ); completionDecimal = outCompleted.charAt( completionLen - DECIMAL_POS ); if ( completionDecimal == '.' ) { outCompleted += "0"; } alignString = outCompleted; spacesDiff = ( COLUMN_WIDTH ) - alignString.length( ); addedSpaces = ""; for ( int spaceAdd = 0; spaceAdd <= spacesDiff; spaceAdd++ ) { addedSpaces += " "; } alignString = addedSpaces + alignString; System.out.print( alignString ); spacesDiff = (COLUMN_WIDTH + (COLUMN_MARGIN / 2)) - alignString.length() + 1; addedSpaces = ""; for ( int spaceAdd = 0; spaceAdd <= spacesDiff; spaceAdd++ ) { addedSpaces += " "; } alignString = addedSpaces; System.out.print( alignString ); outCompleted = "$ " + amountLeft + ""; completionLen = outCompleted.length( ); completionDecimal = outCompleted.charAt( completionLen - DECIMAL_POS ); if ( completionDecimal == '.' ) { outCompleted += "0"; } alignString = outCompleted; spacesDiff = ( COLUMN_WIDTH ) - alignString.length( ); addedSpaces = ""; for ( int spaceAdd = 0; spaceAdd <= spacesDiff; spaceAdd++ ) { addedSpaces += " "; } alignString = addedSpaces + alignString; System.out.print( alignString ); spacesDiff = (COLUMN_WIDTH + (COLUMN_MARGIN / 2)) - alignString.length() + 1; addedSpaces = ""; for ( int spaceAdd = 0; spaceAdd <= spacesDiff; spaceAdd++ ) { addedSpaces += " "; } alignString = addedSpaces; System.out.print( alignString ); } else { overPaid = PAY_AMOUNT + amountLeft; outCompleted = "$ " + overPaid + ""; completionLen = outCompleted.length( ); completionDecimal = outCompleted.charAt( completionLen - DECIMAL_POS ); if ( completionDecimal == '.' ) { outCompleted += "0"; } // OverPaid and no value are right-aligned and .00 completed alignString = outCompleted; spacesDiff = ( COLUMN_WIDTH ) - alignString.length( ); addedSpaces = ""; for ( int spaceAdd = 0; spaceAdd <= spacesDiff; spaceAdd++ ) { addedSpaces += " "; } alignString = addedSpaces + alignString; System.out.print( alignString ); spacesDiff = (COLUMN_WIDTH + (COLUMN_MARGIN / 2)) - alignString.length() + 1; addedSpaces = ""; for ( int spaceAdd = 0; spaceAdd <= spacesDiff; spaceAdd++ ) { addedSpaces += " "; } alignString = addedSpaces; System.out.print( alignString ); alignString = outZero; spacesDiff = ( COLUMN_WIDTH ) - alignString.length( ); addedSpaces = ""; for ( int spaceAdd = 0; spaceAdd <= spacesDiff; spaceAdd++ ) { addedSpaces += " "; } alignString = addedSpaces + alignString; System.out.print( alignString ); spacesDiff = (COLUMN_WIDTH + (COLUMN_MARGIN / 2)) - alignString.length() + 1; addedSpaces = ""; for ( int spaceAdd = 0; spaceAdd <= spacesDiff; spaceAdd++ ) { addedSpaces += " "; } alignString = addedSpaces; System.out.print( alignString ); } System.out.print( "\n" ); } // .00 completion for mortgage amount and pay amount. outCompleted = "$ " + MORTGAGE_AMOUNT + ""; completionLen = outCompleted.length( ); completionDecimal = outCompleted.charAt( completionLen - DECIMAL_POS ); if ( completionDecimal == '.' ) { outCompleted += "0"; } outMortgageAmount = outCompleted; outCompleted = "$ " + PAY_AMOUNT + ""; completionLen = outCompleted.length( ); completionDecimal = outCompleted.charAt( completionLen - DECIMAL_POS ); if ( completionDecimal == '.' ) { outCompleted += "0"; } System.out.println( "\n\nMortage of " + outMortgageAmount + " at " + INTEREST_STR + " interest was paid in chunks of " + outCompleted + " over the course of " + year + " years." ); } }