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