S
SpeedRunner
Guest
Here's my whole program so far, not completed yet. It uses java.util.LinkedList. I just want to point out my "add" method.
here's the add method:
The problem during runtime is that when a new entry is added, it should not allow me to add a duplicate entry. But it does. I don't know why...
Code:
import javax.swing.*;
import java.util.*;
public class RosterTest
{
public static void main(String[] args)
{
String input;
Roster myRoster = new Roster();
do
{
input = menu();
if(input == null || input.equalsIgnoreCase("q"))
{
JOptionPane.showMessageDialog(null,"Goodbye!");
}
else if(input.equalsIgnoreCase("a"))
{
doAdd(myRoster);
}
else if(input.equalsIgnoreCase("f"))
{
doFind(myRoster);
}
else if(input.equalsIgnoreCase("r"))
{
doRemove(myRoster);
}
else if(input.equalsIgnoreCase("l"))
{
JOptionPane.showMessageDialog(null,myRoster);
}
else
{
JOptionPane.showMessageDialog(null,"Invalid - try again!");
}
}while(input != null && !input.equalsIgnoreCase("q"));
System.exit(0);
}
public static String menu()
{
String s = "MENU\na - add\nf - find\nr - remove"
+ "\nl - list\nq - quit\nEnter a letter:";
return JOptionPane.showInputDialog(s);
}
public static void doAdd(Roster myRoster)
{
String name = JOptionPane.showInputDialog(
"Enter student name:");
int number = Integer.parseInt(JOptionPane
.showInputDialog("Enter student number:"));
double gpa = Double.parseDouble(JOptionPane
.showInputDialog("Enter student gpa:"));
boolean added = myRoster.add(new Student(name,number,gpa));
if(!added)
{
JOptionPane.showMessageDialog(null,"Student with number " +
number + " already exists!");
}
}
public static void doFind(Roster myRoster)
{
int number = Integer.parseInt(JOptionPane
.showInputDialog("Enter student number:"));
Student foundStu = myRoster.find(number);
if(foundStu == null)
{
JOptionPane.showMessageDialog(null,"No student with number "
+ number + "!");
}
else
{
JOptionPane.showMessageDialog(null,foundStu);
}
}
public static void doRemove(Roster myRoster)
{
int number = Integer.parseInt(JOptionPane
.showInputDialog("Enter student number:"));
Student removedStu = myRoster.remove(number);
if(removedStu == null)
{
JOptionPane.showMessageDialog(null,"No student with number "
+ number);
}
else
{
JOptionPane.showMessageDialog(null,"Removed student: " + removedStu);
}
}
}
//A collection of students, ordered by student number
/*Remember in particular that using the "enhanced for loop" will be very helpful.
You can use it to your advantage in find, remove, and toString.*/
class Roster
{
private List<Student> stuList = new LinkedList<Student>();
public Roster()
{
}
//Inserts the parameter student into the linked list,
// returning true if the student is actually added, false otherwise
public boolean add(Student addStu)
{
/*In your Roster add, check to see if your list "contains" the student to be added.
(This is why you overrode "equals" in Student.
If a student with that number is in the list,
then contains will return true, telling you that you must not add a student with that
number again. Otherwise, add the new student to the list*/
if (stuList.contains(addStu))
{
return false;
}
else
{
stuList.add(addStu);
return true;
}
}
//Finds a student with a particular number in the list
public Student find(int toFind)
{
/*Your roster find method can use the enhanced for loop,
cycling through all the students and looking for one whose number
matches the search number. If found, return that student.*/
for(int i =0; stuList.contains(toFind); i++)
{
}
return null;
}
//Removes from the list the student having the parameter student number.
public Student remove(int toKill)
{
/*For the remove method, use your own find method to get a reference to
the student to be removed. If a non-null was obtained from find,
then use the List remove method to remove the student.
Return the reference to the student (or null, as the case may be).*/
return null;
}
//Returns a string representation of this student
public String toString()
{
System.out.println(stuList);
return null;
}
}
//This class represents a student in a Roster.
class Student
{
private String name;
private int number;
private double gpa;
//Constructs a new Student object
public Student(String nam, int num, double g)
{
name = nam;
number = num;
gpa = g;
}
//Returns the student number of this student
public int getNumber()
{
return number;
}
public boolean equals(Student stu)
{
/*If "this" student has the same number as the parameter student,
then consider them equal.*/
if(this.getNumber() == stu.getNumber())
{
return true;
}
else
{
return false;
}
}
//Returns a String representing the student's data
public String toString()
{
return("Name " + name + ", ID" + number + ", GPA" + gpa);
}
}
here's the add method:
Code:
public boolean add(Student addStu)
{
/*In your Roster add, check to see if your list "contains" the student to be added.
(This is why you overrode "equals" in Student.
If a student with that number is in the list,
then contains will return true, telling you that you must not add a student with that
number again. Otherwise, add the new student to the list*/
if (stuList.contains(addStu))
{
return false;
}
else
{
stuList.add(addStu);
return true;
}
}
The problem during runtime is that when a new entry is added, it should not allow me to add a duplicate entry. But it does. I don't know why...