|
| |
So where are we? So far, we have:
|ID|Name|Address|Date of Birth|Salary|
|0001|Bloggs, Fred|10 Firebird Crescent|1976-05-20|$40,000.00|
|0002|Charles, Cheryl|5 Lattice Lane|1989-04-03|$50,000.00|
|0003|Romney, Titus|42 St. Martins Rd.|1973-11-19|$45,000.00|
|0004|Clinton, Burton|567 Nebula Circle|1964-01-29|$100,000.00|
|0005|McDuff, Hazel|1 Church Row|1985-03-06|$55,000.00|
|0006|Frazier, Bill|92 S. Main St.|1980-12-05|$70,000.00|
|0007|Zambroski, Heather|589 Trumpington Place|1959-04-03|$50,000.00|
Let's finally fix those spacing and alignment problems.
Here are my final changes:
package inputOutput;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* Class to produce a table of information about customers
*/
public class EmployeeTablePrinter3
{
public static void main(String[] args)
{
List<Employee3> employees = new ArrayList<Employee3>();
// Populate list with Employees
employees.add( new Employee3(1, "Bloggs, Fred", "10 Firebird Crescent",
new Date(1976 - 1900, 5 - 1, 20), 40000.00 ) );
employees.add( new Employee3(2, "Charles, Cheryl", "5 Lattice Lane",
new Date(1989 - 1900, 4 - 1, 3), 50000.00 ) );
employees.add( new Employee3(3, "Romney, Titus", "42 St. Martins Rd.",
new Date(1973 - 1900, 11 - 1, 19), 45000.00 ) );
employees.add( new Employee3(4, "Clinton, Burton", "567 Nebula Circle",
new Date(1964 - 1900, 1 - 1, 29), 100000.00 ) );
employees.add( new Employee3(5, "McDuff, Hazel", "1 Church Row",
new Date(1985 - 1900, 3 - 1, 6), 55000.00 ) );
employees.add( new Employee3(6, "Frazier, Bill", "92 S. Main St.",
new Date(1980 - 1900, 12 - 1, 5), 70000.00 ) );
employees.add( new Employee3(7, "Zambroski, Heather", "589 Trumpington Place",
new Date(1959 - 1900, 4 - 1, 3), 50000.00 ) );
// Print out the table of employees
Employee3.printHeader();
for (Employee3 emp : employees)
{
emp.print();
}
}
}
/**
* Class to represent an employee
*/
class Employee3
{
Employee3(int id, String name, String address, Date dob, double salary)
{
m_id = id;
m_name = name;
m_address = address;
m_dob = dob;
m_salary = salary;
}
static void printHeader()
{
System.out.println(ROW_SEPARATOR);
System.out.printf("| %-4s | %-20s | %-25s | %-14s | %-13s |\n",
"ID", "Name", "Address", "Date of Birth", "Salary");
System.out.println(ROW_SEPARATOR);
}
void print()
{
System.out.printf("| %04d | %-20s | %-25s | %-14tF | $%,12.2f |\n",
m_id, m_name, m_address, m_dob, m_salary);
System.out.println(ROW_SEPARATOR);
}
private int m_id;
private String m_name;
private String m_address;
private Date m_dob;
private double m_salary;
static private String ROW_SEPARATOR =
"+------+----------------------+---------------------------+----------------+---------------+";
}
|
which produces the following output:
+------+----------------------+---------------------------+----------------+---------------+
| ID | Name | Address | Date of Birth | Salary |
+------+----------------------+---------------------------+----------------+---------------+
| 0001 | Bloggs, Fred | 10 Firebird Crescent | 1976-05-20 | $ 40,000.00 |
+------+----------------------+---------------------------+----------------+---------------+
| 0002 | Charles, Cheryl | 5 Lattice Lane | 1989-04-03 | $ 50,000.00 |
+------+----------------------+---------------------------+----------------+---------------+
| 0003 | Romney, Titus | 42 St. Martins Rd. | 1973-11-19 | $ 45,000.00 |
+------+----------------------+---------------------------+----------------+---------------+
| 0004 | Clinton, Burton | 567 Nebula Circle | 1964-01-29 | $ 100,000.00 |
+------+----------------------+---------------------------+----------------+---------------+
| 0005 | McDuff, Hazel | 1 Church Row | 1985-03-06 | $ 55,000.00 |
+------+----------------------+---------------------------+----------------+---------------+
| 0006 | Frazier, Bill | 92 S. Main St. | 1980-12-05 | $ 70,000.00 |
+------+----------------------+---------------------------+----------------+---------------+
| 0007 | Zambroski, Heather | 589 Trumpington Place | 1959-04-03 | $ 50,000.00 |
+------+----------------------+---------------------------+----------------+---------------+
While far from ideal, it certainly beats our original, crude
attempt!
Notes:
- I added a single space on either side of the data item to prevent it from
being forced up against the column separator.
- I set minimum widths for the fields, to allow each column to be a standard
width, including the header.
- I forced left alignment on all the header items, the names, addresses and
dates of birth. The default alignment is right aligned, and that is
most natural for the salary column, where you want the decimal points to
align correctly.
- Note however, that right alignment causes a slight problem with the salary
format: it causes the dollar sign to be separated from the amount. If
you wanted to cause the dollar sign to be right up against the amount, then
we could do that, but we'd have to do two levels of formatting.
|