Search code examples
javaencapsulation

Adding to a string array


Working on a program dealing with encapsulation having trouble adding the user input to the array. And most likely there are other problems in here as well. One being the displaying the output when the user enters option 3. Also, I have no idea how to tell the user that they can't add any more to the bag unless they remove an item. I was just gonna work on figuring out adding and display before I even worry about removing.

import java.util.ArrayList;
import java.util.Scanner;

class Bag {
    private String[] bag = new String[5];

    public String[] bag() {
        return bag;
    }

    public void add(String bag) {
        for (int i = 0; i < bag.length(); i++) {
            //...
        }
        return;
    }

    public void remove(String bag) {
        return;
    }

    void display() {
        System.out.println("The contents are: " + this.bag());
    }

}

Here is the second class:

import java.util.ArrayList;
import java.util.Scanner;


public class testBag {

    public static void main(String[] args) {
        cart obj = new cart();
        int menu;
        int choice;
        choice = 0;

        Scanner input = new Scanner(System.in);
        ArrayList<testcart> cart = new ArrayList<>();


        System.out.println(" 1. Add item ");
        System.out.println(" 2. Remove item ");
        System.out.println(" 3. Display All");
        System.out.println(" 4. Exit ");
        menu = input.nextInt();

        while (menu != 4) {

            switch (menu) {
                case 1:
                    while (choice != 2) {
                        System.out.println("What do you want to enter: ");
                        String bag = input.next();
                        obj.add(bag);

                        System.out.println("Enter another? 1: Yes, 2: No");
                        choice = input.nextInt();
                    }
                    break;

                case 2:
                    System.out.println("Enter item to Remove: ");
                    friends.remove(input.next());
                    break;

                case 3:
                    for (int i = 0; i < obj.bag().length; i++) {
                        obj.display();
                    }
                    break;
            }
            System.out.println(" 1. Add item ");
            System.out.println(" 2. Remove item ");
            System.out.println(" 3. Display All items ");
            System.out.println(" 4. Exit ");
            menu = input.nextInt();
        }
    }
}

Solution

  • Your Bag class has to have a counter for how many bags it already has, and store a new bag in the corresponding position and increment it.

    • To display the bags you cannot System.out.println the array directly
    • To remove you need to loop through all the bags and shift them left from the point where you found the one to remove.

    Implementing all of this in your Bag class:

    public class Bag {
        private String[] bag = new String[5];
        private int count = 0; //the new count here
    
        public void add(String bagToStore) {
            if (count < bag.length){
                bag[count] = bagToStore; //store the new bag in the current position
                count++; //then increment it
            }
        }
    
        //the remove has more logic because it has to shift the bags if it removes one, 
        //not to leave wholes in the array
        public void remove(String bagToRemove) {
            boolean found = false;
            for (int i=0;i < count; ++i){
                if (bag[i].equals(bagToRemove)){ //to compare Strings you must use equals
                    found = true;
                }
    
                if (found && count < bag.length){
                    bag[i] = bag[i+1];
                }
            }
    
            if (found) count--; 
        }
    
        void display() {
            for (int i = 0; i < count; ++i) //the display has to be done with a for 
                System.out.println("The contents are: " + bag[i]); 
        }
    }
    

    Your main class would now have to be adjusted as well:

    public static void main(String[] args) {
        Bag obj = new Bag();
        int menu, choice = 0;
    
        Scanner input = new Scanner(System.in);
        do {
            //only print the menu once, you can use a do while for that
            System.out.println(" 1. Add item ");
            System.out.println(" 2. Remove item ");
            System.out.println(" 3. Display All");
            System.out.println(" 4. Exit ");
            menu = input.nextInt();
    
            switch (menu) {
            case 1:
                while (choice != 2) {
                    System.out.println("What do you want to enter: ");
                    obj.add(input.next()); //you call add with input.next as well if you want
    
                    System.out.println("Enter another? 1: Yes, 2: No");
                    choice = input.nextInt();
                }
                break;
            case 2:
                System.out.println("What do you want to remove: ");
                obj.remove(input.next()); //just call the remove method on Bag
                break;
            case 3: obj.display(); break; //call the display you already implemented!
            }
    
        } while (menu != 4);
    }