/* * DecisionsLoopLab.java * */ //v3 package lab02; import javax.swing.*; public class DecisionsLoopLab { /** * @param args the command line arguments */ public static void main(String[] args) { String categoryStr; //Creates Category list; string type String title; // Value entered by user; string type String costStr; // Value entered by user; string type double cost; // The double form of the user-entered DVD cost int resp = 0; // The user's response as to whether to continue int dvdCounter = 0; //The number of DVDs the user has entered int category = 0; //The user's number choice for the category double total; //The double form of the total of costs // Initialize the output string to the empty string String outputStr = ""; // Loop to input information on the user's DVD collection while (true) { // Read in DVD title from user as a string title = JOptionPane.showInputDialog("Enter DVD title"); //Read in category from user as a number categoryStr = JOptionPane.showInputDialog("Choose category by number \n1 Comedy " +"\n 2 Drama \n 3 Action \n 4 Science Fiction " +"\n 5 Animated"); //Convert from type String to type int category = Integer.parseInt(categoryStr); //Switch for the category switch (category) { case 1: categoryStr = ("Comedy"); break; case 2: categoryStr = ("Drama"); break; case 3: categoryStr = ("Action"); break; case 4: categoryStr = ("Science Fiction"); break; case 5: categoryStr = ("Animated"); break; default: categoryStr = ("Invalid Category.");break; } // Read in cost from user as a string costStr = JOptionPane.showInputDialog("Enter DVD cost"); // Convert from type String to type double cost = Double.parseDouble(costStr); //Keeps the count of DVDs dvdCounter = dvdCounter + 1; //Finding the total of the costs total = cost + total; // Add the user-entered values to the output string outputStr = outputStr + "DVD" + dvdCounter + ", Cat:" + categoryStr + ", Title: " + title + ", "; outputStr = outputStr + "Cost: " + cost + " Total:" + total + "\n"; // Get the user's choice as to whether to continue the loop resp = JOptionPane.showConfirmDialog(null, outputStr + "\nContinue?", "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); // Test whether the user's choice is to continue the loop or not if (resp == JOptionPane.NO_OPTION) break; } // Display final output to the user JOptionPane.showMessageDialog( null, outputStr, "Account Total", JOptionPane.INFORMATION_MESSAGE); // System.exit( 0 ); // Terminate the program } }