Answer to Question #272546 in Java | JSP | JSF for Brendin

Question #272546

Write a Java GUI application that will keep track of sport vehicle details. The application must contain the vehicle name, vehicle price, 0 – 100 in seconds and the engine size.


Q.3.1 On the form create three radio buttons and a list box to display the vehicle details. Also create a submit button that when clicked will display the vehicle name, vehicle price, 0 – 100 in seconds and engine size.


Q.3.2 Create a sequential file (cars.txt) that contains data for the following fields: • The vehicle name • The vehicle price • The speed from 0 to 100 in seconds • The engine size


1
Expert's answer
2021-11-28T05:26:51-0500


SOLUTION CODE


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

class Vehicle{
    private String name;
    private String speed;
    private String price;
    private String engineSize;

    public Vehicle(String name, String price, String speed, String engineSize) {
        this.name = name;
        this.speed = speed;
        this.price = price;
        this.engineSize = engineSize;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSpeed() {
        return speed;
    }

    public void setSpeed(String speed) {
        this.speed = speed;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public String getEngineSize() {
        return engineSize;
    }

    public void setEngineSize(String engineSize) {
        this.engineSize = engineSize;
    }
}

public class UI {

    static JFrame frame;
    static ButtonGroup buttonGroup;
    static JRadioButton button1,button2,button3;
    static JButton button;
    static JList jList;
    static JPanel jPanel;
    static String arr[] = new String[4];

    static Vehicle vehicle1,vehicle2,vehicle3;

    public static void main(String args[]) throws IOException {
        File file = new File("//home//nidhi//Desktop//cars.txt");

        BufferedReader br = new BufferedReader(new FileReader(file));

        String st;
        int i = 0;
        while ((st = br.readLine()) != null) {
            if (st.isEmpty()) continue;

            String st1 = br.readLine();
            String st2 = br.readLine();
            String st3 = br.readLine();

            if (i==0) vehicle1 = new Vehicle(st, st1, st2, st3);
            if (i==1) vehicle2 = new Vehicle(st, st1, st2, st3);
            if (i==2) vehicle3 = new Vehicle(st, st1, st2, st3);

            i++;
        }

        init();
    }

    private static void init() {
        frame = new JFrame("My First GUI");
        jPanel= new JPanel(new FlowLayout());
        jPanel.setBorder(BorderFactory.createTitledBorder("Visible Panel"));
        jPanel.setVisible(true);
        jPanel.setBounds(350,50,300,300);
        frame.setLayout(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(900,700);

        buttonGroup = new ButtonGroup();

        button1 = new JRadioButton();
        button2 = new JRadioButton();
        button3 = new JRadioButton();
        button = new JButton();

        button1.setBounds(100, 50, 200, 50);
        button2.setBounds(100, 100, 200, 50);
        button3.setBounds(100,150,200,50);
        button.setBounds(150, 300, 150, 30);

        button1.setText(vehicle1.getName());
        button2.setText(vehicle2.getName());
        button3.setText(vehicle3.getName());
        button.setText("Submit");

        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.add(button);
        frame.add(jPanel);

        buttonGroup.add(button1);
        buttonGroup.add(button2);
        buttonGroup.add(button3);

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                if (button1.isSelected()){
                    spread(vehicle1);
                }else if(button2.isSelected()){
                    spread(vehicle2);
                }else if(button3.isSelected()){
                    spread(vehicle3);
                }else{
                    jPanel.removeAll();
                    jPanel.add(new JList<String>(new String[]{"Please choose and option"}));
                    jPanel.revalidate();
                    jPanel.repaint();
                    System.out.println("Please choose one option.");
                }
            }
        });

        frame.setVisible(true);
    }

    private static void spread(Vehicle vehicle) {
        arr[0] = vehicle.getName();
        arr[1] = "Price: "+ vehicle.getName();
        arr[2] = "0-100: "+ vehicle.getPrice();
        arr[3] = "Engine: "+vehicle.getEngineSize();

        jList = new JList(arr);
        jPanel.removeAll();
        jPanel.add(jList);
        jPanel.revalidate();
        jPanel.repaint();
    }
}


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS