So I have an addItem() method where I can add a book, tape, or cd object to my inventory. To store the book, tape, or cd object, I obtain the title, author, and price of the object from the user. HOWEVER, when I run the program, it skips the "please enter the title: " and goes right to "please enter the author:". Then when I have it show the information, the title field is 'null'.
static void addItem(int type) {
String title;
String author;
Double price;
System.out.println("Please enter the title");
title = keyboard.nextLine()
System.out.println("Please enter the author");
author = keyboard.nextLine();
System.out.println("Please enter the price");
price = keyboard.nextDouble();
if(type == 0){
Book BookStoreItem;
BookStoreItem = new Book(title, author, price);
System.out.println("You've entered:");
System.out.println("Title: " + BookStoreItem.getTitle());
System.out.println("Author: " + BookStoreItem.getAuthor());
System.out.println("Price: " + BookStoreItem.getPrice());
}
if(type == 1){
Tape BookStoreItem;
BookStoreItem = new Tape(title, author, price);
System.out.println("You've entered:");
System.out.println("Title: " + BookStoreItem.getTitle());
System.out.println("Author: " + BookStoreItem.getAuthor());
System.out.println("Price: " + BookStoreItem.getPrice());
}
if(type == 2){
CD BookStoreItem;
BookStoreItem = new CD(title, author, price);
System.out.println("You've entered:");
System.out.println("Title: " + BookStoreItem.getTitle());
System.out.println("Author: " + BookStoreItem.getAuthor());
System.out.println("Price: " + BookStoreItem.getPrice());
}
}//end of addItem()
Here is a sample output:
Press:
0 To add a book to the inventory
1 To add a tape to the inventory
2 To add a Cd to the inventory
Please enter your choice: 0
Please enter the title:
Please enter the author: John Smith
Please enter the price: 55.50
Title: null
Author: John Smith
Price: 55.5
Here's an update with the menu on it:
static void printMenu() {
System.out.println("\nPress:");
System.out.println("\t" + ADD_BOOK + "\tTo add a book to the inventory\n");
System.out.println("\t" + ADD_TAPE + "\tTo add a tape to the inventory\n");
System.out.println("\t" + ADD_CD + "\tTo add a CD to the inventory\n");
System.out.println("\t" + SHOW_ITEM_LIST + "\tTo display a list of available books\n");
System.out.println("\t" + SEARCH_ITEM + "\tTo search for a specific book\n");
System.out.println("\t" + QUIT + "\tTo exit\n");
}
and with the choices:
static void runBookStoreApplication() {
int userChoice = QUIT;
String quitMessage = "\nThank you for using the BookStoreApplication!";
String invalidOptionMessage = "\nInvalid option!";
do{
printMenu();
userChoice = getUserChoice();
switch (userChoice) {
case ADD_BOOK:
addItem(0);
break;
case ADD_TAPE:
addItem(1);
case ADD_CD:
addItem(2);
case SHOW_ITEM_LIST:
showItemList();
break;
case SEARCH_ITEM:
searchItem();
break;
case QUIT:
System.out.println(quitMessage);
break;
default:
System.out.println(invalidOptionMessage);
}
} while(userChoice != QUIT);
}//end of runBookStoreApplication
with getUserChoice:
static int getUserChoice() {
int choice;
System.out.print("Please enter your choice: ");
choice = keyboard.nextInt();
return choice;
}