In: Computer Science
This is a very simple program that will ONLY print a heading and columns of text to emulate the following information:
Sales This Month |
Admin Fee |
Commission Rate |
---------------------------------------------- |
---------------------------------------------- |
---------------------------------------------- |
Less than $10,000 |
$150 |
5% |
$10,000 – 14,999 |
$150 |
10% |
$15,000 – 17,999 |
$150 |
12% |
$18,000 – 21,999 |
$200 |
15% |
$22,000 or more |
$200 |
16% |
******Salesperson of the month******
Name: STUDENT NAME
ID: STUDENT ID
public class SalesResult {
public static void main(String[] args) {
System.out.println();
. . .
}
}
Create a Java project in Eclipse named SalesProject and add a class named SalesResult to reproduce a table like the above. In your program repeat the System.out.println(); as many times as needed.
I was wondering how to write the code to create such a table...
//------------ SalesResult.java -------------------
public class SalesResult
{
public static void printLine()
{
System.out.println("______________________________________________________________________");
}
public static void main(String[] args)
{
//headers for table here mention
your headers
String[] headers = {"Sales This
Month" , "Admin Fee" , "Commission Rate"};
//create a table of contents to be
displayed
//here mention your table
rows.
//NOTE: IF YOU GIVE MORE COLUMNS
THAN THE HEADERS LENGTH
//FIRST HEADER LENGTH COLUMNS WILL
BE CONSIDERED.
//EXAMPLE HERE HEADERS LENGTH IS 3,
IF I INCLUDE > 3 COLUMNS IN
//MY ROWS THEN FIRST 3 WILL BE
CONSIDERED
String[][] table = {
//ROWS.
{"Less than
$10,000" , "$150" , "5%"},
{"$10,000 –
14,999" ,"$150" ,"10%"},
{"$15,000 –
17,999" , "$150" ,"12%"},
{"$18,000 –
21,999" , "$200" ,"15%"},
{"$22,000 or
more" , "$200" ,"16%"}
};
System.out.println();
int i,j;
//length is the total columns or
headers int table.
int length = headers.length;
//printLine() for printing a
horizentol line
printLine();
//print headers
System.out.print("\n");
for(i = 0 ;i<length;i++)
{
System.out.print(String.format("| %-20s ",headers[i]));
}
//print closing | for row.
System.out.println("|");
//print line
printLine();
System.out.println();
//print dashes like in the given
table for
//since there are length columns we
have to print those many dashes columns.
for(i=0;i<length;i++)
{
System.out.print(String.format("| %-20s
","-----------------"));
}
//print closing | for row.
System.out.println("|");
//print dashes like in the given
table for
//since there are length columns we
have to print those many dashes columns.
for(i = 0;i<length;i++)
{
System.out.print(String.format("| %-20s ","--------------"));
}
//print closing | for row.
System.out.println("|");
printLine();
//now get number of rows.
int rows = table.length;
for(i = 0;i<rows;i++)
{
//print first
length columns of i_th row .
System.out.print("\n");
for(j =
0;j<length;j++)
{
System.out.print(String.format("| %-20s
",table[i][j]));
}
//print closing
| for row and horizentol line.
System.out.println("|");
printLine();
}
System.out.println("\n******
Salesperson of the month ******\n");
}
}
//--------------- OUTPUT -----------------