简体   繁体   中英

StringTokenizer and Array Java

I am completed the whole program, and it is working fine. But, I am having trouble with the StringTokenizer. I have class named Worker.java and a test class named TestWorker.java...

In the test class I have calculated and printed out the total hoursworked, grossSalary and etc.. But I am stuck in 1 position, that is I have to store name,id, and hourly rate in array, and then using the StringTokenizer, I have to prompt the user to enter worker ID and hourly rate.

When the user enter the user worker name and ID, the program output will show the user the hourly rate of the worker...

or in other words...

Allow the user to enter worker-ID and Hours repeatedly until user enter and empty string. Read the values and invoke the methods addWeekly() on the appropriate object(by searching through the array to locate the object with the specified ID). If a nonexistent ID is entered display an appropriate error message.

Please see my codings below and modify as your needs....

//Worker.java

public class Worker {
    public final double bonus=100;
    protected String name;
    protected String workerID;
    protected double hourlyRate;
    protected double hoursWorked;
    protected double weekHour;
    protected double tax;
    protected double grossSalary;
    protected double netSalary;


    public Worker() {
    }

    public Worker(String name,String workerID,double hourlyRate){
        this.name = name;
        this.workerID = workerID;
        this.hourlyRate = hourlyRate;
    }

    public void addWeekly(double weekHour){

        hoursWorked = hoursWorked + weekHour;

    }

    public double gross()
    {
        grossSalary = (hoursWorked*hourlyRate);
        if(hoursWorked>=150)
        {
            grossSalary = grossSalary +100;
        }
        return  grossSalary;
    }

    public double taxAndNet()
    {
        tax = (grossSalary - 500) * 0.3;
        netSalary = (grossSalary-tax);
        return netSalary;
    }

    public String getName()
    {
        return name;
    }

    public String getWorkerID()
    {
        return workerID;
    }

    public double getHourly()
    {
        return hourlyRate;
    }

    public double getTotalHours()
    {
        return hoursWorked;
    }

    public double getGrossSalary()
    {
        return grossSalary;
    }

    public void addToGross(double amt)
    {
        grossSalary = grossSalary + amt;
    }

    public void displaySalary()
    {

        System.out.println("Name :" + name + "\nID :" + workerID +"\nHourly rate : "+ hourlyRate +"\nTotal Hours Worked " + hoursWorked +"\nGross Pay: "+getGrossSalary()+ "\nTax : "+tax +"\nNetpay :" +netSalary);
    }

}


//TestWorker.java

import java.util.StringTokenizer;
import java.util.Scanner;

public class TestWorker {

    public static void main(String args[]) {

        Worker e = new Worker("John Major","s123",15.0);
        e.addWeekly(45);
        e.addWeekly(40);
        e.addWeekly(32);
        e.addWeekly(38);
        e.gross();
        e.taxAndNet();
        e.displaySalary();

        Worker[] worklist = new Worker[5];

        worklist[0] = new Worker("Richard Cooper","s1235",20.0);
        worklist[1] = new Worker("John Major","s1236",18.0);
        worklist[2] = new Worker("Mary Thatcher","s1237",15.0);
        worklist[3] = new Worker("David Benjamin","s1238",16.0);
        worklist[4] = new Worker("Jack Soo","s1239",25.0);

        Scanner input = new Scanner(System.in);
        String name;

        do{
            System.out.print("Please enter your name and worker ID: ");
            name = input.nextLine();

            StringTokenizer string = new StringTokenizer(name,"+");
            System.out.println("******|||||*******");
            while(string.hasMoreElements()){
                System.out.println(string.nextElement());
            }
        }
        while(name.isEmpty()==false);

         String s = "Five+Three=Nine-One";
        String arr[];

          //declare it with 3 tokens as seen above:
      StringTokenizer st = new StringTokenizer(s, "+=-");

      //the array size is the number of tokens in the String:
      arr = new String[st.countTokens()];

          //when there are still more tokens, place it in the array:
      int i = 0;
          while(st.hasMoreTokens()){
        arr[i] = st.nextToken();
                i++;
          }

      //determine the word with the largest length:
          int indexMax = 0;
          for( i = 1; i < arr.length; i++){
             if(arr[i].length() > arr[indexMax].length())
            indexMax = i;
          }

      System.out.println("The largest element is in index: "
                + indexMax);

  } //main
} //class

Please tell us your java version. Since jdk 1.4.2 you should use String.split(...) instead of the old Stringtokenizer. Check out this tutorial TIP: A little tutorial about String.split();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM