From 1f8964b3fd33bd690740cc1fdf55fb1a10f8c075 Mon Sep 17 00:00:00 2001 From: Aadit Agrawal Date: Mon, 28 Oct 2024 00:13:16 +0530 Subject: [PATCH] Add OOP/Java/Assignments/FISAC/Q2.java --- OOP/Java/Assignments/FISAC/Q2.java | 123 +++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 OOP/Java/Assignments/FISAC/Q2.java diff --git a/OOP/Java/Assignments/FISAC/Q2.java b/OOP/Java/Assignments/FISAC/Q2.java new file mode 100644 index 0000000..7eff0a2 --- /dev/null +++ b/OOP/Java/Assignments/FISAC/Q2.java @@ -0,0 +1,123 @@ +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 { + 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 { + 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 sts = new ArrayList<>(); + List 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 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 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); + } + } +}