Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: help cleaning up code

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    13
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default help cleaning up code

    whats going on everyone?

    Can anyone provide tips or suggestions for cleaning up this code?
    the code functions properly, i'm just seeking tips to improve its readability or general tips.

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package chap4.pkg15.phoneswitchexamp;
     
    /**
     *Write a program that calculates and prints the bill for a cellular telephone 
     * company. The company offers two types of service: regular and premium.
     * Rates vary based on the type of service and are computed as follows:
     * 
     * Regular service: $10.00 plus first 50 minutes are free. 
     * Charges for over 50 minutes are $.20 per minute.
     * 
     * Premium service $25.00 plus:
     *  a. For calls made from 6:00 am to 6:00 p.m, the first 75 minutes are free; 
     *  charges for over 75 minutes are $.10 per minute.
     *  b. For Calls made between 6:00 p.m and 6:00 am, the first 100 minutes are
     *  free; charges for over 100 minutes are $.05 per minute.
     * 
     * Program should prompt the user to enter an account number, a service code 
     * (type char - r or R for regular, p or P for premium), and the number of
     * minutes the service was used. The program should use user created methods
     * to calculate regular service and premium service. Output should be account
     * number, type of service, and amount due from user.
     */
     
    import javax.swing.JOptionPane;
     
    public class Chap415PhoneSwitchexamp {
    public static int accountNum, dayMinutes, nightMinutes, totalMinutes,
            acctCall;
    public static char serviceCode, serviceCall;
    public static double amountDue;
    public static String outputStr, acctStr, serviceStr;
    public static void main(String[] args) {
            getAccountNum(accountNum);
            getServiceCode(serviceCode);
            serviceSwitch(serviceCode);
            System.exit(0);
        }
     public static double calculateRegBill(int a)
        {
                   amountDue = 10;
     
                   if (a > 50)
                       amountDue = 10 + (a - 50)*.2;
     
                   return amountDue;
          }
     public static double calculatePremBill(int a, int b)
     {
                   double dayCost = 0, nightCost =0;
     
                   if (a > 75)
                       dayCost = (a - 75)*.1;
                   if (b> 100)
                       nightCost = (b -100)*.05;
     
                   amountDue =  25 + (nightCost +dayCost);
                   return amountDue;
     }
     public static int getAccountNum(int a)
     {
         a =  Integer.
                   parseInt( JOptionPane.showInputDialog("Enter account number"));
         accountNum = a;
         return accountNum;
     }
     public static char getServiceCode(char b)
     {
        b = JOptionPane.showInputDialog("Enter service code").charAt(0);
     
     
     serviceCode = b;
     
         return serviceCode;
     
     }
     public static void serviceSwitch(char a)
     {
         switch (a)
           {
               case 'r':
               case 'R':
     
                   amountDue = calculateRegBill(Integer.parseInt(
                           JOptionPane.showInputDialog("Enter amount of "
                           + " minutes used in billing period")));
     
                   outputStr =  "Account Num: " +accountNum +"\n Account Type"
                           + ": Regular"
                           + "\nAmount Due: $" +String.format("%.2f",amountDue);
     
                   JOptionPane.showMessageDialog(null,outputStr,"Account"
                           + " Information",JOptionPane.INFORMATION_MESSAGE); 
                   break;
     
               case 'p':
               case 'P':
     
                   dayMinutes = Integer.parseInt(JOptionPane.showInputDialog("Enter amount"
                           + "of minutes used between 6:00 a.m. to 6:00 p.m."));
     
                   nightMinutes = Integer.parseInt(JOptionPane.showInputDialog("Enter amount"
                           + "of minutes used between 6:00 p.m. to 6:00 a.m."));
     
                   amountDue = calculatePremBill(dayMinutes,nightMinutes);
     
                   outputStr = "Account Num: " +accountNum +"\n Account Type"
                           + ": Premium"
                           + "\nAmount Due: $" +String.format("%.2f",amountDue);
     
                   JOptionPane.showMessageDialog(null,outputStr,"Account"
                           + " Information",JOptionPane.INFORMATION_MESSAGE);
                   break;
     
               default:
     
                   JOptionPane.showMessageDialog(null,"Invalid Account type",
                           "Account Informatino", JOptionPane.INFORMATION_MESSAGE);
    }}}


  2. #2
    Junior Member
    Join Date
    Jul 2013
    Posts
    13
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: help cleaning up code

    i guess its clean enough..

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: help cleaning up code

    1. Use comments. Especially Javadoc comments on public/protected functions.

    2. Use proper indentation. This means each block should be indented with a "tab" or "4 spaces", which-ever you prefer. Also, keep your indentation consistent. Don't suddenly decrease/increase the indentation of a line of code. This could be a copy-paste issue, but really makes code hard to read if it isn't done right.

    3. I see a lot of static fields and static methods. This screams of non-OO design patterns, which is doable in Java, but really isn't recommended. I don't know if your assignment restricts you to using this design, if not you might want to consider re-writing it in an Object-Oriented fashion.

    4. You really shouldn't call System.exit(0);. There are rare exceptions, but usually this just hides other issues/bugs. Your program should be allowed to terminate normally.

    5. Pick descriptive (but concise) variable names. A variable named a doesn't mean anything. A variable named value is better, and variables named amountDue or dayMinutes are even better.

  4. The Following User Says Thank You to helloworld922 For This Useful Post:

    GStateSwish (July 13th, 2013)

  5. #4
    Junior Member
    Join Date
    Jul 2013
    Posts
    13
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: help cleaning up code

    1. fair enough on the addition of comments.

    2. copy-paste issue.

    3. I'm about a month or so into seriously learning java. I know what ood is but have no idea of how to write in ood just yet.

    4. I was curious of when to use System.exit(0); and its necessity. thanks for the tip, any clarification possible on when it would be wise to include it?

    5. I'm assuming you are talking about variable names in formal parameters of my methods rather than the actual paramater variable names?
    again tips considered and thanked for input
    Last edited by GStateSwish; July 13th, 2013 at 06:18 PM. Reason: removed weird duplicate of text

  6. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: help cleaning up code

    I was curious of when to use System.exit(0); and its necessity
    Using System.exit() is usually done to cover up problems where you program isn't running (or ending) correctly. It's not a great solution, but in real life time is money so sometimes you've got to pick a crummy solution which "works" because the "ideal" solution would take too long to implement properly. There are rare cases where it does make sense that near-immediate program termination is required. Personally I find many of these cases to also be somewhat dubious and a different solution usually can be found. Just avoid it whenever possible, and only use it if you can't find another solution.

    I'm assuming you are talking about variable names in formal parameters of my methods rather than the actual parameter variable names?
    Kind of, of course formal parameters should make sense in the context of the method. Actual parameters are a bit trickier because they don't have a name per-say (they're a value, not a variable). However, all names should make sense, whether they are class names, paremeters/variables, methods, etc. The idea is can you give your code to someone else (even if that's you in two years) and have them understand what that code is suppose to do without having to stare at it.

    String x = "hello"; // probably a bad name
    System.out.println(x); // passing x as an actual parameter, there's nothing wrong with the actual parameter because it doesn't have a name
    // However, x is still a bad variable name as noted above.
     
    String greeting = "hello"; // a good variable name
    System.out.println(greeting + " is " + x); // same logic as above, the actual parameter has no name (it's a temporary), greeting is a well named variable and x is not.
     
    public static void doit(String x) // bad formal parameter x, and bad method name doit, too.
     
    public static double distance(double x1, double y1, double x2, double y2) // here x's/y's makes sense, so this is likely fine.
     
    public class A // bad class name
    {}
     
    public class Point // good class name
    {}

Similar Threads

  1. Replies: 3
    Last Post: April 27th, 2013, 07:19 AM
  2. Replies: 7
    Last Post: January 24th, 2013, 10:41 AM
  3. Replies: 5
    Last Post: November 14th, 2012, 10:47 AM
  4. Help cleaning up my code.
    By Harrald in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 14th, 2012, 01:10 AM