Level 2 Version B - Level 2 above plus
Use of classes and objects to store and manipulate the item data
Example
• Write a new class ‘Event’ to represent details of sports event objects. A sports event consists of (for example) the event title, year and venue (“Olympic Games”, 2012, “London”)
• Your class definition should include an appropriate constructor method, and methods to allow addition, updating, searching and sorting of objects.
• The class should have a method to store all the objects from the array into a file. A further method should allow the contents of a previously stored file of objects to be restored into an array.
• Create a menu of options for the programme including
o Input a new object from a keyboard
o Search for an object and print details to screen
o Edit an object
o Sort the objects
o Print all the entries so far
o Save objects to a file
o Retrieve objects from a file
o Quit
//The main problem is with linear search and array created. I created array of objects, but i get int cannot be dreferenced. Also, problems with the array when storing..Always returns position 0
import javax.swing.JOptionPane;
import java.util.*;
import java.io.*;
class Event
{
public static void main(String[] args) throws IOException
{
final int ARRAYSIZE = 20; //larger array size.:P
Sport[] sportEvent = new Sport[ARRAYSIZE]; //create object
int i =0;
int a = 10;
while (a!=0)
{
a= Integer.parseInt(JOptionPane.showInputDialog("Enter 1 to add\n"
+ "Enter 7 to update\n"
+ "Enter 2 to save\n"
+ "Enter 3 to search\n"
+ "Enter 4 to retrieve data\n"
+ "Enter 5 to edit\n"
+ "Enter 6 to print\n"
+ "Enter 0 to exit"));
if(a==1)
{
add( i, sportEvent);
}
if (a==2)
{
save(i, sportEvent);
}
if (a==3)
{
search(sportEvent);
}
if (a==4)
{
retrieve();
}
if (a ==6)
{
print(i, sportEvent);
}
if (a==7)
{
update(sportEvent);
}
}
}
public static void add( int i ,Sport[] sportEvent)
{
i++;
sportEvent[i] = new Sport();
sportEvent[i].title = JOptionPane.showInputDialog("Enter the Title");
sportEvent[i].year = Integer.parseInt(JOptionPane.showInputDialog("Enter the Year"));
sportEvent[i].venue= JOptionPane.showInputDialog("Enter the Venue");
sportEvent[i].printSport();
}
public static void update(Sport [] SportEvent)
{
}
public static void search(Sport[] sportEvent)
{
String search = JOptionPane.showInputDialog("Enter the data you want to search");
int result = linearSearch(sportEvent, search);
if (result == -1)
{
JOptionPane.showMessageDialog(null, "Sorry " + search + " , not found");
System.out.println("Sorry " + search + " , not found");
}
else
{
JOptionPane.showMessageDialog(null, "Search " + search + " found at position " + result); //position is always 0,when we run the program
System.out.println("Search " + search + " found at position " + result);
}
}
public static int linearSearch(Sport sportEvent[], String key )
{
for (int i =0; i< sportEvent.length; i++)
{
if (sportEvent[i].getTitle.equals(key))
return i;
if (sportEvent[i].getYear.equals(key)) //NullPointerException or int cannot be derefrenced..
return i;
if (sportEvent[i].getVenue.equals(key))
return i;
}
return (-1); //this doesnot work.
}
public static void edit (Sport sportEvent[])
{
}
public static void print(int i, Sport [] sportEvent)
{
for (i = 0; i<sportEvent.length; i ++)
{
System.out.println(sportEvent[i].getTitle());
}
}
public static void save(int i ,Sport sportEvent[]) throws IOException
{
String filename = JOptionPane.showInputDialog(" Enter to Create a filename" );
final FileWriter outputSportFile = new FileWriter(filename);
final BufferedWriter sportBuffer = new BufferedWriter(outputSportFile);
final PrintWriter sportPrint = new PrintWriter (sportBuffer);
sportPrint.println(sportEvent[i].title);
sportPrint.println(sportEvent[i].year);
sportPrint.println(sportEvent[i].venue);
sportPrint.close();
i++ ;
}
public static void retrieve() throws IOException
{
String filename = JOptionPane.showInputDialog("Enter the filename you wish to retrieve" );
final FileReader inputSportFile = new FileReader(filename);
final BufferedReader sportBuffer = new BufferedReader (inputSportFile);
String line1= sportBuffer.readLine();
String line2= sportBuffer.readLine();
String line3= sportBuffer.readLine();
JOptionPane.showMessageDialog(null, line1 + line2 + line3);
Sport x = new Sport();
x.title = line1;
x.year= Integer.parseInt(line2);
x.venue= line3;
sportBuffer.close();
}
}
import javax.swing. *;
class Sport
{
public String title;
public int year;
public String venue; // object of type Sport consists of String title, int year and String venue, fields of
public Sport( String t, int y, String v) //initialise object parameters
{
title = t; //stores 1st argumentpassed into title after new sport
year = y;
venue= v;
}
public Sport() // if no arguments are passed, this will initialise default value as " ", 0, ""
{
title = " ";
year = 0;
venue = " ";
}
public String getTitle()
{
return title;
}
public int getYear()
{
return year;
}
public String getVenue()
{
return venue;
}
public void getTitle(String t)
{
title=t;
}
public void getYear(int y)
{
year=y;
}
public void getVenue(String v)
{
venue=v;
}
public void printSport()
{
System.out.println( "Title: " + title + "\n" + "Year: " + year + "\n" + "Venue:" +venue);
}
}