diff --git a/src/main_com_jeremy/CA/Main.java b/src/main_com_jeremy/CA/Main.java index 2192ac7..5784cd0 100644 --- a/src/main_com_jeremy/CA/Main.java +++ b/src/main_com_jeremy/CA/Main.java @@ -12,4 +12,4 @@ public static void main(String[] args) { } } - +// only for test for branch - develop diff --git a/src/main_com_jeremy/CA/Movie.java b/src/main_com_jeremy/CA/Movie.java index 04db64c..38e69cf 100644 --- a/src/main_com_jeremy/CA/Movie.java +++ b/src/main_com_jeremy/CA/Movie.java @@ -1,11 +1,10 @@ -/* -* Movie -*/ -package main_com_jeremy.CA; +package main_com_jeremy.CA;/* + * Movie + */ import java.util.Scanner; import java.util.InputMismatchException; -class Movie { +class Movie implements Comparable { // declaration protected String title; protected Double duration, rating; @@ -22,25 +21,11 @@ public Double getDuration() { } // setDuration -// public void setDuration() { -// System.out.println("Please enter duration(mins) of the movie"); -// this.duration = doubleInput(); -// -// } - public void setDuration() { - Double duration ; - while(true){ - System.out.println("Please enter duration(mins between (0-1000)) of the movie"); - duration = doubleInput(); - if(duration>0 && duration<=1000){ - this.duration = duration; - break; - }// if - } + System.out.println("Please enter duration(mins) of the movie"); + this.duration = input(Double.class); } - public Double getRating() { return rating; } @@ -50,7 +35,7 @@ public void setRating() { Double rate ; while(true){ System.out.println("Please enter rating(0-10) of the movie "); - rate = doubleInput(); + rate = input(Double.class); if(rate>=0 && rate<=10){ this.rating = rate; break; @@ -67,7 +52,7 @@ public String getTitle() { // setTitle public void setTitle() { System.out.println("Please enter title of the movie"); - this.title = stringInput(); + this.title = input(String.class); } public int getYearOfRelease() { @@ -79,7 +64,7 @@ public void setYearOfRelease() { int year ; while(true){ System.out.println("Please enter release year(1900-2024) of the movie "); - year = intInput(); + year = input(Integer.class); if(year>=1900 && year<=2024){ this.yearOfRelease = year; break; @@ -90,6 +75,8 @@ public void setYearOfRelease() { }// setYearOfRelease // toString + + @Override public String toString() { return "Movie{" + @@ -100,45 +87,31 @@ public String toString() { '}'; } - - // input - public int intInput() { - - while (true) { - try { - System.out.print("Enter an Int: "); - return scanner.nextInt(); - } - catch (InputMismatchException e) { - System.out.println("Invalid input, Please enter an integer !"); - scanner.nextLine(); // Clear the invalid input from the scanner - } - } - } - - public Double doubleInput() { - - while (true) { - try { - System.out.print("Enter an double: "); - return scanner.nextDouble(); - } - catch (InputMismatchException e) { - System.out.println("Invalid input, Please enter an Double !"); - scanner.nextLine(); // Clear the invalid input from the scanner - } - } + @Override + public int compareTo(Movie other) { + // Implement natural ordering based on duration + return Double.compare(this.duration, other.duration); } - public String stringInput() { + // input + public T input(Class c) { while (true) { try { - System.out.print("Enter an string: "); - return scanner.nextLine(); + if(c==Double.class) { + System.out.print("Enter an double: "); + return c.cast(scanner.nextDouble()); + } + else if(c==String.class){ + System.out.print("Enter an string: "); + return c.cast(scanner.nextLine()); + }else if(c==Integer.class) { + System.out.print("Enter an Int: "); + return c.cast(scanner.nextInt()); + } } catch (InputMismatchException e) { - System.out.println("Invalid input, Please enter an string !"); + System.out.println("Invalid input. Please enter an "+c); scanner.nextLine(); // Clear the invalid input from the scanner } } diff --git a/src/main_com_jeremy/CA/MovieArray.java b/src/main_com_jeremy/CA/MovieArray.java index 263d2b7..f633bce 100644 --- a/src/main_com_jeremy/CA/MovieArray.java +++ b/src/main_com_jeremy/CA/MovieArray.java @@ -1,15 +1,13 @@ -/* -* MovieArray - */ package main_com_jeremy.CA; -import java.util.Objects; -// +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + public class MovieArray { // declaration - Movie[] movieArrayList = new Movie[15]; - public int size = 0; // Initialize the size to 0 - + ArrayList movieArrayList = new ArrayList<>(); // loop @@ -18,30 +16,30 @@ public void myLoop(){ // try to reuse input<> Movie reuseInput = new Movie(); do { - System.out.println("1. Enter the details of an item \n" + - "2. Display the details of the last entered movie \n" + - "3. Display the Movie with the smallest duration \n" + - "4. Display the Movie with the longest duration \n" + - "5. Display the average duration of all the Movies entered until that point \n" + - "6. Exit application \n" + "Please enter 1-6 to select \n*********************************"); + System.out.println("1 – Enter the details of an item \n" + + "2 – Display the details of the last entered movie \n" + + "3 – Display the Movie with the smallest duration \n" + + "4 – Display the Movie with the longest duration \n" + + "5 – Display the average duration of all the Movies entered until that point \n" + + "6 – Exit application \n" + "Please enter 1-6 to select \n*********************************"); // I try to reuse the generic functions in Movie, but I don't know better method // Here I make an instance of Movie then , I can reuse input<> do { - selection = reuseInput.intInput(); + selection = reuseInput.input(Integer.class); } while (selection <= 0 || selection >= 7); switch (selection) { case 1: // Enter the details of an item - movieArrayList[size++] = makeInstance(); + movieArrayList.add(makeInstance()); System.out.println("*********************************"); break; case 2: // The details of the last entered movie - if (size != 0) { - Movie item = movieArrayList[size-1]; + if (movieArrayList != null && !movieArrayList.isEmpty()) { + Movie item = movieArrayList.get(movieArrayList.size()-1); System.out.println("Last entered movie is \n"+ item.toString()); }else{ @@ -51,7 +49,7 @@ public void myLoop(){ break; case 3: // The Movie with the smallest duration - if (size != 0) { + if (movieArrayList != null && !movieArrayList.isEmpty()) { System.out.println("The Movie with the smallest duration is "); printSmallDurationMovie(); @@ -62,7 +60,7 @@ public void myLoop(){ break; case 4: // The Movie with the longest duration - if (size != 0) { + if (movieArrayList != null && !movieArrayList.isEmpty()) { System.out.println("The Movie with the longest duration is "); printLongestDurationMovie(); @@ -73,7 +71,7 @@ public void myLoop(){ break; case 5: // Average duration of all the Movies entered until that point - if (size != 0) { + if (movieArrayList != null && !movieArrayList.isEmpty()) { System.out.println("Average duration of all the Movies is "); printAverageDuration(); @@ -107,51 +105,53 @@ private Movie makeInstance(){ // printSmallestDurationMovie private void printSmallDurationMovie(){ // Find the minimum duration using Collections.min - Double minDuration = movieArrayList[0].getDuration(); - for (int i=0; i moviesWithSmallestDuration = new ArrayList<>(); + for (Movie movie : movieArrayList) { + if (movie.getDuration() == minDuration) { + moviesWithSmallestDuration.add(movie); } } + // Print all movies with the smallest duration + for (Movie movie : moviesWithSmallestDuration) { + System.out.println(movie.toString()); + } } // printLongestDurationMovie private void printLongestDurationMovie(){ - // Find the maximum duration using Collections.min - Double maxDuration = movieArrayList[0].getDuration(); - for (int i=0; i=maxDuration){ - maxDuration = movieArrayList[i].getDuration(); - } - - } + // Find the minimum duration using Collections.min + double minDuration = Collections.max(movieArrayList, durationComparator).getDuration(); - // Collect movies with the maximum duration - for (int i=0; i moviesWithLongestDuration = new ArrayList<>(); + for (Movie movie : movieArrayList) { + if (movie.getDuration() == minDuration) { + moviesWithLongestDuration.add(movie); } } + // Print all movies with the longest duration + for (Movie movie : moviesWithLongestDuration) { + System.out.println(movie.toString()); + } } // printAverageDuration private void printAverageDuration(){ Double totalDuration = 0.0; - for (int i=0; i durationComparator = Comparator.comparingDouble(Movie::getDuration); + + } diff --git a/src/main_com_jeremy/CA/test.md b/src/main_com_jeremy/CA/test.md new file mode 100644 index 0000000..2f57a79 --- /dev/null +++ b/src/main_com_jeremy/CA/test.md @@ -0,0 +1 @@ +#### this is only for test of new branch develop \ No newline at end of file diff --git a/src/main_com_jeremy/coreClass/stringTest.java b/src/main_com_jeremy/coreClass/stringTest.java deleted file mode 100644 index 8a05496..0000000 --- a/src/main_com_jeremy/coreClass/stringTest.java +++ /dev/null @@ -1,106 +0,0 @@ -package main_com_jeremy.coreClass; -import org.junit.jupiter.api.Test; -import java.util.Scanner; - -import java.util.Arrays; - -public class stringTest {} - - -class stringTest1 { - String s1 = "Hello"; - String s2 = new String(new char[] {'H', 'e', 'l', 'l', 'o'}); - @Test - public void printString1(){ - System.out.println("s1: "+ s1); - System.out.println("location in memory: "+ s1); - System.out.println("s2: "+ s2); - System.out.println("=======use toUpperCase()===================="); - s1 = s1.toUpperCase(); - System.out.println("s1: "+ s1); - } - - -} -class StringArray{ - private String[] words; - private String sentences="I don't know what is that represent. "; - - //unitTest - @Test - public void print1(){ - setWords(); - System.out.println(Arrays.toString(words)); - - } - - @Test - public void print2(){ - Scanner s1 = new Scanner(System.in); - System.out.println("Please enter a word"); - String word = "app"; - - for (int i =0; i< word.length(); i++){ - if(word.charAt(i)=='a') { - System.out.print('a'); - }else{ - System.out.print("*"); - } - } - - } - - public StringArray() {} - - - public String[] getWords() { - return words; - } - - public void setWords() { - this.words = sentences.split(" "); - } - - public String getSentences() { - return sentences; - } - - public void setSentences(String sentences) { - this.sentences = sentences; - } -} - - -class Employee{ - private String name; - private int age; - - public Employee(String name, int age) { - this.name = name; - this.age = age; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public int getAge() { - return age; - } - - public void setAge(int age) { - this.age = age; - } - - @Override - public String toString() { - return "Employee{" + - "name='" + name + '\'' + - ", age=" + age + - '}'; - } -} \ No newline at end of file diff --git a/src/main_com_jeremy/debug/Main.java b/src/main_com_jeremy/debug/Main.java deleted file mode 100644 index 6b221a0..0000000 --- a/src/main_com_jeremy/debug/Main.java +++ /dev/null @@ -1,13 +0,0 @@ -package main_com_jeremy.debug; - -public class Main { - public static void main(String [] args){ - - int[] arr = {1, 2, 3}; - for(int i =0; i<= args.length; i++){ - System.out.println(arr[i]); - } - System.out.println("finished loop! "); - - } -}