Here's the whole code, i cant explain clearly what i want with my codes but maybe you can understand what i want:
import javax.swing.*;
public class Library
{
private Library[] items = new Library[5];
private int counter = 0;
private int xtr = 0;
private int id=0;
private String date_acquisition="";
private String description="";
private int num_copies=0;
private String title="";
public void setId(int temp_id)
{
id = temp_id;
}
public int getId()
{
return id;
}
public void setDateAcquisition(String temp_date_acquisition)
{
date_acquisition = temp_date_acquisition;
}
public String getDateAcquisition()
{
return date_acquisition;
}
public void setDescription(String temp_description)
{
description = temp_description;
}
public String getDescription()
{
return description;
}
public void setNumberOfCopies(int temp_num_copies)
{
num_copies = temp_num_copies;
}
public int getNumberOfCopies()
{
return num_copies;
}
public void setTitle(String temp_title)
{
title = temp_title;
}
public String getTitle()
{
return title;
}
public void addItem(int ids,String date,String desc,int num,String titles)
{
for(int h =0;h<=4;h++)
{
items[h] = new Library();
}
if(xtr<=5)
{
items[xtr].setId(ids);
items[xtr].setDateAcquisition(date);
items[xtr].setDescription(desc);
items[xtr].setNumberOfCopies(num);
items[xtr].setTitle(titles);
xtr++;
}
}
public void retrieveItem(int id)
{
int z;
for(z=0;z<=4;z++)
{
if(id==items[z].getId())
{
JOptionPane.showMessageDialog(null, "ID number: " + items[z].getId() +
"\nDate of Acquisition: " + items[z].getDateAcquisition() +
"\nDescription: "+ items[z].getDescription() +
"\nNumber of Copies: " + items[z].getNumberOfCopies() +
"\nTitle: " + items[z].getTitle());
}
}
}
}
Here's the testing class:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class TestLibrary
{
static int xtr = 0;
static Library[] book = new Library[5];
public static void main(String[] args)
{
new TestLibrary();
for(int h =0;h<=4;h++)
{
book[h] = new Library();
}
}
JFrame frame1 = new JFrame();
JLabel title = new JLabel("Title:");
JLabel desc = new JLabel("desc: ");
JLabel num = new JLabel("Number of Copies:");
JLabel id = new JLabel("Item ID: ");
JLabel date = new JLabel("Date Of Acquisition: ");
JButton store = new JButton("Add");
JButton search = new JButton("Search");
JTextField titleTF = new JTextField(10);
JTextField descTF = new JTextField(10);
JTextField numTF = new JTextField(10);
JTextField idTF = new JTextField(10);
JTextField dateTF = new JTextField(10);
JTextField searchTF = new JTextField(10);
JPanel panel1 = new JPanel(new FlowLayout());
TestLibrary()
{
frame1.setTitle("Library Catalog");
frame1.setSize(300,400);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setVisible(true);
frame1.add(panel1);
panel1.add(id);
panel1.add(idTF);
panel1.add(desc);
panel1.add(descTF);
panel1.add(date);
panel1.add(dateTF);
panel1.add(title);
panel1.add(titleTF);
panel1.add(num);
panel1.add(numTF);
panel1.add(store);
panel1.add(searchTF);
panel1.add(search);
store.addActionListener(new add());
search.addActionListener(new retrieve());
frame1.pack();
}
class add implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
String idSS = idTF.getText();
int idS = Integer.parseInt(idSS);
String dateS = dateTF.getText();
int num = Integer.parseInt(numTF.getText());
String title = titleTF.getText();
String desc = descTF.getText();
book[xtr].addItem(idS,dateS,desc,num,title);
xtr++;
idTF.setText("");
dateTF.setText("");
numTF.setText("");
titleTF.setText("");
descTF.setText("");
JOptionPane.showMessageDialog(null,"Item Added!");
}
}
class retrieve implements ActionListener
{
public void actionPerformed(ActionEvent ar)
{
int search = Integer.parseInt(searchTF.getText());
for(int y=0;y<=4;y++)
{
book[y].retrieveItem(search);
}
}
}
}