I am writing a simple 'shopping basket' program in Java, having not used it much at all in the last 10 years, I'm just trying to get familiar with it again...
My program currently has 3 classes: Program.java
, Basket.java
& Item.java
.
Program.java
is defined with:
public class Program {
public static int menuSelection = 0;
public static Scanner kb = new Scanner(System.in);
public static Basket basket = new Basket();
public static void main(String[] args) {
// TODO Auto-generated method stub
//Program p = new Program();
menu();
}
public static void menu() {
System.out.println("Press 1 to add an item to the basket. ");
System.out.println("Press 2 to remove an item from the basket. ");
System.out.println("Press 3 to display the basket contents. ");
System.out.println("Press 9 to proceed to checkout. ");
System.out.println("Press 0 to exit program");
menuSelection = kb.nextInt();
if(menuSelection == 1) {
basket.addItemToBasket();
} else if(menuSelection == 2) {
basket.removeItemFromBasket();
} else if (menuSelection == 3) {
basket.displayBasket();
} else if(menuSelection == 9) {
System.out.println("Proceed to checkout");
} else if(menuSelection == 0) {
System.exit(0);
}
}
}
Basket.java
is defined with:
public class Basket {
public ArrayList<Item> basket = new ArrayList<Item>();
public Scanner kb = new Scanner(System.in);
public Item item = new Item();
public Program prog = new Program();
public void addItemToBasket() {
System.out.println("Enter the name of the item: ");
item.name = kb.nextLine();
System.out.println("Enter the price of the item: ");
item.price = kb.nextDouble();
basket.add(item);
item.name = null;
item.price = 0;
Program.menu();
}
public void removeItemFromBasket() {
System.out.println("Which item would you like to remove? ");
item.name = kb.nextLine();
if(basket.contains(item.name) ) {
basket.remove(item);
} else {
System.out.printf("There is no %s currently in the basket! ", item.name);
}
}
public String displayBasket() {
return basket.toString();
}
}
At the moment, if I run the program, the menu is displayed in the console, and when I select '1', the addItemToBasket()
method is called, I enter the name at the first prompt & price at the second prompt, and the menu is then displayed again.
However, at this point, if I then select '1' again, to add another item to the basket, the first & second prompts are displayed together... i.e. instead of:
Enter the name of the item:
// Wait for user input
Enter the price of the item:
// Wait for user input
it shows:
Enter the name of the item:
Enter the price of the item:
and if I try to type anything other than numbers, the program crashes because it's expecting a double value...
What am I doing wrong? Why does the addItemToBasket()
method skip the item.name = kb.nextLine();
line the second & subsequent times it's called?