|
-
April 13th, 2009, 07:59 AM
#5
Re: Displaying/Formatting Output
 Originally Posted by tbk0de
It works well enough and it seems that the printf will do the same thing only it might be shorter to type than me putting in all the spaces, either way doesnt make too much of a difference to me. Is there something that I may be missing or something that I could be doing better or it this just about it?
You could tidy it up by putting the literal spaces into named String constants. You could use the DecimalFormat class to format your amounts with 2 decimals, currency symbol and thousands separators. You could make the decimal points of the money amounts line up so it all looks neat...
If you want to do all this, break the formatting problem down into simple chunks. You want to print out a number of lines, all formatted the same - so make a printLine method. Each line has 3 items aligned in columns, so give the printLine method 3 parameters of items to put in the columns. You want each column to line up right-aligned, which means adjusting the number of leading spaces according to the length of the item, so make a getColumn method that adds the right amount of spaces to an item for printing. It's not the amount of code that matters when deciding to break a method down into smaller methods, it's the structure of the problem and readability. Look for common patterns & themes, avoid repeating code, and simplify to make life easier, for example:
Code:
private static final String SPACES = " ";
private static final String DEP = "Deposit";
private static final String WD = "Withdrawal";
private static final String BAL = "Balance";
public static void main(String[] args) {
//create a scanner
Scanner input = new Scanner(System.in);
//prompt user for account info
System.out.println("Please input your checking account information... ");
System.out.println();
float balance = 0;
int end = 1;
float deposit = 0;
float withdrawal = 0;
int surcharge = 0;
float interest = 0;
// Create a formatter for the amounts (the \u00A4 is the currency symbol for the locale
// you could hard code it instead, e.g: "$###,##0.00"
NumberFormat formatter = new DecimalFormat("\u00A4###,##0.00");
// Print the 3 column headings
printLine("Deposit", "Withdrawal", "Balance");
while (end != 0) {
float nextFloat = input.nextFloat();
if (nextFloat == 0) {
end = 0;
}
if (nextFloat > 0) {
deposit = nextFloat;
balance = balance + nextFloat;
// format the values to print
final String depStr = formatter.format(deposit);
final String balStr = formatter.format(balance);
// print the line (note empty withdrawal string)
printLine(depStr, "", balStr);
}
if (nextFloat < 0) {
withdrawal = withdrawal + nextFloat;
if (balance < 0) {
balance = balance - 10;
surcharge = surcharge + 10;
}
balance = balance + nextFloat;
// format the values to print
final String wdStr = formatter.format(withdrawal);
final String balStr = formatter.format(balance);
// print the line (note empty deposit string)
printLine("", wdStr, balStr);
}
}
System.out.println("-----------------------------------------------------");
System.out.println("Surcharge: " + surcharge + " balance: " + balance);
interest = (float) (balance * .005);
balance = interest + balance;
System.out.println("Interest: " + interest + " balance: " + balance);
}
// print 3 column items
static void printLine(String col1, String col2, String col3) {
String outString = getColumn(col1) + getColumn(col2) + getColumn(col3);
System.out.println(outString);
}
// return a constant-width column string, right aligned
static String getColumn(String columnText) {
return SPACES.substring(columnText.length()) + columnText;
}
Incidentally, using floating-point types for money is a bad idea unless you want to do a lot of fiddly rounding to avoid errors (floating-point can't represent all integers exactly). It's better to use integral types such as integer, long, or the BigInteger class, and calculate the amounts in the smallest denomination (cents, pence, etc). Then you only need the decimals when it comes to formatting your output.
ok how come I cant type multiple spaces in any post?
The forum, like most forums, works with HTML text, so multiple spaces are reduced to one. If you want to keep the formatting and spaces, post your code in CODE tags.
Programs must be written for people to read, and only incidentally for machines to execute...
H. Abelson and G. Sussman
Last edited by dlorde; April 13th, 2009 at 08:15 AM.
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|