Last active
September 12, 2021 04:59
-
-
Save IAFahim/e0aa05e15e24ddc333e45126ce3c1a95 to your computer and use it in GitHub Desktop.
CSE215 Lab Final Task A
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.util.Scanner; | |
public class Main { | |
public static void main(String[] args) throws FileNotFoundException { | |
Scanner sc=new Scanner(System.in); | |
File file = new File("residents.txt"); | |
sc = null; | |
int size=0; | |
sc = new Scanner(file); | |
while (sc.hasNext()){ | |
sc.nextLine(); | |
size++; | |
} | |
sc.close(); | |
sc=new Scanner(file); | |
Residents[] residents = new Residents[size]; | |
int i=0; | |
while (sc.hasNext()) { | |
String[] str = sc.nextLine().split(" "); | |
residents[i]=new Residents(str[0]+" "+str[1],str[2], Double.parseDouble(str[3])); | |
i++; | |
} | |
sc.close(); | |
Residents residents1=new Residents(null,null, 0); | |
residents1.makeList(); | |
} | |
} | |
class Residents implements Validate { | |
private String name; | |
private String nid; | |
private double salary; | |
public Residents(String name, String nid, double salary) { | |
this.name = name; | |
this.nid = nid; | |
this.salary = salary; | |
} | |
public int getAge() { | |
return 2021 - Integer.parseInt(nid.substring(0, 4)); | |
} | |
public void makeList() { | |
File file = new File("residents.txt"); | |
Scanner sc = null; | |
if (file.exists()) { | |
try { | |
sc = new Scanner(file); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} | |
} else { | |
sc = new Scanner(System.in); | |
} | |
while (sc.hasNext()) { | |
String[] str = sc.nextLine().split(" "); | |
name = str[0] + " " + str[1]; | |
nid = str[2]; | |
double age = getAge(); | |
salary = Double.parseDouble(str[3]); | |
if (validateNID() && (age <= 65) && salary<=350000) { | |
System.out.println(this); | |
} | |
} | |
sc.close(); | |
} | |
@Override | |
public boolean validateNID() { | |
return nid.length() == 17; | |
} | |
@Override | |
public String toString() { | |
return "Residents{" + | |
"name='" + name + '\'' + | |
", nid='" + nid + '\'' + | |
", salary=" + salary + | |
'}'; | |
} | |
} | |
interface Validate { | |
boolean validateNID(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment