Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I am having problems running my program. When I run the below code I get File cannot be opened: Could not load, no such directory.
I am having problems running my program. When I run the below code I get "File cannot be opened: Could not load, no such directory".
public 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"; } }
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 ");} } }
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; }
I've tried the below text file
EBook,1,GhostFleet,2016,true,15 MusicCD,2,True,2013,true, 47 MovieDVD,3,PulpFiction,1994,true,4096 EBook,4,GameofThrones,1997,true,41 MusicCD,5,Burnt,2015,true,53 MovieDVD,6,FightClub,1999,true,4024
and a CSV file
EBook | 100 | Ghost Fleet | 2016 | TRUE | 15 | ||
MusicCD | 200 | TRUE | 2013 | TRUE | 47 | ||
MovieDVD | 300 | Pulp Fiction | 1994 | TRUE | 4096 | ||
Ebook | 101 | Game of Thrones | 1997 | TRUE | 41 | ||
MusicCD | 201 | Burnt | 2015 | TRUE | 53 | ||
MovieDVD | 301 | Fight Club | 1999 | TRUE | 4024 | ||
What am I doing wrong?
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