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();
}
}
}
}