Tab and/or Table Question?
Is there a way to make the output print under headings like columns on
a simple Java app?
My code is fine, it complies and works and even has an illusion of
printing the way I want it too. But what I want is for the output to actually
print under headings.
//Inventory Program Part 1
//Inventory enters and displays Part information
/*
*Parts used for testing include Glass Break: part # FG730,
*Motion Detector: part# Aurora, and W/L Contact: part # 5816.
*any quantity for units on hand and unit price is appropriate
*for testing purposes.
*/
import java.util.Scanner;// program uses class Scanner
import java.text.*;
publicclass Inventory
{
privatestaticvoid Quit()
{
System.out.println("Goodbye");
System.exit (0);
}
// main method begins execution of Java application
publicstaticvoid main(String args[])
{
Part part =new Part();
// create Scanner to obtain input from command window
Scanner input =new Scanner(System.in);
while(true)// starts loop to test name
{
System.out.print("\nEnter Part Name (stop to quit): ");// prompt for name
part.setName(input.nextLine());// get name
if(part.getName().compareToIgnoreCase("stop") == 0)
{
System.out.println("Stop entered, Thank you");
Quit();
}//end if
System.out.print("Enter part number for " + part.getName() +": ");// prompt
part.setitemNum(input.nextLine());// read part number from user
System.out.print("Enter Units on hand: ");// prompt
part.setunits(input.nextDouble());// read second number from user
if(part.getunits() <= 0)
{
System.out.println ("Invalid amount, Units on hand worked must be positive");
System.out.print("Please enter actual units on hand: ");
part.setunits(input.nextDouble());
}//end if
System.out.print("Enter Unit Price: $ ");// prompt
part.setprice(input.nextDouble());// read third number from user
if(part.getprice() <= 0)
{
System.out.println ("Invalid amount, Unit Price must be positive");
System.out.print("Please enter actual unit price: $ ");
part.setprice(input.nextDouble());
}//end if
System.out.println("Part #\tPart Name\tUnits On Hand\tUnit Cost\tTotal Cost" );
System.out.printf("%s\t%s\t%.2f\t%.2f\t$%.2f\n",
part.getitemNum(), part.getName(), part.getunits(), part.getprice(), part.getvalue());
input.nextLine();
}//end while
}// end method main
}// end class Inventory
// Class Part holds Part information
class Part
{
private String name;
private String itemNum;
privatedouble units;
privatedouble price;
//default constructor
public Part()
{
name ="";
itemNum ="";
units = 0;
price = 0;
}//end default constructor
//Parameterized Constructor
public Part(String name, String itemNum,double units,double price)
{
this.name = name;
this.itemNum = itemNum;
this.units = units;
this.price = price;
}//end constructor
publicvoid setName(String name){
this.name = name;
}
String getName()
{
return name;
}
publicvoid setitemNum ( String itemNum )
{
this.itemNum = itemNum;
}
public String getitemNum()
{
return itemNum;
}
publicvoid setunits (double units )
{
this.units = units;
}
publicdouble getunits()
{
return units;
}
publicvoid setprice (double price )
{
this.price = price;
}
publicdouble getprice()
{
return price;
}
publicdouble getvalue()
{
return (units * price);
}
}//end Class Part

