Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

topic is library management system this is java project and it still cant run if there is any error pls fix it for me too

topic is library management system

this is java project and it still cant run if there is any error pls fix it for me too and if they have missing switch statement, recursion , and writing files part let me know or you can add it on

package LibraryManagementSystem;

import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner;

public class LibraryManagementSystem { private JFrame frame; private JPanel mainPanel; private JLabel titleLabel; private JMenuBar menuBar; private JMenu fileMenu; private JMenu helpMenu; private JMenuItem openFileItem; private JMenuItem saveFileItem; private JMenuItem exitItem; private JMenuItem helpItem; private JMenuItem aboutItem;

public LibraryManagementSystem() { createGUI(); }

private void createGUI() { // Create Frame frame = new JFrame("Library Management System"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(600, 400); frame.setLocationRelativeTo(null);

// Create Menu Bar menuBar = new JMenuBar(); frame.setJMenuBar(menuBar);

// Create Menus fileMenu = new JMenu("File"); helpMenu = new JMenu("Help"); menuBar.add(fileMenu); menuBar.add(helpMenu);

// Create Menu Items openFileItem = new JMenuItem("Open File"); saveFileItem = new JMenuItem("Save File"); exitItem = new JMenuItem("Exit"); helpItem = new JMenuItem("Help"); aboutItem = new JMenuItem("About"); fileMenu.add(openFileItem); fileMenu.add(saveFileItem); fileMenu.addSeparator(); fileMenu.add(exitItem); helpMenu.add(helpItem); helpMenu.add(aboutItem);

// Add Action Listeners openFileItem.addActionListener(new OpenFileListener()); saveFileItem.addActionListener(new SaveFileListener()); exitItem.addActionListener(new ExitListener()); helpItem.addActionListener(new HelpListener()); aboutItem.addActionListener(new AboutListener());

// Create Main Panel mainPanel = new JPanel(); frame.add(mainPanel);

// Create Title Label titleLabel = new JLabel("Library Management System");

// Add Components to Main Panel mainPanel.add(titleLabel);

// Show Frame frame.setVisible(true); }

// Open File Listener class OpenFileListener extends Component implements ActionListener { public void actionPerformed(ActionEvent e) { // Code for opening a file JFileChooser fileChooser = new JFileChooser(); fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));

int openFile = fileChooser.showOpenDialog(this);

if(openFile == JFileChooser.APPROVE_OPTION) { File select = fileChooser. try { Scanner scanner = new Scanner(select); while (scanner.hasNextLine()) { String line = scanner.nextLine(); System.out.println(line); } scanner.close(); } catch (FileNotFoundException ex) { JOptionPane.showMessageDialog(frame, "Error opening file: File not found.", "Error", JOptionPane.ERROR_MESSAGE); } } } }

// Save File Listener class SaveFileListener implements ActionListener { public void actionPerformed(ActionEvent e) { // Code for saving a file JFileChooser fileChooser = new JFileChooser(); fileChooser.setDialogTitle("Specify a file to save.");

int selection = fileChooser.showSaveDialog(frame); if(selection != JFileChooser.CANCEL_OPTION) { File saveFile = fileChooser.getSelectedFile(); try { saveFile.createNewFile(); } catch (IOException ex) { JOptionPane.showMessageDialog(frame, "Error creating file: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); } } } }

// Exit Listener class ExitListener implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); } }

// Help Listener class HelpListener implements ActionListener { public void actionPerformed(ActionEvent e) { // Code for displaying help JOptionPane.showMessageDialog(frame, "Welcome to the Library Management System! " + "To open a file, click File > Open File. Select the file you want to open from the file chooser. " + "To save a file, click File > Save File. Specify the name and location of the file you want to save in the file chooser. " + "To exit the application, click File > Exit. " + "For more information, click Help > About.", "Help", JOptionPane.INFORMATION_MESSAGE); } }

// About Listener class AboutListener implements ActionListener { public void actionPerformed(ActionEvent e) { // Code for displaying information about the application JOptionPane.showMessageDialog(frame, "Library Management System Version 1.0 Copyright 2021", "About", JOptionPane.INFORMATION_MESSAGE); } }

// Function for Reading and Writing to Files public static void readWriteToFile(String fileName) { // Code for reading and writing to a file String input = ""; try { Scanner scanner = new Scanner(new File(fileName)); while (scanner.hasNextLine()) { input += scanner.nextLine() + " "; } scanner.close(); } catch (FileNotFoundException ex) { JOptionPane.showMessageDialog(null, "Error reading file"); } input = JOptionPane.showInputDialog(null, "Enter text to be written to the file:", "Write to File", JOptionPane.PLAIN_MESSAGE); if (input != null) { try { File file = new File(fileName); file.createNewFile(); java.io.PrintWriter writer = new java.io.PrintWriter(fileName); writer.println(input); writer.close(); JOptionPane.showMessageDialog(null, "Text written to file successfully!", "Success", JOptionPane.INFORMATION_MESSAGE); } catch (IOException ex) { JOptionPane.showMessageDialog(null, "Error writing to file: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); } } }

public static void main(String[] args) { // Use a switch statement to process command line arguments switch (args.length) { case 0: // No arguments: display the GUI new LibraryManagementSystem(); break; case 1: // One argument: read and write to a file readWriteToFile(args[0]); break; default: // More than one argument: display an error message System.out.println("Error: Invalid number of arguments."); break; } } } readWriteToFile(fileName); if (!continueProcessing) { return; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions