From 74cdd20296147ef692c38585ef004cc75abe9ab9 Mon Sep 17 00:00:00 2001 From: Orange Date: Tue, 22 Nov 2016 18:43:02 +0100 Subject: [PATCH] add sort --- src/Concessionaria/Concessionaria.java | 33 ++++++++++++++++++++++++++ src/Concessionaria/Test.java | 6 ++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/Concessionaria/Concessionaria.java b/src/Concessionaria/Concessionaria.java index 888fcbb..d428313 100644 --- a/src/Concessionaria/Concessionaria.java +++ b/src/Concessionaria/Concessionaria.java @@ -34,6 +34,39 @@ public class Concessionaria{ } } + public boolean swap(int i){ + Auto tmp=auto.get(i); + auto.set(i, auto.get(i+1)); + auto.set(i+1, tmp); + return false; + } + + public Concessionaria sortDecrescente(){ + boolean ordinato; + int j=auto.size()-1; + do{ + ordinato=true; + for(int i=0; i1 && !ordinato); + + return new Concessionaria(auto); + } + + public Concessionaria sortCrescente(){ + boolean ordinato; + int j=auto.size()-1; + do{ + ordinato=true; + for(int i=0; iauto.get(i+1).getPrezzo()) + ordinato=swap(i); + }while(j-->1 && !ordinato); + + return new Concessionaria(auto); + } + public Concessionaria filtroUsato(){ ArrayList autoFilter = new ArrayList(); for(Auto a: auto) diff --git a/src/Concessionaria/Test.java b/src/Concessionaria/Test.java index feecd68..51c26d7 100644 --- a/src/Concessionaria/Test.java +++ b/src/Concessionaria/Test.java @@ -7,6 +7,10 @@ public class Test{ Scanner scNuove = new Scanner(new File("nuove.dat")); Scanner scUsate = new Scanner(new File("usate.dat")); Concessionaria con = new Concessionaria(scNuove, scUsate); - con.filtroAnnoImmatricolazione(2015).print(System.out); + //con.filtroAnnoImmatricolazione(2015).print(System.out); + System.out.println("Decrescente"); + con.sortDecrescente().print(System.out); + System.out.println("Crescente"); + con.sortCrescente().print(System.out); } } \ No newline at end of file