Advertisement
Guest User

java sample code

a guest
Apr 14th, 2022
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.19 KB | None | 0 0
  1. public class Main {
  2.  
  3.     static class Report {
  4.         String id;
  5.         ReportData data;
  6.     }
  7.  
  8.     static abstract class ReportData {
  9.         String id;
  10.     }
  11.  
  12.     static class ReportDataTypeA extends ReportData {
  13.         String section1;
  14.         String section2;
  15.     }
  16.  
  17.     static class ReportDataTypeB extends ReportData {
  18.         String header;
  19.         String section1a;
  20.         String section1b;
  21.     }
  22.  
  23.     static ReportData createDataByYear(int year) {
  24.         if (year == 2021) {
  25.             ReportDataTypeA reportData = new ReportDataTypeA();
  26.             reportData.id = "1";
  27.             reportData.section1 = "test1";
  28.             return reportData;
  29.         } else if (year == 2022) {
  30.             ReportDataTypeB reportData = new ReportDataTypeB();
  31.             reportData.id = "2";
  32.             reportData.header = "header";
  33.             return reportData;
  34.         }
  35.         return null;
  36.     }
  37.  
  38.     public static void main(String... args) {
  39.         Report report = new Report();
  40.         report.data = createDataByYear(2021);
  41.         System.out.println(report.data.id);
  42.         report.data = createDataByYear(2022);
  43.         System.out.println(report.data.id);
  44.     }
  45.  
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement