Results 1 to 4 of 4

Thread: Elements 4th Java Assignment!

  1. #1
    Join Date
    Jun 2007
    Posts
    532
    Mentioned
    1 Post(s)
    Quoted
    68 Post(s)

    Default Elements 4th Java Assignment!

    An Internet Service provider has three different subscription packages for its customers:
    Package A: For $9.95 per month 10 hours of access are provided. Additional hours are $2.00 per hour.

    Package B: For $13.95 per month 20 hours of access are provided. Additional hours are $1.00 per hour.

    Package C: For $19.95 per unlimited access is provided.

    Write a program that calculates a customer’s monthly bill. It should ask the user via a dialog box to enter the letter of the package the customer has purchases (A, B, C) and the number of hours that were used. It should then display the total charge in a dialog box.


    How do I make it so I can use A, B or C instead of 1, 2, 3?

    java Code:
    import javax.swing.JOptionPane;

    /*Andrew Varnick
      9/23/2013
      CSCI 1015
      Programming Assignment 4
      A program to calculate a monthly bill
    */


    public class VarnickPass4
    {
    //Constants
    static final double PackA = 9.95;          
    static final double PackB = 13.95;
    static final double PackC = 19.95;



       public static void main(String[] args)
       {
       String input;
       String numberOfHoursUsed;
       int numberOfHours;
       int packageInput;
       double finalPrice;
       double PackA = 9.95;          
       double PackB = 13.95;
       double PackC = 19.95;
     
       
       input = JOptionPane.showInputDialog("Which package do you have?(A = 1, B = 2 or C= 3)");
       packageInput = Integer.parseInt(input);
       
       if (packageInput == 1)
       {
         numberOfHoursUsed = JOptionPane.showInputDialog("How many hours have you used this month?");
         numberOfHours = Integer.parseInt(numberOfHoursUsed);
         finalPrice = ((numberOfHours - 10) * 2 + PackA);
         JOptionPane.showMessageDialog(null, "The final price is $" + finalPrice);
       }
       else
       {
       if (packageInput == 2)
       {
         numberOfHoursUsed = JOptionPane.showInputDialog("How many hours have you used this month?");
         numberOfHours = Integer.parseInt(numberOfHoursUsed);
         finalPrice = ((numberOfHours - 20) * 1 + PackB);
         JOptionPane.showMessageDialog(null, "The final price is $" + finalPrice);
         
       }
       else
       {
       if (packageInput == 3)
         numberOfHoursUsed = JOptionPane.showInputDialog("You have unlimated access!");
         finalPrice = (PackC);
         JOptionPane.showMessageDialog(null, "The final price is $" + finalPrice);
       }  
       }

    }
    }
    Finished B.S. Program in Radiology!!

    Projects: A big one! Total secret! hehe

  2. #2
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    Welll there are a couple things you can do

    input is a string right? That means you can just do

    java Code:
    if (input == "A")
    {
    }

    Or you can make it ignore the case (because it could mess up if you check for "A" but the user puts "a"

    java Code:
    if (input.equalsIgnoreCase("A"))

    This means it will accept a or A. Your teacher or professor may like this better

    java Code:
    import javax.swing.JOptionPane;

    /*Andrew Varnick
      9/23/2013
      CSCI 1015
      Programming Assignment 4
      A program to calculate a monthly bill
    */


    public class VarnickPass4
    {
    //Constants
    static final double PackA = 9.95;          
    static final double PackB = 13.95;
    static final double PackC = 19.95;



       public static void main(String[] args)
       {
       String input;
       String numberOfHoursUsed;
       int numberOfHours;
       int packageInput;
       double finalPrice;
       double PackA = 9.95;          
       double PackB = 13.95;
       double PackC = 19.95;
     
       
       input = JOptionPane.showInputDialog("Which package do you have?(A = 1, B = 2 or C= 3)");
      // packageInput = Integer.parseInt(input);
       if (input.equalsIgnoreCase("A"))
       {
         numberOfHoursUsed = JOptionPane.showInputDialog("How many hours have you used this month?");
         numberOfHours = Integer.parseInt(numberOfHoursUsed);
         finalPrice = ((numberOfHours - 10) * 2 + PackA);
         JOptionPane.showMessageDialog(null, "The final price is $" + finalPrice);
       }
       else
       {
      if (input.equalsIgnoreCase("B"))
       {
         numberOfHoursUsed = JOptionPane.showInputDialog("How many hours have you used this month?");
         numberOfHours = Integer.parseInt(numberOfHoursUsed);
         finalPrice = ((numberOfHours - 20) * 1 + PackB);
         JOptionPane.showMessageDialog(null, "The final price is $" + finalPrice);
         
       }
       else
       {
       if (input.equalsIgnoreCase("C"))
         numberOfHoursUsed = JOptionPane.showInputDialog("You have unlimated access!");
         finalPrice = (PackC);
         JOptionPane.showMessageDialog(null, "The final price is $" + finalPrice);
       }  
       }

    }
    }

    Or you could use the switch statement (but I am unsure how to use or if you can use IgnoreCase with this)

    java Code:
    switch (input) {
            case "A": {
                // blah blah blah
                break;
            }
            case "B": {
                // blah blah blah
                break;
            }
            case "C": {
                // blah blah blah
                break;
            }
       }

    After each case you will want to break since the switch statement is a loop(someone correct me if I'm wrong) If you don't break it wouldn't mess up your program but it would keep trying the next cases if there were any (so if input == a it would do that statement and then check if input is b and then c unless you break.. so in your case if you don't break the program will be like .0000006 ms slower but it's good practice)

  3. #3
    Join Date
    Jun 2007
    Posts
    532
    Mentioned
    1 Post(s)
    Quoted
    68 Post(s)

    Default

    Ahhh sweet! Thank you! I had been trying all morning to figure it out but I could never get it to accept A. Thank you! I agree the case statement does look better, but we haven't "learned" about that yet so I don't want to use it. Because I know is PS it looks better to use case statements but thank you none the less!
    Finished B.S. Program in Radiology!!

    Projects: A big one! Total secret! hehe

  4. #4
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    Quote Originally Posted by Element17 View Post
    Ahhh sweet! Thank you! I had been trying all morning to figure it out but I could never get it to accept A. Thank you! I agree the case statement does look better, but we haven't "learned" about that yet so I don't want to use it. Because I know is PS it looks better to use case statements but thank you none the less!
    You could also check if the input was not 1 character long:

    java Code:
    if (input.length() != 1) {
           JOptionPane.showMessageDialog(null, "You enter a invalid number of charactors!");
           System.exit(0);
       }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •