MIT-Curricular/OOP/Java/Assignments/FISAC/Q2.java

124 lines
2.9 KiB
Java

import java.text.*;
import java.util.*;
interface F {
void srt(int n);
void srt_d(String d);
}
class InvEx extends Exception {
public InvEx(String m) {
super(m);
}
}
class S implements Comparable<S> {
String n;
int r;
String reg;
String br;
public S(String n, int r, String br) {
this.n = n;
this.r = r;
this.br = br;
}
@Override
public int compareTo(S o) {
return Integer.compare(this.r, o.r);
}
@Override
public String toString() {
return "S{n='" + n + "', r=" + r + ", reg='" + reg + "', br='" + br + "'}";
}
}
class Fcl implements Comparable<Fcl> {
String n;
String d;
String dept;
String emp;
public Fcl(String n, String d, String dept) throws InvEx {
if (!d.matches("\\d{2}-[a-zA-Z]{3}-\\d{4}") && !d.matches("\\d{2}-\\d{2}-\\d{4}")) {
throw new InvEx("Invalid date format");
}
this.n = n;
this.d = d;
this.dept = dept;
}
@Override
public int compareTo(Fcl o) {
try {
SimpleDateFormat f = new SimpleDateFormat("dd-MM-yyyy");
return f.parse(this.d).compareTo(f.parse(o.d));
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
@Override
public String toString() {
return "Fcl{n='" + n + "', d='" + d + "', dept='" + dept + "', emp='" + emp + "'}";
}
}
public class M implements F {
List<S> sts = new ArrayList<>();
List<Fcl> fcls = new ArrayList<>();
public static void main(String[] a) {
M m = new M();
m.sts.add(new S("A", 2, "CCE"));
m.sts.add(new S("B", 1, "IT"));
m.sts.add(new S("C", 3, "CCE"));
try {
m.fcls.add(new Fcl("S", "15-Mar-2002", "ICT"));
m.fcls.add(new Fcl("J", "20-03-2002", "ECE"));
m.fcls.add(new Fcl("J2", "10-Feb-2002", "ICT"));
} catch (InvEx e) {
System.out.println(e.getMessage());
}
m.srt(0);
m.srt_d("");
for (S s : m.sts) {
System.out.println(s);
}
for (Fcl f : m.fcls) {
System.out.println(f);
}
}
@Override
public void srt(int n) {
Collections.sort(sts);
Map<String, Integer> brCnt = new HashMap<>();
for (S s : sts) {
brCnt.putIfAbsent(s.br, 1);
s.reg = "2022" + s.br + String.format("%03d", brCnt.get(s.br));
brCnt.put(s.br, brCnt.get(s.br) + 1);
}
}
@Override
public void srt_d(String d) {
Collections.sort(fcls);
Map<String, Integer> deptCnt = new HashMap<>();
for (Fcl f : fcls) {
deptCnt.putIfAbsent(f.dept, 1);
f.emp = "MAHE" + f.dept + String.format("%03d", deptCnt.get(f.dept));
deptCnt.put(f.dept, deptCnt.get(f.dept) + 1);
}
}
}