• Paranoid - курс по анонимности и безопасности в сети от команды codeby. Защита персональных данных, анонимность в сети интернет, настройка виртуальных машин, безопасная передача данных, анти форензика и еще много всего полезного. Подробнее ...

Проблема: Не удаляются списки

  • Автор темы andreus
  • Дата начала
A

andreus

#1
Здравствуйте! Есть проблема с работой кода. Программа запускается, но никак не получается удалять списки (Delete Student) и добавлять (Insert Student) после просмотра списков. Может есть какие-нибудь возможности что-то исправить... Спасибо.



C++:
import java.util.*;
import java.io.*;


public class StudentList extends MyListArray
{
public static void menu()
{
String [] menu = {"...Student List...",
"1. Create List",
"2. Add Student",
"3. Delete Student",
"4. Insert Student",
"5. Display Student",
"6. Quit"};

for(int i = 0; i < menu.length; i++)
{
System.out.println(menu[i]);
}		
}
public static void main(String[] args)
{
MyListArray list = null;
Scanner con = new Scanner(System.in);
String idno = null;
String familyName = null;
String givenName = null;
String mi = null;
String course = null;
int option = 0;
Student t = null;
Student s = null;

while(option != 6)
{
menu();
System.out.print("Select item from above: ");
option = con.nextInt();
switch(option)
{
case 1: System.out.println("Create List");
System.out.print("Enter size: ");
int size = con.nextInt();
list = new MyListArray(size);
break;

case 2: System.out.println("Add Student");
System.out.print("ID Number: ");
idno = con.next();
System.out.print("Family Name: ");
familyName = con.next();
System.out.print("Given Name: ");
givenName = con.next();
System.out.print("Telefon: ");
mi = con.next();
System.out.print("Course: ");
course = con.next();

list.addItem(new Student(idno,familyName, givenName, mi, course));
break;

case 3: System.out.println("Delete Student");
System.out.print("Enter ID Number: ");
idno = con.next();
s = new Student();
s.setIdNo(idno);

list.deleteItem(s);
break;

case 4: System.out.println("Insert Student");
System.out.print("ID Number: ");
idno = con.next();
System.out.print("Family Name: ");
familyName = con.next();
System.out.print("Given Name: ");
givenName = con.next();
System.out.print("Middle Intitial: ");
mi = con.next();
System.out.print("Course: ");
course = con.next();

s = new Student(idno, familyName, givenName, mi, course);

System.out.print("Insert at ID Number: ");
idno = con.next();
t = new Student();

t.setIdNo(idno);
list.insertItem(t,s);
break;

case 5: System.out.println("Student List");
System.out.println(" " + list);
}
}
}	
}

C++:
public class Student extends Person
{
private String idno;
private String course;

public Student(String idno, String familyName, String givenName, String mi, String course) 
{
super(familyName, givenName, mi);
this.idno = idno;
this.course = course;
}

public Student()
{

this.idno = "___";
this.course = "___";			
}
//setters
public void setIdNo(String idno)
{
this.idno = idno;
}
public void setCourse(String course)
{
this.course = course;
}
//getters
public String getIdNo()
{
return idno;
}
public String getCourse()
{
return course;
}
public boolean equals(Object obj)
{
boolean okey = false;
if(obj instanceof StudentList)
{
Student s = (Student)obj;
if(s.getIdNo().equals(idno))
{
okey = true;
}				
}
return okey;
}
public String toString()
{
return "NO: " + idno + " Family: " + familyName.toString() + "  Name: " + givenName.toString() + " Telefon: " + mi.toString() + " Kurs: " + course;
}  
}

C++:
public class MyListArray implements MyList
{
private Object[] list;
private int count;

//constructor	
public MyListArray(int size)
{
list = new Object[size];
count = 0;
}
//
public MyListArray()
{
this(10);
}
//
public boolean isFull()
{
return count == list.length;
}
public boolean isEmpty()
{
return count == 0;		
}
public boolean isElement(Object key)
{
boolean found = false;
for(int i = 0; i < count; i++)
{
if(list[i] == (key))
{
found = true;
break;
}
}
return found;
}
//
public void addItem(Object item)
{
if(!isFull())
{
list[count++] = item;
}
else
{
System.out.println("List is full.");			
}
}  
//
public void insertItem(Object key, Object item)
{
if(!isFull())
{
if(isElement(key))
{
for(int i = 0; i < count; i++)
{
if(list[i].equals(key))
{
for(int j = count - 1; j >= i; j--)
{
list[j+1] = list[j];
}
list[i] = item;
count++;
break;
}
}
}
else
{
System.out.println("Item not found.");				
}
}
else
{
System.out.println("List is full.");			
}		
}
//
public void deleteItem(Object item)
{
if(!isEmpty())
{
if(isElement(item))
{
for(int i = 0; i < count; i++)
{
if(list[i].equals(item))
{
for(int j = i; j < count - 1; j++)
{
list[j] = list[j + 1];
}
count--;
break;
}
}
}
}
else
{
System.out.println("List is empty.");			
}
}
//
public String toString()
{
StringBuffer sb = new StringBuffer();
for(int i = 0; i < count; i++)
{
sb.append(list[i]);
if(i < count - 1)
{
sb.append("\n");
}
}
return sb.toString();
}
//
public static void main(String[] args)
{
MyList m = new MyListArray(5);

m.addItem(new Integer("1"));
m.addItem(new Float("1.2"));
m.addItem(new Double("3.0"));
m.addItem(new Long("5"));
m.addItem(100);

System.out.println(m);

m.deleteItem(new Float("1.2"));

System.out.println(m);

m.insertItem(new Double("3.0"),"Saren");

System.out.println(m);
}	
}

C++:
public class Person 
{
public String familyName;
public String givenName;
public String mi;

public Person(String familyName, String givenName, String mi)
{
this.familyName = familyName;
this.givenName = givenName;
this.mi = mi;
}
public Person()
{
this("Sample","XXX", "X");
}
public void setFamilyName(String f)
{
familyName = f;
}
public void setGivenName(String givenName)
{
this.givenName = givenName;
}
public void setMi(String mi)
{
this.mi = mi;
}
public String getFamilyName()
{
return familyName;
}
public String getGivenName()
{
return givenName;
}
public String getMi()
{
return mi;
}
}

C++:
public interface MyList 
{
public void addItem(Object item);
public void insertItem(Object key, Object item);
public void deleteItem(Object key);
public boolean isFull();
public boolean isEmpty();
public boolean isElement(Object item);
}