please need help ... for the delete method in array
hi there, i really need help in doing a method which deletes a person from an array list. it is basically a dvd rental system that keeps track of clients, salesperson, dvds and loans. Currently I have done a Client class and a SalesPerson class that both inherit from the Person Class. Then I also have the DVD class, the DvdShop class, the Loan class and finally the runner class. Now, my problem is that I need the user to input the id of the person they want to delete from the array and the person is deleted. I have already done some of the code for the delete method my self but my problem is that i don't know where to put that line of code that says remove. ....
Here is the code so far;
import java.io.*;
import java.util.*;
public class DvdShop implements Serializable
{
public DvdShop()
{
//Default Constructor
}
ArrayList aList = new ArrayList();
int location;
int index = 0;
public void AddPerson (Person tmpPerson)
{
aList.add(tmpPerson);
}
public void AddClient (Client tmpClient)
{
aList.add(tmpClient);
}
public void AddSalesPerson (SalesPerson tmpSalesPerson)
{
aList.add(tmpSalesPerson);
}
public void AddDvd (DVD tmpDvd)
{
aList.add(tmpDvd);
}
public void AddLoan (Loan tmpLoan)
{
aList.add(tmpLoan);
}
public int removePerson (int IDToSearch)
{
for(int i=0; i<aList.size(); i++)
{
Person tmpPerson = (Person)aList.get(i);
if(tmpPerson.GetId() == IDToSearch)
{
return i;
}
}
return -1;
}
public int SearchForDVD(int IDToSearch)
{
for(int id=0; id<aList.size(); id++)
{
DVD tmpDVD = (DVD)aList.get(id);
if(tmpDVD.GetID() == IDToSearch)
{
return id;
}
}
return -1;
}
public String DisplayDVD(int index)
{
DVD tmpDVD = (DVD)aList.get(index);
return tmpDVD.toString();
}
public String showAllDvds()
{
String tmpDvd = "";
if(aList.size()>0)
{
for(int id = 0; id<aList.size(); id++)
{
DVD tmpDVD = (DVD) aList.get(id);
tmpDvd += "DVD " + (id+1) + ": " + tmpDVD.toString() + "\n";
}
}
else
{
tmpDvd = "No DVDs found! ";
}
return tmpDvd;
}
public String showAllPersons()
{
String tmpVar = "";
if(aList.size()>0)
{
for(int id = 0; id<aList.size(); id++)
{
Person tmpPerson = (Person) aList.get(id);
tmpVar += "Person " + (id+1) + ": " + tmpPerson.toString() + "\n";
}
}
else
{
tmpVar = "No Persons found! ";
}
return tmpVar;
}
public String showAllLoans()
{
String tmpLoa = "";
if(aList.size()>0)
{
for(int id = 0; id<aList.size(); id++)
{
Loan tmpLoan = (Loan) aList.get(id);
tmpLoa += "Loan " +(id+1) + ": " + tmpLoan.toString() + "\n";
}
}
else
{
tmpLoa = "No Loans! " ;
}
return tmpLoa;
}
public int SearchForPerson(int idToSearch)
{
for(int i=0 ; i<aList.size(); i++)
{
Person tmpPerson = (Person)aList.get(i);
if(tmpPerson.GetId() == idToSearch)
{
return i;
}
}
return -1;
}
public String DisplayPerson(int index)
{
Person tmpPerson = (Person)aList.get(index);
return tmpPerson.toString();
}
public boolean saveToFile()
{
try
{
File outFile = new File("object.dat");
FileOutputStream outFileStream = new FileOutputStream(outFile);
ObjectOutputStream outObjectStream = new ObjectOutputStream(outFileStream);
outObjectStream.writeObject(aList);
outObjectStream.close();
return true;
}
catch(IOException ioe)
{
System.out.println(ioe.toString());
return false;
}
}
public boolean loadFromFile()
{
try
{
File inFile = new File("object.dat");
FileInputStream inFileStream = new FileInputStream(inFile);
ObjectInputStream inObjectStream = new ObjectInputStream(inFileStream);
aList = (ArrayList)inObjectStream.readObject();
inObjectStream.close();
return true;
}
catch(IOException ioe)
{
System.out.println(ioe.toString());
return false;
}
catch(ClassNotFoundException cnfe)
{
System.out.println(cnfe.toString());
return false;
}
}
}
import java.util.*;
public class Runner
{
public static void manin (String [] args)
{
Scanner sc = new Scanner(System.in);
sc.useDelimiter("\n");
//create an instance of DVD Shop, instances of Client and instances of Salesperson
DvdShop myDvdShop = new DvdShop();
int choice =0;
do
{
System.out.println("\n1 - To Add a new Client");
System.out.println("2 - To Add a Sales Person");
System.out.println("3 - to Add a DVD");
System.out.println("4 - To Show all Persons");
System.out.println("5 - To Delete a Client");
System.out.println("6 - To Show all DVDs");
System.out.println("7 - To Make a new DVD Loan");
System.out.println("8 - To Show all Loans");
System.out.println("9 - To Search for a Client or a SalesPerson");
System.out.println("10 - To save to file");
System.out.println("11 - To load from file");
System.out.println("12 - To exit\n");
do
{
System.out.println("Please enter yor choice...");
choice = sc.nextInt();
if((choice<=0 || (choice>12)))
{
System.out.println("Invalid input. Only 1 to 11 accepted");
}
}while((choice<1) || (choice>12));
Person newPerson = new Person();
switch(choice)
{
case 1:
//Adding a new Client
Client newClient = new Client();
//Ask user to input details of client being added
System.out.println("Please enter ID");
newClient.SetId(sc.nextInt());
System.out.println("Please enter name");
newClient.SetName(sc.next());
System.out.println("Please enter surname");
newClient.SetSurname(sc.next());
System.out.println("Please enter email");
newClient.SetEmail(sc.next());
System.out.println("Please enter address");
newClient.SetAddress(sc.next());
System.out.println("Please enter mobile no.");
newClient.SetMobile(sc.nextInt());
//Add to DvdShop
myDvdShop.AddClient(newClient);
break;
case 2:
//Adding a new SalesPerson
SalesPerson newSalesPerson = new SalesPerson();
//Ask user to input details of Salesperson being added
System.out.println("Please enter ID of SalesPerson");
newSalesPerson.SetId(sc.nextInt());
System.out.println("Please enter name");
newSalesPerson.SetName(sc.next());
System.out.println("Please enter surname");
newSalesPerson.SetSurname(sc.next());
System.out.println("Enter mobile no.");
newSalesPerson.SetMobile(sc.nextInt());
System.out.println("Enter no.of hours worked");
newSalesPerson.SetHoursWorked(sc.nextInt());
System.out.println("Salary of SalesPerson is: " );
//Add to DvdShop
myDvdShop.AddSalesPerson(newSalesPerson);
break;
case 3:
//Adding a new DVD
DVD newDVD = new DVD();
//Ask user to input details of DVD being added
System.out.println("Please enter ID of DVD being added");
newDVD.SetID(sc.nextInt());
System.out.println("Please enter name of DVD");
newDVD.SetName(sc.next());
System.out.println("Please enter year published");
newDVD.SetYear(sc.nextInt());
System.out.println("Please enter the amount of this DVD available in stock");
newDVD.SetInStock(sc.nextInt());
//Add to DvdShop
myDvdShop.AddDvd(newDVD);
break;
case 4:
//Show all Clients & SalesPersons
System.out.println(myDvdShop.showAllPersons());
break;
case 5:
//Delete a Client from the DvdShop
System.out.println("Enter ID of person you want to delete: ");
int indexPo = myDvdShop.removePerson(sc.nextInt());
if(indexPo!= -1)
{
//Person exists and his position in array list is retrieved
System.out.println(myDvdShop.removePerson(indexPo) );
}
else
{
//Person does not exists
System.out.println("No matching ID found to delete!! ");
}
System.out.println(myDvdShop.removePerson(indexPo) );
break;
case 6:
//Show all DVDs in DvdShop
System.out.println(myDvdShop.showAllDvds());
break;
case 7:
//Making a new DVD Loan
Loan newLoan = new Loan();
//Ask user to input details of the DVD Loan being done
System.out.println("Please enter the DVD ID being rented");
newLoan.SetDvdID(sc.nextInt());
System.out.println("Please enter SalesPerson ID making the loan");
newLoan.SetSalesPersonID(sc.nextInt());
System.out.println("Please enter Client ID renting the DVD");
newLoan.SetClientID(sc.nextInt());
System.out.println("Please enter number of days the client can keep the DVD");
newLoan.SetRentDays(sc.nextInt());
//Add to DvdShop
myDvdShop.AddLoan(newLoan);
break;
case 8:
//Show all Loans
System.out.println(myDvdShop.showAllLoans());
break;
case 9:
//Search for a person
System.out.print("Enter ID of Person you want to search for: ");
int indexPos = myDvdShop.SearchForPerson(sc.nextInt());
if(indexPos!= -1)
{
//Person exists
System.out.println(myDvdShop.DisplayPerson(indexPo s));
}
else
{
//Person does not exist
System.out.println("No matching ID found!");
}
break;
case 10:
//Saving changes to file
if(myDvdShop.saveToFile())
{
System.out.println("File was successfully saved!");
}
else
{
System.out.println("an error occured while file was being saved!");
}
break;
case 11:
//Loading from File
if(myDvdShop.loadFromFile())
{
System.out.println("File was successfully loaded!");
}
else
{
System.out.println("An error occured while attempting to load from file!");
}
break;
case 12:
//Exiting the program
System.out.println("Program terminating!");
break;
}
}while(choice!=12);
}
}
should I put the line of code that says aList.remove(indexPo); in the RUNNER class or in the DVDSHOP class????
thanks cause i really need help :)
Re: please need help ... for the delete method in array
Quote:
Originally Posted by
yanikapausini:)
should I put the line of code that says aList.remove(indexPo); in the RUNNER class or in the DVDSHOP class????
thanks cause i really need help :)
Where in each class would you put it? What happened when you tried each?
Re: please need help ... for the delete method in array
i tried it in both of them. When I put it in the DvdShop class it says 'cannot find symbol - variable indexPo'. In the DvdShop class I code it this way ;
public int removePerson (int IDToSearch)
{
for(int i=0; i<aList.size(); i++)
{
Person tmpPerson = (Person)aList.get(i);
if(tmpPerson.GetId() == IDToSearch)
{
return i;
}
}
return -1;
aList.remove(indexPo);
}
When I put it in the Runner class it says 'cannot find symbol - variabe aList'. In the runner I coded it this way ;
case 5:
//Delete a Client from the DvdShop
System.out.println("Enter ID of person you want to delete: ");
int indexPo = myDvdShop.removePerson(sc.nextInt());
if(indexPo!= -1)
{
//Person exists and his position in array list is retrieved
System.out.println(myDvdShop.removePerson(indexPo) );
aList.remove(indexPo);
}
else
{
//Person does not exists
System.out.println("No matching ID found to delete!! ");
}
System.out.println(myDvdShop.removePerson(indexPo) );
break;
thanks.
Re: please need help ... for the delete method in array
When you get a compiler error saying that it cannot find the symbol, that means the variable you're trying to use is not available in the scope. Variables declared inside if statements or for loops are only available inside that statement. Variables declared inside methods are only available inside that method. Variables declared inside classes are only available inside that class. To access an instance from one class in another class, you have to pass a reference to that instance between the classes.
Re: please need help ... for the delete method in array
i don t actually know how to do that.. can you please give me an example similar to the one i need to do?? thanks very much for your replies :)
Re: please need help ... for the delete method in array
Quote:
Originally Posted by
yanikapausini:)
i don t actually know how to do that.. can you please give me an example similar to the one i need to do?? thanks very much for your replies :)
Which part don't you know how to do? If you want help, you'll have to provide an SSCCE that demonstrates the problem. In your case, two simple classes- one containing an ArrayList, and one that needs to delete something form that ArrayList. You've posted a lot of extra code. Don't forget the highlight tags. Chances are, during the process of narrowing your code down to an SSCCE, you'll figure it out all by yourself.
Re: please need help ... for the delete method in array
ok thanks very much.. i'll try to do it in another way :) thanks again :)
Re: please need help ... for the delete method in array
One more thing you might want to pay attention to while writing code is making classes separate. That way, when you find an error, you handle classes one at a time. Also, as said to you by someone in this forum, highlight your code with these tags [highlight = java] code here[/java] to make it easier for us to read and maybe help you with it.