PDA

View Full Version : Elements 4th Java Assignment!



Element17
09-25-2013, 07:01 PM
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?

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);
}
}

}
}

rj
09-25-2013, 07:13 PM
Welll there are a couple things you can do

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


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"

if (input.equalsIgnoreCase("A"))


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

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)

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 :D but it's good practice)

Element17
09-25-2013, 07:39 PM
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!

rj
09-25-2013, 07:53 PM
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:

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