update
This commit is contained in:
parent
880852443a
commit
e2aa4c2894
@ -0,0 +1,18 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project-shared-configuration>
|
||||||
|
<!--
|
||||||
|
This file contains additional configuration written by modules in the NetBeans IDE.
|
||||||
|
The configuration is intended to be shared among all the users of project and
|
||||||
|
therefore it is assumed to be part of version control checkout.
|
||||||
|
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
|
||||||
|
-->
|
||||||
|
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
|
||||||
|
<!--
|
||||||
|
Properties that influence various parts of the IDE, especially code formatting and the like.
|
||||||
|
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
|
||||||
|
That way multiple projects can share the same settings (useful for formatting rules for example).
|
||||||
|
Any value defined here will override the pom.xml file value but is only applicable to the current project.
|
||||||
|
-->
|
||||||
|
<netbeans.hint.jdkPlatform>JDK_17</netbeans.hint.jdkPlatform>
|
||||||
|
</properties>
|
||||||
|
</project-shared-configuration>
|
@ -13,6 +13,31 @@ abstract public class Bicycle
|
|||||||
private int cadence;
|
private int cadence;
|
||||||
private int gear;
|
private int gear;
|
||||||
private int speed;
|
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)
|
public Bicycle(int cadence, int gear, int speed)
|
||||||
{
|
{
|
||||||
this.cadence = cadence;
|
this.cadence = cadence;
|
||||||
|
@ -11,126 +11,219 @@ import java.util.Collection;
|
|||||||
*
|
*
|
||||||
* @author caleb
|
* @author caleb
|
||||||
*/
|
*/
|
||||||
public class BikeStores
|
public class BikeStores {
|
||||||
{
|
|
||||||
ArrayList<ArrayList<Bicycle>> storesOfBikes = new ArrayList<ArrayList<Bicycle>>();
|
ArrayList<ArrayList<Bicycle>> storesOfBikes = new ArrayList<ArrayList<Bicycle>>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a bike at storeNumber ( i.e. the storeNumber indicates the store)
|
* Add a bike at storeNumber ( i.e. the storeNumber indicates the store)
|
||||||
*
|
*
|
||||||
* @param storeNumber which store
|
* @param storeNumber which store
|
||||||
* @param b the bike to add
|
* @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++) {
|
for (int i = 0; i < 3; i++) {
|
||||||
storesOfBikes.add(new ArrayList<Bicycle>());
|
storesOfBikes.add(new ArrayList<Bicycle>());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean addBike( int storeNumber, Bicycle b)
|
public boolean addBike(int storeNumber, Bicycle b)
|
||||||
{
|
{
|
||||||
|
if (storeNumber < 0 || storeNumber > this.storesOfBikes.size() - 1) {
|
||||||
try {
|
|
||||||
System.out.println("Attempting to add " + b + " to store at index " + storeNumber);
|
|
||||||
//Unpack array
|
|
||||||
ArrayList<Bicycle> 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);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (b == null) {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
/** Removes a bike at a specific store.
|
System.out.println("Attempting to add " + b + " to store at index " + storeNumber);
|
||||||
*
|
//Unpack array
|
||||||
* @param store the store that has the bike
|
ArrayList<Bicycle> bikesInInventory = storesOfBikes.get(storeNumber);
|
||||||
* @param bthe bike to be removed
|
bikesInInventory.add(b);
|
||||||
* @return true of the bike is removed, false if the bike does not exist, or bike is null.
|
|
||||||
*/
|
// Repack array
|
||||||
|
storesOfBikes.set(storeNumber, bikesInInventory);
|
||||||
public boolean removeBike( int store , Bicycle b)
|
System.out.println("success!");
|
||||||
{
|
return true;
|
||||||
try{
|
}
|
||||||
System.out.println("Attempting to remove " + b + " to store at index " + store);
|
|
||||||
// Remove bike from array.
|
/**
|
||||||
ArrayList<Bicycle> bikesInInventory = storesOfBikes.get(store);
|
* Removes a bike at a specific store.
|
||||||
bikesInInventory.remove(b); //YEET
|
*
|
||||||
System.out.println("success!");
|
* @param storeNumber the store that has the bike
|
||||||
return true;
|
* @param b the bike to be removed
|
||||||
} catch (Exception ex) {
|
* @return true of the bike is removed, false if the bike does not exist, or bike is null.
|
||||||
System.err.println(ex);
|
*/
|
||||||
return false;
|
public boolean removeBike(int storeNumber, Bicycle b)
|
||||||
}
|
{
|
||||||
|
if (storeNumber < 0 || storeNumber > this.storesOfBikes.size() - 1) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
if (b == null) {
|
||||||
/**Prints the indexes before each bike , one store per line
|
return false;
|
||||||
*
|
}
|
||||||
* @param stores stores of bikes
|
System.out.println("Attempting to remove " + b + " to store at index " + storeNumber);
|
||||||
*/
|
// Remove bike from array.
|
||||||
|
ArrayList<Bicycle> 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<ArrayList<Bicycle>> stores)
|
public static void print(ArrayList<ArrayList<Bicycle>> stores)
|
||||||
{
|
{
|
||||||
for ( int i=0; i < stores.size(); ++i) {
|
for (int i = 0; i < stores.size(); ++i) {
|
||||||
System.out.println("---------- " + "printing store " + (i + 1) + " ----------");
|
System.out.println("---------- " + "printing row " + (i + 1) + " ----------");
|
||||||
for ( int j=0; j < stores.get(i).size(); ++j)
|
for (int j = 0; j < stores.get(i).size(); ++j) {
|
||||||
{
|
System.out.println(stores.get(i).get(j) + " ");
|
||||||
System.out.print(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<Bicycle> stores)
|
||||||
|
{
|
||||||
|
System.out.println("----------" + "printSingle" + "----------");
|
||||||
|
for (int j = 0; j < stores.size(); ++j) {
|
||||||
|
System.out.println(stores.get(j) + " ");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void printRank(ArrayList<Bicycle> 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
|
* @param stores the stores of bikes
|
||||||
* @return a newly created ArrayList<ArrayList<Bicycle>> with the bikes sorted.
|
* @return a newly created ArrayList<ArrayList<Bicycle>> with the bikes sorted.
|
||||||
*/
|
*/
|
||||||
public ArrayList<ArrayList<Bicycle>> sortBikesByType( final ArrayList<ArrayList<Bicycle>> stores)
|
public static ArrayList<ArrayList<Bicycle>> sortBikesByType(final ArrayList<ArrayList<Bicycle>> stores)
|
||||||
{
|
{
|
||||||
ArrayList<ArrayList<Bicycle>> newStore = new ArrayList<ArrayList<Bicycle>>();
|
ArrayList<ArrayList<Bicycle>> newStore = new ArrayList<ArrayList<Bicycle>>();
|
||||||
|
// group arrayLists
|
||||||
return newStore;
|
ArrayList<Bicycle> mountainBikes = new ArrayList<Bicycle>();
|
||||||
}
|
ArrayList<Bicycle> speedBikes = new ArrayList<Bicycle>();
|
||||||
|
ArrayList<Bicycle> childBikes = new ArrayList<Bicycle>();
|
||||||
public static void print(Object[] arr) {
|
System.out.println("---------- " + "grouping by bike type... " + " ----------");
|
||||||
for (Object o: arr) {
|
for (int i = 0; i < stores.size(); ++i) {
|
||||||
System.out.println(o);
|
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<Bicycle> 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++) {
|
for (int i = 0; i < arr.length; i++) {
|
||||||
System.out.println("---------- " + "printing row " + (i + 1) + " ----------");
|
System.out.println("---------- " + "printing row " + (i + 1) + " ----------");
|
||||||
print(arr[i]);
|
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.
|
* 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.
|
* @return a 2D array of all stores with bikes.
|
||||||
*/
|
*/
|
||||||
public Object[][] toArray() {
|
public Object[][] toArray()
|
||||||
Object[][] arr = new Object[storesOfBikes.size()][];
|
{
|
||||||
for (int i = 0; i < storesOfBikes.size(); i++) {
|
Object[][] arr = new Object[storesOfBikes.size()][];
|
||||||
arr[i] = storesOfBikes.toArray();
|
for (int i = 0; i < storesOfBikes.size(); i++) {
|
||||||
}
|
arr[i] = storesOfBikes.toArray();
|
||||||
return arr;
|
}
|
||||||
}
|
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.
|
* 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.
|
||||||
*/
|
*
|
||||||
public boolean retainAll (Collection<Bicycle> c) {
|
* @param c the bikes to be removed
|
||||||
return false;
|
* @return true if any store changed as a result of the call.
|
||||||
}
|
*/
|
||||||
|
public boolean retainAll(Collection<Bicycle> 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<Bicycle>) 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<Bicycle>) c);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String[] args)
|
public static void main(String[] args)
|
||||||
{
|
{
|
||||||
BikeStores bikes = new BikeStores();
|
BikeStores bikes = new BikeStores();
|
||||||
@ -148,22 +241,38 @@ public class BikeStores
|
|||||||
bikes.addBike(1, new ChildBike(true, 1, 1, 15));
|
bikes.addBike(1, new ChildBike(true, 1, 1, 15));
|
||||||
//add 6 bikes to store 2 ( 2 speedBikes, 2 mountain 2 child )
|
//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(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(2, 3, 15, 33));
|
||||||
bikes.addBike(2, new MountainBike(1, 3, 15, 24));
|
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));
|
bikes.addBike(2, new ChildBike(false, 1, 1, 18));
|
||||||
//remove one child bike from store 2
|
//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.
|
bikes.removeBike(2, new ChildBike(true, 1, 2, 20)); // Java reuses the object created earlier since it has the same values.
|
||||||
//PRINT
|
//PRINT
|
||||||
print(bikes);
|
print(bikes.storesOfBikes);
|
||||||
//SORT
|
//SORT
|
||||||
|
ArrayList<ArrayList<Bicycle>> sortedBikes = sortBikesByType(bikes.storesOfBikes);
|
||||||
//PRINT what the method return
|
//PRINT what the method return
|
||||||
|
System.out.println("SORTED BIKES");
|
||||||
|
print(sortedBikes);
|
||||||
//PRINT the original store
|
//PRINT the original store
|
||||||
|
System.out.println("ORIGINAL STORE ARRAYLIST");
|
||||||
|
print(bikes.storesOfBikes);
|
||||||
|
// ---------- TEST retainAll ----------
|
||||||
|
System.out.println("Testing retainAll();...");
|
||||||
|
Collection<Bicycle> c1 = new ArrayList<Bicycle>();
|
||||||
|
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<Bicycle>) c1).get(0);
|
||||||
|
System.out.println(b1.equals(b2));
|
||||||
|
bikes.retainAll(c1);
|
||||||
|
print(bikes.storesOfBikes);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class NotABike{
|
class NotABike {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,21 @@ package com.calebfontenot.mp4_calebfontenot;
|
|||||||
*/
|
*/
|
||||||
public class ChildBike extends Bicycle //remove comment in front of extends
|
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;
|
private boolean helpWheels;
|
||||||
|
|
||||||
|
@ -13,6 +13,22 @@ public class MountainBike extends Bicycle
|
|||||||
|
|
||||||
private int seatHeight;
|
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)
|
public MountainBike(int seatHeight, int cadence, int gear, int speed)
|
||||||
{
|
{
|
||||||
super(cadence, gear, speed);
|
super(cadence, gear, speed);
|
||||||
|
@ -10,6 +10,29 @@ package com.calebfontenot.mp4_calebfontenot;
|
|||||||
*/
|
*/
|
||||||
public class SpeedBike extends Bicycle
|
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;
|
private double weight;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user