Bonjour,
Je vous expose mon problème. En classe, nous devons créer un programme de caisse enregistreuse qui enregistre la commande et nous imprime le tickets sur notre disque dur. Chose faites, il me reste un problème. Après une impression de ticket, la classe scanner n'accepte plus de nouvelle saisie en jetant une erreur NotSuchElement. Du coup je ne sais plus quoi faire. J'ai tenté de ré instancier un nouveau scanner si le premier ne fonctionner plus. Déplacer la méthode qui se trouver dans la condition de sortie de mon do while et la remplacer par une variable locale. Le scanner n'est fermé nulle part. Mais je suppose qu'a la sortie d'une methode le garbage collector delivre les ressources attribuée à l'excecution de la méthode ?
Voila les 3 pages de code qui me pose le plus de problème
Ce qui s'occupe de l'impression, ici pas d'erreur mais c'est surment ici que cause le problème du scanner
Partie Gestion de la commande
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156 package commandGestion; import java.io.*; import java.text.*; import java.util.*; import com.sun.jndi.url.dns.dnsURLContext; import com.sun.org.apache.xpath.internal.operations.And; public class PrintWorker { final static int RankNbComso = 30; final static int RankSoloPrice = 38; final static int RankTotalPrices = 46; public static void printTicket(List<String> tickets, int nbTransac) { try(Scanner sc = new Scanner(System.in)) { SimpleDateFormat format = new SimpleDateFormat("dd_MM_yyyy HH_mm_ss_sss"); Calendar cldr = Calendar.getInstance(); String path = "D:\\Iram\\Algoritme et programation\\Projet\\Ticket\\TicketImprimé"; String fileName = format.format(cldr.getTime())+"_"+nbTransac; File temp = new File(path+"\\"+fileName+".txt"); if(temp.exists()) { int i =0; do { i++; } while (temp.renameTo(new File(path + "\\"+fileName +"_"+i+".txt"))); temp.createNewFile(); } else { temp.createNewFile(); } PrintWriter prt = new PrintWriter(new FileWriter(temp)); for (String string : tickets) { prt.write(string + "\n"); } prt.close(); System.out.println("Impression réussie !"); } catch(IOException ex) { System.out.println(ex.getMessage()); } } public static List<String> FormatTicket(String nameCompany, String[] items, double[] prices, double[] tvas, boolean isReduc, int table,List<int[][]> command) { String emptyLine = ""; SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss-sss"); Calendar cldr = Calendar.getInstance(); //Entete List <String> tickets = new ArrayList<String>(); tickets.add("Café Brasserie :" ); tickets.add(nameCompany); tickets.add(format.format(cldr.getTime())); tickets.add(emptyLine); tickets.add("Table : "+table); //separation tickets.add(emptyLine); String separatorline = ""; for(int i = 0; i <= RankTotalPrices+1; i++) { separatorline = separatorline.concat("-"); } tickets.add(separatorline); tickets.add(emptyLine); //Commande for (int[][] is : command) { if(is[0][3] >= 1) { String tempString = CreateLine(items[is[0][0]], Integer.toString(is[0][3]), RankNbComso); tempString = CreateLine(tempString, Double.toString(prices[is[0][1]]), RankSoloPrice); tempString = CreateLine(tempString, Double.toString((prices[is[0][1]]*is[0][3])), RankTotalPrices); tickets.add(tempString); } } //Separation tickets.add(emptyLine); tickets.add(separatorline); tickets.add(emptyLine); //Tva double[] ammountToApplyTva = new double[tvas.length +1]; for (int i = 0; i < ammountToApplyTva.length; i++) { if (i < tvas.length){ for (int j = 0; j < command.size(); j++) { int p = command.get(j)[0][2]; int n = command.get(j)[0][3]; if (p == i && n > 0) { ammountToApplyTva[i] += prices[command.get(j)[0][1]]*command.get(j)[0][3]; } } ammountToApplyTva[i] = TvaWorker.GetTvaPrice(tvas[i], ammountToApplyTva[i]); if(ammountToApplyTva[i] > 0) { tickets.add(CreateLine("Total tva "+Double.toString(tvas[i])+" %", Double.toString(ammountToApplyTva[i]), RankSoloPrice)); } } else if (i == tvas.length) { for (int j = 0; j < ammountToApplyTva.length-1; j++) { ammountToApplyTva[i] += ammountToApplyTva[j]; } tickets.add(CreateLine("Total tva", Double.toString(ammountToApplyTva[i]), RankSoloPrice)); } } //Separation tickets.add(emptyLine); tickets.add(separatorline); tickets.add(emptyLine); //Total double total = 0.0; for (int[][] d : command) { if(d[0][3]> 0) { total += d[0][3]*d[0][1]; } } tickets.add(CreateLine("Total TVA", isReduc ? Double.toString(total - (total*0.2)) : Double.toString(total), RankTotalPrices)); return tickets; } private static String CreateLine (String item, String toAdd, int rank) { int spaceCount=0; String strTemp=item; spaceCount=rank-item.length()-toAdd.length(); for(int i=1; i<spaceCount; i++) { strTemp=strTemp + " "; } strTemp=strTemp+toAdd; return strTemp; } }
Et ici, c'est la méthode qui pose problème en tant que telle.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112 package commandGestion; import java.util.*; import Helper.*; public class CreateTicket { private final static String nameCompany = "Brasse-Temps"; private final static String[] tabNames= {"Spa reine 25 ","Bru plate 50","Bru pét 50","Pepsi","Spa orange", "Schweppes Tonic","Schweppes Agr","Ice Tea","Ice Tea Pêche","Jus d'orange Looza", "Cécémel", "RedBull","Petit Expresso","Grand Expresso","Café décaféiné ","Lait Russe ","Thé et infusions", "Irish Coffee ","French Coffee ","Cappuccino","Cécémel chaud","Passione Italiano","Amour Intense", "Rhumba Caliente ","Irish Kisses ","Cuvée Trolls 25","Cuvee Trolls 50", "Ambrasse-Temps 25","Ambrasse-Temps 50 ","Brasse-Temps Cerises 25","Brasse-Temps Cerises 50", "La Blanche Ste Waudru 25","Blanche Ste Waudru 50","Brasse-Temps citr 25","Brasse-Temps citr 50", "Spaghetti Bolo", "Tagl Carbonara","Penne poulet baslc ","Tagl American","Tagl saum"}; private final static double[] tabPrices={2.2,2.3,3.9,2.2,2.2,2.6,2.6,2.6,2.6,2.6,2.6,4.5, 2.2,2.2,2.2,2.5,2.5,7.0,7.0,2.8,2.8,6.2,6.2,6.2,6.2,2.9,5.5,2.7,5.1,3.1, 5.8,2.6,4.9,2.6,4.9,10.8,11.2,12.2,14.5,16.9}; private final static byte[] TypeTVA= {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1}; private final static double tabTva[] = {6.0,12.0,21.0}; // Tableau 2d avec 4 colonne (int [1][4]) private static List<int[][]> command = new ArrayList<int[][]>(); private static boolean isHappyHour; public static void main(String[] args) { boolean isQuit = true; int nbTransac = 1; do { int table = Help.CheckInputUserInt("Entrez la table : "); if (table > 20 || table < 0) { System.out.println("Appelez votre developpeur !"); } else { isHappyHour = Help.CheckInputUserBool("Est-ce happy hour ?"); boolean finishCommand = false; do { switch(Help.CheckInputUserChar("Que voulez vous faire ?\nM : Modifier la commande \nD : Afficher la commande\nA : Afficher le menu \nV : Valider la commande \nQ : Annuler la commande", "admvq")) { case 'a' : int i = 1; for(String name : tabNames) { System.out.println(i+"\t"+name); i++; } break; case 'd' : if(command.size()> 0) { boolean isPrint = false; for (int[][] line : command) { if(line[0][3] > 0) { System.out.println(tabNames[line[0][0]] + "\t" + Integer.toString(line[0][3])); isPrint = true; } } if(!isPrint) { System.out.println("Commande vide"); } } else { System.out.println("Commande vide"); } break; case 'm' : int index; do { index = Help.CheckInputUserInt("Nurmero de l'article :") -1; if (index >= tabNames.length || index < 0) { System.out.println("Article inconnu"); } } while(index >= tabNames.length || index < 0); command = CommandWorker.addRessource(command, TypeTVA[index], index, index, Help.CheckInputUserInt("Quantité demandée : ")); break; case 'v' : if(Help.CheckInputUserBool("Etes vous sur ?")) { PrintWorker.printTicket(PrintWorker.FormatTicket(nameCompany, tabNames, tabPrices, tabTva, isHappyHour, table, command ), nbTransac); isHappyHour = false; finishCommand = true; command = new ArrayList<int[][]>(); nbTransac ++; } break; case 'q' : if(Help.CheckInputUserBool("Etes vous sur ?")) { command = new ArrayList<int[][]>(); isHappyHour = false; finishCommand = true; } break; } }while(!finishCommand); // C'est ici que ca déconne après validation du ticket mais pas après le quitter la commande isQuit = Help.CheckInputUserBool("Une nouvelle commande ?"); } } while(isQuit); } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 public static boolean CheckInputUserBool (String wordToDisplay) { Scanner scan = new Scanner(System.in); do { String input ="b"; System.out.print(wordToDisplay + "\nRepondez par Y/N : "); try { input = scan.next(); } catch(Exception ex) { System.out.println(ex.getMessage()); } if(input.toLowerCase().charAt(0) == 'y') { return true; } else if(input.toLowerCase().charAt(0) == 'n') { return false; } } while (true); }
Merci d'avance !
Ps : Voila la soluction complète Ticket.zip
Partager