简体   繁体   中英

Modyifying an arraylist from user input java

With my code the menu runs. But when the user has typed in the name of a fruit (to reserve it) I'd like it to say 'Reserved' when the user then presses 'V' (to go and view all fruit)

I have read Oracle Java Collections Arraylist and from my understanding I either have to code using the get method or equals method but I'm not too sure how to (I did try though)

Please help!

import java.util.ArrayList;
import java.util.Scanner;
public class FruitApril {
public static void main(String[] args) {

     TheMenu();  
 }
public static void TheMenu()
{
    String Customer[] = new String[10]; 
    Scanner input = new Scanner(System.in);
    ArrayList<String> fruits = initFruits();


    String option; 
    do {   // loop back to here as long as Q isn't selected           
    System.out.println("\nMenu");
    System.out.println("V: Views all fruit");
    System.out.println("R: To reserve fruit"); 
    System.out.println("Q: To exit");

    option = input.next();  

    if (option.charAt(0) == 'V' ) 
    { 
        viewAllFruit(fruits,Customer);
    } 


    if (option.charAt(0) == 'R' ) 
    { 
        reserveFruit(fruits,Customer);
    }

    }
    while (option.charAt(0) != 'Q');
    }   



    public static ArrayList<String> initFruits() {
    ArrayList<String> theFruit = new ArrayList<String>();
    theFruit.add("Plums");
    theFruit.add("Grapes");
    theFruit.add("Oranges");
    theFruit.add("Prunes");
    theFruit.add("Apples");
    return theFruit;
  }

 public static void viewAllFruit(ArrayList<String> fruits, String
 CustomerRef[])
  {
    for (String fruit : fruits) {
        System.out.println("Fruit " + fruit + " is in stock ");   
    }
  }

 public static void reserveFruit(ArrayList<String> fruits,String
  CustomerRef[])
{

    Scanner input = new Scanner(System.in);

    while(true) {
        System.out.println("Please enter the name of the fruit you'd like 
        to reserve");
        String Fruitn = input.next();

        if (Fruitn.equals(fruits))
        {
            fruits.get(Fruitn);
      }
   }
}

Hi
i have changed your code

import java.util.ArrayList;
import java.util.Scanner;
public class FruitApril {
public static void main(String[] args) {

 TheMenu();  
}
public static void TheMenu()
{
String Customer[] = new String[10]; 
Scanner input = new Scanner(System.in);
ArrayList<String> fruits = initFruits();


String option; 
do {   // loop back to here as long as Q isn't selected           
System.out.println("\nMenu");
System.out.println("V: Views all fruit");
System.out.println("R: To reserve fruit"); 
System.out.println("Q: To exit");

option = input.next();  

if (option.charAt(0) == 'V' ) 
{ 
    viewAllFruit(fruits,Customer);
} 


if (option.charAt(0) == 'R' ) 
{ 
    reserveFruit(fruits,Customer);
}
if(option.charAt(0) == 'Q') // added this condition
{
    System.exit(0);
}

}while (option.charAt(0) == 'Q');

}

public static ArrayList<String> initFruits() {
ArrayList<String> theFruit = new ArrayList<String>();
theFruit.add("Plums");
theFruit.add("Grapes");
theFruit.add("Oranges");
theFruit.add("Prunes");
theFruit.add("Apples");
return theFruit;

}

 public static void viewAllFruit(ArrayList<String> fruits, String CustomerRef[])
  {
for (String fruit : fruits) {
    System.out.println("Fruit " + fruit + " is in stock ");   
}
  }

 public static void reserveFruit(ArrayList<String> fruits,String  CustomerRef[])
{

    Scanner input = new Scanner(System.in);

   while(true) {
    System.out.println("Please enter the name of the fruit you'd like to reserve");
    String Fruitn = input.next();
int index = fruits.indexOf(Fruitn);//added this line
    if (index>=0)// added this condition
    {
        String fruit = fruits.get(index); //added this line
    System.out.println("Reversed Fruit:"+fruit); // do according to your requirement
    }
  }
}
}

I have taken the input form the user.Using that input i find the index of the fruit in the ArrayList then i used get method to take that fruit.You didn't mention your requirement about reversing of that string .So i just put sopComplete the remaining code according to your requirement.

If ArrayList is mandatory, i suggest you create another arraylist for the reserved fruits:

ArrayList<String> reservedFruits = new ArrayList<String>();

Then once a fruit is reserved, just add the fruit into this list:

public static void reserveFruit(String fruitToReserve){
  reservedFruits.add(fruitToReserve);

  // you will want to remove the reserved fruit on your fruits Arraylist here
  // so that once you view the fruits, it wont be printed as available
  // i leave this method to you.
  removeTheReservedFruitFromTheList(fruitToReserve);
}

Then when the customer views your lists (presses V) just sysout both of your arraylists:

// print all available fruits
for(String fruit:fruits){
  System.out.println(fruit + " is available.");
}

// print all reserved fruits
for(String fruit:reservedFruits ){
  System.out.println(fruit + " is reserved.");
}

I gave you code snippets, but that should point you in the right direction. Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM