From e2aa4c2894ad1dc2ace2ef07087fd2a5cfa91ad1 Mon Sep 17 00:00:00 2001 From: Caleb Fontenot Date: Thu, 16 Mar 2023 14:07:35 -0500 Subject: [PATCH] update --- .../MP4_CalebFontenot/nb-configuration.xml | 18 ++ .../mp4_calebfontenot/Bicycle.java | 25 ++ .../mp4_calebfontenot/BikeStores.java | 301 ++++++++++++------ .../mp4_calebfontenot/ChildBike.java | 15 + .../mp4_calebfontenot/MountainBike.java | 16 + .../mp4_calebfontenot/SpeedBike.java | 23 ++ 6 files changed, 302 insertions(+), 96 deletions(-) create mode 100644 Semester 2/Assignments/MP4_CalebFontenot/nb-configuration.xml diff --git a/Semester 2/Assignments/MP4_CalebFontenot/nb-configuration.xml b/Semester 2/Assignments/MP4_CalebFontenot/nb-configuration.xml new file mode 100644 index 0000000..974cb30 --- /dev/null +++ b/Semester 2/Assignments/MP4_CalebFontenot/nb-configuration.xml @@ -0,0 +1,18 @@ + + + + + + JDK_17 + + diff --git a/Semester 2/Assignments/MP4_CalebFontenot/src/main/java/com/calebfontenot/mp4_calebfontenot/Bicycle.java b/Semester 2/Assignments/MP4_CalebFontenot/src/main/java/com/calebfontenot/mp4_calebfontenot/Bicycle.java index 434e71c..4077f33 100644 --- a/Semester 2/Assignments/MP4_CalebFontenot/src/main/java/com/calebfontenot/mp4_calebfontenot/Bicycle.java +++ b/Semester 2/Assignments/MP4_CalebFontenot/src/main/java/com/calebfontenot/mp4_calebfontenot/Bicycle.java @@ -13,6 +13,31 @@ abstract public class Bicycle private int cadence; private int gear; private int speed; + + //@Override + public boolean equals(Object obj) + { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final Bicycle other = (Bicycle) obj; + if (this.cadence != other.cadence) { + return false; + } + if (this.gear != other.gear) { + return false; + } + return this.speed == other.speed; + } + + + public Bicycle(int cadence, int gear, int speed) { this.cadence = cadence; diff --git a/Semester 2/Assignments/MP4_CalebFontenot/src/main/java/com/calebfontenot/mp4_calebfontenot/BikeStores.java b/Semester 2/Assignments/MP4_CalebFontenot/src/main/java/com/calebfontenot/mp4_calebfontenot/BikeStores.java index 75d3bcd..9a42ce7 100644 --- a/Semester 2/Assignments/MP4_CalebFontenot/src/main/java/com/calebfontenot/mp4_calebfontenot/BikeStores.java +++ b/Semester 2/Assignments/MP4_CalebFontenot/src/main/java/com/calebfontenot/mp4_calebfontenot/BikeStores.java @@ -11,126 +11,219 @@ import java.util.Collection; * * @author caleb */ -public class BikeStores -{ +public class BikeStores { + ArrayList> storesOfBikes = new ArrayList>(); + /** - * Add a bike at storeNumber ( i.e. the storeNumber indicates the store) - * - * @param storeNumber which store + * Add a bike at storeNumber ( i.e. the storeNumber indicates the store) + * + * @param storeNumber which store * @param b the bike to add - * @return true if the bike added, false if the storeNumber is invalid or b is null. + * @return true if the bike added, false if the storeNumber is invalid or b is null. */ - public BikeStores() { + public BikeStores() + { for (int i = 0; i < 3; i++) { storesOfBikes.add(new ArrayList()); } } - - public boolean addBike( int storeNumber, Bicycle b) - { - - try { - System.out.println("Attempting to add " + b + " to store at index " + storeNumber); - //Unpack array - ArrayList bikesInInventory = storesOfBikes.get(storeNumber); - bikesInInventory.add(b); - - // Repack array - storesOfBikes.set(storeNumber, bikesInInventory); - System.out.println("success!"); - return true; - } catch (Exception ex){ - System.err.println(ex); + + public boolean addBike(int storeNumber, Bicycle b) + { + if (storeNumber < 0 || storeNumber > this.storesOfBikes.size() - 1) { return false; } - - - } - /** Removes a bike at a specific store. - * - * @param store the store that has the bike - * @param bthe bike to be removed - * @return true of the bike is removed, false if the bike does not exist, or bike is null. - */ - - public boolean removeBike( int store , Bicycle b) - { - try{ - System.out.println("Attempting to remove " + b + " to store at index " + store); - // Remove bike from array. - ArrayList bikesInInventory = storesOfBikes.get(store); - bikesInInventory.remove(b); //YEET - System.out.println("success!"); - return true; - } catch (Exception ex) { - System.err.println(ex); - return false; - } - - - - } - /**Prints the indexes before each bike , one store per line - * - * @param stores stores of bikes - */ + if (b == null) { + return false; + } + System.out.println("Attempting to add " + b + " to store at index " + storeNumber); + //Unpack array + ArrayList bikesInInventory = storesOfBikes.get(storeNumber); + bikesInInventory.add(b); + + // Repack array + storesOfBikes.set(storeNumber, bikesInInventory); + System.out.println("success!"); + return true; + } + + /** + * Removes a bike at a specific store. + * + * @param storeNumber the store that has the bike + * @param b the bike to be removed + * @return true of the bike is removed, false if the bike does not exist, or bike is null. + */ + public boolean removeBike(int storeNumber, Bicycle b) + { + if (storeNumber < 0 || storeNumber > this.storesOfBikes.size() - 1) { + return false; + } + if (b == null) { + return false; + } + System.out.println("Attempting to remove " + b + " to store at index " + storeNumber); + // Remove bike from array. + ArrayList bikesInInventory = storesOfBikes.get(storeNumber); + bikesInInventory.remove(b); //YEET + System.out.println("success!"); + return true; + } + + /** + * Prints the indexes before each bike , one store per line + * + * @param stores stores of bikes + */ public static void print(ArrayList> stores) { - for ( int i=0; i < stores.size(); ++i) { - System.out.println("---------- " + "printing store " + (i + 1) + " ----------"); - for ( int j=0; j < stores.get(i).size(); ++j) - { - System.out.print(stores.get(i).get(j) + " "); - + for (int i = 0; i < stores.size(); ++i) { + System.out.println("---------- " + "printing row " + (i + 1) + " ----------"); + for (int j = 0; j < stores.get(i).size(); ++j) { + System.out.println(stores.get(i).get(j) + " "); + } } } - /** Groups bikes by type ans sorts them by the ranking of Details. - * There will be three groups and each group stored in decending order by rank. + + public static void printSingle(ArrayList stores) + { + System.out.println("----------" + "printSingle" + "----------"); + for (int j = 0; j < stores.size(); ++j) { + System.out.println(stores.get(j) + " "); + } + + } + + public static void printRank(ArrayList stores) + { + System.out.println("----------" + "printRank" + "----------"); + for (int j = 0; j < stores.size(); ++j) { + System.out.println(stores.get(j) + " " + stores.get(j).calculatedDetails().getRank()); + } + + } + + /** + * Groups bikes by type ans sorts them by the ranking of Details. There will be three groups and each group stored in descending order by rank. + * * @param stores the stores of bikes - * @return a newly created ArrayList> with the bikes sorted. + * @return a newly created ArrayList> with the bikes sorted. */ - public ArrayList> sortBikesByType( final ArrayList> stores) + public static ArrayList> sortBikesByType(final ArrayList> stores) { ArrayList> newStore = new ArrayList>(); - - return newStore; - } - - public static void print(Object[] arr) { - for (Object o: arr) { - System.out.println(o); + // group arrayLists + ArrayList mountainBikes = new ArrayList(); + ArrayList speedBikes = new ArrayList(); + ArrayList childBikes = new ArrayList(); + System.out.println("---------- " + "grouping by bike type... " + " ----------"); + for (int i = 0; i < stores.size(); ++i) { + for (int j = 0; j < stores.get(i).size(); ++j) { + // System.out.println(stores.get(i).get(j).calculatedDetails().getRank() + " "); + Bicycle bike = stores.get(i).get(j); + if (bike instanceof ChildBike) { + childBikes.add(bike); + } else if (bike instanceof MountainBike) { + mountainBikes.add(bike); + } else if (bike instanceof SpeedBike) { + speedBikes.add(bike); + } } + } + //printRank(childBikes); + //printRank(mountainBikes); + //printRank(speedBikes); + System.out.println("sorting..."); + sortType(childBikes); + sortType(mountainBikes); + sortType(speedBikes); + System.out.println("sorted"); + //printRank(childBikes); + //printRank(mountainBikes); + //printRank(speedBikes); + newStore.add(childBikes); + newStore.add(mountainBikes); + newStore.add(speedBikes); + return newStore; } - - public static void print(Object[][] arr) { + + private static void sortType(ArrayList arrList) + { + for (int x = 0; x < arrList.size(); ++x) { + for (int i = 0; i < arrList.size(); ++i) { + for (int j = i + 1; j < arrList.size(); ++j) { + int compare1 = arrList.get(i).calculatedDetails().getRank(); + int compare2 = arrList.get(j).calculatedDetails().getRank(); + if (compare1 < compare2) { + Bicycle tmp = arrList.get(i); + arrList.set(i, arrList.get(j)); + arrList.set(j, tmp); + } + } + } + } + } + + public static void print(Object[] arr) + { + for (Object o : arr) { + System.out.println(o); + } + } + + public static void print(Object[][] arr) + { for (int i = 0; i < arr.length; i++) { System.out.println("---------- " + "printing row " + (i + 1) + " ----------"); print(arr[i]); - + } } - + /** * Returns a 2D array containing all the bikes in the store in proper sequence (from first to last element). Store 0 with all its bikes, store 1 with all its bikes, and so on. + * * @return a 2D array of all stores with bikes. */ - public Object[][] toArray() { - Object[][] arr = new Object[storesOfBikes.size()][]; - for (int i = 0; i < storesOfBikes.size(); i++) { - arr[i] = storesOfBikes.toArray(); - } - return arr; - } - /** - * Retains only the Bicycles in the stores that are contained in the specified collection. In other words, removes from this list all of bikes that are not contained in the specified location. - * @param c the bikes to be removed - * @return true if any store changed as a result of the call. - */ - public boolean retainAll (Collection c) { - return false; - } + public Object[][] toArray() + { + Object[][] arr = new Object[storesOfBikes.size()][]; + for (int i = 0; i < storesOfBikes.size(); i++) { + arr[i] = storesOfBikes.toArray(); + } + return arr; + } + + /** + * Retains only the Bicycles in the stores that are contained in the specified collection. In other words, removes from this list all of bikes that are not contained in the specified location. + * + * @param c the bikes to be removed + * @return true if any store changed as a result of the call. + */ + public boolean retainAll(Collection c) + { + /* + for (int i = 0; i < storesOfBikes.size(); ++i) { + for (int j = 0; j < storesOfBikes.get(i).size(); ++j) { + //for (int x = 0; x < c.size(); ++x) { + Bicycle compare1 = storesOfBikes.get(i).get(j); + //Bicycle compare2 = ((ArrayList) c).get(x); + //if (!compare1.equals(compare2)) { + if (!storesOfBikes.get(i).contains(c)) { + storesOfBikes.get(i).remove(j); + System.out.println("Removing " + compare1); + //} + } + } + } +*/ + System.out.println("Bikes to remove:"); + printSingle((ArrayList) c); + return true; + } + public static void main(String[] args) { BikeStores bikes = new BikeStores(); @@ -148,22 +241,38 @@ public class BikeStores bikes.addBike(1, new ChildBike(true, 1, 1, 15)); //add 6 bikes to store 2 ( 2 speedBikes, 2 mountain 2 child ) bikes.addBike(2, new SpeedBike(7, 10, 20, 25)); - bikes.addBike(2, new SpeedBike(0, 8, 40, 55)); + bikes.addBike(2, new SpeedBike(0, 8, 40, 55)); // retainAll should match this one bikes.addBike(2, new MountainBike(2, 3, 15, 33)); bikes.addBike(2, new MountainBike(1, 3, 15, 24)); - bikes.addBike(2, new ChildBike(true, 1, 2, 20)); + bikes.addBike(2, new ChildBike(true, 1, 2, 20)); // retainAll should match this one bikes.addBike(2, new ChildBike(false, 1, 1, 18)); //remove one child bike from store 2 bikes.removeBike(2, new ChildBike(true, 1, 2, 20)); // Java reuses the object created earlier since it has the same values. //PRINT - print(bikes); + print(bikes.storesOfBikes); //SORT + ArrayList> sortedBikes = sortBikesByType(bikes.storesOfBikes); //PRINT what the method return + System.out.println("SORTED BIKES"); + print(sortedBikes); //PRINT the original store + System.out.println("ORIGINAL STORE ARRAYLIST"); + print(bikes.storesOfBikes); + // ---------- TEST retainAll ---------- + System.out.println("Testing retainAll();..."); + Collection c1 = new ArrayList(); + c1.add(new SpeedBike(0, 8, 40, 55)); + c1.add(new ChildBike(true, 1, 2, 20)); + Bicycle b1 = bikes.storesOfBikes.get(2).get(1); + Bicycle b2 = ((ArrayList) c1).get(0); + System.out.println(b1.equals(b2)); + bikes.retainAll(c1); + print(bikes.storesOfBikes); + } - + } -class NotABike{ - +class NotABike { + } diff --git a/Semester 2/Assignments/MP4_CalebFontenot/src/main/java/com/calebfontenot/mp4_calebfontenot/ChildBike.java b/Semester 2/Assignments/MP4_CalebFontenot/src/main/java/com/calebfontenot/mp4_calebfontenot/ChildBike.java index 01bbb60..15faa04 100644 --- a/Semester 2/Assignments/MP4_CalebFontenot/src/main/java/com/calebfontenot/mp4_calebfontenot/ChildBike.java +++ b/Semester 2/Assignments/MP4_CalebFontenot/src/main/java/com/calebfontenot/mp4_calebfontenot/ChildBike.java @@ -10,6 +10,21 @@ package com.calebfontenot.mp4_calebfontenot; */ public class ChildBike extends Bicycle //remove comment in front of extends { + @Override + public boolean equals(Object obj) + { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final ChildBike other = (ChildBike) obj; + return this.helpWheels == other.helpWheels && super.equals(obj); + } private boolean helpWheels; diff --git a/Semester 2/Assignments/MP4_CalebFontenot/src/main/java/com/calebfontenot/mp4_calebfontenot/MountainBike.java b/Semester 2/Assignments/MP4_CalebFontenot/src/main/java/com/calebfontenot/mp4_calebfontenot/MountainBike.java index 64ac113..fb3da18 100644 --- a/Semester 2/Assignments/MP4_CalebFontenot/src/main/java/com/calebfontenot/mp4_calebfontenot/MountainBike.java +++ b/Semester 2/Assignments/MP4_CalebFontenot/src/main/java/com/calebfontenot/mp4_calebfontenot/MountainBike.java @@ -13,6 +13,22 @@ public class MountainBike extends Bicycle private int seatHeight; + @Override + public boolean equals(Object obj) + { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final MountainBike other = (MountainBike) obj; + return this.seatHeight == other.seatHeight && super.equals(obj); + } + public MountainBike(int seatHeight, int cadence, int gear, int speed) { super(cadence, gear, speed); diff --git a/Semester 2/Assignments/MP4_CalebFontenot/src/main/java/com/calebfontenot/mp4_calebfontenot/SpeedBike.java b/Semester 2/Assignments/MP4_CalebFontenot/src/main/java/com/calebfontenot/mp4_calebfontenot/SpeedBike.java index 4fd96e3..ea9904d 100644 --- a/Semester 2/Assignments/MP4_CalebFontenot/src/main/java/com/calebfontenot/mp4_calebfontenot/SpeedBike.java +++ b/Semester 2/Assignments/MP4_CalebFontenot/src/main/java/com/calebfontenot/mp4_calebfontenot/SpeedBike.java @@ -10,6 +10,29 @@ package com.calebfontenot.mp4_calebfontenot; */ public class SpeedBike extends Bicycle { + + @Override + public int hashCode() + { + int hash = 5; + return hash; + } + + @Override + public boolean equals(Object obj) + { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final SpeedBike other = (SpeedBike) obj; + return Double.doubleToLongBits(this.weight) == Double.doubleToLongBits(other.weight) && super.equals(obj); + } private double weight;