[Tutor] printing puzzlement

Rob rob@uselesspython.com
Sat, 10 Aug 2002 09:27:41 -0500


This is a multi-part message in MIME format.

------=_NextPart_000_000A_01C24050.2CC4EDE0
Content-Type: text/plain;
	charset="us-ascii"
Content-Transfer-Encoding: 7bit

I completely agree that a refactoring is in order. What I had in mind to do
was to demonstrate how the code could be translated more or less literally,
then show how it could be improved, and why. (The C++ code was donated
as-is, and I had in mind to at least make it more readable. The original
.cpp file is attached to this message.)

After I've had some coffee, I'll take a good look at what you've got here.

Thanks,
Rob

> -----Original Message-----
> From: Danny Yoo [mailto:dyoo@hkn.eecs.berkeley.edu]
> Sent: Saturday, August 10, 2002 1:43 AM
> To: Rob
> Cc: Python Tutor
> Subject: Re: [Tutor] printing puzzlement
>
>
>
>
> On Fri, 9 Aug 2002, Rob wrote:
>
> > As seen in the C++ code snippet here, calculations are performed in the
> > middle of the display of each line of output.
>
> Yikes.  It's usually not a good thing when "presentation logic" --- the
> table display --- is so closely tied to the calculation logic.  Perhaps we
> can try to refactor this, and yank out the calculation into separate
> functions.
>
>
>
> > 	cout <<	"Pmt #  Cur. Prin.  Payment  Int. Paid  Rem. Prin." << endl;
> > 	cout <<
> > 		"-----  ----------  -------  ---------  ----------" << endl;
> > 	for (int index = 0; index < numPayments; index++)
> > 	{
> > 		cout << setw(5) << right << (index + 1);
> > 		cout << setw(12) << right << fixed
> >                    << setprecision(2) << principle;
> > 		cout << setw(9) << right << fixed << amtPayment;
> > 		float interest =
> >                   (float (int (principle * monthRate * 100) ) ) / 100.0;
> > 		cout << setw(11) << right << fixed << interest;
> > 		principle = principle + interest - amtPayment;
> > 		cout << setw(12) << right << fixed << principle;
> > 		cout << endl;
> > 	}
>
>
> Here's one possible way to recode this:
>
>
> ###
> ## Warning: untested code
>
> class AmortizationSchedulePrinter:
>
>     def __init__(self, starting_principle, amtPayment, monthRate):
>         self.startingPrinciple = startingPrinciple
>         self.amtPayment = amtPayment
>         self.monthRate = monthRate
>
>
>     def printAmortizationSchedule(self):
>         self.printHeader()
>         p = self.startingPrinciple
>         for index in range(self.numPayments):
>             printReportLine(index, p)
>             p = self.nextPrinciple(p)
>
>
>     def printReportLine(self, index, principle):
>         print ("%5f"
>                "%12.2f"
>                "%9.2f"
>                "%11.2f"
>                "%12.2f") % \
>               (index + 1,
>                principle,
>                self.amtPayment,
>                self.interest(principle),
>                self.nextPrinciple(principle))
>
>
>     def nextPrinciple(self, principle):
>         return principle + self.interest(principle) - self.amtPayment;
>
>
>     def interest(self, principle):
>         return (float (int (principle * self.monthRate * 100) ) ) / 100.0;
>
>
>     def printHeader(self):
>         print "Pmt #  Cur. Prin.  Payment  Int. Paid  Rem. Prin."
>         print "-----  ----------  -------  ---------  ----------"
> ###
>
>
> It is a bit longer than the original version, but it does allow us to make
> changes to the line format more easily, since all of the formatting stuff
> is in printHeader() and printReportLine().
>
>
> Hope this helps!
>
>

------=_NextPart_000_000A_01C24050.2CC4EDE0
Content-Type: application/octet-stream;
	name="amortization.cpp"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="amortization.cpp"

#include <iostream>
#include <iomanip>
#include <math.h>

using namespace std;

int main()
{
	// Get the loan specifics
	cout << "Enter the loan amount: ";
	float principle;
	cin >> principle;

	cout << "Enter the annual percentage rate (APR) as a percent: ";
	float annualRate;
	cin >> annualRate;
	float monthRate =3D annualRate / 1200.0;

	cout << "Enter the number of payments: ";
	unsigned numPayments;
	cin >> numPayments;

	// Calculate the payment amount
	float amtPayment =3D (principle * monthRate * pow((1.0 + monthRate), =
numPayments))
		/ (pow((1.0 + monthRate), numPayments) - 1.0);
	// Truncate payment to whole cents
	amtPayment =3D (float (int (amtPayment * 100))) / 100.0;
	cout << endl;
	cout << "The monthly payment is $" << fixed << setprecision(2) << =
amtPayment << endl;

	// Display the amortization schedule
	cout <<
		"Pmt #  Cur. Prin.  Payment  Int. Paid  Rem. Prin." << endl;
	cout <<
		"-----  ----------  -------  ---------  ----------" << endl;
	for (int index =3D 0; index < numPayments; index++)
	{
		cout << setw(5) << right << (index + 1);
		cout << setw(12) << right << fixed << setprecision(2) << principle;
		cout << setw(9) << right << fixed << amtPayment;
		float interest =3D (float (int (principle * monthRate * 100) ) ) / =
100.0;
		cout << setw(11) << right << fixed << interest;
		principle =3D principle + interest - amtPayment;
		cout << setw(12) << right << fixed << principle;
		cout << endl;
	}

	// Wrapup and exit
	cout << endl;
	return 0;
}

------=_NextPart_000_000A_01C24050.2CC4EDE0--