Enum type Disease : MALARIA(1990,”ITALY”)
Need to : - declare enum attribute (year & country) - implement enum with constructor (two parameters in header)
- appropriate public methods to access private attribute
- Input file(Disease type , person age, person name)
- Need another classnamed as Person
import java.util.*;
import java.io.*;
enum Disease{MALARIA(1990,"Italy"), COVID(2020,"China"), DENGGI(2013,"Malaysia");
private int year;
private String country;
private Disease(int y, String c){
year=y;
country=c;
}
public int getYear(){
return year;}
public String getCountry(){
return country;
}
public void print(){
System.out.println("Year: "+ year +" "+"Country: "+ country);
}
}
class Person{
private int pAge;
private String name;
Person(int pAge, String name){
this.pAge=pAge;
this.name=name;
}
public int getAge(){
return pAge;
}
public void print(){
System.out.println("Age: " + pAge + "Name: " + name);
}
}
public class TestDisease{
public static void main(String args[]) throws FileNotFoundException{
//creating File instance to reference text file in Jaca
File text= new File("C://JavaPrograms//dataDisease.txt");
int i=0;
String type, name;
int age;
ArrayList<Disease> dList= new ArrayList<Disease>();
ArrayList<Person> pList= new ArrayList<Person>();
//Creating Scanner instance to read File in Java
Scanner input = new Scanner(text);
while(input.hasNext()){
type=input.next();
age=input.nextInt();
name=input.nextLine();
Disease dis=Enum.valueOf(Disease.class,type.toUpperCase());
dList.add(dis);
Person dataP= new Person(age,name);
pList.add(dataP);
}
System.out.println("--------------------------------");
for(Disease ds: dList)
ds.print();
System.out.println("--------------------------------");
for(Person ps: pList)
ps.print();
}//main
}//TestDisease
Input File
Covid 70 Goh Shaw
Malaria 80 Marry Jone
Denggi 65 Suhana Ali
Output
0 Comments