코드가 복잡 해지면 죄송합니다.다른 if 문으로 ArrayList에서 if 문으로 작성된 객체를 제거하는 방법이 있습니까?
여기 새내기. 사용자가 입력에 따라 App 유형의 객체를 제거하도록하고 ArrayList는 'App'유형의 객체 만 허용합니다. 그러나 if 문에 App 유형의 객체를 만들었 기 때문에 제거 할 수 없으며 액세스하는 방법을 모르겠습니다. 다른 if 문에 의해 작성된 객체를 제거하고 편집 할 수있는 방법이 있습니까?
public static void main(String[] args) throws IOException {
// TODO code application logic here
Scanner myScan = new Scanner(System.in);
System.out.println("Enter your option from the list below");
System.out.println("1.App Store");
System.out.println("2.Customer stuff");
int input = myScan.nextInt();
if (input == 1){
wholeSystem();
}
}
public static void wholeSystem()
{
boolean choice = true;
int input2 = 0;
Shop appStore = new Shop(new ArrayList<App>());
while (choice == true)
{
Scanner myScan2 = new Scanner(System.in);
System.out.println("Enter another option from the list below");
System.out.println("1. Add new App details");
System.out.println("2. Delete App details");
System.out.println("3. Edit App details");
System.out.println("4. List App details");
System.out.println("5. Search App details");
System.out.println("6. Quit");
input2 = myScan2.nextInt();
if (input2 == 6)
{
System.out.println("You have exited.");
break;
}
else if (input2 == 1)
{
System.out.println("You have selected feature "+input2+".");
Scanner myScan3 = new Scanner(System.in);
App myObj = new App();
System.out.println("Please enter the app name:");
myObj.setAppName(myScan3.nextLine());
System.out.println("Please enter the developer name:");
myObj.setDeveloperName(myScan3.nextLine());
System.out.println("Please enter the app's function:");
myObj.setAppFunction(myScan3.nextLine());
System.out.println("Please enter the app's type");
myObj.setType(myScan3.nextLine());
System.out.println("Please enter the app's cost:");
myObj.setCost(myScan3.nextInt());
System.out.println("Please enter the app's popularity:");
myObj.setPopularity(myScan3.nextInt());
System.out.println(myObj.getAppName()+myObj.getDeveloperName());
appStore.addApp(myObj);
File AppStore = new File("AppStore");
try{
BufferedWriter out;
out = new BufferedWriter (new FileWriter(AppStore,true));
out.write(myObj.getAppName());
out.newLine();
out.write(myObj.getDeveloperName());
out.newLine();
out.write(myObj.getAppFunction());
out.newLine();
out.write(myObj.getType());
out.newLine();
out.write (String.valueOf(myObj.getCost()));
out.newLine();
out.write(String.valueOf(myObj.getPopularity()));
out.newLine();
out.write("========================================================");
out.newLine();
//Close the output stream
out.close();
}
catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
else if (input2 == 2)
{
int choiceInput = 0;
System.out.println("You have selected feature "+input2+". Which app do you want deleted?");
// here's where I tried to remove an object
appStore.printApps();
choiceInput = myScan2.nextInt();
appStore.deleteApp(choiceInput); with user input
다음은 ArrayList를 만든 가게 클래스입니다. 당신은 단지 객체 참조에 의해 삭제 지원 지금
public void deleteApp(int index){apps.remove(index);}
을하지만, 기본 ArrayList를 : 나는 잘 이해한다면 공용 클래스 숍 {
private ArrayList<App> apps = new ArrayList<App>();
public Shop(ArrayList apps)
{
this.apps = apps;
}
public ArrayList<App> getApps(){return apps;}
public void setApps(ArrayList<App>apps){this.apps = apps;}
public void addApp(App app){apps.add(app);}
public void deleteApp(App app){apps.remove(app);}
... 작동하지 않는 것은 무엇입니까? 오류 메시지, 스택 추적 또는 동작 설명을 참조하십시오. 정수 인수를 사용하여 deleteApp를 호출하고 있지만 App을 전달해야하는 선언은 없습니다. – RobP
int가 앱과 호환되지 않으며 변환 할 수 없다고합니다. 그리고 사용자가 스캐너에 숫자를 입력하고 주어진 숫자를 기반으로 arraylist에서 객체를 삭제할 수 있기를 원하기 때문에 정수로 호출하려고합니다. – user3479469
아래 내 대답을 참조하십시오. Arraylist가 지원하기 때문에 ArrayList의 색인을 기반으로하는 앱을 삭제할 수있는 코드는 더 이상 필요하지 않습니다. – RobP