public class DVD implements Comparable { private String title; private int year;
ID: 3728197 • Letter: P
Question
public class DVD implements Comparable {
private String title;
private int year;
private int duration;
public DVD () { this ("",0,0); }
public DVD (String newTitle, int y, int minutes) {
title = newTitle;
year = y;
duration = minutes;
}
public int compareTo(Object obj) {
if (obj instanceof DVD) {
DVD aDVD = (DVD)obj;
return title.compareTo(aDVD.title);
}
return 0;
}
public String getTitle() { return title; }
public int getDuration() { return duration; }
public int getYear() { return year; }
public void setTitle(String t) { title = t; }
public void setDuration(int d) { duration = d; }
public void setYear(int y) { year = y; }
public String toString() {
return ("DVD (" + year + "): "" + title + "" with length: " + duration + " minutes");
}
}
public class DVDCollection {
public static final int MAX_DVDS = 100;
private DVD[] dvds;
private int dvdCount;
private int selectedDVD;
public DVDCollection() {
dvds = new DVD[MAX_DVDS];
selectedDVD = -1;
}
public void setSelectedDVD(int i) {
selectedDVD = i;
}
public DVD[] getDvds() { return dvds; }
public DVD[] getDVDList() {
DVD[] list = new DVD[dvdCount];
for (int i=0; i<dvdCount; i++)
list[i] = dvds[i];
return list;
}
public int getDvdCount() { return dvdCount; }
public String toString() { return ("DVD Collection of size " + dvdCount); }
public void add(DVD aDvd) {
if (dvdCount < MAX_DVDS)
dvds[dvdCount++] = aDvd;
}
public boolean remove(String title) {
for (int i=0; i<dvdCount; i++) {
DVD d = dvds[i];
if (dvds[i].getTitle().equals(title)) {
dvds[i] = dvds[dvdCount-1];
dvdCount--;
return true;
}
}
return false;
}
public static DVDCollection example1() {
DVDCollection c = new DVDCollection();
c.add(new DVD("If I Was a Student", 2007, 65));
c.add(new DVD("Don't Eat Your Pencil !", 1984, 112));
c.add(new DVD("The Exam", 2001, 180));
c.add(new DVD("Tutorial Thoughts", 2003, 128));
c.add(new DVD("Fried Monitors", 1999, 94));
return c;
}
public static DVDCollection example2() {
DVDCollection c = new DVDCollection();
c.add(new DVD("Titanic",1997,194));
c.add(new DVD("Star Wars",1977,121));
c.add(new DVD("E.T. - The Extraterrestrial",1982,115));
c.add(new DVD("Spider Man",2002,121));
c.add(new DVD("Jurassic Park",1993,127));
c.add(new DVD("Finding Nemo",2003,100));
c.add(new DVD("The Lion King",1994,89));
c.add(new DVD("Up",2009,96));
c.add(new DVD("The Incredibles",2004,115));
c.add(new DVD("Monsters Inc.",2001,92));
c.add(new DVD("Cars",2006,117));
c.add(new DVD("Beverly Hills Cop",1984,105));
c.add(new DVD("Home Alone",1990,103));
c.add(new DVD("Jaws",1975,124));
c.add(new DVD("Men in Black",1997,98));
c.add(new DVD("Independence Day",1996,145));
c.add(new DVD("National Treasure: Book of Secrets",2007,124));
c.add(new DVD("Aladdin",1992,90));
c.add(new DVD("Twister",1996,113));
c.add(new DVD("Iron Man",2008,126));
c.add(new DVD("Alice in Wonderland",2010,108));
c.add(new DVD("Transformers",2007,144));
return c;
}
public static DVDCollection example3() {
DVDCollection c = new DVDCollection();
c.add(new DVD("Avatar",2009,162));
c.add(new DVD("The Avengers",2012,143));
c.add(new DVD("Titanic",1997,194));
c.add(new DVD("The Dark Knight",2008,152));
c.add(new DVD("Star Wars",1977,121));
c.add(new DVD("Shrek 2",2004,93));
c.add(new DVD("E.T. - The Extraterrestrial",1982,115));
c.add(new DVD("Star Wars - Episode I",1999,136));
c.add(new DVD("Pirates of the Caribbean: Dead Man's Chest",2006,151));
c.add(new DVD("Toy Story 3",2010,103));
c.add(new DVD("Spider Man",2002,121));
c.add(new DVD("Transformers: Revenge of the Fallen",2009,150));
c.add(new DVD("Star Wars - Episode III",2005,140));
c.add(new DVD("Spider Man 2",2004,127));
c.add(new DVD("Jurassic Park",1993,127));
c.add(new DVD("Transformers: Dark of the Moon",2011,154));
c.add(new DVD("Finding Nemo",2003,100));
c.add(new DVD("Spider Man 3",2007,139));
c.add(new DVD("Forrest Gump",1994,142));
c.add(new DVD("The Lion King",1994,89));
c.add(new DVD("Shrek the Third",2007,93));
c.add(new DVD("The Sixth Sense",1999,107));
c.add(new DVD("Up",2009,96));
c.add(new DVD("I am Legend",2007,101));
c.add(new DVD("Shrek",2001,90));
c.add(new DVD("The Incredibles",2004,115));
c.add(new DVD("Monsters Inc.",2001,92));
c.add(new DVD("Brave",2012,100));
c.add(new DVD("Toy Story 2",1999,92));
c.add(new DVD("Cars",2006,117));
c.add(new DVD("Signs",2002,106));
c.add(new DVD("Hancock",2008,92));
c.add(new DVD("Rush Hour 2",2001,90));
c.add(new DVD("Meet the Fockers",2004,115));
c.add(new DVD("The Hangover",2009,100));
c.add(new DVD("My Big Fat Greek Wedding",2002,95));
c.add(new DVD("Beverly Hills Cop",1984,105));
c.add(new DVD("Mrs. Doubtfire",1993,125));
c.add(new DVD("The Hangover Part II",2011,102));
c.add(new DVD("The Matrix Reloaded",2003,138));
c.add(new DVD("Home Alone",1990,103));
c.add(new DVD("Jaws",1975,124));
c.add(new DVD("The Blind Side",2009,129));
c.add(new DVD("Men in Black",1997,98));
c.add(new DVD("X-Men: The Last Stand",2006,104));
c.add(new DVD("The Bourne Ultimatum",2007,115));
c.add(new DVD("WALL-E",2008,98));
c.add(new DVD("Inception",2010,148));
c.add(new DVD("Independence Day",1996,145));
c.add(new DVD("Pirates of the Caribbean: The Curse of the Black Pearl",2003,143));
c.add(new DVD("The Chronicles of Narnia",2005,143));
c.add(new DVD("War of the Worlds",2005,116));
c.add(new DVD("National Treasure: Book of Secrets",2007,124));
c.add(new DVD("Aladdin",1992,90));
c.add(new DVD("How to Train Your Dragon",2010,98));
c.add(new DVD("Shrek Forever After",2010,93));
c.add(new DVD("Twister",1996,113));
c.add(new DVD("Star Wars - Episode VI",1983,134));
c.add(new DVD("Iron Man",2008,126));
c.add(new DVD("Alice in Wonderland",2010,108));
c.add(new DVD("Transformers",2007,144));
c.add(new DVD("Star Wars - Episode II",2002,142));
return c;
}
}
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
public class DVDCollectionApp extends Application {
private DVDCollection model;
private ListView<String> tList;
private ListView<Integer> yList, lList;
public DVDCollectionApp() {
model = DVDCollection.example1();
}
public void start(Stage primaryStage) {
BorderPane borderPane = new BorderPane();
// Create the labels
HBox labelPane = new HBox();
labelPane.setPadding(new Insets(0,0,0,10));
labelPane.setSpacing(10);
Label label1 = new Label("Title");
label1.setMinSize(300,30);
label1.setPrefSize(2000,30);
Label label2 = new Label("Year");
label2.setMinSize(60,30);
label2.setPrefSize(60,30);
Label label3 = new Label("Length");
label3.setMinSize(60,30);
label3.setPrefSize(60,30);
labelPane.getChildren().addAll(label1, label2, label3);
borderPane.setTop(labelPane);
// Create the lists
GridPane listPane = new GridPane();
listPane.setPadding(new Insets(10));
listPane.setHgap(10);
tList = new ListView<String>();
listPane.add(tList, 0, 0);
tList.setMinSize(300,60);
tList.setPrefSize(2000,2000);
yList = new ListView<Integer>();
listPane.add(yList, 1, 0);
yList.setMinSize(60,60);
yList.setPrefSize(60,500);
lList = new ListView<Integer>();
listPane.add(lList, 2, 0);
lList.setMinSize(60,60);
lList.setPrefSize(60,500);
borderPane.setCenter(listPane);
// Create the button pane
HBox buttonPane = new HBox();
buttonPane.setPadding(new Insets(10));
buttonPane.setSpacing(10);
Button addButton = new Button("Add");
addButton.setStyle("-fx-font: 12 arial; -fx-base: rgb(0,100,0); -fx-text-fill: rgb(255,255,255);");
addButton.setPrefSize(90,30);
Button deleteButton = new Button("Delete");
deleteButton.setStyle("-fx-font: 12 arial; -fx-base: rgb(200,0,0); -fx-text-fill: rgb(255,255,255);");
deleteButton.setPrefSize(90,30);
Button statsButton = new Button("Stats");
statsButton.setStyle("-fx-font: 12 arial;");
statsButton.setPrefSize(90,30);
buttonPane.getChildren().addAll(addButton, deleteButton, statsButton);
borderPane.setBottom(buttonPane);
addButton.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent actionEvent) {
String title = javax.swing.JOptionPane.showInputDialog("Please enter the DVD Title: ");
String year = javax.swing.JOptionPane.showInputDialog("Please enter the DVD Year: ");
String length = javax.swing.JOptionPane.showInputDialog("Please enter the DVD Duration: ");
if ((title != null) && (year != null) && (length != null) && (title.length() > 0) && (year.length() > 0) && (length.length() > 0)) {
DVD d = new DVD(title, Integer.parseInt(year), Integer.parseInt(length));
model.add(d);
update(model, -1);
}
}
});
deleteButton.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent actionEvent) {
if (tList.getSelectionModel().getSelectedItem() != null) {
model.remove(tList.getSelectionModel().getSelectedItem());
update(model, -1);
}
}
});
tList.setOnMousePressed(new EventHandler<MouseEvent>() {
public void handle(MouseEvent mouseEvent) {
model.setSelectedDVD(tList.getSelectionModel().getSelectedIndex());
update(model, tList.getSelectionModel().getSelectedIndex());
}
});
yList.setOnMousePressed(new EventHandler<MouseEvent>() {
public void handle(MouseEvent mouseEvent) {
model.setSelectedDVD(yList.getSelectionModel().getSelectedIndex());
update(model, yList.getSelectionModel().getSelectedIndex());
}
});
lList.setOnMousePressed(new EventHandler<MouseEvent>() {
public void handle(MouseEvent mouseEvent) {
model.setSelectedDVD(lList.getSelectionModel().getSelectedIndex());
update(model, lList.getSelectionModel().getSelectedIndex());
}
});
// Populate the lists
DVD[] theList = model.getDVDList();
String[] titles = new String[theList.length];
Integer[] years = new Integer[theList.length];
Integer[] lengths = new Integer[theList.length];
for (int i=0; i<theList.length; i++) {
titles[i] = theList[i].getTitle();
years[i] = theList[i].getYear();
lengths[i] = theList[i].getDuration();
}
tList.setItems(FXCollections.observableArrayList(titles));
yList.setItems(FXCollections.observableArrayList(years));
lList.setItems(FXCollections.observableArrayList(lengths));
primaryStage.setTitle("My DVD Collection");
primaryStage.setScene(new Scene(borderPane, 600, 300));
primaryStage.show();
}
// Update the view to reflect the model
public void update(DVDCollection model, int selectedDVD) {
DVD[] theList = model.getDVDList();
String[] titles = new String[theList.length];
Integer[] years = new Integer[theList.length];
Integer[] lengths = new Integer[theList.length];
for (int i=0; i<theList.length; i++) {
titles[i] = theList[i].getTitle();
years[i] = theList[i].getYear();
lengths[i] = theList[i].getDuration();
}
tList.setItems(FXCollections.observableArrayList(titles));
yList.setItems(FXCollections.observableArrayList(years));
lList.setItems(FXCollections.observableArrayList(lengths));
tList.getSelectionModel().select(selectedDVD);
yList.getSelectionModel().select(selectedDVD);
lList.getSelectionModel().select(selectedDVD);
}
public static void main(String[] args) {
launch(args);
}
}
Explanation / Answer
package guiinventoryprogram;
import java.awt.GridLayout; // required to create a grid layout
import java.awt.BorderLayout; // required to create a border layout
import java.awt.event.ActionEvent; // required for click events
import java.awt.event.ActionListener; // required for click events
import javax.swing.JPanel; // required to create panels
import javax.swing.JFrame; // required to use the frame
import javax.swing.JButton; // required to create buttons
import javax.swing.JLabel; // required to create text fields
import javax.swing.JTextField; // required to create text fields
import java.util.Arrays; // required to sort array
import java.text.DecimalFormat;
public class GUIInventoryProgram extends JFrame implements ActionListener
{
// Declare Class Variables
//Declare two panels. gridPanel is main panel. panel is inserted into gridPanel.
private JPanel gridPanel = new JPanel(); // one panel that contains my gridlayout.
private JPanel panel = new JPanel(); // panel used to contain buttons, labels and text fields
// Declare buttons
JButton nextButton; // first button
// Declare Text Fields
JTextField TitleField; // Dvd Title
JTextField UnitsField; // Units Field
JTextField ItemNumberField; // Item Number field
JTextField PriceField; // Dvd Price field
JTextField ValueField; // Value field
JTextField DirectorField;
// Declare Labels
JLabel lblTitle; // Title label
JLabel lblUnits; // Units Label
JLabel lblItemNumber; // Item Number label
JLabel lblPrice; // Price Label
JLabel lblValue; // Value of DVDs Label
JLabel lblDirector;
// Declare 5 Dvd Objecs
Dvd dvd1;
Dvd dvd2;
Dvd dvd3;
Dvd dvd4;
Dvd dvd5;
private static final int MAX_dvds = 5; // set maximum size for DVD Array
Dvd[] dvd = new Dvd[MAX_dvds]; // create Dvd Array object
DecimalFormat formatter = new DecimalFormat("$##,###.00");
private static DecimalFormat currency = new DecimalFormat("$#,##0.00");
private static int ArrayIndex = 0; // array index
public GUIInventoryProgram() // constructor
{
MovieDirector d1 = new MovieDirector("Transformers", 7, 1002, 19.98, "Michael Bay");
// Creates an instance of the DVD class and initialize class instance variables
MovieDirector d2 = new MovieDirector("Green Mile", 3, 542, 14.50, "Frank Darabont");
// Creates an instance of the DVD class and initialize class instance variables
MovieDirector d3 = new MovieDirector("Lord of the Rings", 5, 937, 24.98, "Peter Jackson");
// Creates an instance of the DVD class and initialize class instance variables
MovieDirector d4 = new MovieDirector("Jar Head", 2, 865, 9.75, "Sam Mendes");
// Creates an instance of the DVD class and initialize class instance variables
MovieDirector d5 = new MovieDirector("Inception", 6, 1157, 21.97, "Christopher Nolan");
// Creates an instance of the DVD class and initialize class instance variables
// adding Dvd objects to the array
dvd[0] = d1; // adding Transformers to the array of dvds
dvd[1] = d2; // adding Green Mile to the array of dvds
dvd[2] = d3; // adding Lord of the Rings to the array of dvds
dvd[3] = d4; // adding Jar Head to the array of dvds
dvd[4] = d5; // adding Inception to the array of dvds
Dvd.sortByTitle(dvd); // sort the array by name of title
gridPanel.setLayout(new BorderLayout()); // create a border layout
gridPanel.add(this.createLabelPanel(), BorderLayout.WEST); // add label panel
gridPanel.add(this.createTextPanel(), BorderLayout.CENTER); // add field panel
gridPanel.add(this.createButtonPanel(), BorderLayout.SOUTH); // add button panel
add(gridPanel);
}
// helper method creates a panel for the button
private JPanel createButtonPanel()
{
// ActionListener btnListen = new ButtonListener(); // create listener
// create button objects
nextButton = new JButton("Display DVD Information"); // create and label button
nextButton.setActionCommand("Next"); // action command for click button event
// nextButton.addActionListener(btnListen); // button listener for click button event
nextButton.addActionListener(this);
// create panel object
JPanel pobj = new JPanel();
pobj.add(nextButton); // add next button to panel
return pobj; // return panel
} // end createButtonPanel method
private JPanel createLabelPanel()
{
// create instance of label objects
lblTitle = new JLabel("Title:"); // label for Title of DVD
lblUnits = new JLabel("Units:"); // label for Units in stock for DVD
lblItemNumber = new JLabel("Item Number:"); // label for DVD Item Number
lblPrice = new JLabel("Price:"); // label for price of DVD
lblValue = new JLabel("Value of Items:"); // lable for value of DVD
lblDirector = new JLabel ("Directors Name:");
panel = new JPanel();
panel.setLayout(new GridLayout(6, 1));
// add labels to the panel
panel.add(lblTitle);
panel.add(lblUnits);
panel.add(lblItemNumber);
panel.add(lblPrice);
panel.add(lblValue);
panel.add(lblDirector);
return panel;
} // end createLabelPanel method
private JPanel createTextPanel()
{
//create instances of text box objects
TitleField = new JTextField(); // Title text field
TitleField.setEditable(false); // set field to not editable to prevent user from changing the data
UnitsField = new JTextField(); // Units text field
UnitsField.setEditable(false); // set field to not editable to prevent user from changing the data
ItemNumberField = new JTextField(); // Item Number field
ItemNumberField.setEditable(false); // set field to not editable to prevent user from changing the data
PriceField = new JTextField(); // Price text field
PriceField.setEditable(false); // set field to not editable to prevent user from changing the data
ValueField = new JTextField(); // Value text field to sum the total of the DVDs value.
ValueField.setEditable(false); // set field to not editable to prevent user from changing the data
DirectorField = new JTextField();
DirectorField.setEditable(false);
panel = new JPanel(); // create panel object
panel.setLayout(new GridLayout(6, 1)); // create grid layout for fields.
panel.add(TitleField); // add the Title field to the grid layout
panel.add(UnitsField); // add the Units field to the grid layout
panel.add(ItemNumberField); // add the Item Number field to the grid layout
panel.add(PriceField); // add the Price field to the grid layout
panel.add(ValueField); // add the total value field to the grid layout
panel.add(DirectorField);
return panel; // return the panel
} // end createTextPanel method
@ Override public void actionPerformed(ActionEvent e)
{
// add button functions
if ("Next".equals(e.getActionCommand()))
if (ArrayIndex < dvd.length)
{
TitleField.setText(dvd[ArrayIndex].getDvdTitle()); // get the Dvds title
// and assign it the name text field.
UnitsField.setText(String.valueOf(dvd[ArrayIndex].getDvdUnits()));
// get the units in stock and assign it the Units text field.
ItemNumberField.setText(String.valueOf(dvd[ArrayIndex].getDvdItemNumber()));
// get Dvds item number and assign it the item number text field.
PriceField.setText(String.format("$ %8.2f", dvd[ArrayIndex].getDvdPrice()));
// get the Price of the DVD and assign it the price text field.
ValueField.setText(String.format("$ %8.2f", dvd[ArrayIndex].getDvdValue()));
// display the sum of the total of the DVDs in stock
DirectorField.setText(String.valueOf(dvd[ArrayIndex].getdvdDirector()));
// display the Directors name of the DVD
ArrayIndex = ArrayIndex + 1; // increment the array index to get to the next value
} // end if
} // end actionPerformed
public static void main(String[] args)
{
JFrame frame = new GUIInventoryProgram(); // creat a frame object
frame.setSize(400, 300); // set the size of the window - 400 is width, 200 is height
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // close the application
frame.setTitle("DVD Inventory"); // set the title of the window
frame.setLocationRelativeTo( null ); // center the form
frame.setVisible(true); // display form
} // end main
} // end GUIInventoryProgram class
package guiinventoryprogram;
class Dvd {
// declare instance variables
private String dvdTitle; //title of dvd
private int dvdUnits; // number of units of the dvd
private double dvdItemNumber; // item number of the dvd
private double dvdPrice; // Price of the dvd
private String dvdDirector;
public Dvd()
{
}
//declare DVD constructor that shows title, stock, item, and price
public Dvd(String Title, int Units, double ItemNumber, double Price, String Director) {
//call to Object constructor occurs here
dvdTitle = Title;
dvdUnits = Units;
dvdItemNumber = ItemNumber;
dvdPrice = Price;
dvdDirector = Director;
} //end four-argument constructor
//set DVD name
public void setdvdTitle(String Title) {
setdvdTitle(Title);
} //end method setDvdTitle
//return dvd Title
public String getDvdTitle() {
return dvdTitle;
} //end method getDvdTitle
//set Dvd stock
public void setDvdUnits(int Units) {
dvdUnits = (Units < 0) ? 0 : Units;
} //end method setDvdStock
//return dvd stock
public double getDvdUnits() {
return dvdUnits;
} //end method getDvdStock
public void setDvdItemNumber(double ItemNumber) {
dvdItemNumber = (ItemNumber < 0.0) ? 0.0 : ItemNumber;
} //end method set dvd Item
//return dvd item
public double getDvdItemNumber() {
return dvdItemNumber;
} //end method getDvdItem
public void setDvdPrice(double Price) {
dvdPrice = (Price < 0.0) ? 0.0 : Price;
} //end method SetDvdPrice
//return dvd price
public double getDvdPrice() {
return dvdPrice;
} //end method get Dvd Price
// calculate inventory value
public double getDvdValue() {
return getDvdPrice() * dvdUnits;
} //end method value
public double getdvdRestockingfee() {
return getDvdValue() * .05;
}
public void setdvdDirector(String Director) {
setdvdDirector(Director);
} //end method setDvdTitle
//return dvd Title
public String getdvdDirector() {
return dvdDirector;
} //end method getDvdTitle
public static void sortByTitle(Dvd dvd[])
{ // sort the dvd by title
Dvd temp;
for(double j = dvd.length - 2; j >= 0; j--)
for(int i = 0 ; i <= j ; i++)
if(dvd [ i ].getDvdTitle().compareTo(dvd[ i + 1 ].getDvdTitle())>0)
{
temp=dvd[ i ];
dvd[ i ] = dvd[ i + 1 ];
dvd[ i + 1 ] = temp;
}
} // end sort by title method
} // end class dvd
package guiinventoryprogram;
class MovieDirector extends Dvd{
private String Director;
private double Restockingfee;
public MovieDirector(String Title, int Units, double ItemNumber, double Price, String Director)
{
super(Title, Units, ItemNumber, Price);
this.Director=Director;
this.Restockingfee = Restockingfee;
}
public String getDirector() {
return Director;
}
public void setDirector(String Director) {
this.Director = Director;
}
public double getRestockingfee() {
return getDvdValue() * 0.05;
}
public void setRestockingfee (double Restockingfee){
this.Restockingfee = Restockingfee;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.