Creazione Fattura + fix
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
fatture/
|
||||
scontrini/
|
||||
@@ -31,6 +31,7 @@ public class FatturaScontrini {
|
||||
static Scanner sc = new Scanner(System.in);
|
||||
static final String PATH_MENU = "./src/fatturascontrini/menu.txt";
|
||||
static final String PATH_SCONTRINI = "./src/fatturascontrini/scontrini/";
|
||||
static final String PATH_FATTURA = "./src/fatturascontrini/fatture/";
|
||||
static final String ERRORE_DEFAULT = "Errore: opzione non valida.";
|
||||
static final String ERRORE_FILE = "Errore nella lettura del file: ";
|
||||
static int codiceScontrino = 0;
|
||||
@@ -38,8 +39,6 @@ public class FatturaScontrini {
|
||||
public static void main(String[] args) {
|
||||
int scelta = -1;
|
||||
ArrayList<int[]> ordinazione = null;
|
||||
boolean pagamentoEffettuato = false;
|
||||
|
||||
|
||||
do {
|
||||
System.out.println("Scegliere un'opzione:");
|
||||
@@ -63,7 +62,6 @@ public class FatturaScontrini {
|
||||
case 2:
|
||||
if (ordinazione != null) {
|
||||
pagare(ordinazione);
|
||||
pagamentoEffettuato = true;
|
||||
} else {
|
||||
System.out.println(
|
||||
"Errore: è necessario effettuare un'ordinazione prima di procedere al pagamento.");
|
||||
@@ -71,8 +69,8 @@ public class FatturaScontrini {
|
||||
pausa();
|
||||
break;
|
||||
case 3:
|
||||
if (pagamentoEffettuato) {
|
||||
trovaScontriniPerData();
|
||||
if (!trovaScontrini().isEmpty()) {
|
||||
fattura();
|
||||
} else {
|
||||
System.out.println(
|
||||
"Errore: è necessario pagare prima di procedere alla creazione della fattura.");
|
||||
@@ -182,7 +180,7 @@ public class FatturaScontrini {
|
||||
ritorno = sc.nextInt();
|
||||
sc.nextLine();
|
||||
|
||||
if (ritorno < 0 || ritorno > getMenuSize()) {
|
||||
if (ritorno < 0 || ritorno >= getMenuSize()) {
|
||||
System.out.println(ERRORE_DEFAULT);
|
||||
pausa();
|
||||
error = true;
|
||||
@@ -240,7 +238,7 @@ public class FatturaScontrini {
|
||||
ritorno = sc.nextInt();
|
||||
sc.nextLine();
|
||||
|
||||
if (ritorno < 0) {
|
||||
if (ritorno <= 0) {
|
||||
System.out.println("Errore: non è possibile selezionare una quantità minore di zero.");
|
||||
pausa();
|
||||
error = true;
|
||||
@@ -363,7 +361,7 @@ public class FatturaScontrini {
|
||||
return ritorno;
|
||||
}
|
||||
|
||||
static void trovaScontriniPerData() {
|
||||
static void fattura() {
|
||||
boolean error;
|
||||
String formatoDataInserimento = "dd/MM/yyyy";
|
||||
String formatoDataScontrini = "yyyy_MM_dd";
|
||||
@@ -375,7 +373,7 @@ public class FatturaScontrini {
|
||||
do {
|
||||
error = false;
|
||||
System.out.print("Inserire la data per cui fatturare nel formato "
|
||||
+ formatoDataInserimento.replace("d", "g").replace("y", "a") + ": ");
|
||||
+ formatoDataInserimento.replace("d", "g").replace("M", "m").replace("y", "a") + ": ");
|
||||
try {
|
||||
dataRicerca = sdf.parse(sc.nextLine());
|
||||
} catch (ParseException _) {
|
||||
@@ -388,6 +386,13 @@ public class FatturaScontrini {
|
||||
sdf = new SimpleDateFormat(formatoDataScontrini);
|
||||
|
||||
listaScontrini = trovaScontriniPerData(trovaScontrini(), sdf.format(dataRicerca));
|
||||
|
||||
if (listaScontrini.isEmpty()) {
|
||||
System.out.println("Errore: non sono stati trovati scontrini per la data inserita.");
|
||||
}
|
||||
else{
|
||||
stampaFattura(new Fattura(listaScontrini.size(), calcolaTotaleScontrini(listaScontrini)), sdf.format(dataRicerca));
|
||||
}
|
||||
}
|
||||
|
||||
static ArrayList<File> trovaScontrini() {
|
||||
@@ -397,8 +402,7 @@ public class FatturaScontrini {
|
||||
if (listaFile != null) {
|
||||
for (int i = 0; i < listaFile.length; i++) {
|
||||
if (listaFile[i].isFile() &&
|
||||
listaFile[i].getName().split("_")[0].equals("scontrino") &&
|
||||
listaFile[i].getName().split(".")[1].equals("txt")) {
|
||||
listaFile[i].getName().split("_")[0].equals("scontrino")) {
|
||||
ritorno.add(listaFile[i]);
|
||||
}
|
||||
}
|
||||
@@ -409,13 +413,46 @@ public class FatturaScontrini {
|
||||
|
||||
static ArrayList<File> trovaScontriniPerData(ArrayList<File> scontrini, String data) {
|
||||
ArrayList<File> ritorno = new ArrayList<>();
|
||||
|
||||
|
||||
for (File scontrino : scontrini) {
|
||||
if(scontrino.getName().contains(data)){
|
||||
if (scontrino.getName().contains(data)) {
|
||||
ritorno.add(scontrino);
|
||||
}
|
||||
}
|
||||
|
||||
return ritorno;
|
||||
}
|
||||
|
||||
static double calcolaTotaleScontrini(ArrayList<File> scontrini) {
|
||||
double ritorno = 0;
|
||||
for (File scontrino : scontrini) {
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(scontrino))) {
|
||||
String riga = br.readLine();
|
||||
|
||||
do {
|
||||
riga = br.readLine();
|
||||
} while (!riga.contains("Totale: "));
|
||||
|
||||
ritorno += Double.parseDouble(riga.split(":")[1]);
|
||||
} catch (IOException e) {
|
||||
System.out.println(ERRORE_FILE + e.getMessage());
|
||||
}
|
||||
}
|
||||
return ritorno;
|
||||
}
|
||||
|
||||
static void stampaFattura(Fattura fattura, String data) {
|
||||
File f = new File(PATH_FATTURA);
|
||||
|
||||
if (!f.exists()) {
|
||||
f.mkdir();
|
||||
}
|
||||
|
||||
try (BufferedWriter bw = new BufferedWriter(new FileWriter(PATH_FATTURA + "fattura_" + data + ".txt"))) {
|
||||
System.out.println(fattura.toString());
|
||||
bw.write(fattura.toString());
|
||||
} catch (IOException e) {
|
||||
System.out.println(ERRORE_FILE + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user