299 lines
33 KiB
HTML
299 lines
33 KiB
HTML
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||
|
<html>
|
||
|
<head>
|
||
|
<title>BikeStores.java</title>
|
||
|
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
||
|
<style type="text/css">
|
||
|
<!--
|
||
|
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace}
|
||
|
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace}
|
||
|
table {color: #888888; background-color: #313335; font-family: monospace}
|
||
|
.literal {color: #cc7832}
|
||
|
.ST0 {color: #287bde}
|
||
|
.ST2 {color: #9876aa}
|
||
|
.ST4 {color: #ffc66d}
|
||
|
.number {color: #6897bb}
|
||
|
.string {color: #6a8759}
|
||
|
.ST5 {color: #9876aa; font-family: monospace; font-style: italic}
|
||
|
.ST6 {color: #ffc66d; font-family: monospace; font-style: italic}
|
||
|
.ST3 {color: #8a653b}
|
||
|
.ST8 {font-family: monospace; font-style: italic}
|
||
|
.ST7 {color: #9999ff; font-family: monospace; font-style: italic}
|
||
|
.comment {color: #808080}
|
||
|
.whitespace {color: #505050}
|
||
|
.ST1 {color: #808080; font-family: monospace; font-weight: bold}
|
||
|
-->
|
||
|
</style>
|
||
|
</head>
|
||
|
<body>
|
||
|
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 2/Assignments/MP4_CalebFontenot/src/main/java/com/calebfontenot/mp4_calebfontenot/BikeStores.java</td></tr></table>
|
||
|
<pre>
|
||
|
<span class="comment">/*</span>
|
||
|
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
|
||
|
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
|
||
|
<span class="comment"> */</span>
|
||
|
<span class="literal">package</span> com.calebfontenot.mp4_calebfontenot;
|
||
|
|
||
|
<span class="literal">import</span> java.util.ArrayList;
|
||
|
<span class="literal">import</span> java.util.Collection;
|
||
|
|
||
|
<span class="comment">/**</span>
|
||
|
<span class="comment"> *</span>
|
||
|
<span class="comment"> * </span><span class="ST1">@author</span> <span class="comment">caleb</span>
|
||
|
<span class="comment">*/</span>
|
||
|
<span class="literal">public</span> <span class="literal">class</span> BikeStores {
|
||
|
|
||
|
ArrayList<ArrayList<Bicycle>> <span class="ST2">storesOfBikes</span> = <span class="literal">new</span> ArrayList<ArrayList<Bicycle>>();
|
||
|
|
||
|
<span class="comment">/**</span>
|
||
|
<span class="comment"> * </span><span class="comment">Add</span> <span class="comment">a</span> <span class="comment">bike</span> <span class="comment">at</span> <span class="comment">storeNumber</span><span class="comment"> ( </span><span class="comment">i</span><span class="comment">.</span><span class="comment">e</span><span class="comment">.</span> <span class="comment">the</span> <span class="comment">storeNumber</span> <span class="comment">indicates</span> <span class="comment">the</span> <span class="comment">store</span><span class="comment">)</span>
|
||
|
<span class="comment"> *</span>
|
||
|
<span class="comment"> * </span><span class="ST1">@param</span> <span class="ST3">storeNumber</span> <span class="comment">which</span> <span class="comment">store</span>
|
||
|
<span class="comment"> * </span><span class="ST1">@param</span> <span class="ST3">b</span> <span class="comment">the</span> <span class="comment">bike</span> <span class="comment">to</span> <span class="comment">add</span>
|
||
|
<span class="comment"> * </span><span class="ST1">@return</span> <span class="comment">true</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">bike</span> <span class="comment">added</span><span class="comment">, </span><span class="comment">false</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">storeNumber</span> <span class="comment">is</span> <span class="comment">invalid</span> <span class="comment">or</span> <span class="comment">b</span> <span class="comment">is</span> <span class="comment">null</span><span class="comment">.</span>
|
||
|
<span class="comment">*/</span>
|
||
|
<span class="literal">public</span> BikeStores() {
|
||
|
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i < <span class="number">3</span>; i++) {
|
||
|
<span class="ST2">storesOfBikes</span>.add(<span class="literal">new</span> ArrayList<Bicycle>());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST4">addBike</span>(<span class="literal">int</span> storeNumber, Bicycle b) {
|
||
|
<span class="literal">if</span> (storeNumber < <span class="number">0</span> || storeNumber > <span class="literal">this</span>.<span class="ST2">storesOfBikes</span>.size() - <span class="number">1</span>) {
|
||
|
<span class="literal">return</span> <span class="literal">false</span>;
|
||
|
}
|
||
|
<span class="literal">if</span> (b == <span class="literal">null</span>) {
|
||
|
<span class="literal">return</span> <span class="literal">false</span>;
|
||
|
}
|
||
|
System.<span class="ST5">out</span>.println(<span class="string">"</span><span class="string">Attempting to add </span><span class="string">"</span> + b + <span class="string">"</span><span class="string"> to store at index </span><span class="string">"</span> + storeNumber);
|
||
|
<span class="comment">//Unpack array</span>
|
||
|
ArrayList<Bicycle> bikesInInventory = <span class="ST2">storesOfBikes</span>.get(storeNumber);
|
||
|
bikesInInventory.add(b);
|
||
|
|
||
|
<span class="comment">// Repack array</span>
|
||
|
<span class="ST2">storesOfBikes</span>.set(storeNumber, bikesInInventory);
|
||
|
System.<span class="ST5">out</span>.println(<span class="string">"</span><span class="string">success!</span><span class="string">"</span>);
|
||
|
<span class="literal">return</span> <span class="literal">true</span>;
|
||
|
}
|
||
|
|
||
|
<span class="comment">/**</span>
|
||
|
<span class="comment"> * </span><span class="comment">Removes</span> <span class="comment">a</span> <span class="comment">bike</span> <span class="comment">at</span> <span class="comment">a</span> <span class="comment">specific</span> <span class="comment">store</span><span class="comment">.</span>
|
||
|
<span class="comment"> *</span>
|
||
|
<span class="comment"> * </span><span class="ST1">@param</span> <span class="ST3">storeNumber</span> <span class="comment">the</span> <span class="comment">store</span> <span class="comment">that</span> <span class="comment">has</span> <span class="comment">the</span> <span class="comment">bike</span>
|
||
|
<span class="comment"> * </span><span class="ST1">@param</span> <span class="ST3">b</span> <span class="comment">the</span> <span class="comment">bike</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">removed</span>
|
||
|
<span class="comment"> * </span><span class="ST1">@return</span> <span class="comment">true</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">bike</span> <span class="comment">is</span> <span class="comment">removed</span><span class="comment">, </span><span class="comment">false</span> <span class="comment">if</span> <span class="comment">the</span> <span class="comment">bike</span> <span class="comment">does</span> <span class="comment">not</span> <span class="comment">exist</span><span class="comment">, </span><span class="comment">or</span> <span class="comment">bike</span> <span class="comment">is</span> <span class="comment">null</span><span class="comment">.</span>
|
||
|
<span class="comment">*/</span>
|
||
|
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST4">removeBike</span>(<span class="literal">int</span> storeNumber, Bicycle b) {
|
||
|
<span class="literal">if</span> (storeNumber < <span class="number">0</span> || storeNumber > <span class="literal">this</span>.<span class="ST2">storesOfBikes</span>.size() - <span class="number">1</span>) {
|
||
|
<span class="literal">return</span> <span class="literal">false</span>;
|
||
|
}
|
||
|
<span class="literal">if</span> (b == <span class="literal">null</span>) {
|
||
|
<span class="literal">return</span> <span class="literal">false</span>;
|
||
|
}
|
||
|
System.<span class="ST5">out</span>.println(<span class="string">"</span><span class="string">Attempting to remove </span><span class="string">"</span> + b + <span class="string">"</span><span class="string"> to store at index </span><span class="string">"</span> + storeNumber);
|
||
|
<span class="comment">// Remove bike from array.</span>
|
||
|
ArrayList<Bicycle> bikesInInventory = <span class="ST2">storesOfBikes</span>.get(storeNumber);
|
||
|
bikesInInventory.remove(b); <span class="comment">//YEET</span>
|
||
|
System.<span class="ST5">out</span>.println(<span class="string">"</span><span class="string">success!</span><span class="string">"</span>);
|
||
|
<span class="literal">return</span> <span class="literal">true</span>;
|
||
|
}
|
||
|
|
||
|
<span class="comment">/**</span>
|
||
|
<span class="comment"> * </span><span class="comment">Prints</span> <span class="comment">the</span> <span class="comment">indexes</span> <span class="comment">before</span> <span class="comment">each</span> <span class="comment">bike</span><span class="comment"> , </span><span class="comment">one</span> <span class="comment">store</span> <span class="comment">per</span> <span class="comment">line</span>
|
||
|
<span class="comment"> *</span>
|
||
|
<span class="comment"> * </span><span class="ST1">@param</span> <span class="ST3">stores</span> <span class="comment">stores</span> <span class="comment">of</span> <span class="comment">bikes</span>
|
||
|
<span class="comment">*/</span>
|
||
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST6">print</span>(ArrayList<ArrayList<Bicycle>> stores) {
|
||
|
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i < stores.size(); ++i) {
|
||
|
System.<span class="ST5">out</span>.println(<span class="string">"</span><span class="string">---------- </span><span class="string">"</span> + <span class="string">"</span><span class="string">printing row </span><span class="string">"</span> + (i + <span class="number">1</span>) + <span class="string">"</span><span class="string"> ----------</span><span class="string">"</span>);
|
||
|
<span class="literal">for</span> (<span class="literal">int</span> j = <span class="number">0</span>; j < stores.get(i).size(); ++j) {
|
||
|
System.<span class="ST5">out</span>.println(stores.get(i).get(j) + <span class="string">"</span> <span class="string">"</span>);
|
||
|
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST6">printSingle</span>(ArrayList<Bicycle> stores) {
|
||
|
System.<span class="ST5">out</span>.println(<span class="string">"</span><span class="string">----------</span><span class="string">"</span> + <span class="string">"</span><span class="string">printSingle</span><span class="string">"</span> + <span class="string">"</span><span class="string">----------</span><span class="string">"</span>);
|
||
|
<span class="literal">for</span> (<span class="literal">int</span> j = <span class="number">0</span>; j < stores.size(); ++j) {
|
||
|
System.<span class="ST5">out</span>.println(stores.get(j) + <span class="string">"</span> <span class="string">"</span>);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST6">printRank</span>(ArrayList<Bicycle> stores) {
|
||
|
System.<span class="ST5">out</span>.println(<span class="string">"</span><span class="string">----------</span><span class="string">"</span> + <span class="string">"</span><span class="string">printRank</span><span class="string">"</span> + <span class="string">"</span><span class="string">----------</span><span class="string">"</span>);
|
||
|
<span class="literal">for</span> (<span class="literal">int</span> j = <span class="number">0</span>; j < stores.size(); ++j) {
|
||
|
System.<span class="ST5">out</span>.println(stores.get(j) + <span class="string">"</span> <span class="string">"</span> + stores.get(j).calculatedDetails().getRank());
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
<span class="comment">/**</span>
|
||
|
<span class="comment"> * </span><span class="comment">Groups</span> <span class="comment">bikes</span> <span class="comment">by</span> <span class="comment">type</span> <span class="comment">ans</span> <span class="comment">sorts</span> <span class="comment">them</span> <span class="comment">by</span> <span class="comment">the</span> <span class="comment">ranking</span> <span class="comment">of</span> <span class="comment">Details</span><span class="comment">.</span> <span class="comment">There</span> <span class="comment">will</span> <span class="comment">be</span> <span class="comment">three</span> <span class="comment">groups</span> <span class="comment">and</span> <span class="comment">each</span> <span class="comment">group</span> <span class="comment">stored</span> <span class="comment">in</span> <span class="comment">descending</span> <span class="comment">order</span> <span class="comment">by</span> <span class="comment">rank</span><span class="comment">.</span>
|
||
|
<span class="comment"> *</span>
|
||
|
<span class="comment"> * </span><span class="ST1">@param</span> <span class="ST3">stores</span> <span class="comment">the</span> <span class="comment">stores</span> <span class="comment">of</span> <span class="comment">bikes</span>
|
||
|
<span class="comment"> * </span><span class="ST1">@return</span> <span class="comment">a</span> <span class="comment">newly</span> <span class="comment">created</span> <span class="comment">ArrayList</span><span class="ST7"><ArrayList</span><span class="ST7"><Bicycle></span><span class="comment">> </span><span class="comment">with</span> <span class="comment">the</span> <span class="comment">bikes</span> <span class="comment">sorted</span><span class="comment">.</span>
|
||
|
<span class="comment">*/</span>
|
||
|
<span class="literal">public</span> <span class="literal">static</span> ArrayList<ArrayList<Bicycle>> <span class="ST6">sortBikesByType</span>(<span class="literal">final</span> ArrayList<ArrayList<Bicycle>> stores) {
|
||
|
ArrayList<ArrayList<Bicycle>> newStore = <span class="literal">new</span> ArrayList<ArrayList<Bicycle>>();
|
||
|
<span class="comment">// group arrayLists</span>
|
||
|
ArrayList<Bicycle> mountainBikes = <span class="literal">new</span> ArrayList<Bicycle>();
|
||
|
ArrayList<Bicycle> speedBikes = <span class="literal">new</span> ArrayList<Bicycle>();
|
||
|
ArrayList<Bicycle> childBikes = <span class="literal">new</span> ArrayList<Bicycle>();
|
||
|
System.<span class="ST5">out</span>.println(<span class="string">"</span><span class="string">---------- </span><span class="string">"</span> + <span class="string">"</span><span class="string">grouping by bike type... </span><span class="string">"</span> + <span class="string">"</span><span class="string"> ----------</span><span class="string">"</span>);
|
||
|
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i < stores.size(); ++i) {
|
||
|
<span class="literal">for</span> (<span class="literal">int</span> j = <span class="number">0</span>; j < stores.get(i).size(); ++j) {
|
||
|
<span class="comment">// System.out.println(stores.get(i).get(j).calculatedDetails().getRank() + " ");</span>
|
||
|
Bicycle bike = stores.get(i).get(j);
|
||
|
<span class="literal">if</span> (bike <span class="literal">instanceof</span> ChildBike) {
|
||
|
childBikes.add(bike);
|
||
|
} <span class="literal">else</span> <span class="literal">if</span> (bike <span class="literal">instanceof</span> MountainBike) {
|
||
|
mountainBikes.add(bike);
|
||
|
} <span class="literal">else</span> <span class="literal">if</span> (bike <span class="literal">instanceof</span> SpeedBike) {
|
||
|
speedBikes.add(bike);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
<span class="comment">//printRank(childBikes);</span>
|
||
|
<span class="comment">//printRank(mountainBikes);</span>
|
||
|
<span class="comment">//printRank(speedBikes);</span>
|
||
|
System.<span class="ST5">out</span>.println(<span class="string">"</span><span class="string">sorting...</span><span class="string">"</span>);
|
||
|
<span class="ST8">sortType</span>(childBikes);
|
||
|
<span class="ST8">sortType</span>(mountainBikes);
|
||
|
<span class="ST8">sortType</span>(speedBikes);
|
||
|
System.<span class="ST5">out</span>.println(<span class="string">"</span><span class="string">sorted</span><span class="string">"</span>);
|
||
|
<span class="comment">//printRank(childBikes);</span>
|
||
|
<span class="comment">//printRank(mountainBikes);</span>
|
||
|
<span class="comment">//printRank(speedBikes);</span>
|
||
|
newStore.add(childBikes);
|
||
|
newStore.add(mountainBikes);
|
||
|
newStore.add(speedBikes);
|
||
|
<span class="literal">return</span> newStore;
|
||
|
}
|
||
|
|
||
|
<span class="literal">private</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST6">sortType</span>(ArrayList<Bicycle> arrList) {
|
||
|
<span class="literal">for</span> (<span class="literal">int</span> x = <span class="number">0</span>; x < arrList.size(); ++x) {
|
||
|
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i < arrList.size(); ++i) {
|
||
|
<span class="literal">for</span> (<span class="literal">int</span> j = i + <span class="number">1</span>; j < arrList.size(); ++j) {
|
||
|
<span class="literal">int</span> compare1 = arrList.get(i).calculatedDetails().getRank();
|
||
|
<span class="literal">int</span> compare2 = arrList.get(j).calculatedDetails().getRank();
|
||
|
<span class="literal">if</span> (compare1 < compare2) {
|
||
|
Bicycle tmp = arrList.get(i);
|
||
|
arrList.set(i, arrList.get(j));
|
||
|
arrList.set(j, tmp);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST6">print</span>(Object[] arr) {
|
||
|
<span class="literal">for</span> (Object o : arr) {
|
||
|
System.<span class="ST5">out</span>.println(o);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST6">print</span>(Object[][] arr) {
|
||
|
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i < arr.<span class="ST2">length</span>; i++) {
|
||
|
System.<span class="ST5">out</span>.println(<span class="string">"</span><span class="string">---------- </span><span class="string">"</span> + <span class="string">"</span><span class="string">printing row </span><span class="string">"</span> + (i + <span class="number">1</span>) + <span class="string">"</span><span class="string"> ----------</span><span class="string">"</span>);
|
||
|
<span class="ST8">print</span>(arr[i]);
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
<span class="comment">/**</span>
|
||
|
<span class="comment"> * </span><span class="comment">Returns</span> <span class="comment">a</span><span class="comment"> 2</span><span class="comment">D</span> <span class="comment">array</span> <span class="comment">containing</span> <span class="comment">all</span> <span class="comment">the</span> <span class="comment">bikes</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">store</span> <span class="comment">in</span> <span class="comment">proper</span> <span class="comment">sequence</span><span class="comment"> (</span><span class="comment">from</span> <span class="comment">first</span> <span class="comment">to</span> <span class="comment">last</span> <span class="comment">element</span><span class="comment">)</span><span class="comment">.</span> <span class="comment">Store</span><span class="comment"> 0 </span><span class="comment">with</span> <span class="comment">all</span> <span class="comment">its</span> <span class="comment">bikes</span><span class="comment">, </span><span class="comment">store</span><span class="comment"> 1 </span><span class="comment">with</span> <span class="comment">all</span> <span class="comment">its</span> <span class="comment">bikes</span><span class="comment">, </span><span class="comment">and</span> <span class="comment">so</span> <span class="comment">on</span><span class="comment">.</span>
|
||
|
<span class="comment"> *</span>
|
||
|
<span class="comment"> * </span><span class="ST1">@return</span> <span class="comment">a</span><span class="comment"> 2</span><span class="comment">D</span> <span class="comment">array</span> <span class="comment">of</span> <span class="comment">all</span> <span class="comment">stores</span> <span class="comment">with</span> <span class="comment">bikes</span><span class="comment">.</span>
|
||
|
<span class="comment">*/</span>
|
||
|
<span class="literal">public</span> Object[][] <span class="ST4">toArray</span>() {
|
||
|
Object[][] arr = <span class="literal">new</span> Object[<span class="ST2">storesOfBikes</span>.size()][];
|
||
|
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i < <span class="ST2">storesOfBikes</span>.size(); i++) {
|
||
|
arr[i] = <span class="ST2">storesOfBikes</span>.toArray();
|
||
|
}
|
||
|
<span class="literal">return</span> arr;
|
||
|
}
|
||
|
|
||
|
<span class="comment">/**</span>
|
||
|
<span class="comment"> * </span><span class="comment">Retains</span> <span class="comment">only</span> <span class="comment">the</span> <span class="comment">Bicycles</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">stores</span> <span class="comment">that</span> <span class="comment">are</span> <span class="comment">contained</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">collection</span><span class="comment">.</span> <span class="comment">In</span> <span class="comment">other</span> <span class="comment">words</span><span class="comment">, </span><span class="comment">removes</span> <span class="comment">from</span> <span class="comment">this</span> <span class="comment">list</span> <span class="comment">all</span> <span class="comment">of</span> <span class="comment">bikes</span> <span class="comment">that</span> <span class="comment">are</span> <span class="comment">not</span> <span class="comment">contained</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">specified</span> <span class="comment">location</span><span class="comment">.</span>
|
||
|
<span class="comment"> *</span>
|
||
|
<span class="comment"> * </span><span class="ST1">@param</span> <span class="ST3">c</span> <span class="comment">the</span> <span class="comment">bikes</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">removed</span>
|
||
|
<span class="comment"> * </span><span class="ST1">@return</span> <span class="comment">true</span> <span class="comment">if</span> <span class="comment">any</span> <span class="comment">store</span> <span class="comment">changed</span> <span class="comment">as</span> <span class="comment">a</span> <span class="comment">result</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">call</span><span class="comment">.</span>
|
||
|
<span class="comment">*/</span>
|
||
|
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST4">retainAll</span>(Collection<Bicycle> c) {
|
||
|
<span class="literal">boolean</span> modified = <span class="literal">false</span>;
|
||
|
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="ST2">storesOfBikes</span>.size() - <span class="number">1</span>; i >= <span class="number">0</span>; i--) {
|
||
|
ArrayList<Bicycle> bikes = <span class="ST2">storesOfBikes</span>.get(i);
|
||
|
<span class="literal">for</span> (<span class="literal">int</span> j = bikes.size() - <span class="number">1</span>; j >= <span class="number">0</span>; j--) {
|
||
|
Bicycle bike = bikes.get(j);
|
||
|
<span class="literal">if</span> (!c.contains(bike)) {
|
||
|
bikes.remove(j);
|
||
|
System.<span class="ST5">out</span>.println(<span class="string">"</span><span class="string">Removing </span><span class="string">"</span> + bike);
|
||
|
modified = <span class="literal">true</span>;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
System.<span class="ST5">out</span>.println(<span class="string">"</span><span class="string">Bikes to remove:</span><span class="string">"</span>);
|
||
|
<span class="ST8">printSingle</span>((ArrayList<Bicycle>) c);
|
||
|
<span class="literal">return</span> modified;
|
||
|
}
|
||
|
|
||
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST6">main</span>(String[] args) {
|
||
|
BikeStores bikes = <span class="literal">new</span> BikeStores();
|
||
|
<span class="comment">//add 5 bikes to store 0 ( 2 speedBikes, 2 mountain 1 child)</span>
|
||
|
bikes.addBike(<span class="number">0</span>, <span class="literal">new</span> SpeedBike(<span class="number">4</span>, <span class="number">3</span>, <span class="number">1</span><span class="number">0</span>, <span class="number">4</span><span class="number">0</span>));
|
||
|
bikes.addBike(<span class="number">0</span>, <span class="literal">new</span> SpeedBike(<span class="number">1</span>, <span class="number">2</span>, <span class="number">1</span><span class="number">0</span>, <span class="number">3</span><span class="number">0</span>));
|
||
|
bikes.addBike(<span class="number">0</span>, <span class="literal">new</span> MountainBike(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">2</span><span class="number">0</span>));
|
||
|
bikes.addBike(<span class="number">0</span>, <span class="literal">new</span> MountainBike(<span class="number">3</span>, <span class="number">2</span>, <span class="number">1</span><span class="number">0</span>, <span class="number">2</span><span class="number">5</span>));
|
||
|
bikes.addBike(<span class="number">0</span>, <span class="literal">new</span> ChildBike(<span class="literal">f</span><span class="literal">alse</span>, <span class="number">1</span>, <span class="number">1</span>, <span class="number">1</span><span class="number">0</span>));
|
||
|
<span class="comment">//add 5 bikes to store 1 ( 3 speedbikes, 1 mountain 1 child)</span>
|
||
|
bikes.addBike(<span class="number">1</span>, <span class="literal">new</span> SpeedBike(<span class="number">1</span><span class="number">0</span>, <span class="number">4</span>, <span class="number">1</span><span class="number">0</span>, <span class="number">2</span><span class="number">0</span>));
|
||
|
bikes.addBike(<span class="number">1</span>, <span class="literal">new</span> SpeedBike(<span class="number">0</span>, <span class="number">2</span>, <span class="number">1</span><span class="number">0</span>, <span class="number">5</span><span class="number">0</span>));
|
||
|
bikes.addBike(<span class="number">1</span>, <span class="literal">new</span> SpeedBike(<span class="number">3</span>, <span class="number">2</span>, <span class="number">1</span><span class="number">0</span>, <span class="number">3</span><span class="number">8</span>));
|
||
|
bikes.addBike(<span class="number">1</span>, <span class="literal">new</span> MountainBike(<span class="number">1</span>, <span class="number">3</span>, <span class="number">5</span>, <span class="number">4</span><span class="number">4</span>));
|
||
|
bikes.addBike(<span class="number">1</span>, <span class="literal">new</span> ChildBike(<span class="literal">t</span><span class="literal">rue</span>, <span class="number">1</span>, <span class="number">1</span>, <span class="number">1</span><span class="number">5</span>));
|
||
|
<span class="comment">//add 6 bikes to store 2 ( 2 speedBikes, 2 mountain 2 child )</span>
|
||
|
bikes.addBike(<span class="number">2</span>, <span class="literal">new</span> SpeedBike(<span class="number">7</span>, <span class="number">1</span><span class="number">0</span>, <span class="number">2</span><span class="number">0</span>, <span class="number">2</span><span class="number">5</span>));
|
||
|
bikes.addBike(<span class="number">2</span>, <span class="literal">new</span> SpeedBike(<span class="number">0</span>, <span class="number">8</span>, <span class="number">4</span><span class="number">0</span>, <span class="number">5</span><span class="number">5</span>)); <span class="comment">// retainAll should match this one</span>
|
||
|
bikes.addBike(<span class="number">2</span>, <span class="literal">new</span> MountainBike(<span class="number">2</span>, <span class="number">3</span>, <span class="number">1</span><span class="number">5</span>, <span class="number">3</span><span class="number">3</span>));
|
||
|
bikes.addBike(<span class="number">2</span>, <span class="literal">new</span> MountainBike(<span class="number">1</span>, <span class="number">3</span>, <span class="number">1</span><span class="number">5</span>, <span class="number">2</span><span class="number">4</span>));
|
||
|
bikes.addBike(<span class="number">2</span>, <span class="literal">new</span> ChildBike(<span class="literal">t</span><span class="literal">rue</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">2</span><span class="number">0</span>));
|
||
|
bikes.addBike(<span class="number">2</span>, <span class="literal">new</span> ChildBike(<span class="literal">f</span><span class="literal">alse</span>, <span class="number">1</span>, <span class="number">1</span>, <span class="number">1</span><span class="number">8</span>));<span class="comment">// retainAll should match this one</span>
|
||
|
<span class="comment">//remove one child bike from store 2</span>
|
||
|
bikes.removeBike(<span class="number">2</span>, <span class="literal">new</span> ChildBike(<span class="literal">t</span><span class="literal">rue</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">2</span><span class="number">0</span>)); <span class="comment">// Java reuses the object created earlier since it has the same values.</span>
|
||
|
<span class="comment">//PRINT</span>
|
||
|
<span class="ST8">print</span>(bikes.<span class="ST2">storesOfBikes</span>);
|
||
|
<span class="comment">//SORT</span>
|
||
|
ArrayList<ArrayList<Bicycle>> sortedBikes = <span class="ST8">sortBikesByType</span>(bikes.<span class="ST2">storesOfBikes</span>);
|
||
|
<span class="comment">//PRINT what the method return</span>
|
||
|
System.<span class="ST5">out</span>.println(<span class="string">"</span><span class="string">SORTED BIKES</span><span class="string">"</span>);
|
||
|
<span class="ST8">print</span>(sortedBikes);
|
||
|
<span class="comment">//PRINT the original store</span>
|
||
|
System.<span class="ST5">out</span>.println(<span class="string">"</span><span class="string">ORIGINAL STORE ARRAYLIST</span><span class="string">"</span>);
|
||
|
<span class="ST8">print</span>(bikes.<span class="ST2">storesOfBikes</span>);
|
||
|
<span class="comment">// ---------- TEST retainAll ----------</span>
|
||
|
System.<span class="ST5">out</span>.println(<span class="string">"</span><span class="string">Testing retainAll();...</span><span class="string">"</span>);
|
||
|
Collection<Bicycle> c1 = <span class="literal">new</span> ArrayList<Bicycle>();
|
||
|
c1.add(<span class="literal">new</span> SpeedBike(<span class="number">0</span>, <span class="number">8</span>, <span class="number">4</span><span class="number">0</span>, <span class="number">5</span><span class="number">5</span>));
|
||
|
c1.add(<span class="literal">new</span> ChildBike(<span class="literal">f</span><span class="literal">alse</span>, <span class="number">1</span>, <span class="number">1</span>, <span class="number">1</span><span class="number">8</span>));
|
||
|
Bicycle b1 = bikes.<span class="ST2">storesOfBikes</span>.get(<span class="number">2</span>).get(<span class="number">1</span>);
|
||
|
Bicycle b2 = bikes.<span class="ST2">storesOfBikes</span>.get(<span class="number">2</span>).get(<span class="number">4</span>);
|
||
|
Bicycle b3 = ((ArrayList<Bicycle>) c1).get(<span class="number">0</span>);
|
||
|
Bicycle b4 = ((ArrayList<Bicycle>) c1).get(<span class="number">1</span>);
|
||
|
|
||
|
System.<span class="ST5">out</span>.println(b1.equals(b3) + <span class="string">"</span> <span class="string">"</span> + b2.equals(b4));
|
||
|
bikes.retainAll(c1);
|
||
|
<span class="ST8">print</span>(bikes.<span class="ST2">storesOfBikes</span>);
|
||
|
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
<span class="literal">class</span> <span class="comment">NotABike</span> {
|
||
|
|
||
|
}
|
||
|
|
||
|
</pre></body>
|
||
|
</html>
|