Note that there are some explanatory texts on larger screens.

plurals
  1. POLoops in an assignment for beginner JAVA
    primarykey
    data
    text
    <p>I have an assignment for a beginner's JAVA class and I cannot seem to be able to resolve it. We need to create a program that calculates monthly commission based on transactions. The transaction amount is defined using a math.random().</p> <p>The month starts in January. We use JOPtionPane.showconfirmdialog to ask if there is a customer. If yes, there is another confirmdialog asking if the client wishes to buy the item. If the client accepts to buy the item, we calculate commission.</p> <p>If there are no customers or if we reach 15K in commission in a month, we skip to the following month and it repeats.</p> <p>Finally, once we reach 100K in commission before the year ends, the program ends, telling the seller to go on vacation for the rest of the year. If only this was true in real life...</p> <p>However, my problem is I am unable for some reason to program a loop that will exit the program (and display a message) if it happens that I do not reach 100K after December. I keep on getting </p> <p>Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 12 at Commission.main(Commission.java:42)</p> <pre><code>public static void main(String[] args) { String[] months = { "January", "February", "March", "April", "May", // Initialize array for months of the year "June", "July", "August", "September", "October", "November", "December" }; String[] diamonds = { "diamond", "ruby", "sapphire", "emerald", "topaz", "zircon" }; // Initialize array for different precious gems types double[] monthlyCommission; // Initialize double variable for total commission for the month monthlyCommission = new double[12]; // Initialize array with 12 values for monthly commission double yearTotal; // Initialize variable for total commission in the year double transaction; // Initialize value for a sale transaction double commission; // Initialize value for commission based on value of sale transaction int month = 0; int diamond; yearTotal = 0; JOptionPane.showMessageDialog(null, "Welcome to X's Jewelry Store!"); // Display welcome message dialog for (; monthlyCommission[month] &lt; 15000;) { int storeCustomer = JOptionPane.showConfirmDialog(null, "Is there a customer in the store?", months[month] + " month", JOptionPane.YES_NO_OPTION); while (storeCustomer == JOptionPane.NO_OPTION) { // Statement to process if there is no customer { month += 1; storeCustomer = JOptionPane.showConfirmDialog(null, "Is there a customer in the store?", months[month] + " month", JOptionPane.YES_NO_OPTION); } } if (storeCustomer == JOptionPane.YES_OPTION) { // Statement to process if there is a customer diamond = (int) (Math.random() * 6); // Choose randomly a value between 0-5 transaction = Math.random() * 50000.0; transaction = Math.round(transaction * 100) / 100; int buyItem = JOptionPane.showConfirmDialog(null, "Do you wish to buy this " + diamonds[diamond] + " for " + String.format("$%4.2f", transaction) + "?"); if (buyItem == JOptionPane.NO_OPTION) // Statement to process if user does not want the item JOptionPane.showMessageDialog(null, "No problem. See you next time."); if (buyItem == JOptionPane.YES_OPTION) { // Statement to process if user wishes to buy the item if (transaction &lt;= 10000) commission = transaction * 0.1; else if (transaction &gt; 30000.0) commission = 10000 * 0.10 + 20000 * 0.15 + (transaction - 30000) * 0.20; else commission = 10000 * 0.10 + (transaction - 10000) * 0.15; commission = Math.round(commission * 100) / 100; monthlyCommission[month] += commission; yearTotal += commission; JOptionPane.showMessageDialog(null, String.format( "Your commission for this transaction is $%4.2f", commission)); System.out.println(yearTotal); // Displays the commission total for the transaction if (yearTotal &gt; 100000) // Exit loop if total commission for the year is greater than 100000 break; } } if (monthlyCommission[month] &gt;= 15000) { JOptionPane.showMessageDialog(null, "You have earned $" + String.format("%4.2f", monthlyCommission[month]) + ". You can rest the remainder of the month!"); // Display dialog once the monthly commission reaches 15000 month += 1; } } JOptionPane.showMessageDialog(null, "Congratulations!! You have earned a total of $" + String.format("%4.2f", yearTotal) + ". Enjoy your vacation in Honolulu!"); // Display dialog once the yearly commission reaches 100000 } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload