My goal was to create a 4 button GUI lending library, that must be fully operable in Eclipse.
This is what I have, and I need someone to look this over before I submit it. My prof is a dckhead, and he knows me for EVERYTHING. Even the title, where I comment my name, etc..
Code
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import java.awt.event.*;
import javax.swing.ButtonGroup;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import java.awt.Font;
import java.io.File;
import java.io.FileNotFoundException;
import javax.swing.JList;
import java.awt.Color;
import javax.swing.JMenuItem;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
public class LibraryGUI extends JPanel implements ListSelectionListener {
Library lib = new Library();
private DefaultListModel<String> listModel;
private JFrame frameMyLibrary;
private final ButtonGroup buttonGroup = new ButtonGroup();
String title, format, date, name, format1, stored;
public static JList<String> listPanel;
final List<MediaItem> s1 = new ArrayList<MediaItem>();
ArrayList<String> alist = new ArrayList<String>();
String[] items =new String[lib.list2.size()];
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LibraryGUI window = new LibraryGUI();
window.frameMyLibrary.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public LibraryGUI() {
initialize();
Library lib = new Library();
}
private void initialize() {
frameMyLibrary = new JFrame();
frameMyLibrary.setTitle("My Library");
frameMyLibrary.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frameMyLibrary.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent evt){// saving event
try{
lib.save();
} catch(FileNotFoundException e){
JOptionPane.showMessageDialog(LibraryGUI.this,
"There was a problem saving the library to file");
}
System.exit(0);
}});
frameMyLibrary.setBounds(100, 100, 522, 608);
frameMyLibrary.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SpringLayout springLayout = new SpringLayout();
listModel = new DefaultListModel<String>();
final JList<String> listPanel = new JList<String>();
listPanel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);//single click
lib.open();//open method of library class
String[] items = new String[lib.list2.size()];
lib.listAllItems().toArray(items);
if (items.length != 0) {
listPanel.setListData(items);
listPanel.setModel(model);
listPanel.invalidate();
JList<String> list = new JList<String>();
springLayout.putConstraint(SpringLayout.NORTH, listPanel, 0,
SpringLayout.NORTH, frameMyLibrary.getContentPane());
springLayout.putConstraint(SpringLayout.WEST, listPanel, 0,
SpringLayout.WEST, frameMyLibrary.getContentPane());
springLayout.putConstraint(SpringLayout.SOUTH, listPanel, 491,
SpringLayout.NORTH, frameMyLibrary.getContentPane());
springLayout.putConstraint(SpringLayout.EAST, listPanel, 506,
SpringLayout.WEST, frameMyLibrary.getContentPane());
listPanel.setBackground(Color.WHITE);
frameMyLibrary.getContentPane().add(listPanel);
frameMyLibrary.getContentPane().setLayout(springLayout);
add(listPanel);
// Add
JButton btnAdd = new JButton("Add");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lib.addNewItem();
DefaultListModel<String> model = new DefaultListModel<String>();
for (String s : ) {
model.addElement(s);
}
JList<String> contactList = new JList<String>(model);
String[] items = new String[lib.list2.size()];
lib.listAllItems().toArray(items);
listPanel.setListData(items);
listPanel.setModel(model);
listPanel.invalidate();
}
});
springLayout.putConstraint(SpringLayout.WEST, btnAdd, 101,
SpringLayout.WEST, frameMyLibrary.getContentPane());
springLayout.putConstraint(SpringLayout.SOUTH, btnAdd, -25,
SpringLayout.SOUTH, frameMyLibrary.getContentPane());
frameMyLibrary.getContentPane().add(btnAdd);
// Check out
JButton btnCheckOut = new JButton("Check Out");
btnCheckOut.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Object selected = listPanel.getSelectedValue();
if (selected != null) {
String s = selected.toString();
String title = s.substring(0, s.lastIndexOf("("));
title.trim();
lib.markItemOnLoan(title);// markIteamOnLoan is called
String[] items = new String[lib.list2.size()];
lib.listAllItems().toArray(items);
listPanel.setListData(items);
listPanel.setModel(model);
listPanel.invalidate();
/*
* name = JOptionPane
* .showInputDialog("Who are you loaning it to (name)?");
* date = JOptionPane
* .showInputDialog("When did you loan it (date)?:"); String
* str = "is loaned to "+ name+ " on "+ date;
*/
}
}
});
springLayout.putConstraint(SpringLayout.NORTH, btnCheckOut, 0,
SpringLayout.NORTH, btnAdd);
springLayout.putConstraint(SpringLayout.WEST, btnCheckOut, 7,
SpringLayout.EAST, btnAdd);
frameMyLibrary.getContentPane().add(btnCheckOut);
// Check in
JButton btnCheckIn = new JButton("Check In");
btnCheckIn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Object selected = listPanel.getSelectedValue();
if (selected != null) {
String s = selected.toString();
String title = s.substring(0, s.lastIndexOf("("));
title.trim();
lib.markItemReturned(title);
String[] items = new String[lib.list2.size()];
lib.listAllItems().toArray(items);
listPanel.setListData(items);
listPanel.setModel(model);
listPanel.invalidate();
/*
* name = JOptionPane
* .showInputDialog("Who are you loaning it to (name)?");
* date = JOptionPane
* .showInputDialog("When did you loan it (date)?:"); String
* str = "is loaned to "+ name+ " on "+ date;
*/
}
}
});
springLayout.putConstraint(SpringLayout.NORTH, btnCheckIn, 0,
SpringLayout.NORTH, btnAdd);
springLayout.putConstraint(SpringLayout.WEST, btnCheckIn, 7,
SpringLayout.EAST, btnCheckOut);
frameMyLibrary.getContentPane().add(btnCheckIn);
// Delete
JButton btnDelete = new JButton("Delete");
btnDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Object selected = listPanel.getSelectedValue();
if (selected != null) {
String s = selected.toString();
String title = s.substring(0, s.lastIndexOf("("));
title.trim();
lib.delete(title);
String[] items = new String[lib.list2.size()];
lib.listAllItems().toArray(items);
listPanel.setListData(items);
listPanel.setModel(model);
listPanel.invalidate();
}
}
});
springLayout.putConstraint(SpringLayout.NORTH, btnDelete, 0,
SpringLayout.NORTH, btnAdd);
springLayout.putConstraint(SpringLayout.WEST, btnDelete, 6,
SpringLayout.EAST, btnCheckIn);
frameMyLibrary.getContentPane().add(btnDelete);
}
@Override
public void valueChanged(ListSelectionEvent arg0) {
// TODO Auto-generated method stub
}
}
Media Items
Code
public class MediaItem {
private String title;
private String format;
private boolean onLoan = false;
private String loanedTo;
private String dateLoaned;
public MediaItem() {
title = null;
format = null;
onLoan = false;
loanedTo = null;
dateLoaned = null;
}
public MediaItem(String title, String format) {//constructor to initialize the title and format
this.title = title;
this.format = format;
}
public void setTitle(String title) { //setter
this.title = title;
System.out.println(title);
}
public String getTitle() { //getter
return title;
}
public void setFormat(String format) { //setter
this.format = format;
System.out.println(format);
}
public String getFormat() { //getter
return format;
}
public void setonLoan(boolean onLoan) { //setter
this.onLoan = onLoan;
System.out.println(onLoan);
}
public boolean getonLoan() {
return onLoan;
}
public String getDateLoaned() {
return dateLoaned;
}
public void setDateLoaned(String dateLoaned) {
this.dateLoaned=dateLoaned;
System.out.println(dateLoaned);
}
public String getLoanedTo() {
return this.loanedTo;
}
public void setLoanedTo(String name) {
this.loanedTo=name;
System.out.println(name);
}
public void markOnLoan(String name, String date) {
this.setonLoan(true);
this.setDateLoaned(date);
this.setLoanedTo(name);
}
public void markReturned() {
this.setonLoan(false);
}
}
And Library
Code
import java.awt.image.BufferedImageFilter;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.ArrayList;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
public class Library {
static ArrayList<MediaItem> list = new ArrayList<MediaItem>();
static int numberOfItems = 100; // for used for the elements
ArrayList<MediaItem> list2 = new ArrayList<MediaItem>();
static int itemsCount = 0;
static String title;
static String format;
static String name;
static String StringAdded;
static String date;
private ArrayList<MediaItem> ;
public void addNewItem() {
MediaItem m1 = new MediaItem();// new allocated new object MediaItem
title = JOptionPane.showInputDialog("Title:");
m1.setTitle(title);// same title is set by the object
format = JOptionPane.showInputDialog("Format");
m1.setFormat(format); // set
list2.add(m1);
JOptionPane.showMessageDialog(null, title +" is added.");
}
// Marks the Item on loan
public void markItemOnLoan(String title) {
name = JOptionPane.showInputDialog("Who are you loaning it to (name)?");
date = JOptionPane.showInputDialog("When did you loan it (date)?:");
String loaned = "";
for (int i = 0; i < list2.size(); i++) {
MediaItem m = list2.get(i);
if (m.getTitle().trim().equals(title)) {
name = JOptionPane.showInputDialog("Who are you loaning it to (name)?");
date = JOptionPane.showInputDialog("When did you loan it (date)?:");
if(!m.getonLoan())JOptionPane.showMessageDialog(null, "Already Checked Out!");
else{
m.markOnLoan(name, date);
JOptionPane.showMessageDialog(null, "Successful");
return;
}
}
JOptionPane.showMessageDialog(null, title + " Can't Find the title");
}
// This is a method that list all items in the library in a array list
public ArrayList<String> listAllItems() {
ArrayList<String> items = new ArrayList<String>();
for (int i = 0; i < list2.size(); i++) {
MediaItem m = list2.get(i);
String loanedTo = " ", date = " ";
if (m.getonLoan()) {
if (m.getLoanedTo() != null) {
loanedTo = " loaned to " + m.getLoanedTo();
date = " on " + m.getDateLoaned();
}
}
items.add(m.getTitle() + "(" + m.getFormat() + ")" + loanedTo
+ date);
}
JOptionPane.showMessageDialog(null, items.size() + " lai Successful");
return items;
}
public void markItemReturned(String title) {
title = JOptionPane.showInputDialog("Title");
for (int i = 0; i < list2.size(); i++) {
MediaItem m = list2.get(i);
if (m.getonLoan() && m.getTitle().trim().equals(title)) {
m.markReturned();
JOptionPane.showMessageDialog(null, title
+ " returned.");
return;
}
}
JOptionPane.showMessageDialog(null, title
+" is not out on loan.");
}
public void delete(String title) {
title = JOptionPane.showInputDialog("Title");
for (int i = 0; i < list2.size(); i++) {
MediaItem m = list2.get(i);
if (m.getTitle().trim().equals(title)) {
list2.remove(i);
JOptionPane.showMessageDialog(null, title +" is deleted");
return;
}
}
JOptionPane.showMessageDialog(null, "Title not found on the list");
}
public void save() throws FileNotFoundException {
PrintWriter writer = new PrintWriter("library.txt");
String item;
for (int i = 0; i < list2.size(); i++) {
MediaItem m = list2.get(i);
item = m.getTitle() + "#" + m.getFormat() + "#" + m.getonLoan()
+ "#" + m.getLoanedTo() + "#" + m.getDateLoaned();
writer.println(item);
}
writer.close();
}
public void open() {
int count = 0;
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("library.txt"));
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(null, count
+ " Library.txt is (not) found");
System.err.println("Error: " + e.getMessage());
}
try {
String strLine = br.readLine();
while (strLine != null) {
count++;
String title, loanTo, date, format, text;
boolean loaned;
MediaItem m = new MediaItem();
int begX = 0, endX = strLine.indexOf("#", begX);
text = strLine.substring(begX, endX);
if (text.equals("null")) {
title = "";
} else
title = text;
begX = endX++;
endX = strLine.indexOf("#", begX + 1);
text = strLine.substring(begX + 1, endX);
if (text.equals("null")) {
format = "null";
} else
format = text;
System.out.println(begX + " " + endX + " " + format);
// ---------------------------------//
begX = endX++;
endX = strLine.indexOf("#", begX + 1);
text = strLine.substring(begX + 1, endX);
if (text.equals("null")) {
loaned = false;
} else if (text.equals("true"))
loaned = true;
else
loaned = false;
System.out.println(begX + " " + endX + " " + loaned);
// ----------------------------------------------------------------
begX = endX++;
endX = strLine.indexOf("#", begX + 1);
text = strLine.substring(begX + 1, endX);
if (text.equals("null")) {
loanTo = "null";
} else
loanTo = text;
System.out.println(begX + " " + endX + " " + loanTo);
// -----------------------------//
begX = endX++;
endX = strLine.indexOf("#", begX+2);
text = strLine.substring(begX + 1);
if (text.equals("null")) {
date = "";
} else
date = text;
System.out.println(begX + 1 + " " + date);
m.setTitle(title);
m.setFormat(format);
m.setLoanedTo(loanTo);
m.setonLoan(loaned);
m.setDateLoaned(date);
list2.add(m);
strLine = br.readLine();
}
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(null, count + "IOError");
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}