Advertisement
dzocesrce

[SKIT] Student Conference

May 22nd, 2024
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.87 KB | None | 0 0
  1. // lab 4 task
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import org.junit.jupiter.api.BeforeEach;
  7. import org.junit.jupiter.api.Test;
  8.  
  9. import java.util.ArrayList;
  10. import java.util.List;
  11.  
  12. import static org.junit.jupiter.api.Assertions.assertEquals;
  13. import static org.junit.jupiter.api.Assertions.assertSame;
  14.  
  15. public class Conference {
  16.  
  17.     public static double TICKET_PRICE = 10.90;
  18.     public static double EMT_DISCOUNT = 0.5;
  19.     public static double WEB_DISCOUNT = 0.3;
  20.  
  21.     private List<Student> attendees;
  22.     private int capacity;
  23.  
  24.     public Conference(int capacity) {
  25.         this.attendees = new ArrayList<>(capacity);
  26.         this.capacity = capacity;
  27.     }
  28.  
  29.     public double calculateTotalPricePaid() {
  30.         double price = 0;
  31.  
  32.         for (Student p : attendees) {
  33.             if (p.getCourse().name().equals("EMT"))
  34.                 price += (1 - EMT_DISCOUNT) * TICKET_PRICE;
  35.             else if (p.getCourse().name().equals("WEB"))
  36.                 price += (1 - WEB_DISCOUNT) * TICKET_PRICE;
  37.             else
  38.                 price += TICKET_PRICE;
  39.         }
  40.         return price;
  41.     }
  42.  
  43.     public boolean addAttendeeToConference(Student student) {
  44.         if ((this.attendees.size() < capacity) || tripleCapacity()) {
  45.             this.attendees.add(student);
  46.             return true;
  47.         } else return false;
  48.     }
  49.  
  50.     public boolean tripleCapacity() {
  51.         if (this.capacity * 3 > 10000)
  52.             return false;
  53.         this.capacity *= 3;
  54.         return true;
  55.     }
  56.  
  57.     public List<Student> getAttendees() {
  58.         return attendees;
  59.     }
  60.  
  61.     public int getCapacity() {
  62.         return capacity;
  63.     }
  64. }
  65.  
  66. class Student {
  67.  
  68.     private String name;
  69.     private String surname;
  70.     private Course course;
  71.     private int age;
  72.  
  73.     public Student(String name, String surname, Course course, int age) {
  74.         this.name = name;
  75.         this.surname = surname;
  76.         this.course = course;
  77.         this.age = age;
  78.     }
  79.  
  80.     public String getName() {
  81.         return name;
  82.     }
  83.  
  84.     public void setName(String name) {
  85.         this.name = name;
  86.     }
  87.  
  88.     public String getSurname() {
  89.         return surname;
  90.     }
  91.  
  92.     public void setSurname(String surname) {
  93.         this.surname = surname;
  94.     }
  95.  
  96.     public Course getCourse() {
  97.         return course;
  98.     }
  99.  
  100.     public void setCourse(Course course) {
  101.         this.course = course;
  102.     }
  103.  
  104.     public int getAge() {
  105.         return age;
  106.     }
  107.  
  108.     public void setAge(int age) {
  109.         this.age = age;
  110.     }
  111.  
  112.     @Override
  113.     public String toString() {
  114.         return "Student{" +
  115.                 "name='" + name + '\'' +
  116.                 ", surname='" + surname + '\'' +
  117.                 ", course=" + course +
  118.                 ", age=" + age +
  119.                 '}';
  120.     }
  121. }
  122.  
  123. enum Course {
  124.     OS,
  125.     EMT,
  126.     SKIT,
  127.     WEB,
  128.     OTHER
  129. }
  130.  
  131. class ConferenceTest {
  132.  
  133.     public int CAPACITY = 3333;
  134.     Conference conference = new Conference(CAPACITY);
  135.     // TODO: Add tests here!
  136.     // Create a new Conference for every new test
  137.     @BeforeEach
  138.     void setUp() {
  139.         conference = new Conference(CAPACITY);
  140.     }
  141.  
  142.     // TEST 1: Calculate Total price with no EMT or Web students attending
  143.     @Test
  144.     public void CalculatePriceWithEMTandWebStudentsExcluded(){
  145.         List<Student> attendees = new ArrayList<>();
  146.  
  147.         Student student = new Student("Dzoce","Srce",Course.valueOf("OS"),22);
  148.         conference.addAttendeeToConference(student);
  149.         double price = conference.calculateTotalPricePaid();
  150.         assertEquals(10.90,price,0);
  151.  
  152.     }
  153.  
  154.     // TEST 2: Calculate Total price with EMT and Web as well as 'normal' students attending
  155.     // Also Test set methods for Student Class
  156.     @Test
  157.     public void CalculatePriceWithEMTandWebStudentsIncluded(){
  158.         List<Student> attendees = new ArrayList<>();
  159.         Student student1 = new Student("Dzoce","Srce",Course.valueOf("OS"),22);
  160.         Student student2 = new Student("Ev","Genija",Course.valueOf("WEB"),21);
  161.         Student student3 = new Student("Angela","Angeleska",Course.valueOf("OTHER"),20);
  162.         student3.setAge(20);
  163.         student3.setName("Angela");
  164.         student3.setCourse(Course.valueOf("EMT"));
  165.         student3.setSurname("Angeleska");
  166.         conference.addAttendeeToConference(student1);
  167.         conference.addAttendeeToConference(student2);
  168.         conference.addAttendeeToConference(student3);
  169.         double price = conference.calculateTotalPricePaid();
  170.         assertEquals(23.98,price,0);
  171.  
  172.     }
  173.  
  174.     // TEST 3: Test getCapacity() function after creating a Conference object
  175.     @Test
  176.     public void ConferenceCapacity(){
  177.         assertEquals(CAPACITY,conference.getCapacity());
  178.     }
  179.  
  180.     // TEST 4: Test TripleCapacity() function
  181.     @Test
  182.     public void ConferenceTripleCapacity(){
  183.         int initial_capacity = conference.getCapacity();
  184.         conference.tripleCapacity();
  185.         assertEquals(initial_capacity*3,conference.getCapacity());
  186.     }
  187.  
  188.     // TEST 5: Test Attendess List, or in other words the getAttendees() method
  189.     @Test
  190.     public void ConferenceAttendeesList(){
  191.         List<Student> attendees = new ArrayList<>();
  192.         Student student1 = new Student("Dzoce","Srce",Course.valueOf("OS"),22);
  193.         Student student2 = new Student("Ev","Genija",Course.valueOf("WEB"),21);
  194.         Student student3 = new Student("Angela","Angeleska",Course.valueOf("OTHER"),20);
  195.         attendees.add(student1);
  196.         attendees.add(student2);
  197.         attendees.add(student3);
  198.         conference.addAttendeeToConference(student1);
  199.         conference.addAttendeeToConference(student2);
  200.         conference.addAttendeeToConference(student3);
  201.         assertEquals(attendees,conference.getAttendees());
  202.     }
  203.  
  204.     // TEST 6: Test Adding Attendees over the limit, but this time the capacity is more than 3333 students (3334 exactly)
  205.     @Test
  206.     public void AddingAttendeeToConferenceOverTheLimit(){
  207.         Student student = new Student("Dzoce","Srce",Course.valueOf("OS"),22);
  208.         Conference conference2 = new Conference(3334);
  209.         for(int i=0;i<3334;i++){
  210.             conference2.addAttendeeToConference(student);
  211.         }
  212.         assertEquals(false,conference2.addAttendeeToConference(student));
  213.     }
  214.  
  215.     // TEST 7: Simular situation as the previous test, but now we are trying to kill the mutant in line 35 of the Conference class,
  216.     // , that is this.attendees.size() >= capacity || tripleCapacity()
  217.     @Test
  218.     public void AddingFirstAttendeeToConference(){
  219.         Student student = new Student("Dzoce","Srce",Course.valueOf("OS"),22);
  220.         Conference conference2 = new Conference(3334);
  221.         assertEquals(true,conference2.addAttendeeToConference(student));
  222.     }
  223.  
  224.     // TEST 8: Testing the tripleCapacity() method with less than 3334 capacity.
  225.     @Test
  226.     public void TripleCapacityLessThan3334(){
  227.         assertEquals(true, conference.tripleCapacity());
  228.     }
  229.  
  230.     // TEST 9: Testing the tripleCapacity() method with more than 3333 capacity.
  231.     @Test
  232.     public void TripleCapacityNotLessThan3334(){
  233.         Conference conference2 = new Conference(3334);
  234.         assertEquals(false, conference2.tripleCapacity());
  235.  
  236.     }
  237.  
  238.     // TEST 10: Testing getter in Student Class
  239.     @Test
  240.     public void StudentName(){
  241.         Student student = new Student("Dzoce","Srce",Course.valueOf("OS"),22);
  242.         String name = student.getName();
  243.         assertEquals("Dzoce",name);
  244.     }
  245.  
  246.     // TEST 11: Testing getter in Student Class
  247.     @Test
  248.     public void StudentSurname(){
  249.         Student student = new Student("Dzoce","Srce",Course.valueOf("OS"),22);
  250.         String surname = student.getSurname();
  251.         assertEquals("Srce",surname);
  252.     }
  253.  
  254.     // TEST 12: Testing getter in Student Class
  255.     @Test
  256.     public void StudentCourse(){
  257.         Student student = new Student("Dzoce","Srce",Course.valueOf("OS"),22);
  258.         Course course = student.getCourse();
  259.         assertEquals("OS",course.name());
  260.     }
  261.  
  262.     // TEST 13: Testing getter in Student Class
  263.     @Test
  264.     public void StudentAge(){
  265.         Student student = new Student("Dzoce","Srce",Course.valueOf("OS"),22);
  266.         int age = student.getAge();
  267.         assertEquals(22,age);
  268.     }
  269.  
  270.     // TEST 14: Testing getter in Student Class
  271.     @Test
  272.     public void StudentInfo(){
  273.         Student student = new Student("Dzoce","Srce",Course.valueOf("OS"),22);
  274.         String info = student.toString();
  275.         assertEquals("Student{name='Dzoce', surname='Srce', course=OS, age=22}",info);
  276.     }
  277.     //We are unable to strongly kill the mutant in line 42 because the mutanted version is of the if statement ' this.capacity*3 >= 10000 '
  278.     //however there is no int for which this.capacity*3 > 10000 and this.capacity*3 >= 10000 return either the pair T-F or the pair F-T.
  279.     //thus by definition we are unable to kill the mutant.
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement