6
\$\begingroup\$

Any advice would be great. I watched a few videos over the last 3 weeks and made a program up; what do you think?

The program is a business ideal for a few computers in a shop, some computers with training and the rest of the computers without. (for gaming, etc.)

import java.util.Scanner;

public class n_com {

    public static void main (String args []){

        Scanner input = new Scanner (System.in);


    double training,p_h,computer_training,computers,open;

    double max,two,thre,four,five;  
    double two2,thre3,four4,five5,p;    




    System.out.print("please enter cost of training people up on the computer p/h £ ");
        training =input.nextDouble();

    System.out.print("please enter cost to use the pc p/h for general use £");
             p_h=input.nextDouble();

    System.out.print("please enter number of computer in the shop for training :");
             computer_training =input.nextInt();

    System.out.print("please enter number of computer in the shop for general use :");
            computers =input.nextInt();

    System.out.print("please enter hours open per week :");
             open=input.nextDouble();


    training = training * computer_training * open; //max income per week w/training
       p_h = p_h * computers * open;  //max income per week (gamers ect)

    max = training + p_h; 


            two  =0.2 * max;    // 20%  max total 
            thre =0.3 * max;    // 30%  max total 
            four =0.4 * max;    
            five =0.5 * max;    


            p = 0.45;  // % of shop taking paid per week as wages 


            two2  = p * two;  // 45% of the estimated shop taking (two = 0.20% of max) 
            thre3 = p * thre;
            four4 = p * four;
            five5 = p * five;

            max = (max * 52 / 12);
            System.out.println();   

       System.out.println ("maxim monthly income from the computers £ "+ max);

            System.out.println();

       System.out.println ("total income estimated over the next 2 years");

       System.out.println();

       System.out.println("estimated monthly income at mouth 1-3 \t:"+ two );
       System.out.println("estimated monthly income at mouth 3-6 \t:"+ thre);
       System.out.println("estimated monthly income at mouth 6-12\t:"+ four);
       System.out.println("estimated monthly income at year  1-2 \t:"+ five);

            System.out.println();

        System.out.println("wages will be 45% of total income at the end of each mouth" );  

            System.out.println();

       System.out.println("estimated monthly wages for mouth 1 - 3  £ "+ two2  );
       System.out.println("estimated monthly wages for mouth 4 - 6  £ "+ thre3 );
       System.out.println("estimated monthly wages for mouth 7 - 12 £ "+ four4 );
      System.out.println("estimated monthly wages after the first year £"+five5);
     }
 }  
\$\endgroup\$
0

3 Answers 3

4
\$\begingroup\$

Wording

First, the terminology you chose seems a bit off. I believe that you are writing this program from the point of view of an entrepreneur or investor in a business, looking to earn income by making computers available for training and general use. Instead of "cost", you should be talking about price.

There are also some spelling mistakes ("Maximum" → "maxim", "number of computers" → "number of computer", and "month" → "mouth").

Misuse of variables

This program is basically a textual spreadsheet that accomplishes this (inputs in italic, outputs in bold):

Spreadsheet equivalent of program

As such, there are many variables to track, and therefore proper naming is critical to keep track of all of those numbers. More importantly, you should not repurpose a variable midway through the program, as you have done here:

training = training * computer_training * open;

and

max = (max * 52 / 12);

Such reassignments prevent you from naming the variable appropriately, and from reasoning about the code. In fact, the resulting confusion has led to a bug. The assignment two = 0.2 * max is based on max as a weekly amount. You subsequently reassigned max = (max * 52 / 12) as a monthly amount. You then report two as a monthly amount:

System.out.println("estimated monthly income at mouth 1-3 \t:"+ two );

You would be better off with fewer variables, by eliminating variables two, etc.

Style

Nobody likes to read huge declaration blocks:

double training,p_h,computer_training,computers,open;

double max,two,thre,four,five;
double two2,thre3,four4,five5,p; 

Instead, declare the variables at the assignment point.

You can make use of import static to avoid writing System.out everywhere.

import java.util.Scanner;
import static java.lang.System.out;

public class BusinessEstimator {
    public static void main (String args []) {
        try (Scanner input = new Scanner(System.in)) {
            out.print("please enter price of training people up on the computer p/h: £");
            double trainingPrice = input.nextDouble();

            out.print("please enter price to use the pc p/h for general use: £");
            double generalPrice = input.nextDouble();

            out.print("please enter number of computers in the shop for training: ");
            double trainingComputers = input.nextInt();

            out.print("please enter number of computers in the shop for general use: ");
            double generalComputers = input.nextInt();

            out.print("please enter hours open per week: ");
            double hoursPerWeek = input.nextDouble();

            double trainingWeeklyIncome = trainingPrice * trainingComputers * hoursPerWeek;
            double generalWeeklyIncome = generalPrice * generalComputers * hoursPerWeek;
            double maxWeeklyIncome = trainingWeeklyIncome + generalWeeklyIncome;
            double maxMonthlyIncome = maxWeeklyIncome * 52 / 12;

            double WAGES_PCT = 0.45;  // % of shop taking paid as wages

            out.println();

            out.println ("maximum monthly income from the computers £ "+ maxMonthlyIncome);
            out.println();

            out.println ("total income estimated over the next 2 years");
            out.println();

            out.println("estimated monthly income at month 1-3 \t:"+ 0.2 * maxMonthlyIncome);
            out.println("estimated monthly income at month 3-6 \t:"+ 0.3 * maxMonthlyIncome);
            out.println("estimated monthly income at month 6-12\t:"+ 0.4 * maxMonthlyIncome);
            out.println("estimated monthly income at year  1-2 \t:"+ 0.5 * maxMonthlyIncome);
            out.println();

            out.println("wages will be 45% of total income at the end of each month" );
            out.println();

            out.println("estimated monthly wages for month 1 - 3  £ "+ 0.2 * WAGES_PCT * maxMonthlyIncome);
            out.println("estimated monthly wages for month 4 - 6  £ "+ 0.3 * WAGES_PCT * maxMonthlyIncome);
            out.println("estimated monthly wages for month 7 - 12 £ "+ 0.4 * WAGES_PCT * maxMonthlyIncome);
            out.println("estimated monthly wages after the first year £"+ 0.5 * WAGES_PCT * maxMonthlyIncome);
        }
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ thanks I will follow the advice and make my code more easy to read, just the help I was looking for \$\endgroup\$
    – gavin
    Jan 30, 2015 at 20:47
  • \$\begingroup\$ Please remember to upvote all answers you find useful, and optionally accept a best answer. \$\endgroup\$ Jan 30, 2015 at 20:54
3
\$\begingroup\$

I would follow the Java coding conventions, e.g. computer_training --> computerTraining, n_com --> NCom. Also, I would give more descriptive names to the variables, e.g. two --> twentyPercent, p --> payingPercentage.

\$\endgroup\$
2
\$\begingroup\$
  • Indentation: If you aren't within a nested section, all your code should be indented the same amount. Even if you were going to indent code that conceptually is grouped, you should be consistent. The assignment to training is indented once, all the other variables are indented twice.

  • Naming (beyond what was mentioned by Attilio):

    thre Don't truncate a variable name just to make the code align. Good descriptive names improve readability must more than code that lines up in a group. Trying to maintain aligned code becomes unmanageable as the program gets bigger.

    two2 This doesn't tell the reader anything. It might imply that it is related to two, but it doesn't indicate how they are different.

  • Create your variables when you need them. Declaring them at the start means that the type information is farther away in the code from where the variable is used. That make the reader's job harder.

  • Be consistent with when you uses spaces around operators. It is preferable to always have a space on both sides. Your code has all of the possible combinations.

  • Extract magic numbers from your code and replace them with named constants.

  • Be careful about number literals and mathematical operations. Integer division is different than double division.

    max = (max * 52 / 12);
    

    Is a different result than

    max = (52 / 12 * max);
    
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.