1

Day two of my journey into the world of java and I have seem to run into a road block even after scouring this wonderfully helpful website.

So precontexual 411:

I want an array of an unknown length to create its entries by calling on classes. I have the strangest feeling I am not calling on them properly, but instead of pulling my hair out more so than I have done already today, I figure to ask the experts for a point in the right direction.

here is the main function that seems to have the issue(left out imported text for simplicity sake:

SalesPreInt.java

class SalesPreInt { 
    public static void main(String[] args) {
        Scanner userInput = new Scanner(System.in);
        System.out.println("How many employees are we looking at?");
        int noOfReps = userInput.nextInt();

        EmpSales[] employees = new EmpSales[noOfReps]; //error cannot be resolved ot a type

        for (int i = 0; i < noOfReps; i ++) {
            Name name = new Name(String); //error here cannot be resolved to a variable
            Calc totalComp = new Calc(totalComp); //error here constructor is undefined
            employees[i] = new EmpSales(name,totalComp); //error here    cannot be resolved to a type
    }
}

Here are the classes which I have saved as individual java files(for some reason I felt like this was a goof idea?):

Pay.java

public class Pay {
    public BigDecimal basePay() {
        int basePay = 50000;

        return new BigDecimal(String.valueOf(basePay));
    }
}

Calc.java

public class Calc {
    public BigDecimal totalComp() {
        Pay pay = new Pay();
        pay.basePay();

        BigDecimal intCalc = new BigDecimal("0.15");

        Scanner userInput = new Scanner(System.in);

        System.out.println("What were your total sales?");
        BigDecimal salesPre = userInput.nextBigDecimal();
        System.out.println("You're Total Sales were "+salesPre);

        userInput.close();

        BigDecimal postIntCalc = salesPre.multiply(intCalc);
        BigDecimal totalComp = postIntCalc.add(pay.basePay());

        System.out.println("Your total sales including commission is "+postIntCalc);
        System.out.println("Your total pay is"+totalComp);

        return new BigDecimal(String.valueOf(totalComp()));
    }
}

SalesPreInt.java

public class Name {
    public String name() {
        Scanner userInput = new Scanner(System.in);

        System.out.println("What is the Sales Rep Name?");
        String name = userInput.toString();

        userInput.close();

        return new String(String.valueOf(name));
    }
}

I would like to say thanks in advance, I truly appreciate you all for helping me get my "newbie" q&a out of the way so that I might eventually put some useful things together with java.

So after playing with it for a while, and renaming some of my class objects er your advice, I have this now. As you can see, I am still getting an error with the constructor and the variables. I thought it was created?

    package Week4;

import java.util.Scanner;
import java.math.BigDecimal;


class SalesPreInt
{   
    public static void main(String[] args)

{
Scanner userInput = new Scanner(System.in);
System.out.println("How many employees are we looking at?");
int noOfReps = userInput.nextInt();

userInput.close();

EmpSales[] employees = new EmpSales[noOfReps];


for(int i = 0; i< noOfReps; i ++){
    Name empName = new Name();//The method empName() is undefined for the type SalesPreInt
    Calc totalComp = new Calc();//The method empName() is undefined for the type SalesPreInt
    employees[i] = new EmpSales(empName,totalComp); // The constructor EmpSales(Name, Calc) is undefined

 class EmpSales{
        String empName; 
        BigDecimal totalComp;            
 public EmpSales(String empName, BigDecimal totalComp){ 
             this.empName = empName; 
             this.totalComp = totalComp; 

             }        

class Pay {

public BigDecimal basePay() {
    int basePay = 50000;
    return new BigDecimal(String.valueOf(basePay));
}
}

class Calc {

public BigDecimal totalComp(){
    Pay pay = new Pay();
    pay.basePay();

    BigDecimal intCalc = new BigDecimal("0.15");

    Scanner userInput = new Scanner(System.in);

    System.out.println("What were your total sales?");
    BigDecimal salesPre = userInput.nextBigDecimal();
    System.out.println("You're Total Sales were "+salesPre);

    userInput.close();

    BigDecimal postIntCalc = salesPre.multiply(intCalc);
    BigDecimal totalComp = postIntCalc.add(pay.basePay());

    System.out.println("Your total sales including commission is "+postIntCalc);
    System.out.println("Your total pay is"+totalComp);

    return new BigDecimal(String.valueOf(totalComp()));
}
}

class Name {
public String empName(){

    Scanner userInput = new Scanner(System.in);

    System.out.println("What is the Sales Rep Name?");
    String empName = userInput.toString();

    userInput.close();

    return empName;
}
}


    }
 }


}
}
Doug Coats
  • 6,255
  • 9
  • 27
  • 49
  • Oooo yea, your code structure is a bit off. You're creating classes around every method when you don't really need to. I'll try to help you out. – River Jun 17 '15 at 23:21
  • All of those classes are in the same folder as `SalesPreInt`, right? – Makoto Jun 17 '15 at 23:21
  • @Makoto, yes they are in the same source folder in eclipse. at River, i guess I went over board uh? :/ – Doug Coats Jun 17 '15 at 23:25
  • its worth mentioning that the classes are seperate files, i just put them in one comment – Doug Coats Jun 17 '15 at 23:26
  • I guess you really need to start with a good java tutorial. From indent to architecture,it isn't "java-like". – pwillemet Jun 17 '15 at 23:27
  • @DougCoats ok I have corrected most of it, but I can't see your `EmpSales` class. I suspect it has similar errors. – River Jun 17 '15 at 23:34
  • I am guessing that one of my main issues was I didnt create a EmpSales class and it is thinking that I have in fact called upon a class with no such name.... awesome, im learning stuff. I guess having a method with the same name as the class is stupid too. :/ THanks very Much River! Ill play around with it soon and see if I cant figure it out from here. – Doug Coats Jun 17 '15 at 23:38
  • @Kwoinkwoin- I guess the tutorial plus book I am using arent doing me much good lol I will try and find a better one – Doug Coats Jun 17 '15 at 23:39
  • @DougCoats check out the oracle java tutorials: https://docs.oracle.com/javase/tutorial/. I find they are quite good. – River Jun 17 '15 at 23:40
  • @DougCoats I also added an example `EmpSales` class with constructor, since class construction seemed to be what was giving you the most trouble. – River Jun 17 '15 at 23:47
  • 1
    Truthfully, it may be more valuable to focus on the fundamentals of object-oriented design than to learn about Java itself. Most good Java programs are based on an excellent object-based description of the problem domain as a domain object model. Having something like this can enable the classes and their relationships to develop naturally and freely. Your classes lack this kind of discipline. – scottb Jun 18 '15 at 00:51
  • @scottb thank you very much. im very new to OOP and having this kind of insight is invaluable. Sorry you had to deal with my newbish-ness, I thought i had a good grasp and decided to jump "head first" with something more complicated and clearly I have much to learn. I am still playing with River;s feedback to try and wrap my head around it. Im thinking the book i bought on amazon was a waste of 20$ :( – Doug Coats Jun 18 '15 at 01:00

1 Answers1

3

You seem to be a bit confused about classes. They are for creating a new, custom type of variable. I'd also read up on constructors (initializing method for a class).

Ok, as for your code, you don't need all those classes. You just need the methods.

I would also recommend renaming these methods to getXXXX as this is the standard in Java for this type of method.

Ok here's what you had with my edits commented in:

class SalesPreInt
{   
    public static void main(String[] args)

        {
        Scanner userInput = new Scanner(System.in);
        System.out.println("How many employees are we looking at?");
        int noOfReps = userInput.nextInt();

        userInput.close();

        EmpSales[] employees = new EmpSales[noOfReps];


        for(int i = 0; i< noOfReps; i ++){
            //Name empName = new Name(); //Don't make a Name variable
            String empName = empName(); //Make a String variable to hold the name instead

            //Calc totalComp = new Calc(); //Don't make a Calc variable
            BigDecimal totalComp = totalComp(); //Make a BigDecimal variable instead
            employees[i] = new EmpSales(empName,totalComp); //Should work now that the above are of the correct type

//The error below was because you were passing a (Name, Calc) variables to a constructor that only took (String, BigDecimal) parameters.
// The constructor EmpSales(Name, Calc) is undefined


        }//missing close bracket for for loop here
    }//missing close bracket for main method here

//Empsales class moved to bottom so it can be outside SalesPreInt class

//class Pay { //remove this class, you only need the method

    public static BigDecimal basePay() {
        int basePay = 50000;
        return new BigDecimal(String.valueOf(basePay));
    }
//}

//class Calc { //remove this class, you only need the method

    public static BigDecimal totalComp(){
        //Pay pay = new Pay(); //Don't create a Pay variable
        //pay.basePay();

        BigDecimal intCalc = new BigDecimal("0.15");

        Scanner userInput = new Scanner(System.in);

        System.out.println("What were your total sales?");
        BigDecimal salesPre = userInput.nextBigDecimal();
        System.out.println("You're Total Sales were "+salesPre);

        userInput.close();

        BigDecimal postIntCalc = salesPre.multiply(intCalc);
        BigDecimal totalComp = postIntCalc.add(basePay()); //Just use the basePay() method instead

        System.out.println("Your total sales including commission is "+postIntCalc);
        System.out.println("Your total pay is"+totalComp);

        return new BigDecimal(String.valueOf(totalComp()));
    }
//}

//class Name { //Don't use this class, just the method inside
    public static String empName(){

        Scanner userInput = new Scanner(System.in);

        System.out.println("What is the Sales Rep Name?");
        String empName = userInput.toString();

        userInput.close();

        return empName;
    }
//}


/*    }
 }


}*/ //extra brackets not needed
}//close bracket for SalesPreInt class here

class EmpSales{ //This looks ok, could be in another file, but since it's a helper class, looks fine here
    String empName; 
    BigDecimal totalComp;            
    public EmpSales(String empName, BigDecimal totalComp){ 
        this.empName = empName; 
        this.totalComp = totalComp; 
        //Initializing stuff
    }

}//missing close bracket for Empsales here     
Community
  • 1
  • 1
River
  • 8,585
  • 14
  • 54
  • 67