6 Replies - 342 Views - Last Post: 19 June 2013 - 11:29 AM Rate Topic: -----

#1 SniffiestFiddle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-June 13

Char inputs via scanner method

Posted 19 June 2013 - 09:31 AM

I am brand new to Java and for class I have been asked to create a program for an ISP. The ISP has 3 packages for pricing (A,B,C). I am supposed to get the users package letter and then ask the user for the amount of hours used. I want to be able to have the user enter either A, B, or C. Based on that I have calculations to handle the amounts. Should I grab the package letter as a Char? If so, then how do I use it in the if else statements?


package main;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        
        char aChar;
        int hours;
        double overageHours;
        double overageFee;
        double overageTotal;
        double baseFee;
        double totalBill;
        
        Scanner input = new Scanner(System.in);
        
        //Get the package letter
        System.out.println("Enter your package letter (a,b,c):");
        String aString = input.next();
        aChar = aString.charAt(0);
        
        //Get the amount of hours used
        System.out.println("Enter amount of hours used: ");
        hours = input.nextInt();
        
        //Calculate the bill based on package and hours used
        if (aChar = 'a')
            {
        
                if (hours > 10)
                    {
                        baseFee = 9.95;
                        overageHours = (hours - 10);
                        overageFee = 2;
                        overageTotal = (overageHours * overageFee);
                        totalBill = (baseFee + overageTotal);
                        System.out.println("Your total is $" + totalBill);
                    }
                else
                    {
                        baseFee = 9.95;
                        System.out.println("Your total is $" + baseFee);
                    }
            }
        
        if (aChar = 'b')
            {
        
                if (hours > 10)
                    {
                        baseFee = 13.95;
                        overageHours = (hours - 10);
                        overageFee = 1;
                        overageTotal = (overageHours * overageFee);
                        totalBill = (baseFee + overageTotal);
                        System.out.println("Your total is $" + totalBill);
                    }
                else
                    {
                        baseFee = 13.95;
                        System.out.println("Your total is $" + baseFee);
                    }
            }
        
        if (aChar = 'c')
            {
                baseFee = 19.95;
                System.out.println("Your total is $" + baseFee);
            }
    }
}



I am not looking for the answer. I am hoping someone can point me in the right direction on how to handle the Char input.

Thank you!

Is This A Good Question/Topic? 0
  • +

Replies To: Char inputs via scanner method

#2 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 417
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Char inputs via scanner method

Posted 19 June 2013 - 09:38 AM

= is the assignment operator that changes the value of a variable. Use == to determine equality.
Was This Post Helpful? 1
  • +
  • -

#3 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7961
  • View blog
  • Posts: 13,580
  • Joined: 19-March 11

Re: Char inputs via scanner method

Posted 19 June 2013 - 09:39 AM

Your current handling looks fine to me. You might want to do a bit of error handling - what if they enter "d"? - and you could use the trim method in case the user puts a space before the character, I guess. Otherwise, this is a perfectly reasonable way to get a char from the user.

I could say a few words about what you do with that char... they would include the phrase "eliminate repetition"... but you seem happy with that part, so I'll leave it be.

EDIT: good eye, @flukeshot, I missed the inadvertent assignment

This post has been edited by jon.kiparsky: 19 June 2013 - 09:40 AM

Was This Post Helpful? 0
  • +
  • -

#4 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 417
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Char inputs via scanner method

Posted 19 June 2013 - 09:50 AM

They put that kind of stuff in the OCJP exam too, the evil sods. Never let your guard down! :P
Was This Post Helpful? 0
  • +
  • -

#5 SniffiestFiddle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-June 13

Re: Char inputs via scanner method

Posted 19 June 2013 - 10:41 AM

View PostFlukeshot, on 19 June 2013 - 09:38 AM, said:

= is the assignment operator that changes the value of a variable. Use == to determine equality.


Thank you! I made that simple change and now it works just how I need it to.

View Postjon.kiparsky, on 19 June 2013 - 09:39 AM, said:

I could say a few words about what you do with that char... they would include the phrase "eliminate repetition"... but you seem happy with that part, so I'll leave it be.


Could you please elaborate?
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7961
  • View blog
  • Posts: 13,580
  • Joined: 19-March 11

Re: Char inputs via scanner method

Posted 19 June 2013 - 11:11 AM

If you look at the three branches of your if, they all do exactly the same thing, with just a few constants changed. What if you just set those constants and use the same code each time?

This requires that you set up your fee schedule in a collection ahead of time. A Map is probably the right structure - HashMap is the implementation you'd like to use. Look it up and study the API if you're not familiar with it.
Once you have that, it's a piece of cake:

                       overageFee = overageFees.get(aChar);
                       baseFee = baseFees.get(aChar); 
                if (hours > baseHours)
                    {
                        overageHours = (hours - baseHours);
                        overageTotal = (overageHours * overageFee);
                        totalBill = (baseFee + overageTotal);
                        System.out.println("Your total is $" + totalBill);
                    }
                else
                    {
                        System.out.println("Your total is $" + baseFee);
                    }
   




This can be simplified further:
                        overageFee = overageFees.get(aChar);
                        baseFee = baseFees.get(aChar); 
                        overageHours = (hours - baseHours);
                        overageHours = Math.max(overageHours, 0);  // don't allow negative overage hours
                        overageTotal = (overageHours * overageFee);  
                        totalBill = (baseFee + overageTotal);
                        System.out.println("Your total is $" + totalBill);
                   



This seems to me a lot simpler to understand, to maintain, and to change as the business model changes. You can add a new service charge, and you add it once, in one place (not in multiple branches of the code). You can add a new fee schedule by adding a few items to your pricing maps. You can have a Saturday sale, and you don't have to test new code to do it - so you, the back end guy, can still have your Saturday, and you're not cleaning it up on Sunday.

This is a popular slogan in programming now, it's called DRY, "Don't Repeat Yourself". It's a good idea.
Was This Post Helpful? 1
  • +
  • -

#7 SniffiestFiddle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-June 13

Re: Char inputs via scanner method

Posted 19 June 2013 - 11:29 AM

Thank you so much. That makes perfect sense. I will read up on the API.

Thanks again!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1