M Winda/src/winda/logic/AlgorytmGoraDol.java
A Winda/src/winda/logic/PasazerSort.java
This commit is contained in:
parent
c12def7212
commit
c461ef0550
2 changed files with 125 additions and 4 deletions
|
@ -5,18 +5,88 @@
|
|||
|
||||
package winda.logic;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import sun.misc.Sort;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Tomek
|
||||
* Algortym poruszania się windą przy wsiadaniu pasażerów
|
||||
* jadących tylko w kierunku w którym porusza się winda.
|
||||
* @author Przemo
|
||||
*/
|
||||
public class AlgorytmGoraDol implements IAlgorytm {
|
||||
private int start_floor;
|
||||
private int floor_count;
|
||||
private ArrayList<Integer> floors;
|
||||
private Pasazer[] pasazerowie;
|
||||
private int acctual_floor;
|
||||
|
||||
public AlgorytmGoraDol(int floor_count){
|
||||
this.start_floor = 0;
|
||||
this.floor_count = floor_count;
|
||||
}
|
||||
|
||||
public AlgorytmGoraDol(int floor_count, int start_floor){
|
||||
this.floor_count = floor_count;
|
||||
this.start_floor = start_floor;
|
||||
}
|
||||
|
||||
public void setStartFloor(int start_floor){
|
||||
this.start_floor = start_floor;
|
||||
}
|
||||
|
||||
public int getStartFloor(){
|
||||
return this.start_floor;
|
||||
}
|
||||
|
||||
public void setFloorCount(int floor_count){
|
||||
this.floor_count = floor_count;
|
||||
}
|
||||
|
||||
public int getFloorCount(int floor_count){
|
||||
return this.floor_count;
|
||||
}
|
||||
|
||||
private void sortUp(ArrayList<Integer> array){
|
||||
Sort.quicksort(array.toArray(), null);
|
||||
}
|
||||
|
||||
private void goUp(){
|
||||
int i =0;
|
||||
ArrayList<Integer> tmp = new ArrayList<Integer>();
|
||||
while(this.pasazerowie[i].GetStart()<this.acctual_floor)
|
||||
i++;
|
||||
while(i<this.pasazerowie.length)
|
||||
if(this.pasazerowie[i].GetStart()<this.pasazerowie[i].GetStop()){
|
||||
if(tmp.contains(this.pasazerowie[i].GetStart()))
|
||||
tmp.add(this.pasazerowie[i].GetStart());
|
||||
if(tmp.contains(this.pasazerowie[i].GetStop()))
|
||||
tmp.add(this.pasazerowie[i].GetStop());
|
||||
}
|
||||
this.sortUp(tmp);
|
||||
this.floors.addAll(tmp);
|
||||
}
|
||||
|
||||
public int[] setCourse(){
|
||||
this.floors = new ArrayList<Integer>();
|
||||
floors.add(this.start_floor);
|
||||
|
||||
PasazerSort sort = new PasazerSort(pasazerowie);
|
||||
sort.sortByStart();
|
||||
this.acctual_floor = this.start_floor;
|
||||
|
||||
int []ret = new int[floors.size()];
|
||||
for(int i=0;i<floors.size();i++)
|
||||
ret[i] = floors.get(i).intValue();
|
||||
return ret;
|
||||
}
|
||||
|
||||
public int[] Trasa(Pasazer[] pasazerowie) {
|
||||
throw new UnsupportedOperationException("Not supported yet.");
|
||||
this.pasazerowie = pasazerowie;
|
||||
return this.setCourse();
|
||||
}
|
||||
|
||||
public void SetMaxPietro(int maxPietro) {
|
||||
throw new UnsupportedOperationException("Not supported yet.");
|
||||
this.setFloorCount(floor_count);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
51
Winda/src/winda/logic/PasazerSort.java
Normal file
51
Winda/src/winda/logic/PasazerSort.java
Normal file
|
@ -0,0 +1,51 @@
|
|||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package winda.logic;
|
||||
|
||||
/**
|
||||
* Sortowanie pasażerów oparta o quicksort
|
||||
* @author nerull7
|
||||
*/
|
||||
public class PasazerSort {
|
||||
Pasazer []array;
|
||||
|
||||
public PasazerSort(Pasazer []array){
|
||||
this.array = array;
|
||||
}
|
||||
|
||||
private int splitByStart(int start, int end){
|
||||
int i,j;
|
||||
|
||||
for( i = 1+ (j = start);i<end;i++)
|
||||
if(this.array[i].GetStart()<this.array[start].GetStart()){
|
||||
Pasazer tmp = this.array[++j];
|
||||
this.array[++j] = this.array[i];
|
||||
this.array[i] = tmp;
|
||||
}
|
||||
|
||||
Pasazer tmp = this.array[start];
|
||||
this.array[start] = this.array[j];
|
||||
this.array[j] = tmp;
|
||||
|
||||
return j;
|
||||
}
|
||||
|
||||
private void mksortByStart(int start, int end){
|
||||
if( start < end ){
|
||||
int tmp = this.splitByStart( start, end );
|
||||
this.mksortByStart( start, tmp-1 );
|
||||
this.mksortByStart( tmp+1, end );
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* QuickSort pasażerów po numerze piętra na którym wsiadają.
|
||||
*/
|
||||
|
||||
public void sortByStart(){
|
||||
this.mksortByStart( 0, this.array.length );
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue