Question
Below is my two codes..I am trying to figure out how to add string builder to the second code to display the records in a
Below is my two codes..I am trying to figure out how to add string builder to the second code to display the records in a table format. please advise. Thanks!!
class Automobile
{
private int ID; // private instance variables
private String model;
private String color;
private int year;
private int milesPerGallon;
Automobile(int ID, String model, String color, int year, int milesPerGallon)
{
setID(ID);
setModel(model);
setColor(color);
setYear(year);
setMilesPerGallon(milesPerGallon);
}
//getters
public int getID()
{
return this.ID;
}
public String getModel()
{
return this.model;
}
public String getColor()
{
return this.color;
}
public int getYear()
{
return this.year;
}
public int getMilesPerGallon()
{
return this.milesPerGallon;
}
//setters
public void setID(int ID)
{
if(ID<0 || id>9999)
{this.ID = 0;}
else
{this.ID = ID;}
}
public void setModel(String model)
{this.model = model;}
public void setColor(String color)
{this.color = color;}
public void setYear(int year)
{
if(year<2005 || year>2021)
{this.year = 0;}
else
{this.year = year;}
}
public void setMilesPerGallon(int milesPerGallon)
{
if(milesPerGallon<10 || milespergallon>60)
{this.milesPerGallon = 0;}
else
{this.milesPerGallon = milesPerGallon;}
}
}
_____________________________________________________________________________________________________
import javax.swing.JOptionPane;
import java.util.*;
public class TestAutomobiles
{
public static Automobile createAutomobile()
{
String model,color;
int ID,year,milesPerGallon;
JOptionPane.showMessageDialog(null, "Please Enter Automobile Data:",
"Welcome to Auto Inventory!",
JOptionPane.INFORMATION_MESSAGE);
int id = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter Auto ID"));
model = JOptionPane.showInputDialog(null, "Enter Model");
color = JOptionPane.showInputDialog(null, "Enter Color");
int Year = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter Year"));
int MilesPerGallon = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter Miles Per Gallon"));
Automobile res = new Automobile(id, model, color, Year, MilesPerGallon);
return res;
}
public static void main(String[] args)
{
Automobile automobile1 = createAutomobile();
Automobile automobile2 = createAutomobile();
displayAutomobile(automobile1);
displayAutomobile(automobile2);
}
public static void displayAutomobile(Automobile auto)
{
String msg;
if(auto.getID()==0 || auto.getYear()==0 || auto.getMilesPerGallon()==0)
{msg = "Bad Inventory Record";}
else
{ msg = "Good Inventory Record";}
/* String builder class here to display
inventory records in a table format with headers if possible*/
//try to add header here but it prints twice
System.out.println(" Type of Inventory: " + " " + "ID" + " " + "Model" + " " + "Color" + " " + "Year" + " " + " MPG");
System.out.println(msg + " " + auto.getID() + " " + auto.getModel() +
" " + auto.getColor() + " " + auto.getYear() + " " +
auto.getMilesPerGallon() + " miles per gallon");
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Answer To display the inventory records in a table format with headers using StringBuilder you can m...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started