I have written a code for data management and parallel arrays but it has to be broken into methods/subroutines. Whenever I try to do so, some aspect of my code stops working. Would it be possible for anyone to roughly outline how and what I can break into subroutines without it causing problems in my code?
Scanner keyedInput = new Scanner(System.in); // input
// variables
String userInput;
int numberOfBooks = 0; // setting to 0
int userChoice = 0; // setting to 0
final double taxAmount = 1.13;
boolean valid = false;
double total = 0; // setting to 0
double taxedTotal;
// while loop for number of books
while (valid == false)
{
// user enters info
System.out.print("Please enter the number of books you have purchased: ");
userInput = keyedInput.nextLine();
System.out.println();
// try catch statements for invalid input/exceptions
try
{
numberOfBooks = Integer.parseInt(userInput); // converting to int
valid = true; // setting to true so loop won't repeat
} // end of try
// outputting invalid input message
catch (NumberFormatException e)
{
System.out.println("Your input is not valid");
System.out.println();
} // end of catch
} // end of valid == false while loop
// arrays to hold info user will input
String bookName [ ] = new String [numberOfBooks];
double price [ ] = new double [numberOfBooks];
// user enters all data required
System.out.println("* * * * DATA ENTRY * * * *");
// for loop that prompts for and stores name of books purchases
for (int i = 0; i < numberOfBooks; i = i + 1)
{
System.out.println();
System.out.print("Please enter the name of book " + (i + 1) + ": ");
bookName[i] = keyedInput.nextLine(); // stores the name of each book in the array
// set valid to false again for loop below
valid = false;
// while loop for price of books
while (valid == false)
{
System.out.println();
System.out.print("Please enter the price of '" + (bookName[i] + "': "));
userInput = keyedInput.nextLine();
// try catch statements for invalid input/exceptions
try
{
price[i] = Double.parseDouble(userInput); // converting to double
valid = true; // setting to true so loop won't repeat
} // end of try
// outputting invalid input message
catch (NumberFormatException e)
{
System.out.println("Your input is not valid");
} // end of catch
} // end of valid == false while loop
} // end of for loop
// while loop to output data
while (userChoice != 3)
{
// set valid to false for loop below
valid = false;
while (valid == false)
{
// outputting choices menu
System.out.println();
System.out.println("* * * *\n" + "1. Output original data \n" + "2. Output calculated data \n" + "3. Exit \n" + "* * * *\n");
// user enters their choice
System.out.print("Please enter the number in front of your choice: ");
userInput = keyedInput.nextLine();
System.out.println();
// try catch statements for invalid input/exceptions
try
{
userChoice = Integer.parseInt(userInput); // converting to int
valid = true; // setting to true so loop won't repeat
} // end of try
// outputting invalid input message
catch (NumberFormatException e)
{
System.out.println("Your input is not valid");
System.out.println();
} // end of catch
} // end of valid == false while loop
// switch statements for each option
switch (userChoice)
{
// option 1
case 1:
{
// for loop to output existing data
for (int i = 0; i < numberOfBooks; i = i + 1)
{
System.out.println("'" + bookName[i] + "'" + " cost: " + price[i]);
} // end of for loop
break;
} // end of case 1
// option 2
case 2:
{
// calculations for total + taxed total
for (double value : price)
{
total = total + value;
total = Math.round(total * 100.0) / 100.0; // rounding
//totalAmount = totalAmount + total;
} // end of for loop
taxedTotal = (total * taxAmount); // calculating total with taxes
taxedTotal = Math.round(taxedTotal * 100.0) / 100.0; // rounding
System.out.println("Total: $" + total);
System.out.println("Taxed total: $" + taxedTotal);
break;
} // end of case 2
// option 3
case 3:
{
System.out.println("Goodbye!");
} // end of case 3
} // end of switch statements
} // end of userChoice while loop