Goodiny777

College/Lecturer Exr. 9.1

Mar 24th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.49 KB | None | 0 0
  1. import College.*;
  2.  
  3. public class Main {
  4.     public static void main(String[] args) throws FileNotFoundException  {
  5.             Scanner sc = new Scanner(System.in);
  6.         //default filename
  7.         String fileName = "default.txt";
  8.         System.out.println("Do you want to read information from some file? 1-yes 0-no");
  9.         if (sc.nextInt() == 1) {
  10.             System.out.println("Write a name of a file");
  11.             fileName = sc.next() + ".txt";
  12.             //Read info from file
  13.             College read = readFromFile(fileName);
  14.         }
  15.         //new college
  16.         College harvard = new College("Harvard", 20);
  17.         //adding two lecturers
  18.         harvard.addLecturer(new Lecturer("Bob", "magnum", 52));
  19.         harvard.addLecturer(new Lecturer("gogo", "fistuk", 15));
  20.         //checking sort function
  21.         harvard.sortLecturersByLessFall();
  22.         //prints all information about college
  23.         System.out.println(harvard.toString());
  24.         //function printing college to file
  25.         printToFile(fileName, harvard);
  26.     }
  27.  
  28.     public static void printToFile(String fileName, College college) throws FileNotFoundException {
  29.         PrintWriter pw = new PrintWriter(new File(fileName));
  30.         pw.println(college.getCollegeName());
  31.         pw.println(college.getNumOfLecturers());
  32.         //write all lecturers to file
  33.         for (int i = 0; i < college.getNumOfLecturers(); i += 1) {
  34.             Lecturer lect = college.getLecturerById(i);
  35.             pw.println(lect.getLect_name());
  36.             pw.println(lect.getFavoriteIceCream());
  37.             pw.println(lect.getNumOfTimesPensFalls());
  38.  
  39.         }
  40.         pw.close();
  41.     }
  42.  
  43.     public static College readFromFile(String fileName) throws FileNotFoundException {
  44.         Scanner s = new Scanner(new File(fileName));
  45.         String name, iceCream;
  46.         int penFall;
  47.         //first two lines to declare the college
  48.         College temp = new College(s.nextLine(), s.nextInt());
  49.         //read and add all lectures from file
  50.         while (s.hasNextLine()) {
  51.             name = s.next();
  52.             s.nextLine();
  53.             iceCream = s.next();
  54.             s.nextLine();
  55.             penFall = s.nextInt();
  56.             s.nextLine();
  57.             temp.addLecturer(new Lecturer(name, iceCream, penFall));
  58.         }
  59.         return temp;
  60.     }
  61.     }
  62. }
  63.  
  64. =====================Lecturer======================================================================================
  65.  
  66.  
  67. package College;
  68.  
  69. public class Lecturer {
  70.     private String lect_name, favoriteIceCream;
  71.     private int numOfTimesPensFalls;
  72.     //Our ids will start form 1000 so we needs private(have no access from outside of class)
  73.     //and ststic counter which starts from 1000
  74.     private static int idStartFrom = 1000;
  75.     private int id;
  76.  
  77.     public Lecturer(String lect_name, String favoriteIceCream, int numOfTimesPensFalls) {
  78.         this.lect_name = lect_name;
  79.         this.favoriteIceCream = favoriteIceCream;
  80.         //for every new lecturer increment id;
  81.         this.numOfTimesPensFalls = numOfTimesPensFalls;
  82.         this.id = ++idStartFrom;
  83.     }
  84.  
  85.       public int getNumOfTimesPensFalls() {
  86.         return this.numOfTimesPensFalls;
  87.     }
  88.  
  89.     public String getLect_name() {
  90.         return lect_name;
  91.     }
  92.  
  93.     public String getFavoriteIceCream() {
  94.         return favoriteIceCream;
  95.     }
  96.  
  97.     public int getId() {
  98.         return id;
  99.     }
  100.  
  101.     @Override
  102.     public String toString() {
  103.         return "Lecturer: " + lect_name +
  104.                 "\nId: " + id +
  105.                 "\nFavorine ice creame: " + favoriteIceCream +
  106.                 "\nNum of times pens falls: " + numOfTimesPensFalls+"\n";
  107.     }
  108. }
  109.  
  110.  
  111. ===================College=============================================================================
  112.  
  113.  
  114. package College;
  115.  
  116. public class College {
  117.     private String collegeName;
  118.     private int numOfLecturers;
  119.     private Lecturer[] allLecturers;
  120.  
  121.    public College(String collegeName, int maxNumOfLecturers) {
  122.         this.collegeName = collegeName;
  123.         this.numOfLecturers = 0;
  124.         this.allLecturers = new Lecturer[maxNumOfLecturers];
  125.     }
  126.  
  127.     //copy constructor
  128.     public College(College other) {
  129.         this.collegeName = other.getCollegeName();
  130.         this.numOfLecturers = other.getNumOfLecturers();
  131.         // for arrays we need to copy all elements and not to bring a link from other's array to constructors one
  132.         this.allLecturers = new Lecturer[other.allLecturers.length];
  133.         //we need to add lecturers from other's array so we do this just for amount of
  134.         //lecturers who exist in other's array
  135.         for (int i = 0; i < other.numOfLecturers; i += 1) {
  136.             this.allLecturers[i] = other.allLecturers[i];
  137.         }
  138.     }
  139.  
  140.     public String getCollegeName() {
  141.         return collegeName;
  142.     }
  143.  
  144.     public int getNumOfLecturers() {
  145.         return numOfLecturers;
  146.     }
  147.  
  148.     public Lecturer getLecturerById(int id){
  149.         return this.allLecturers[id];
  150.     }
  151.  
  152.     public boolean addLecturer(Lecturer lecturer) {
  153.         //if current amount of lecturers is less than alloved put lecturer to the array,
  154.         // increment current amount of lecturers returns true
  155.         // else return false
  156.         if (this.numOfLecturers <= this.allLecturers.length) {
  157.             this.allLecturers[numOfLecturers] = lecturer;
  158.             this.numOfLecturers += 1;
  159.             return true;
  160.         } else {
  161.             return false;
  162.         }
  163.     }
  164.  
  165.     //Using buble sort for the array
  166.     public void sortLecturersByLessFall() {
  167.         //array's last element is current number less one
  168.         int ln = this.numOfLecturers;
  169.         Lecturer temp;
  170.         for (int i = 0; i < ln; i += 1) {
  171.             for (int j = 1; j < ln - i; j += 1) {
  172.                 //compare by fallen pens
  173.                 if (this.allLecturers[j - 1].getNumOfTimesPensFalls() > this.allLecturers[j].getNumOfTimesPensFalls()) {
  174.                     //change lecturer's places
  175.                     temp = this.allLecturers[j - 1];
  176.                     this.allLecturers[j - 1] = this.allLecturers[j];
  177.                     this.allLecturers[j] = temp;
  178.                 }
  179.             }
  180.         }
  181.     }
  182.  
  183.     @Override
  184.     public String toString() {
  185.         String str =  "College: " + collegeName +
  186.                 "\nCurrent number of lecturers: " + numOfLecturers+"\n";
  187.         for(int i=0;i<numOfLecturers;i+=1){
  188.             str+=allLecturers[i].toString();
  189.         }
  190.         return str;
  191.     }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment