-1

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.

Joel Peltonen
  • 13,025
  • 6
  • 64
  • 100
brazjul
  • 339
  • 2
  • 5
  • 19
  • 3
    Unless this is an assignment, a better setup would just be to use a `Map` that uses product names as the key, and quantities as the value. – Carcigenicate Nov 20 '16 at 20:56
  • It's not really clear what you're asking here. Can you show us your full code? In particular where `Bag`, `sum()` and `newPriority` are defined. – Todd Sewell Nov 20 '16 at 21:17
  • 1
    It looks like an assignment. But I'm sure whoever gave you this exercise didn't mean what you think he meant. Read your instructions carefully again, and use HashMap afterwards. – Amir Uval Nov 20 '16 at 21:26
  • @ToddSewell I updated my code here. I have multiple classes, but this is the class for this code. As I add products to the 'bag' arraylist, I'm also adding prices to a separate arraylist called `bagPrice`. `Sum();` is a function to determine when the condition is true. What I'm trying to do is take 1 copy of an item in one array and create 2 copies of that same item, so I can add into another array. – brazjul Nov 20 '16 at 21:33
  • 1
    Okay let's say I have a list `source` and I want to add the first item to another list `target` twice. The code looks something like this: `item = source.get(0); target.add(item); target.add(item);`. Is it something else you're having trouble with? I still don't really get your question, sorry. – Todd Sewell Nov 20 '16 at 21:42
  • @todd No worries, and thank you for you help. Sorry, it's a bit confusing what I'm trying to do. But essentially what I want is tell my code when to stop once the shopping bag value has reached $58 @uval gave me an idea which which I can work around and get the same results. I'll ask the user for a quantity and multiply the quantity by the the price of that item. I can do something like this `bagPrice.add(price.get(item.choice - 1) * quantity);` I know, it's confusing :) – brazjul Nov 20 '16 at 21:55
  • @brazjul for future reference - try to remove as much as possible from your example before posting :) and keep it as short and to the point as you can can. Also, think about naming, I think naming and code style improvements will solve / would have solved this issue (What is a priority, is that the correct word?). Also add one clear question. Also add calling code - new Priority is still missing. – Joel Peltonen Nov 01 '17 at 07:45

1 Answers1

-1

You might have to use clone() method to get a copy, hope this would be helpful Clone method for Java arrays

Community
  • 1
  • 1