Opzione 1 + menù migliorato
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
package rettangoli_quadrati;
|
||||
|
||||
import java.util.Scanner;
|
||||
import java.util.InputMismatchException;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -19,44 +20,51 @@ public class Rettangoli_Quadrati {
|
||||
static Scanner sc = new Scanner(System.in);
|
||||
|
||||
public static void main(String[] args) {
|
||||
int scelta;
|
||||
Rettangolo forme[] = Init();
|
||||
int scelta, nextPosizioneLibera = 0;
|
||||
|
||||
do {
|
||||
System.out.println("Scegliere un'opzione:");
|
||||
System.out.println("1. Ingresso auto");
|
||||
System.out.println("2. Stampa posti occupati");
|
||||
System.out.println("3. Stampa posti liberi");
|
||||
System.out.println("4. Uscita auto");
|
||||
System.out.println("1. Inserimento dimensioni");
|
||||
System.out.println("2. Confronta aree");
|
||||
System.out.println("0. Esci");
|
||||
System.out.print("Opzione: ");
|
||||
|
||||
scelta = sc.nextInt();
|
||||
sc.nextLine();
|
||||
try {
|
||||
scelta = sc.nextInt();
|
||||
sc.nextLine();
|
||||
|
||||
switch (scelta) {
|
||||
case 0:
|
||||
break;
|
||||
case 1:
|
||||
System.out.println(garage.IngressoAuto());
|
||||
Pausa();
|
||||
break;
|
||||
case 2:
|
||||
System.out.println(garage.GetPostiOccupati());
|
||||
Pausa();
|
||||
break;
|
||||
case 3:
|
||||
System.out.println(garage.GetPostiLiberi());
|
||||
Pausa();
|
||||
break;
|
||||
case 4:
|
||||
System.out.println(garage.UscitaAuto());
|
||||
Pausa();
|
||||
break;
|
||||
default:
|
||||
System.out.println("Opzione non valida.");
|
||||
Pausa();
|
||||
break;
|
||||
switch (scelta) {
|
||||
case 0:
|
||||
break;
|
||||
case 1:
|
||||
forme[nextPosizioneLibera] = CreaRettangolo(IngressoDati(1), IngressoDati(2));
|
||||
forme[nextPosizioneLibera + 1] = CreaQuadrato(IngressoDati(3));
|
||||
nextPosizioneLibera = +2;
|
||||
System.out.println("Forme create con successo");
|
||||
Pausa();
|
||||
break;
|
||||
case 2:
|
||||
// System.out.println(garage.GetPostiOccupati());
|
||||
//Debug: Controllo che funzioni tutto
|
||||
System.out.println(forme[0].dimensioneUno);
|
||||
System.out.println(forme[0].dimensioneDue);
|
||||
System.out.println(forme[0].isQuadrato);
|
||||
System.out.println(forme[1].dimensioneUno);
|
||||
System.out.println(forme[1].dimensioneDue);
|
||||
System.out.println(forme[1].isQuadrato);
|
||||
Pausa();
|
||||
break;
|
||||
default:
|
||||
System.out.println("Opzione non valida.");
|
||||
Pausa();
|
||||
break;
|
||||
|
||||
}
|
||||
} catch (InputMismatchException e) {
|
||||
System.out.println("Opzione non valida.");
|
||||
Pausa();
|
||||
scelta = -1;
|
||||
}
|
||||
} while (scelta != 0);
|
||||
}
|
||||
@@ -65,4 +73,73 @@ public class Rettangoli_Quadrati {
|
||||
System.out.println("Premere un tasto per continuare. . .");
|
||||
sc.nextLine();
|
||||
}
|
||||
|
||||
static Rettangolo[] Init() {
|
||||
boolean error;
|
||||
int dimensione = 0;
|
||||
|
||||
do {
|
||||
error = false;
|
||||
try {
|
||||
System.out.println("Quante coppie di forme gestire? ");
|
||||
|
||||
dimensione = sc.nextInt();
|
||||
sc.nextLine();
|
||||
|
||||
if (dimensione <= 0) {
|
||||
System.out.println("Errore: inserire una dimensione "
|
||||
+ (dimensione == 0 ? "diversa da" : "maggiore di") + " 0.");
|
||||
Pausa();
|
||||
error = true;
|
||||
}
|
||||
} catch (InputMismatchException e) {
|
||||
System.out.println("Errore: dimensione non valida.");
|
||||
Pausa();
|
||||
error = true;
|
||||
}
|
||||
} while (error);
|
||||
|
||||
return new Rettangolo[dimensione];
|
||||
}
|
||||
|
||||
static double IngressoDati(int p_selettore) {
|
||||
double ritorno = 0;
|
||||
boolean error;
|
||||
|
||||
do {
|
||||
error = false;
|
||||
try {
|
||||
if (p_selettore == 1) {
|
||||
System.out.println("Inserisci la prima dimensione del rettangolo: ");
|
||||
} else if (p_selettore == 2) {
|
||||
System.out.println("Inserisci la seconda dimensione del rettangolo: ");
|
||||
} else if (p_selettore == 3) {
|
||||
System.out.println("Inserisci dimensione del quadrato: ");
|
||||
}
|
||||
|
||||
ritorno = sc.nextDouble();
|
||||
sc.nextLine();
|
||||
|
||||
if (ritorno <= 0) {
|
||||
System.out.println("Errore: inserire una dimensione "
|
||||
+ (ritorno == 0 ? "diversa da" : "maggiore di") + " 0.");
|
||||
Pausa();
|
||||
error = true;
|
||||
}
|
||||
} catch (InputMismatchException e) {
|
||||
System.out.println("Errore: dimensione non valida.");
|
||||
Pausa();
|
||||
error = true;
|
||||
}
|
||||
} while (error);
|
||||
return ritorno;
|
||||
}
|
||||
|
||||
static Rettangolo CreaRettangolo(double p_dimensioneUno, double p_dimensioneDue) {
|
||||
return new Rettangolo(p_dimensioneUno, p_dimensioneDue, false);
|
||||
}
|
||||
|
||||
static Rettangolo CreaQuadrato( double p_dimensione) {
|
||||
return new Rettangolo(p_dimensione, p_dimensione, true);
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,9 @@
|
||||
package rettangoli_quadrati;
|
||||
|
||||
public class Rettangolo {
|
||||
private boolean isQuadrato;
|
||||
private double dimensioneUno, dimensioneDue;
|
||||
//Debug: public per testare che funzioni tutto
|
||||
public boolean isQuadrato;
|
||||
public double dimensioneUno, dimensioneDue;
|
||||
|
||||
public Rettangolo(double p_dimensioneUno, double p_dimensioneDue, boolean p_isQuadrato) {
|
||||
this.dimensioneUno = p_dimensioneUno;
|
||||
|
||||
Reference in New Issue
Block a user