I have a shopping list that contains a set number of items. Through user input, I want to ask the user how many of the same item would they like to add to their shopping bag.
For Example: I have the following ArrayList newProduct = {"apple","orange","banana"};
. Now I want to prompt the user to choose which (and how many) of the items in newProduct would they like to add into a second array called bag
. If the user chooses a an apple with a quantity of 2, then 2 apples would be added to bag
and it would look like this bag = {"apple","apple"};
I tried to useoperands that would duplicate the value. But it didn't work very well. What is wrong with this?
import java.util.*;
import java.util.stream.IntStream;
public class Bag extends Price {
static Scanner input = new Scanner(System.in);
@SuppressWarnings("unused")
private static String newName;
private int choice;
public ArrayList<String> bag = new ArrayList<String>();
public List<Double> bagPrice = new ArrayList<Double>();
public void addProduct() {
Bag item = new Bag();
while (sum(bagPrice) <= 58.00) {
System.out.println("Choose a Priority");
item.choice = input.nextInt();
System.out.println("Choose Quantity");
int quantity = input.nextInt();
if (IntStream.of(newPriority).anyMatch(x -> x == item.choice)) {
item.bag.add(newProduct.get(item.choice));
System.out.print("PRICE $ " + Double.valueOf(formatter.format(price.get(item.choice - 1))));
bagPrice.add(price.get(item.choice - 1));
System.out.println("BAG: " + item.bag);
System.out.println("TOTAL : " + Double.valueOf(formatter.format(sum(bagPrice))));
} else {
System.out.println("Priority Does Not Exist");
}
}
}
public double sum(List<Double> bagPrice) {
double sum = 0.00;
for (double s : bagPrice) {
sum = sum + s;
}
return sum;
}
}
I'm fairly new to programming so if there is any better suggestion I would greatly appreciate it.