hi everyone
can you help me with this code i spent the past week trying to find out what is wrong with it
this is what the program should do (the user enters the car specifications and i have to search for it from a car list in a txt file
i created 3 classes one is Interface ,Car and CarSpec the code is this
the code for the interface class
the code for the Car classCode:import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.JOptionPane;
public class Interface implements ActionListener {
// declaring variables
private JPanel firstWindow,secWindow,infoPanel,labelsPanel,headerPanel,specPanel,btnPanel,secHeaderPanel,secListPanel,secBtnPanel;
private JButton btnSearch,btnBack,btnCancel;
private JLabel lblTitle,lblRequest,lblTypeCar,lblMake,lblModel,lblEngineSize,lblFuel,lblAge,lblColour,lblName,lblPhone,lblEmail,lblMr,lblResult,lblList;
private JComboBox typeCB,makeCB,modelCB,engineCB,fuelCB,ageCB,colourCB;
static String Name,Email,Phone;
String typeString,makeString,modelString,engineString,fuelString,ageString,colourString;
String []type={"Any","Saloon","Estat","SUV","Sports"};
String []make={"Any","Ford" ,"Honda" ,"Toyota","BMW"};
String[]model={"Any","Mondeo" ,"CR-V Sport","Celica Coupe","Series 3","Z4 Coupe","Series 6 M5 5.0","Jazz" ,"Focus C-Max","Aygo","Landcruiser Amazon","Fiesta","X3 Sport"};
String []engine={"Any","1.6","1.8","2.0","2.2","3.0","5.0","1.2", "5.0", "1.0", "1.1","4.7","1.4","2.5"};
String []fuel={"Any","Diesel","Petrol"};
String []age ={"Any","12","6","17","18","14","8","5","25","24","8","28","38", "39","26"};
String []colour={"Any","Blue","Red","Silver","Black","White","Green" };
String carResult[][]=new String [15][9];
public static final int BOLD = 0;
//the main method of the program
public static void main(String[] args) {
// TODO Auto-generated method stub
Name = JOptionPane.showInputDialog ( "Enter your name ");
Phone = JOptionPane.showInputDialog ( "Enter your Phone number");
Email = JOptionPane.showInputDialog ( "Enter your Email ");
SwingUtilities.invokeLater(new Runnable(){
public void run(){//calling the creatAndShowGUI (the frame) method
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
// TODO Auto-generated method stub
JFrame.setDefaultLookAndFeelDecorated(true);//setting the look of the frame
JFrame frame = new JFrame("Whizz Cars");//naming the frame
Interface demo = new Interface();
frame.setContentPane(demo.createContentPane());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(650,520);//setting the size of the frame
frame.setVisible(true);
}
public JPanel createContentPane() {
// TODO Auto-generated method stub
JPanel TotalGUI=new JPanel();
TotalGUI.setBackground(Color.lightGray);
firstWindow=new JPanel();
firstWindow.setBackground(Color.LIGHT_GRAY);
firstWindow.setVisible(true);
firstWindow.setLayout(new BorderLayout(15,15));
TotalGUI.add(firstWindow);
headerPanel = new JPanel ();
headerPanel.setBackground(Color.LIGHT_GRAY);
headerPanel.setPreferredSize(new Dimension(600,80));
headerPanel.setVisible(true);
firstWindow.add(headerPanel,BorderLayout.PAGE_START);
Font bigFont = new Font("Braggadocio",BOLD,30);
lblTitle = new JLabel(" Whizz Car Sales");
lblTitle.setForeground(Color.RED);
lblTitle.setPreferredSize(new Dimension(450,50));
lblTitle.setFont(bigFont);
headerPanel.add(lblTitle);
Font midFont=new Font("Arial",BOLD,17);
lblRequest=new JLabel("Please select the car's specifications form below... ");
lblRequest.setForeground(Color.black);
lblRequest.setFont(midFont);
lblRequest.setPreferredSize(new Dimension(600,20));
headerPanel.add(lblRequest);
infoPanel= new JPanel();
infoPanel.setBackground(Color.GRAY);
infoPanel.setVisible(true);
infoPanel.setPreferredSize(new Dimension(250,300));
firstWindow.add(infoPanel,BorderLayout.LINE_START);
lblName=new JLabel("Hello Mr/Mrs : "+Name);
lblName.setForeground(Color.BLACK);
lblName.setPreferredSize(new Dimension(240,30));
infoPanel.add(lblName);
lblPhone=new JLabel("Phone Number : "+Phone);
lblPhone.setForeground(Color.BLACK);
lblPhone.setPreferredSize(new Dimension(240,30));
infoPanel.add(lblPhone);
lblEmail=new JLabel("Email : "+Email);
lblEmail.setForeground(Color.BLACK);
lblEmail.setPreferredSize(new Dimension(240,30));
infoPanel.add(lblEmail);
labelsPanel =new JPanel();
labelsPanel.setBackground(Color.GRAY);
labelsPanel.setVisible(true);
labelsPanel.setPreferredSize(new Dimension(170,300));
firstWindow.add(labelsPanel,BorderLayout.CENTER);
lblTypeCar=new JLabel("Type of the Car");
lblTypeCar.setForeground(Color.ORANGE);
lblTypeCar.setPreferredSize(new Dimension(100,40));
labelsPanel.add(lblTypeCar);
lblMake=new JLabel("Car Maker");
lblMake.setForeground(Color.ORANGE);
lblMake.setPreferredSize(new Dimension(100,40));
labelsPanel.add(lblMake);
lblModel=new JLabel("Car Model");
lblModel.setForeground(Color.ORANGE);
lblModel.setPreferredSize(new Dimension(100,40));
labelsPanel.add(lblModel);
lblEngineSize =new JLabel("Engine Size");
lblEngineSize.setForeground(Color.ORANGE);
lblEngineSize.setPreferredSize(new Dimension(100,40));
labelsPanel.add(lblEngineSize);
lblFuel =new JLabel("Type of Fuel ");
lblFuel.setForeground(Color.ORANGE);
lblFuel.setPreferredSize(new Dimension(100,40));
labelsPanel.add(lblFuel);
lblAge =new JLabel("Car's Age");
lblAge.setForeground(Color.ORANGE);
lblAge.setPreferredSize(new Dimension(100,40));
labelsPanel.add(lblAge);
lblColour =new JLabel(" Colour of the Car ");
lblColour.setForeground(Color.ORANGE);
lblColour.setPreferredSize(new Dimension(120,40));
labelsPanel.add(lblColour);
specPanel = new JPanel();
specPanel.setBackground(Color.GRAY);
specPanel.setVisible(true);
specPanel.setPreferredSize(new Dimension(170,320));
firstWindow.add(specPanel,BorderLayout.LINE_END);
// String[]type = {"ANY","Saloon","Estate","SUV","Sports"};
typeCB = new JComboBox(type);
typeCB.setPreferredSize(new Dimension (150,40));
typeCB.setVisible(true);
specPanel.add(typeCB);
// String[]make = {"ANY","Ford","Toyota","BMW","Honda"};
makeCB = new JComboBox(make);
makeCB.setPreferredSize(new Dimension (150,40));
makeCB.setVisible(true);
specPanel.add(makeCB);
// String[]model = {"ANY","Mondeo","CR-V Sport","Celica Coupe","Series 3","Z4 Coupe","Series 6 M5 5.0","Jazz","Focus C-Max","Aygo","Landcruiser Amazon","Fiesta","X3 Sport"};
modelCB = new JComboBox(model);
modelCB.setPreferredSize(new Dimension (150,40));
modelCB.setVisible(true);
specPanel.add(modelCB);
// String[]engin={"ANY","1.2","1.4","1.6","1.8","2.0"};
engineCB=new JComboBox(engine);
engineCB.setPreferredSize(new Dimension (150,40));
engineCB.setVisible(true);
specPanel.add(engineCB);
// String []fuel={"Any","Unleaded","Diesel"};
fuelCB=new JComboBox(fuel);
fuelCB.setPreferredSize(new Dimension (150,40));
fuelCB.setVisible(true);
specPanel.add(fuelCB);
// String[]age = {"Any","0-12","13-24","25-36"};
ageCB=new JComboBox(age);
ageCB.setPreferredSize(new Dimension (150,40));
ageCB.setVisible(true);
specPanel.add(ageCB);
colourCB=new JComboBox(colour);
colourCB.setPreferredSize(new Dimension (150,40));
colourCB.setVisible(true);
specPanel.add(colourCB);
btnPanel = new JPanel();
btnPanel.setBackground(Color.GRAY);
btnPanel.setVisible(true);
btnPanel.setPreferredSize(new Dimension(170,50));
firstWindow.add(btnPanel,BorderLayout.PAGE_END);
btnSearch= new JButton("Search...");
btnSearch.setPreferredSize(new Dimension (100,40));
btnSearch.setVisible(true);
btnSearch.addActionListener(this);
btnPanel.add(btnSearch);
secWindow=new JPanel();
secWindow.setBackground(Color.LIGHT_GRAY);
secWindow.setVisible(false);
secWindow.setLayout(new BorderLayout(30,15));
TotalGUI.add(secWindow);
secHeaderPanel = new JPanel();
secHeaderPanel.setBackground(Color.LIGHT_GRAY);
secHeaderPanel.setVisible(true);
secHeaderPanel.setPreferredSize(new Dimension(600,80));
secWindow.add(secHeaderPanel,BorderLayout.PAGE_START);
lblTitle = new JLabel(" Whizz Car Sales");
lblTitle.setForeground(Color.RED);
lblTitle.setPreferredSize(new Dimension(450,50));
lblTitle.setFont(bigFont);
secHeaderPanel.add(lblTitle);
lblMr = new JLabel(" Mr/Mrs : "+Name);
lblMr.setForeground(Color.BLACK);
lblMr.setFont(midFont);
lblMr.setPreferredSize(new Dimension (600,20));
secHeaderPanel.add(lblMr);
secListPanel=new JPanel();
secListPanel.setBackground(Color.gray);
secListPanel.setVisible(true);
secListPanel.setPreferredSize(new Dimension (600,320));
secWindow.add(secListPanel);
lblResult = new JLabel("These are the cars that match your selections .... ");
lblResult.setForeground(Color.BLACK);
lblResult.setFont(midFont);
lblResult.setPreferredSize(new Dimension(580,20));
secListPanel.add(lblResult);
lblList =new JLabel();
lblList.setForeground(Color.BLACK);
lblList.setFont(midFont);
lblList.setPreferredSize(new Dimension(580,350));
secListPanel.add(lblList);
secBtnPanel = new JPanel();
secBtnPanel.setBackground(Color.GRAY);
secBtnPanel.setVisible(true);
secBtnPanel.setPreferredSize(new Dimension(170,50));
secWindow.add(secBtnPanel,BorderLayout.PAGE_END);
btnBack= new JButton("Go Back");
btnBack.setPreferredSize(new Dimension (100,40));
btnBack.setVisible(true);
btnBack.addActionListener(this);
secBtnPanel.add(btnBack);
btnCancel= new JButton("Cancel");
btnCancel.setPreferredSize(new Dimension (100,40));
btnCancel.setVisible(true);
btnCancel.addActionListener(this);
secBtnPanel.add(btnCancel);
TotalGUI.setOpaque(true);
return TotalGUI;
}
public void display(){
for (int i=0;i<15;i++){
for(int j=0;j<9;j++){
if (carResult[i][0]!="0"){
lblList.setText(carResult[i][j]);
}
}
}
}
// @Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if (arg0.getSource()== btnSearch){
secWindow.setVisible(true);
firstWindow.setVisible(false);
typeString = (String)typeCB.getSelectedItem();
makeString = (String)makeCB.getSelectedItem();
modelString = (String)modelCB.getSelectedItem();
colourString = (String)colourCB.getSelectedItem();
fuelString = (String)fuelCB.getSelectedItem();
float engSize;
if (engineString.equals("ANY")){
engSize=0;
}
else{
engSize=Float.valueOf(engineString);
}
int ageInM;
if (ageString.equals("Any")){
ageInM=0;
}
else{
ageInM=Integer.valueOf(ageString);
}
CarSpec userCar =new CarSpec (typeString,makeString,modelString,engSize,fuelString,ageInM,colourString);
carResult=userCar.search();
}
if (arg0.getSource()== btnBack){
secWindow.setVisible(false);
firstWindow.setVisible(true);
}
if (arg0.getSource()== btnCancel){
secWindow.setVisible(false);
firstWindow.setVisible(true);
typeCB.setSelectedItem(type[0]);
makeCB.setSelectedItem(make[0]);
modelCB.setSelectedItem(model[0]);
engineCB.setSelectedItem(engine[0]);
fuelCB.setSelectedItem(fuel[0]);
ageCB.setSelectedItem(age[0]);
colourCB.setSelectedItem(colour[0]);
}
}
}
the code for the CarSpec classCode:public class Car {
private String regNumber ;
private double price;
CarSpec spec;
String carResult[][]=new String [15][9];
String [][]carList={ {"MA06 XVU", "8999.0", "Saloon" ,"Ford" ,"Mondeo","1.6" ,"Diesel", "12","Blue"},
{"MA06 UXW", "12900.0","Estate","Ford" ,"Mondeo","1.8" ,"Petrol", "6", "Red"},
{"MA04 EST", "15999.0", "SUV" ,"Honda" ,"CR-V Sport" ,"2.0" ,"Diesel", "17", "Silver" },
{"VZX 9512", "15499.0", "Sports" ,"Toyota","Celica Coupe","1.8" ,"Petrol","18", "Black" },
{"DV06 UHQ", "25499.0","Saloon" ,"BMW" ,"Series 3" ,"2.2" ,"Diesel", " 14", "Black" },
{"DV55 WVX", "21499.0","Sports" ,"BMW" ,"Z4 Coupe" ,"3.0" ,"Petrol", "8", "Silver"},
{"BN06 DHQ", "65499.0","Saloon" ,"BMW" ,"Series 6 M5 5.0" ,"5.0" ,"Petrol", " 5", "Black"},
{"LA54 VUN", "4499.0", "Saloon" ,"Honda" ,"Jazz" ,"1.2" ,"Diesel", " 25", "Green" },
{"DV06 MHQ", "65499.0", "Saloon" ,"BMW" ,"Series 6 M5 5.0","5.0" ,"Petrol", " 5", "Black"},
{"MW04 CXW", "6900.0","Estate" ,"Ford" ,"Focus C-Max" ,"1.6" ,"Petrol", "24", "Blue"},
{"VZ55 LRT", "5499.0", "Saloon" ,"Toyota","Aygo","1.0" ,"Petrol"," 8", "White" },
{"GH03 RPK", "4000.0", "Saloon" ,"Toyota","Aygo" ,"1.1" ,"Diesel", " 28", "Green" },
{"VH03 ZUK", "17500.0","SUV" ,"Toyota","Landcruiser Amazon", "4.7" ,"Diesel","38", "Black"},
{"WT05 KXW", "3900.0", "Estate" ,"Ford" ,"Fiesta" ,"1.4" ,"Petrol", "39", "Red"},
{"SA06 JTX", "17499.0","SUV" ,"BMW" ,"X3 Sport" ,"2.5" ,"Petrol", "26", "Blue"},};
public Car(String regNumber,double price,CarSpec spec)
{
this.regNumber=regNumber;
this.price = price;
this.spec = spec;
}
public double getPrice()
{// getter method of the price
return price;
}
public void setprice(double price)
{// setter method of the price
this.price = price;
}
public String getregNumber()
{// getter method of the RegNumber
return regNumber;
}
public CarSpec getspec()
{// getter method of the Spec
return spec;
}
public String[][] search (int[]Search){
int k=0,x=0;
for (int i=0;i<15;i++){
carResult[i][0]="0";
}
for (int z=0;z < Search.length;z++){
if (x==Search[z]){
for (int j=0;j<9;j++){
carResult [k][j]=carList[z][j];
}k++;
}
}
return carResult;
}
}
Code:public class CarSpec {
private String type;
private String make;
private String model;
private float engineSize;
private String fuel;
private int ageInMonths;
private String colour;
Interface g= new Interface();
int Search []=new int[15];
Car c =new Car(null, 0, null);
String carResult[][]=new String [15][9];
String [][]carList={
{"MA06 XVU", "8999.0", "Saloon" ,"Ford" ,"Mondeo","1.6" ,"Diesel", "12","Blue"},
{"MA06 UXW", "12900.0","Estate","Ford" ,"Mondeo","1.8" ,"Petrol", "6", "Red"},
{"MA04 EST", "15999.0", "SUV" ,"Honda" ,"CR-V Sport" ,"2.0" ,"Diesel", "17", "Silver" },
{"VZX 9512", "15499.0", "Sports" ,"Toyota","Celica Coupe","1.8" ,"Petrol","18", "Black" },
{"DV06 UHQ", "25499.0","Saloon" ,"BMW" ,"Series 3" ,"2.2" ,"Diesel", " 14", "Black" },
{"DV55 WVX", "21499.0","Sports" ,"BMW" ,"Z4 Coupe" ,"3.0" ,"Petrol", "8", "Silver"},
{"BN06 DHQ", "65499.0","Saloon" ,"BMW" ,"Series 6 M5 5.0" ,"5.0" ,"Petrol", " 5", "Black"},
{"LA54 VUN", "4499.0", "Saloon" ,"Honda" ,"Jazz" ,"1.2" ,"Diesel", " 25", "Green" },
{"DV06 MHQ", "65499.0", "Saloon" ,"BMW" ,"Series 6 M5 5.0","5.0" ,"Petrol", " 5", "Black"},
{"MW04 CXW", "6900.0","Estate" ,"Ford" ,"Focus C-Max" ,"1.6" ,"Petrol", "24", "Blue"},
{"VZ55 LRT", "5499.0", "Saloon" ,"Toyota","Aygo","1.0" ,"Petrol"," 8", "White" },
{"GH03 RPK", "4000.0", "Saloon" ,"Toyota","Aygo" ,"1.1" ,"Diesel", " 28", "Green" },
{"VH03 ZUK", "17500.0","SUV" ,"Toyota","Landcruiser Amazon", "4.7" ,"Diesel","38", "Black"},
{"WT05 KXW", "3900.0", "Estate" ,"Ford" ,"Fiesta" ,"1.4" ,"Petrol", "39", "Red"},
{"SA06 JTX", "17499.0","SUV" ,"BMW" ,"X3 Sport" ,"2.5" ,"Petrol", "26", "Blue"}};
public CarSpec(String type ,String make, String model,float engineSize ,String fuel , int ageInMonths,String colour)
{// making the constructor of the class
this.type = type;
this.make = make;
this.model = model;
this.engineSize = engineSize;
this.fuel= fuel;
this.ageInMonths = ageInMonths;
this.colour = colour;
}
public String gettype()
{// the getter method of the type
return type;
}
public String getmake()
{// the getter method of the make
return make;
}
public String getmodel()
{// the getter method of the model
return model;
}
public int getageInMonths()
{// the getter method of the age
return ageInMonths;
}
public float getengineSize()
{// the getter method of the engine size
return engineSize;
}
public String getfuel()
{// the getter method of the fuel
return fuel;
}
public String getcolour()
{// the getter method of the color
return colour;
}
public String[][] search(){
int x=0;
for(int y=0;y<=15;y++){
Search[y]=0;
}
for (int i=0;i>0;i++){
if (g.typeString !="Any"){
x++;
if (g.typeString == carList[i][2]){
Search[i]++;
}
}
}
for (int i=0;i>0;i++){
if (g.makeString !="Any"){
x++;
if (g.makeString == carList[i][2]){
Search[i]++;
}
}
}
for (int i=0;i>0;i++){
if (g.modelString !="Any"){
x++;
if (g.modelString == carList[i][2]){
Search[i]++;
}
}
}
for (int i=0;i>0;i++){
if (g.engineString !="Any"){
x++;
if (g.engineString== carList[i][2]){
Search[i]++;
}
}
}
for (int i=0;i>0;i++){
if (g.fuelString !="Any"){
x++;
if (g.fuelString== carList[i][2]){
Search[i]++;
}
}
}
for (int i=0;i>0;i++){
if (g.ageString !="Any"){
x++;
if (g.ageString== carList[i][2]){
Search[i]++;
}
}
}
for (int i=0;i>0;i++){
if (g.colourString !="Any"){
x++;
if (g.colourString== carList[i][2]){
Search[i]++;
}
}
}
carResult=c.search(Search);
return carResult;
}
}
and this is the car list text file
Code:// List of cars to use:
("MA06 XVU", 8999.0, new CarSpec("Saloon" ,"Ford" ,"Mondeo" , (float)1.6 ,"Diesel", (int) 12,"Blue" ));
("MA06 UXW", 12900.0, new CarSpec("Estate","Ford" ,"Mondeo" , (float)1.8 ,"Petrol", (int) 6, "Red" ));
("MA04 EST", 15999.0, new CarSpec("SUV" ,"Honda" ,"CR-V Sport" , (float)2.0 ,"Diesel", (int) 17, "Silver" ));
("VZX 9512", 15499.0, new CarSpec("Sports" ,"Toyota","Celica Coupe" , (float)1.8 ,"Petrol",(int) 18, "Black" ));
("DV06 UHQ", 25499.0, new CarSpec("Saloon" ,"BMW" ,"Series 3" , (float)2.2 ,"Diesel", (int) 14, "Black" ));
("DV55 WVX", 21499.0, new CarSpec("Sports" ,"BMW" ,"Z4 Coupe" , (float)3.0 ,"Petrol", (int) 8, "Silver" ));
("BN06 DHQ", 65499.0, new CarSpec("Saloon" ,"BMW" ,"Series 6 M5 5.0" ,(float)5.0 ,"Petrol", (int) 5, "Black" ));
("LA54 VUN", 4499.0, new CarSpec("Saloon" ,"Honda" ,"Jazz" , (float)1.2 ,"Diesel", (int) 25, "Green" ));
("DV06 MHQ", 65499.0, new CarSpec("Saloon" ,"BMW" ,"Series 6 M5 5.0" , (float)5.0 ,"Petrol", (int) 5, "Black" ));
("MW04 CXW", 6900.0, new CarSpec("Estate" ,"Ford" ,"Focus C-Max" , (float)1.6 ,"Petrol", (int) 24, "Blue" ));
("VZ55 LRT", 5499.0, new CarSpec("Saloon" ,"Toyota","Aygo" , (float)1.0 ,"Petrol",(int) 8, "White" ));
("GH03 RPK", 4000.0, new CarSpec("Saloon" ,"Toyota","Aygo" , (float)1.1 ,"Diesel", (int) 28, "Green" ));
("VH03 ZUK", 17500.0, new CarSpec("SUV" ,"Toyota","Landcruiser Amazon", (float)4.7 ,"Diesel", (int) 38, "Black" ));
("WT05 KXW", 3900.0, new CarSpec("Estate" ,"Ford" ,"Fiesta" , (float)1.4 ,"Petrol", (int) 39, "Red" ));
("SA06 JTX", 17499.0, new CarSpec("SUV" ,"BMW" ,"X3 Sport" , (float)2.5 ,"Petrol", (int) 26, "Blue" ));
i'm really really desperate i would have solved it my self but my deadline is close
thanx

