Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import College.*;
- public class Main {
- public static void main(String[] args) throws FileNotFoundException {
- Scanner sc = new Scanner(System.in);
- //default filename
- String fileName = "default.txt";
- System.out.println("Do you want to read information from some file? 1-yes 0-no");
- if (sc.nextInt() == 1) {
- System.out.println("Write a name of a file");
- fileName = sc.next() + ".txt";
- //Read info from file
- College read = readFromFile(fileName);
- }
- //new college
- College harvard = new College("Harvard", 20);
- //adding two lecturers
- harvard.addLecturer(new Lecturer("Bob", "magnum", 52));
- harvard.addLecturer(new Lecturer("gogo", "fistuk", 15));
- //checking sort function
- harvard.sortLecturersByLessFall();
- //prints all information about college
- System.out.println(harvard.toString());
- //function printing college to file
- printToFile(fileName, harvard);
- }
- public static void printToFile(String fileName, College college) throws FileNotFoundException {
- PrintWriter pw = new PrintWriter(new File(fileName));
- pw.println(college.getCollegeName());
- pw.println(college.getNumOfLecturers());
- //write all lecturers to file
- for (int i = 0; i < college.getNumOfLecturers(); i += 1) {
- Lecturer lect = college.getLecturerById(i);
- pw.println(lect.getLect_name());
- pw.println(lect.getFavoriteIceCream());
- pw.println(lect.getNumOfTimesPensFalls());
- }
- pw.close();
- }
- public static College readFromFile(String fileName) throws FileNotFoundException {
- Scanner s = new Scanner(new File(fileName));
- String name, iceCream;
- int penFall;
- //first two lines to declare the college
- College temp = new College(s.nextLine(), s.nextInt());
- //read and add all lectures from file
- while (s.hasNextLine()) {
- name = s.next();
- s.nextLine();
- iceCream = s.next();
- s.nextLine();
- penFall = s.nextInt();
- s.nextLine();
- temp.addLecturer(new Lecturer(name, iceCream, penFall));
- }
- return temp;
- }
- }
- }
- =====================Lecturer======================================================================================
- package College;
- public class Lecturer {
- private String lect_name, favoriteIceCream;
- private int numOfTimesPensFalls;
- //Our ids will start form 1000 so we needs private(have no access from outside of class)
- //and ststic counter which starts from 1000
- private static int idStartFrom = 1000;
- private int id;
- public Lecturer(String lect_name, String favoriteIceCream, int numOfTimesPensFalls) {
- this.lect_name = lect_name;
- this.favoriteIceCream = favoriteIceCream;
- //for every new lecturer increment id;
- this.numOfTimesPensFalls = numOfTimesPensFalls;
- this.id = ++idStartFrom;
- }
- public int getNumOfTimesPensFalls() {
- return this.numOfTimesPensFalls;
- }
- public String getLect_name() {
- return lect_name;
- }
- public String getFavoriteIceCream() {
- return favoriteIceCream;
- }
- public int getId() {
- return id;
- }
- @Override
- public String toString() {
- return "Lecturer: " + lect_name +
- "\nId: " + id +
- "\nFavorine ice creame: " + favoriteIceCream +
- "\nNum of times pens falls: " + numOfTimesPensFalls+"\n";
- }
- }
- ===================College=============================================================================
- package College;
- public class College {
- private String collegeName;
- private int numOfLecturers;
- private Lecturer[] allLecturers;
- public College(String collegeName, int maxNumOfLecturers) {
- this.collegeName = collegeName;
- this.numOfLecturers = 0;
- this.allLecturers = new Lecturer[maxNumOfLecturers];
- }
- //copy constructor
- public College(College other) {
- this.collegeName = other.getCollegeName();
- this.numOfLecturers = other.getNumOfLecturers();
- // for arrays we need to copy all elements and not to bring a link from other's array to constructors one
- this.allLecturers = new Lecturer[other.allLecturers.length];
- //we need to add lecturers from other's array so we do this just for amount of
- //lecturers who exist in other's array
- for (int i = 0; i < other.numOfLecturers; i += 1) {
- this.allLecturers[i] = other.allLecturers[i];
- }
- }
- public String getCollegeName() {
- return collegeName;
- }
- public int getNumOfLecturers() {
- return numOfLecturers;
- }
- public Lecturer getLecturerById(int id){
- return this.allLecturers[id];
- }
- public boolean addLecturer(Lecturer lecturer) {
- //if current amount of lecturers is less than alloved put lecturer to the array,
- // increment current amount of lecturers returns true
- // else return false
- if (this.numOfLecturers <= this.allLecturers.length) {
- this.allLecturers[numOfLecturers] = lecturer;
- this.numOfLecturers += 1;
- return true;
- } else {
- return false;
- }
- }
- //Using buble sort for the array
- public void sortLecturersByLessFall() {
- //array's last element is current number less one
- int ln = this.numOfLecturers;
- Lecturer temp;
- for (int i = 0; i < ln; i += 1) {
- for (int j = 1; j < ln - i; j += 1) {
- //compare by fallen pens
- if (this.allLecturers[j - 1].getNumOfTimesPensFalls() > this.allLecturers[j].getNumOfTimesPensFalls()) {
- //change lecturer's places
- temp = this.allLecturers[j - 1];
- this.allLecturers[j - 1] = this.allLecturers[j];
- this.allLecturers[j] = temp;
- }
- }
- }
- }
- @Override
- public String toString() {
- String str = "College: " + collegeName +
- "\nCurrent number of lecturers: " + numOfLecturers+"\n";
- for(int i=0;i<numOfLecturers;i+=1){
- str+=allLecturers[i].toString();
- }
- return str;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement