4 Replies Latest reply: Apr 18, 2007 9:19 AM by 807599 RSS

    urgent array help needed! come get some duke points

    807599
      this program keeps track of rentals for a shop. Other methods and classes not shown (like Customer, Transaction classes) are also in this program. Every item (created with a constructor in Item class) can hold an array of up to 20 transactions; part of a Transaction object is a rental total, based on a number of rental days * specific rental price for that item rented (backhoe, edger, etc.) If the user presses "t" for total amount of transactions, the following method is called, which calls the getTotalPrice() in another class called Item. My problem here is that even if I enter in transaction amounts, when I press "t", the program just quits, not producing the report and returning me to the main class. I have been over it with the debugger and I am still lost. Below is some selected code; I hope it is sufficient. Please let me know if I need to put up more

      //main
      String input = JOptionPane.showInputDialog(null, "Enter a rental transaction (x)" +
                                                        "\nAdd a customer (c)" +
                                                        "\nAdd an item (i)" +
                                                        "\nReport on a specific item (r)" +
                                                        "\nReport on total dollar amounts of rentals for all items (t)" +
                                                        "\nReport on total rentals for a specific customer (s)" +
                                                        "\nStrike cancel to exit from the program");
                
                //big huge while
                while (input != null){
                     if (input.equals("x")){
                          rentalTrans();
                     }//if
                     if (input.equals("c")){
                          addCustomer();
                     }//if
                     if (input.equals("i")){
                          addItem();
                     }//if
                     if (input.equals("r")){
                          specificItemReport();
                     }//if
                     if (input.equals("t")){
                          allItemReport();
                     }//if
                     if (input.equals("s")){
                          customerReport();
                     }//if
                     
                     input = JOptionPane.showInputDialog(null, "Enter a rental transaction (x)" +
                               "\nAdd a customer (c)" +
                               "\nAdd an item (i)" +
                               "\nReport on a specific item (r)" +
                               "\nReport on total dollar amounts of rentals for all items (t)" +
                               "\nReport on total rentals for a specific customer (s)" +
                               "\nStrike cancel to exit from the program");
      
      //allItemReport()
      public static void allItemReport(){ //menu item t
                Item temp = null;
                for (int index = 0; index < items.length; index++){
                     temp = items[index];
                }//for
                JOptionPane.showMessageDialog(null, "Total rental transactions to date amount to: $" + temp.getTotalPrice());
      
      //Item Class
      public String getTotalPrice() {
                double total = 0;
                String output = "";
                for (int i = 0; i < trans.length; i++) {
                     
                     if (trans[i] == null) {
                          if (i == 0) {
                               output = "There are currently no transactions.";
                          }// if
                          break;
                     }// if
                     else{
                     total += getPerDayRentalPrice();
                     }//else
                }// for
                output+= total;
      
                return output;
           }//getTotalPrice
        • 1. Re: urgent array help needed! come get some duke points
          800282
          Don't flag your questions as urgent. It's rude. Also don't think that waving with a couple of worthless Dukes is going to get you better/quicker help.
          The reason I respond to your question is because you have explained your problem well and used code tags.
          Try this:
          class Main {
              
              public static void main (String[] args) {
                  
                  String message = "Enter a rental transaction (x)\nAdd a customer (c)" +
                      "\nAdd an item (i)\nReport on a specific item (r)\nReport on total "+
                      "dollar amounts of rentals for all items (t)\nReport on total rentals"+
                      " for a specific customer (s)\nStrike cancel to exit from the program";
                  
                  String input = JOptionPane.showInputDialog(null, message);
          
                  while(input != null){
                      if (input.equals("x")) rentalTrans();
                      else if (input.equals("c")) addCustomer();
                      else if (input.equals("i")) addItem();
                      else if (input.equals("r")) specificItemReport();
                      else if (input.equals("t")) allItemReport();
                      else if (input.equals("s")) customerReport();
                      else System.out.println("Invalid option!");
                      input = JOptionPane.showInputDialog(null, message);
                  }
                  System.out.println("Bye!");
              }
          
              // the rest of your methods ...
          }
          • 2. Re: urgent array help needed! come get some duke points
            807599
            Don't flag your questions as urgent. It's rude. Also
            don't think that waving with a couple of worthless
            Dukes is going to get you better/quicker help.
            The reason I respond to your question is because you
            have explained your problem well and used code tags.
            you know, I had the same thought come to me, so I just barely posted the same things again, under a different title with no points. Yeah, I realized, too, that my post was a little immature. I wanted to change the title after I submitted it, but that wasn't possible. TO ALL, I apologize. Well, thanks for your reply, I'll see what I can get figured out
            • 3. Re: urgent array help needed! come get some duke points
              807599
              when you say "just quits", what actually happens? there's no way it "just quits", it must give you some error output. what is it? also, that method won't do what you expect it to do, since you just assign a different value to 'temp' on each iteration of the loop
              • 4. Re: urgent array help needed! come get some duke points
                807599
                There are a couple/few things I see:

                1. This is not accumulating anything. It just keeps resetting temp to the current index
                for (int index = 0; index < items.length; index++){
                               temp = items[index];
                2. No ending brace for the while loop (just not shown?)

                3. While loop construct pblm
                while (input != null){
                Logically it will continue to loop as long as something is entered. I don't think that's what you want.