Adding Data from user input to ArrayList and creating constructor
How can I get data from user input to store it in Array and creating new constructor and so that the array should be dynamic?
Code:
//Täiusta seda, et kysib input scanneriga 5 inimest->
//loob constructorid->
//->ütleb, kui miinimumpalk on
//sorteerib nimekirja palga suuruse j2rgi
//Catchib errorid
//salvesta faili
import java.util.*;
public class Workplace {
public static void main(String[] args) {
ArrayList<String> nameAl = new ArrayList<String>();
ArrayList<String> hoursAl = new ArrayList<String>();
ArrayList<String> perHourAl = new ArrayList<String>();
Scanner sc = new Scanner(System.in);
String name;
String hours;
String perHour;
int count = 0;
int i;
//Tuleb loopida,
do {
count++;
print("Sisesta nimi: ");
name = sc.nextLine();
nameAl.add(name);
for ( i=0;i<nameAl.size();i++) {
nameAl.get(i);
}
print("Sisesta tunnid: ");
hours = sc.nextLine();
hoursAl.add(hours);
for ( i=0;i<hoursAl.size();i++) {
hoursAl.get(i);
}
print("Sisesta tunnipalk: ");
perHour = sc.nextLine();
perHourAl.add(perHour);
for ( i=0;i<perHourAl.size();i++) {
perHourAl.get(i);
}
Worker ID = new Worker(nameAl.get(i),Double.parseDouble(hoursAl.get(i)),
Double.parseDouble(perHourAl.get(i)));
// print(ID.getName(i)+" "+ID.getSalary(i));
} while (count!=5);
}
public static String print(String s) {
System.out.print(s);
return s;
}
}
class Worker {
private double hours;
private double perHour;
private String name;
Worker(String name,double hours,double perHour) {
this.name = name;
this.hours = hours;
this.perHour= perHour;
}
public double getHours() {
if(hours>60.0) {
throw new RuntimeException("Hours, peab väiksem, kui 60 olema");
}
return hours;
}
public double getPerHour() {
if(perHour<8.0) {
throw new RuntimeException("P/H, peab suurem, kui 8 olema");
}
return perHour;
}
public String getName() {
return name;
}
public double getSalary() {
double salary;
if (getHours()<=40) {
salary =1.0*getPerHour()*getHours();
} else {
salary =(getHours()-40)*1.5*getPerHour()+(40*getPerHour());
}
//salary = ratio * getPerHour();
return salary;
}
}