/** collegeDetail is a function which is read college detail from file and store in queue */
  public void readCollageDetail() {

    try {
      // Reading the college data from file
      BufferedReader br =
          new BufferedReader(
              new FileReader(
                  "C:/Users/khemanshu/workspace/Assignment/src/QueueImplemetation/college.txt"));
      while ((line = br.readLine()) != null) {
        String[] token = line.split(",");
        if (token.length < 2) continue;
        Colleges collages = new Colleges();
        collages.setName(token[0]);
        collages.setSeatAvaliable(Integer.parseInt(token[1]));

        collegeQueue.enqueue(collages);
        collegeList.add(collages);
      }
      br.close();
    } catch (FileNotFoundException e) {
      System.out.println(e);

    } catch (IOException e) {
      System.out.println(e);
    }
  }
  /**
   * SeatAllotment is a function which is used to allot a seat of desire college if seat is
   * available.
   */
  void SeatAllotment() {

    int collegeCounter = calculteTotalSeat();

    int sizeStudent = studentQueue.getSize();
    int sizeColllege = collegeQueue.getSize();
    Colleges colleges = new Colleges();
    Student student = new Student();

    while (sizeStudent != 0) {
      if (collegeCounter == 0) break;

      student = (Student) studentQueue.dequeue();

      System.out.println("Student Name ->" + student.getName() + "\n" + "Select any college");

      showListOfCollage();
      String name = ValidationForCollegeName();

      for (int count = 0; count < sizeColllege; count++) {
        colleges = (Colleges) collegeQueue.dequeue();
        if (colleges.getName().equalsIgnoreCase(name) == true) {
          student.setCollegeName(name);
          colleges.setSeatAvaliable(colleges.getSeatAvaliable() - 1);
        }

        collegeQueue.enqueue(colleges);
      }

      System.out.println("Allotement is successfully done");
      studentQueue.enqueue(student);
      sizeStudent--;
      collegeCounter--;
    }
  }
  /**
   * calculteTotalSeat is a function which is used to calculated total available seat in all college
   *
   * @return total seat
   */
  int calculteTotalSeat() {
    int totalSeat = 0;

    for (Colleges college : collegeList) {
      totalSeat += college.getSeatAvaliable();
    }

    return totalSeat;
  }
  /** showListOfCollage is a function which is show list of college in which seat is available */
  void showListOfCollage() {
    int size = collegeQueue.getSize();
    Colleges colleges = new Colleges();
    System.out.println("College Name" + "\t" + "Seats");
    for (int count = 0; count < size; count++) {
      colleges = (Colleges) collegeQueue.dequeue();
      if (colleges.getSeatAvaliable() > 0) {
        System.out.println(colleges.getName() + "\t" + colleges.getSeatAvaliable());

        collegeQueue.enqueue(colleges);
      }
    }
  }
示例#5
0
  public static void main(String[] args) {
    Colleges c = new Colleges();
    Student s = new Student();
    Teacher t = new Teacher();
    c.setName("***科技学院");
    c.setNum(16000);
    c.setArea(800);

    s.setName1("秦*盛");
    s.setGrade("测绘B111");
    s.setMajor("建筑工程学院");

    t.setName("王老师");
    t.setCurriculum("Java 编程");

    System.out.println(c.getName() + " 有 " + c.getNum() + "名学生" + "占地面积" + c.getArea() + "亩");

    System.out.println(t.getName() + "在" + c.getName() + "教授" + t.getCurriculum());

    System.out.println(
        s.getName1()
            + "就读于"
            + s.getMajor()
            + s.getGrade()
            + "在上"
            + t.getName()
            + "的"
            + t.getCurriculum());
  }
  /**
   * ValidationForCollegeName is a recursive function which is used for validation that entered
   * college name is correct or not
   */
  String ValidationForCollegeName() {
    boolean flag = false;
    Scanner scanner = new Scanner(System.in);
    String name = scanner.nextLine();
    for (Colleges collage : collegeList) {
      if (collage.getName().equalsIgnoreCase(name) && collage.getSeatAvaliable() > 0) {
        flag = true;
        return name;
      }
    }
    if (!flag) {
      System.out.println("please enter correct college name");
      name = ValidationForCollegeName();
    }

    return name;
  }