0

So I have two classes one is called bag the other is called TestBag. The goal is to ask the user what they want: to add or remove, and show what they have in the cart.

I'm kinda new to encapsulation, and I don't know how to get the user input and put it in the add method and get that to go the the cart string to show what the user has in the cart. This is what I have so far. I'm doing the add part first before the remove.

bag class:

import java.util.Arrays;

class bag {
    private String[] cart = new String[5];
    private int add;


    public String[] getcart(){
        return Arrays.copyOf(cart, getcart().length);
    }

    public int getAdd(){
        return add;
    }


    public void setAdd(int newValue){
        add = newValue;

    }

    public void setcart(String [] cart){
        cart = cart;
    }


}

TestBag:

import java.util.Scanner;

public class TestBag {

    public static void main(String[] args) {
         bag obj = new bag();

        System.out.println("Enter one of the following commands:");
        System.out.println("1 - add");
        System.out.println("2 - remove");
        System.out.println("3 - exit");
        Scanner input = new Scanner(System.in);
        System.out.println();
        System.out.println("Enter \"1\", \"2\" or \"3\"");
        int choice =input.nextInt();




        while (choice != 3) {
            if(choice == 1) {
                System.out.println("What do you want to add? ");

                for (int i = 0 ; i < obj.setAdd.length; i++ ) {
                 obj.setAdd[i] = input.nextInt();
                }
                System.out.println("Here's whats in your cart: ");
                printArray(obj.getcart());


            }
            else if(choice == 2) {
                //remove
            }
            else if(choice == 3) {
                //...exit program
            }
            else{

                System.out.println("Enter \"1\", \"2\", \"3\"");
                choice = input.nextInt();

            }

        }   
    }

}
Glorksnork
  • 67
  • 1
  • 9
  • 3
    better use `List` than `array`, just a suggestion. – msagala25 Oct 16 '17 at 00:10
  • 2
    1. class `bag` should be called `Bag` with a capital B. 2. This code doesn't compile - fix the compilation errors first! You can start with fixing: `obj.setAdd.length` what did you try to do there ? – Nir Alfasi Oct 16 '17 at 00:11
  • If you can't use `List`/`ArrayList` (like because it's a homework assignment), then you need to keep some sort of `count` so you know for every call of `add` which index to put the next element in the array. – markspace Oct 16 '17 at 00:12
  • @alfasin I used obj.setAdd.length thinking it would to the length of add but, thinking about it now I realize that's only if it's an array. – Glorksnork Oct 16 '17 at 00:22
  • `setAdd` is a method - it's not an array or a list – Nir Alfasi Oct 16 '17 at 00:32

1 Answers1

0

Thank you for sharing your code. Looking at your bag class, you're trying to achieve encapsulation through data hiding:

  1. Declare the variables of a class as private.

  2. Provide public setter and getter methods to modify and view the variables values.

However, declaring the getters and setters that way exposes the variables you declared as private in the first step. Well, the getters are just fine since one really has to see the contents of the cart and the number of items in the cart. The setters are not fine. It's like declaring your variables as public since through the setters, you allow them to be modified at any time.

The main point of encapsulation through data hiding is to restrict access to the class's variables to selected methods. Here's how I'd go about it:

public class Bag{
    //Assuming that the bag has a dynamic size, a list would be appropriate here
    public List<String> items;
    
    public Bag(){
        items = new ArrayList<String>();
    }
    
    //This modifies the contents of the bag. The modification is restricted 
    //through one of the methods (adding an item into the bag) 
    //that are really part of the task.
    public void add(String item){
        items.add(item)
    }

    public List<String> getItems(){
        return new ArrayList<String>(items);
    }

    public int getNumberOfItems(){
        return items.size();
    }
}
Community
  • 1
  • 1
Rocket Pingu
  • 621
  • 9
  • 26