diff --git a/README.md b/README.md index e9b0a9e..70c17bb 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ [![Build Status](https://travis-ci.org/Krishnom/UdemyJavaCourse.svg?branch=master)](https://travis-ci.org/Krishnom/UdemyJavaCourse) - +[![Coverage Status](https://coveralls.io/repos/github/Krishnom/UdemyJavaCourse/badge.svg?branch=master)](https://coveralls.io/github/Krishnom/UdemyJavaCourse?branch=master) # UdemyJavaCourse Course related code diff --git a/Section10/src/main/java/generics/challenge/LeagueImplMain.java b/Section10/src/main/java/generics/challenge/LeagueImplMain.java index 4db98f2..65f307f 100644 --- a/Section10/src/main/java/generics/challenge/LeagueImplMain.java +++ b/Section10/src/main/java/generics/challenge/LeagueImplMain.java @@ -32,8 +32,8 @@ public static void main(String[] args) { } System.out.println("Match #" + match); - Team team1 = worldCupLeague.getTeam(team1Code); - Team team2 = worldCupLeague.getTeam(team2Code); + Team team1 = worldCupLeague.getTeam(team1Code); + Team team2 = worldCupLeague.getTeam(team2Code); int team1Score = (int) (Math.random() * 400); int team2Score = (int) (Math.random() * 400); diff --git a/Section10/src/main/java/generics/example/Team.java b/Section10/src/main/java/generics/example/Team.java index a865a4d..728bcb2 100644 --- a/Section10/src/main/java/generics/example/Team.java +++ b/Section10/src/main/java/generics/example/Team.java @@ -75,12 +75,6 @@ public int getRanking() { @Override public int compareTo(Team opponentTeam) { - if (this.getRanking() > opponentTeam.getRanking()) { - return -1; - } else if (this.getRanking() < opponentTeam.getRanking()) { - return 1; - } else { - return 0; - } + return Integer.compare(opponentTeam.getRanking(), this.getRanking()); } } diff --git a/Section10/src/main/java/generics/example/TeamMain.java b/Section10/src/main/java/generics/example/TeamMain.java index bf517aa..d9ebf56 100644 --- a/Section10/src/main/java/generics/example/TeamMain.java +++ b/Section10/src/main/java/generics/example/TeamMain.java @@ -57,7 +57,7 @@ private static void playCricketMatch() { private static void announceWinner(Team team1, Team team2) { int results = team1.compareTo(team2); - if (results == 1) { + if (results > 0) { System.out.println(team2.getName() + " Wins the series"); } else if (results == -1) { System.out.println(team1.getName() + " Wins the series"); diff --git a/Section10/src/test/java/generics/example/TeamTest.java b/Section10/src/test/java/generics/example/TeamTest.java index b7de7c4..2560b9f 100644 --- a/Section10/src/test/java/generics/example/TeamTest.java +++ b/Section10/src/test/java/generics/example/TeamTest.java @@ -10,14 +10,14 @@ class TeamTest { @Test void cricketPlayerIsAbleToJoinCricketTeam() { - Team cricketPlayerTeam = new Team("Cricket Team"); + Team cricketPlayerTeam = new Team<>("Cricket Team"); cricketPlayerTeam.addPlayer(new CricketPlayer("A Cricket Player")); } @Test void teamWithHighestRankingWins() { - Team cricketPlayerTeamA = new Team("Cricket Team A"); - Team cricketPlayerTeamB = new Team("Cricket Team B"); + Team cricketPlayerTeamA = new Team<>("Cricket Team A"); + Team cricketPlayerTeamB = new Team<>("Cricket Team B"); cricketPlayerTeamA.matchResults(cricketPlayerTeamB, 340, 330); //A wins cricketPlayerTeamA.matchResults(cricketPlayerTeamB, 330, 330); //Draw //A will win with 2 points @@ -26,8 +26,8 @@ void teamWithHighestRankingWins() { @Test void everyWinGivesPointsToWinningTeam() { - Team cricketPlayerTeamA = new Team("Cricket Team A"); - Team cricketPlayerTeamB = new Team("Cricket Team B"); + Team cricketPlayerTeamA = new Team<>("Cricket Team A"); + Team cricketPlayerTeamB = new Team<>("Cricket Team B"); cricketPlayerTeamA.matchResults(cricketPlayerTeamB, 340, 330); //A wins //A will win with WINNING_PONTS points assertEquals(WINNING_POINTS, cricketPlayerTeamA.getPoints()); diff --git a/Section11Challenge/pom.xml b/Section11Challenge/pom.xml new file mode 100644 index 0000000..b7a7cd6 --- /dev/null +++ b/Section11Challenge/pom.xml @@ -0,0 +1,23 @@ + + + 4.0.0 + + + udemyjavacourse + Section11 + 0.0.1 + compile + + + + + udemyjavacourse + JavaUdemyCourse + 0.0.1 + + Section11Challenge + + + \ No newline at end of file diff --git a/Section11Challenge/src/main/java/Main.java b/Section11Challenge/src/main/java/Main.java new file mode 100644 index 0000000..7518421 --- /dev/null +++ b/Section11Challenge/src/main/java/Main.java @@ -0,0 +1,30 @@ +import challenge.packages.Series; + +public class Main { + public static void main(String[] args) { + int[] series = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + + //Print nth sum + System.out.println("----------------Sum Series-------------------------"); + for (int i : series) { + System.out.println("Sum of first " + i + " numbers is " + Series.nSum(i)); + } + System.out.println("------------------------------------------------"); + + + //Print nth sum + System.out.println("----------------Factorial series-------------------------"); + for (int i : series) { + System.out.println("Factorial(" + i + ") : " + Series.factorial(i)); + } + System.out.println("------------------------------------------------"); + + //Print fibonacci series + System.out.println("----------------Fibonacci Series-------------------------"); + for (int i : series) { + System.out.println("Fibonacci(" + i + ") : " + Series.fibonacci(i)); + } + System.out.println("------------------------------------------------"); + + } +} diff --git a/Section12/pom.xml b/Section12/pom.xml new file mode 100644 index 0000000..af9d697 --- /dev/null +++ b/Section12/pom.xml @@ -0,0 +1,15 @@ + + + 4.0.0 + + + udemyjavacourse + JavaUdemyCourse + 0.0.1 + + Section12 + + + \ No newline at end of file diff --git a/Section12/src/main/java/exercise/collection/Theatre.java b/Section12/src/main/java/exercise/collection/Theatre.java new file mode 100644 index 0000000..6bad94a --- /dev/null +++ b/Section12/src/main/java/exercise/collection/Theatre.java @@ -0,0 +1,102 @@ +package exercise.collection; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class Theatre { + private final String theatreName; + List seats = new ArrayList<>(); + + public Theatre(String theatreName, int numOfRows, int numOfSeatsPerRow) { + this.theatreName = theatreName; + char lastSeat = (char) ('A' + numOfRows); + for (char row = 'A'; row < lastSeat; row++) { + for (int i = 1; i <= numOfSeatsPerRow; i++) { + seats.add(new Seat(row + String.format("%02d", i))); + } + } + /* for (Seat seat : seats) { + System.out.println(seat.getName()); + } + */ + } + + public String getTheatreName() { + return theatreName; + } + + public boolean reserveSeat(String seatNumber) { + Seat seat = getSeat(seatNumber); + if (seat != null) { + return seat.reserve(); + } else { + System.out.println("Seat " + seatNumber + " not found."); + return false; + } + } + + + public boolean cancelSeat(String seatNumber) { + Seat seat = getSeat(seatNumber); + if (seat != null) { + return seat.cancel(); + } else { + System.out.println("Seat " + seatNumber + " not found."); + return false; + } + } + + private Seat getSeat(String seatName) { + int foundSeat = Collections.binarySearch(seats, new Seat(seatName), null); //this will return index of seat if found + if (foundSeat >= 0) { + return seats.get(foundSeat); + } else { + return null; + } + +// for (Seat seat : seats) { +// if (seat.getName().equals(seatName)) { +// return seat; +// } +// } +// return null; + } + + private static class Seat implements Comparable { + private String name; + private boolean reserved = false; + + public Seat(String seatName) { + this.name = seatName; + } + + public String getName() { + return name; + } + + public boolean cancel() { + if (reserved) { + reserved = false; + System.out.println("Seat " + this.name + " is successfully cancelled"); + return true; + } else { + return false; + } + } + + public boolean reserve() { + if (!reserved) { + reserved = true; + System.out.println("Seat " + this.name + " is successfully reserved"); + return true; + } + return false; + } + + @Override + public int compareTo(Seat seat) { + return this.name.compareToIgnoreCase(seat.getName()); + } + } +} diff --git a/Section12/src/main/java/exercise/collection/TheatreMain.java b/Section12/src/main/java/exercise/collection/TheatreMain.java new file mode 100644 index 0000000..cda7a08 --- /dev/null +++ b/Section12/src/main/java/exercise/collection/TheatreMain.java @@ -0,0 +1,21 @@ +package exercise.collection; + +public class TheatreMain { + public static void main(String[] args) throws Exception { + Theatre theatre = new Theatre("Kalptaru", 10, 12); + + System.out.println("Welcome to " + theatre.getTheatreName()); + + theatre.reserveSeat("A10"); + theatre.reserveSeat("A02"); + theatre.reserveSeat("H12"); + theatre.reserveSeat("J12"); +// theatre.reserveSeat("K-12"); //Invalid seat + + theatre.cancelSeat("A10"); + //try to cancel seat that is not reserved + if (!theatre.cancelSeat("A11")) { + System.out.println("Seat Cancellation failed. Possibly because seat is already reserved"); + } + } +} diff --git a/Section12/src/test/java/exercise/collection/TheatreTest.java b/Section12/src/test/java/exercise/collection/TheatreTest.java new file mode 100644 index 0000000..62d5aff --- /dev/null +++ b/Section12/src/test/java/exercise/collection/TheatreTest.java @@ -0,0 +1,53 @@ +package exercise.collection; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class TheatreTest { + private Theatre theatre; + + @BeforeEach + public void setup() { + theatre = new Theatre("Dummy", 10, 10); + } + + @Test + public void seatReservationSuccessfulWhenSeatIsUnreserved() throws Exception { + assertTrue(theatre.reserveSeat("A01")); + } + + + @Test + public void seatReservationFailsWhenASeatIsReserved() throws Exception { + theatre.reserveSeat("A01"); + assertFalse(theatre.reserveSeat("A01")); + } + + + @Test + public void seatReservationFailsWhenSeatNumberIsInvalid() throws Exception { + assertFalse(theatre.reserveSeat("A#1")); + } + + @Test + public void seatCancellationSuccessfulWhenSeatIsReserved() throws Exception { + theatre.reserveSeat("A01"); + assertTrue(theatre.cancelSeat("A01")); + } + + + @Test + public void seatCancellationFailsWhenASeatIsUnReserved() throws Exception { + assertFalse(theatre.cancelSeat("A01")); + } + + + @Test + public void seatCancellationFailsWhenSeatNumberIsInvalid() throws Exception { + assertFalse(theatre.cancelSeat("A#1")); + } + +} \ No newline at end of file diff --git a/Section4/src/main/java/exercise3/BarkingDog.java b/Section4/src/main/java/exercise3/BarkingDog.java index 62743d9..b3ef594 100644 --- a/Section4/src/main/java/exercise3/BarkingDog.java +++ b/Section4/src/main/java/exercise3/BarkingDog.java @@ -1,16 +1,12 @@ package exercise3; + class BarkingDog { - public static boolean shouldWakeUp(boolean barking, int hourOfDay) - { + public static boolean shouldWakeUp(boolean barking, int hourOfDay) { boolean wakeUp; - if(hourOfDay < 0 || hourOfDay > 23){ - wakeUp = false; - } else if(barking && (hourOfDay < 8 || hourOfDay > 22)){ - wakeUp = true; - } else { + if (hourOfDay < 0 || hourOfDay > 23) { wakeUp = false; - } + } else wakeUp = barking && (hourOfDay < 8 || hourOfDay > 22); return wakeUp; } diff --git a/Section4/src/main/java/exercise4/LeapYear.java b/Section4/src/main/java/exercise4/LeapYear.java index 0442523..ffdf863 100644 --- a/Section4/src/main/java/exercise4/LeapYear.java +++ b/Section4/src/main/java/exercise4/LeapYear.java @@ -11,10 +11,8 @@ public static boolean isLeapYear(int year){ leapYear = false; }else if(divisibleBy4 && !divisibleBy100){ leapYear = true; - }else if(divisibleBy400){ - leapYear = true; - }else{ - leapYear = false; + } else { + leapYear = divisibleBy400; } return leapYear; diff --git a/Section6/src/main/java/challenge/inheritance/Car.java b/Section6/src/main/java/challenge/inheritance/Car.java index 6fe0156..0001827 100644 --- a/Section6/src/main/java/challenge/inheritance/Car.java +++ b/Section6/src/main/java/challenge/inheritance/Car.java @@ -7,6 +7,22 @@ public class Car extends Vehicle { private int doors; private boolean isManual; + public int getGears() { + return gears; + } + + public int getWheels() { + return wheels; + } + + public int getDoors() { + return doors; + } + + public boolean isManual() { + return isManual; + } + public Car(String name, String size, int gears, int wheels, int doors, boolean isManual) { super(name, size); this.gears = gears; diff --git a/Section6/src/main/java/challenge/inheritance/Ferrari.java b/Section6/src/main/java/challenge/inheritance/Ferrari.java index 29d17af..4353715 100644 --- a/Section6/src/main/java/challenge/inheritance/Ferrari.java +++ b/Section6/src/main/java/challenge/inheritance/Ferrari.java @@ -14,11 +14,11 @@ public void accelerate(int rate) { if (newVelocity <= 0) { stop(); changeGear(0); - } else if (newVelocity > 0 && newVelocity < 10) { + } else if (newVelocity < 10) { changeGear(1); - } else if (newVelocity >= 10 && newVelocity < 30) { + } else if (newVelocity < 30) { changeGear(2); - } else if (newVelocity >= 30 && newVelocity < 60) { + } else if (newVelocity < 60) { changeGear(3); } else if (newVelocity > 60 && newVelocity < 90) { changeGear(4); diff --git a/Section6/src/main/java/challenge/lecture78/Main.java b/Section6/src/main/java/challenge/lecture78/Main.java index 194236d..909adb4 100644 --- a/Section6/src/main/java/challenge/lecture78/Main.java +++ b/Section6/src/main/java/challenge/lecture78/Main.java @@ -5,6 +5,7 @@ public class Main { public static void main(String[] args) { BankAccount account = new BankAccount(); account.deposit(29000); + account.deposit(23000); account.withdraw(15000); System.out.println(account.toString()); diff --git a/Section6/src/main/java/exercise30/Person.java b/Section6/src/main/java/exercise30/Person.java index 032ff20..5089539 100644 --- a/Section6/src/main/java/exercise30/Person.java +++ b/Section6/src/main/java/exercise30/Person.java @@ -6,15 +6,11 @@ public class Person { private String lastName; public boolean isTeen() { - if (age < 20 && age > 12) { - return true; - } else { - return false; - } + return age < 20 && age > 12; } public String getFullName() { - String name = ""; + String name; if (firstName.isEmpty() && lastName.isEmpty()) { name = ""; } else if (firstName.isEmpty()) { diff --git a/Section6/src/main/java/exercise32/Point.java b/Section6/src/main/java/exercise32/Point.java index 5d2fe86..062109a 100644 --- a/Section6/src/main/java/exercise32/Point.java +++ b/Section6/src/main/java/exercise32/Point.java @@ -21,8 +21,7 @@ public double distance(int x, int y) { //D=√(x2−x1)^2+(y2−y1)^2 int xFactor = x - this.getX(); int yFactor = y - this.getY(); - double distance = Math.sqrt(xFactor * xFactor + yFactor * yFactor); - return distance; + return Math.sqrt(xFactor * xFactor + yFactor * yFactor); } public double distance() { diff --git a/Section6/src/main/java/exercise36/Cuboid.java b/Section6/src/main/java/exercise36/Cuboid.java index 8de75a5..b65503e 100644 --- a/Section6/src/main/java/exercise36/Cuboid.java +++ b/Section6/src/main/java/exercise36/Cuboid.java @@ -6,11 +6,7 @@ public class Cuboid extends Rectangle { public Cuboid(double width, double length, double height) { super(width, length); - if (height < 0.0) { - this.height = 0.0; - } else { - this.height = height; - } + this.height = Math.max(height, 0.0); } public double getHeight() { diff --git a/Section6/src/test/java/exercise30/PersonTest.java b/Section6/src/test/java/exercise30/PersonTest.java index 2b7b02d..87e9f38 100644 --- a/Section6/src/test/java/exercise30/PersonTest.java +++ b/Section6/src/test/java/exercise30/PersonTest.java @@ -38,13 +38,13 @@ public void personWithAge18isTeen() { @Test public void fullNameIsFirstNameWhenLastNameIsEmpty() { person.setLastName(""); - assertTrue(person.getFullName().equals(person.getFirstName())); + assertEquals(person.getFullName(), person.getFirstName()); } @Test public void fullNameIsLastNameWhenFirstNameIsEmpty() { person.setFirstName(""); - assertTrue(person.getFullName().equals(person.getLastName())); + assertEquals(person.getFullName(), person.getLastName()); } @Test diff --git a/Section7/src/main/java/challenge/encapsulation/Printer.java b/Section7/src/main/java/challenge/encapsulation/Printer.java index 5a5bed8..8352856 100644 --- a/Section7/src/main/java/challenge/encapsulation/Printer.java +++ b/Section7/src/main/java/challenge/encapsulation/Printer.java @@ -14,7 +14,7 @@ public Printer(boolean isDuplex, int maxPrintCapacity) { } public void printPage(int pages) { - int pagesNeeded = pages; + int pagesNeeded; if (tonerLevel < (double) pages * perPageTonerCost) { System.out.println("Replace toner to print pages"); diff --git a/Section7/src/main/java/challenge/polymorphism/Car.java b/Section7/src/main/java/challenge/polymorphism/Car.java index a5ab084..7f85d07 100644 --- a/Section7/src/main/java/challenge/polymorphism/Car.java +++ b/Section7/src/main/java/challenge/polymorphism/Car.java @@ -2,11 +2,31 @@ public class Car { protected String engine; - String model; + private String model; private int wheels; private int cylinders; private String name; + public String getEngine() { + return engine; + } + + public String getModel() { + return model; + } + + public int getWheels() { + return wheels; + } + + public int getCylinders() { + return cylinders; + } + + public String getName() { + return name; + } + public Car(int cylinders, String name) { this.cylinders = cylinders; this.name = name; diff --git a/Section7/src/main/java/challenge/polymorphism/Main.java b/Section7/src/main/java/challenge/polymorphism/Main.java index 2cd45f2..75defb4 100644 --- a/Section7/src/main/java/challenge/polymorphism/Main.java +++ b/Section7/src/main/java/challenge/polymorphism/Main.java @@ -4,6 +4,7 @@ public class Main { public static void main(String[] args) { Car car = getRandomCar(); + assert car != null; car.startEngine(); car.accelerate(); car.brake(); diff --git a/Section7/src/main/java/examples/composition/Case.java b/Section7/src/main/java/examples/composition/Case.java index 7f5bd1e..fbb8f2a 100644 --- a/Section7/src/main/java/examples/composition/Case.java +++ b/Section7/src/main/java/examples/composition/Case.java @@ -5,6 +5,18 @@ public class Case { private String manufacturer; private int power; + public Dimension getDimension() { + return dimension; + } + + public String getManufacturer() { + return manufacturer; + } + + public int getPower() { + return power; + } + public Case(Dimension dimension, String manufacturer, int power) { this.dimension = dimension; this.manufacturer = manufacturer; diff --git a/Section7/src/main/java/examples/composition/Dimension.java b/Section7/src/main/java/examples/composition/Dimension.java index 4f702a7..a3c6d7e 100644 --- a/Section7/src/main/java/examples/composition/Dimension.java +++ b/Section7/src/main/java/examples/composition/Dimension.java @@ -10,4 +10,16 @@ public Dimension(int width, int height, int length) { this.height = height; this.length = length; } + + public int getWidth() { + return width; + } + + public int getHeight() { + return height; + } + + public int getLength() { + return length; + } } diff --git a/Section7/src/main/java/examples/polymorphism/Main.java b/Section7/src/main/java/examples/polymorphism/Main.java index 5d539a4..3d802f7 100644 --- a/Section7/src/main/java/examples/polymorphism/Main.java +++ b/Section7/src/main/java/examples/polymorphism/Main.java @@ -3,6 +3,7 @@ public class Main { public static void main(String[] args) { Movie movie = getRandomMovie(); + assert movie != null; System.out.println("Playing : " + movie.getName()); movie.plot(); } diff --git a/Section8/src/main/java/challenge/arraylist/Mobile.java b/Section8/src/main/java/challenge/arraylist/Mobile.java index df2db4a..6875422 100644 --- a/Section8/src/main/java/challenge/arraylist/Mobile.java +++ b/Section8/src/main/java/challenge/arraylist/Mobile.java @@ -54,8 +54,7 @@ public String getContact(String name) { } private Contact findContact(String name) { - for (int i = 0; i < contacts.size(); i++) { - Contact contact = contacts.get(i); + for (Contact contact : contacts) { if (contact.getName().equals(name)) { return contact; } @@ -67,8 +66,8 @@ public void printAllContacts() { System.out.println("---------------------------------------"); System.out.println("My contact list"); System.out.println(self.toString()); - for (int i = 0; i < contacts.size(); i++) { - System.out.println(contacts.get(i).toString()); + for (Contact contact : contacts) { + System.out.println(contact.toString()); } } } diff --git a/Section8/src/main/java/challenge/arrays/MinElement.java b/Section8/src/main/java/challenge/arrays/MinElement.java index cf23265..939c10d 100644 --- a/Section8/src/main/java/challenge/arrays/MinElement.java +++ b/Section8/src/main/java/challenge/arrays/MinElement.java @@ -27,9 +27,9 @@ static int[] readInteger(int count) { static int findMin(int[] array) { int min = Integer.MAX_VALUE; - for (int i = 0; i < array.length; i++) { - if (min > array[i]) { - min = array[i]; + for (int value : array) { + if (min > value) { + min = value; } } return min; diff --git a/Section8/src/main/java/challenge/arrays/SortIntegerArray.java b/Section8/src/main/java/challenge/arrays/SortIntegerArray.java index cf68f22..cd45da3 100644 --- a/Section8/src/main/java/challenge/arrays/SortIntegerArray.java +++ b/Section8/src/main/java/challenge/arrays/SortIntegerArray.java @@ -7,11 +7,18 @@ public class SortIntegerArray { public static void main(String[] args) { int[] array = getIntegerArray(5); + int[] array2 = getIntegerArray(15); printArray(array); sortArray(array); - System.out.println("Sorted array"); + System.out.println("Sorted array1"); printArray(array); + + System.out.println("Array 2"); + printArray(array2); + sortArray(array2); + System.out.println("Sorted array2"); + printArray(array2); } //using selection sort diff --git a/Section8/src/main/java/challenge/autoboxing/Bank.java b/Section8/src/main/java/challenge/autoboxing/Bank.java index 2ed9736..4c7ff1d 100644 --- a/Section8/src/main/java/challenge/autoboxing/Bank.java +++ b/Section8/src/main/java/challenge/autoboxing/Bank.java @@ -4,7 +4,7 @@ public class Bank { private final String name; - ArrayList branches = new ArrayList(); + ArrayList branches = new ArrayList<>(); public Bank(String name) { this.name = name; @@ -32,9 +32,9 @@ public boolean addCustomer(String branchName, String customerName, double initia } private Branch findBranch(String branchName) { - for (int i = 0; i < branches.size(); i++) { - if (branchName.equals(branches.get(i).getName())) { - return branches.get(i); + for (Branch branch : branches) { + if (branchName.equals(branch.getName())) { + return branch; } } return null; @@ -51,8 +51,7 @@ public void transact(String customerName, double amount) { } public Customer getCustomer(String customerName) { - for (int i = 0; i < branches.size(); i++) { - Branch branch = branches.get(i); + for (Branch branch : branches) { if (branch.findCustomer(customerName) != null) { return branch.findCustomer(customerName); } @@ -70,8 +69,8 @@ public void printCustomerList(String branchName, boolean showTransactions) { } public void printCustomerList(boolean showTransactions) { - for (int i = 0; i < branches.size(); i++) { - branches.get(i).printCustomersList(showTransactions); + for (Branch branch : branches) { + branch.printCustomersList(showTransactions); } } } diff --git a/Section8/src/main/java/challenge/autoboxing/Branch.java b/Section8/src/main/java/challenge/autoboxing/Branch.java index 4a35df9..5f76d42 100644 --- a/Section8/src/main/java/challenge/autoboxing/Branch.java +++ b/Section8/src/main/java/challenge/autoboxing/Branch.java @@ -4,13 +4,13 @@ public class Branch { private String name; - private ArrayList customers = new ArrayList(); + private ArrayList customers; private String location; public Branch(String name, String location) { this.name = name; this.location = location; - customers = new ArrayList(); + customers = new ArrayList<>(); } @Override @@ -51,9 +51,9 @@ public Customer findCustomer(Customer customer) { } public Customer findCustomer(String customerName) { - for (int i = 0; i < customers.size(); i++) { - if (customerName.equals(customers.get(i).getName())) { - return customers.get(i); + for (Customer customer : customers) { + if (customerName.equals(customer.getName())) { + return customer; } } return null; diff --git a/Section8/src/main/java/challenge/autoboxing/Customer.java b/Section8/src/main/java/challenge/autoboxing/Customer.java index f067f5b..71dace0 100644 --- a/Section8/src/main/java/challenge/autoboxing/Customer.java +++ b/Section8/src/main/java/challenge/autoboxing/Customer.java @@ -51,8 +51,8 @@ public String toString() { } public void printTransactions() { - for (int i = 0; i < transactions.size(); i++) { - System.out.println(transactions.get(i)); + for (Double transaction : transactions) { + System.out.println(transaction); } } diff --git a/Section8/src/main/java/challenge/linkedlist/Albums.java b/Section8/src/main/java/challenge/linkedlist/Albums.java index 3a822e4..0230224 100644 --- a/Section8/src/main/java/challenge/linkedlist/Albums.java +++ b/Section8/src/main/java/challenge/linkedlist/Albums.java @@ -1,7 +1,6 @@ package challenge.linkedlist; import java.util.ArrayList; -import java.util.Iterator; import java.util.LinkedList; import java.util.ListIterator; @@ -32,10 +31,9 @@ void addSongToPlaylist(int trackNumber, LinkedList playlist) { } public boolean addSong(Song song) { - Iterator songIterator = songs.iterator(); - while (songIterator.hasNext()) { - if (songIterator.next().getTitle().equals(song.getTitle())) { + for (Song value : songs) { + if (value.getTitle().equals(song.getTitle())) { return false; } } diff --git a/Section8/src/main/java/example/linkedlist/LinkedListMain.java b/Section8/src/main/java/example/linkedlist/LinkedListMain.java index 8d84b28..61a8ff2 100644 --- a/Section8/src/main/java/example/linkedlist/LinkedListMain.java +++ b/Section8/src/main/java/example/linkedlist/LinkedListMain.java @@ -90,9 +90,8 @@ private static void addInOrder(String city) { listIterator.previous(); listIterator.add(city); return; - } else { - //Nothing to do here as we have already moved to next element while comparing the city - } + } //Nothing to do here as we have already moved to next element while comparing the city + } //If no element is lexicographically greater than the city argument. add in the last listIterator.add(city); diff --git a/Section8/src/test/java/challenge/arrays/MinElementTest.java b/Section8/src/test/java/challenge/arrays/MinElementTest.java index f1670ee..cffd7ba 100644 --- a/Section8/src/test/java/challenge/arrays/MinElementTest.java +++ b/Section8/src/test/java/challenge/arrays/MinElementTest.java @@ -9,13 +9,13 @@ public class MinElementTest extends TestHelper { public void checkReadInputsWorks() { String input = "1\r\n2\r\n3\r\n4\r\n"; setInputContent(input); - int array[] = MinElement.readInteger(4); + int[] array = MinElement.readInteger(4); assertEquals(1, MinElement.findMin(array)); } @Test public void findMinWorksForAllInteger() { - int array[] = {1, -1, 0, 1212}; + int[] array = {1, -1, 0, 1212}; assertEquals(-1, MinElement.findMin(array)); } } \ No newline at end of file diff --git a/Section9/src/main/java/challenge/abstractclasses/MyLinkedList.java b/Section9/src/main/java/challenge/abstractclasses/MyLinkedList.java index 8827a77..da51384 100644 --- a/Section9/src/main/java/challenge/abstractclasses/MyLinkedList.java +++ b/Section9/src/main/java/challenge/abstractclasses/MyLinkedList.java @@ -144,7 +144,7 @@ public String toString() { StringBuilder printValue = new StringBuilder(); printValue.append("["); while(currentItem.next() != null){ - printValue.append(currentItem.getValue()+","); + printValue.append(currentItem.getValue()).append(","); currentItem = currentItem.next(); } diff --git a/Section9/src/main/java/challenge/abstractclasses/NodeList.java b/Section9/src/main/java/challenge/abstractclasses/NodeList.java index 0a14aae..a8c6d94 100644 --- a/Section9/src/main/java/challenge/abstractclasses/NodeList.java +++ b/Section9/src/main/java/challenge/abstractclasses/NodeList.java @@ -2,11 +2,15 @@ public interface NodeList { - public ListItem getRoot(); - public boolean addNode(ListItem node); - public void traverse(); - public void traverse(ListItem root); - public boolean removeNode(ListItem node); + ListItem getRoot(); + + boolean addNode(ListItem node); + + void traverse(); + + void traverse(ListItem root); + + boolean removeNode(ListItem node); ListItem searchNode(ListItem node); int size(); } diff --git a/Section9/src/main/java/challenge/abstractclasses/SearchTree.java b/Section9/src/main/java/challenge/abstractclasses/SearchTree.java index 38bdf88..e6b33e0 100644 --- a/Section9/src/main/java/challenge/abstractclasses/SearchTree.java +++ b/Section9/src/main/java/challenge/abstractclasses/SearchTree.java @@ -57,7 +57,6 @@ public boolean addNode(ListItem node) { public void traverse() { if(root == null) { System.out.println("Tree is empty"); - return; }else{ traverse(root); } diff --git a/Section9/src/main/java/challenge/innerclass/Albums.java b/Section9/src/main/java/challenge/innerclass/Albums.java index 360c81f..3270515 100644 --- a/Section9/src/main/java/challenge/innerclass/Albums.java +++ b/Section9/src/main/java/challenge/innerclass/Albums.java @@ -56,7 +56,7 @@ public void removeSongFromAlbum(String title) { } } - private class SongList { + private static class SongList { private ArrayList songs = new ArrayList<>(); private int getSize() { diff --git a/Section9/src/main/java/example/innerclass/Main.java b/Section9/src/main/java/example/innerclass/Main.java index b360a37..3f2cd13 100644 --- a/Section9/src/main/java/example/innerclass/Main.java +++ b/Section9/src/main/java/example/innerclass/Main.java @@ -7,12 +7,7 @@ public class Main { static Button button = new Button("Print"); public static void main(String[] args) { - button.setOnClickListener(new Button.OnClickListener() { - @Override - public void onClick(String title) { - System.out.println(title + " button clicked"); - } - }); + button.setOnClickListener(title -> System.out.println(title + " button clicked")); start(); } diff --git a/pom.xml b/pom.xml index 1078034..6d067ac 100644 --- a/pom.xml +++ b/pom.xml @@ -19,6 +19,8 @@ Section9 Section10 Section11 + Section12 + Section11Challenge