Answered step by step
Verified Expert Solution
Question
1 Approved Answer
This is the current code. What does my text document need to look like for a proper output? import java.io.File; import javax.swing.*; public class MediaRentalSystem
This is the current code. What does my text document need to look like for a proper output?
import java.io.File; import javax.swing.*; public class MediaRentalSystem extends javax.swing.JFrame { /** * */ private static final long serialVersionUID = -50037594406635831L; public MediaRentalSystem() { initComponents(); } Manager manager = new Manager(); //Constructor private void initComponents() { jMenuBar1 = new javax.swing.JMenuBar(); jMenu3 = new javax.swing.JMenu(); jMenuItem1 = new javax.swing.JMenuItem(); jMenuItem2 = new javax.swing.JMenuItem(); jMenuItem3 = new javax.swing.JMenuItem(); jMenuItem4 = new javax.swing.JMenuItem(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Welcome To Media Rental System "); jMenu3.setText("Menu"); jMenuItem1.setText("Load Media object..."); jMenuItem1.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { jMenuItem1MouseClicked(evt); } }); jMenuItem1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItem1ActionPerformed(evt); } }); jMenu3.add(jMenuItem1); jMenuItem2.setText("Find Media object..."); jMenuItem2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItem2ActionPerformed(evt); } }); jMenu3.add(jMenuItem2); jMenuItem3.setText("Rent Media object..."); jMenuItem3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItem3ActionPerformed(evt); } }); jMenu3.add(jMenuItem3); jMenuItem4.setText("Quit "); jMenuItem4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItem4ActionPerformed(evt); } }); jMenu3.add(jMenuItem4); jMenuBar1.add(jMenu3); setJMenuBar(jMenuBar1); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 400, Short.MAX_VALUE)); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 300, Short.MAX_VALUE)); pack(); setLocationRelativeTo(null); } private void jMenuItem1MouseClicked(java.awt.event.MouseEvent evt) { } private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser chooser = new JFileChooser(); int fChooser = chooser.showOpenDialog(null); if (fChooser == JFileChooser.APPROVE_OPTION) { System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName()); File file = chooser.getSelectedFile(); String filename = file.getName(); System.out.println("You have selected: " + filename); manager.LoadMedia(filename); } } private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) { String title = JOptionPane.showInputDialog(null, "Enter the title"); //CODE ADDED if (title != null) { manager.findMedia(title); } else { JOptionPane.showMessageDialog(null, "Invalid title Value"); } } private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) { String id = JOptionPane.showInputDialog(null, "Enter The id"); //CODE ADDED if (id != null) { manager.rentMedia(Integer.parseInt(id)); } else { JOptionPane.showMessageDialog(null, "Invalid id Value"); } } private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) { System.exit(0); } public static void main(String args[]) { try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(MediaRentalSystem.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(MediaRentalSystem.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(MediaRentalSystem.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(MediaRentalSystem.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new MediaRentalSystem().setVisible(true); } }); } private javax.swing.JMenu jMenu3; private javax.swing.JMenuBar jMenuBar1; private javax.swing.JMenuItem jMenuItem1; private javax.swing.JMenuItem jMenuItem2; private javax.swing.JMenuItem jMenuItem3; private javax.swing.JMenuItem jMenuItem4; } ================================================== import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import javax.swing.JOptionPane; public class Manager { static List mediaList = new ArrayList<>(); Manager() {} public boolean LoadMedia(String path) { try { File Selection = new File(path); try (Scanner File = new Scanner(Selection)) { while (File.hasNextLine()) { String data = File.nextLine(); String[] str = data.split(","); switch (str[0]) { case "EBook": mediaList.add(new EBook(Integer.parseInt(str[1]), str[2], Integer.parseInt(str[3]), Boolean.parseBoolean(str[4]), Integer.parseInt(str[5]))); break; case "MusicCD": mediaList.add(new MusicCD(Integer.parseInt(str[1]), str[2], Integer.parseInt(str[3]), Boolean.parseBoolean(str[4]), Integer.parseInt(str[5]))); break; default: mediaList.add(new MovieDVD(Integer.parseInt(str[1]), str[2], Integer.parseInt(str[3]), Boolean.parseBoolean(str[4]), Double.parseDouble(str[5]))); break;} } JOptionPane.showMessageDialog(null, mediaList);} return true;} catch (FileNotFoundException e) { JOptionPane.showMessageDialog(null, "File cannot be opened: Could not load, no such directory"); return false;} } public void findMedia(String title) { String result = ""; boolean inStock = false; for (int i = 0; i < mediaList.size(); i++) { if (mediaList.get(i).getTitle().equals(title)) { result += mediaList.get(i).toString(); System.out.print(mediaList.get(i).toString()); inStock = true;} } if (!result.isEmpty()) { JOptionPane.showMessageDialog(null, result);} if (!inStock) { JOptionPane.showMessageDialog(null, "There is no media with this title: " + title);} } public void rentMedia(int id) { boolean inStock = false; for (Media m : mediaList) { if (m.getId() == id) { inStock = true; if (m.isAvailable()) { JOptionPane.showMessageDialog(null, "Media was successfully rented. Rental Fee = $" + m.calculateRentalFee()); m.available = false;} else { JOptionPane.showMessageDialog(null, "Media with id=" + id + " is not available for rent "); break;} } } if (!inStock) { JOptionPane.showMessageDialog(null, "The media object id=" + id + " is not found ");} } } ==================== class Media { // Attributes int id; String title; int year; boolean available; // Constructor public Media(int id, String title, int year, boolean available) { this.id = id; this.title = title; this.year = year; this.available = available;} // Get methods public int getId() { return id;} public void setId(int id) { this.id = id;} public String getTitle() { return title;} public void setTitle(String title) { this.title = title;} public int getYear() { return year;} public void setYear(int year) { this.year = year;} public boolean isAvailable() { return available;} public void setAvailable(boolean available) { this.available = available;} // Calculate rental fee; flat fee $3.00 public double calculateRentalFee() { return 3.00;} } ================================================== import java.util.Calendar; public class EBook extends Media { // Local Attributes private int numChapters; // Constructor public EBook(int id, String title, int year, boolean available, int numChapters) { super(id, title, year, available); this.numChapters = numChapters;} // Get Method public int getNumChapters() { return numChapters;} // Set Method public void setNumChapters(int numChapters) { this.numChapters = numChapters;} @Override public double calculateRentalFee() { double fee = numChapters * 0.10; int currYear = Calendar.getInstance().get(Calendar.YEAR); if (this.getYear() == currYear) { fee += 1;} return fee;} @Override public String toString() { return "EBook [id:" + this.id + " title:" + this.title + " chapter:" + this.numChapters + " year:" + this.year + " available:" + this.available + "]n";} } ================================================== public class MovieDVD extends Media { // Local Attributes private double size; // Constructor public MovieDVD(int id, String title, int year, boolean available, double size) { super(id, title, year, available); this.size = size;} //Get Method public double getSize() { return size;} //Set Method public void setSize(double size) { this.size = size;} @Override public double calculateRentalFee() { return super.calculateRentalFee();} @Override public String toString() { return "MovieDVD [id:" + this.getId() + " title:" + this.getTitle() + " size:" + this.getSize()+ " year:" + this.getYear() + " available:" + this.isAvailable() + "]n";} } ================================================== import java.util.Calendar; public class MusicCD extends Media { // Local Attributes private int length; // Constructor public MusicCD(int id, String title, int year, boolean available, int length) { super(id, title, year, available); this.length = length;} // Get Method public int getLength() { return length;} // Set Method public void setLength(int length) { this.length = length;} @Override public double calculateRentalFee() { double fee = length * 0.02; int currYear = Calendar.getInstance().get(Calendar.YEAR); if (this.getYear() == currYear) { fee += 1;} return fee;} @Override public String toString() { return "MusicCD [id:" + this.id + " title:" + this.title + " length:" + this.getLength() + " year:" + this.year + " available:" + this.available + "]n"; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
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