Java - necesito una ayuda

 
Vista:
sin imagen de perfil

necesito una ayuda

Publicado por juanjo (1 intervención) el 05/06/2017 02:43:38
Buenas gengo un array list con 4 public static voy qe cada una hace su accion al pulsar del 1 al 4 pues quiero hacer otro boton con lo ultimo qe dimos de escribir en un punto txt y quiero qe me imprima la opcion 4 qe es mostrarme todo los articulos comprado el precio y el precio total y qe cuando pulse el 5 pues salga como una factura en el txt

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
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
 
public class Main {
 
    public static void main(String[] args) {
        ArrayList<Producto> compraList = new ArrayList();
 
        int option;
        while ((option = showMenu(compraList)) != 0) {
            switch (option) {
                case 1:
                    // Añadir producto
                    addProd(compraList);
                    break;
                case 2:
                    //Eliminar producto
                    if (compraList.size() > 0) {
                        deleteProd(compraList);
                    }
                    break;
                case 3:
                    // Ver Lista de producto
                    showProd(compraList);
                    break;
                case 4:
                    //Total de precio y todos los productos
                    showPrice(compraList);
                    break;
                case 5:
                    //Pagar el dinero total y si no es igual decir la vuelta
                    PayReturn(compraList);
 
                default:
            }
        }
    }
 
    public static void deleteProd(ArrayList<Producto> myList) {
        Scanner input = new Scanner(System.in);
        int index;
 
        showProd(myList);
 
        do {
            System.out.printf("Introducir el índice: ");
            index = input.nextInt();
        } while (!correctIndex(index, myList));
 
 
        myList.remove(index);
 
    }
 
    public static boolean correctIndex(int index, ArrayList<Producto> myList) {
        if (index >= 0 && index < myList.size()) {
            return true;
        } else {
            return false;
        }
    }
 
    public static void addProd(ArrayList<Producto> myList) {
        Scanner input = new Scanner(System.in);
        String list;
        Double price;
 
        do {
            System.out.println("Introducir Producto:");
            list = input.nextLine();
 
            System.out.println("Introducir Precio:");
            price = input.nextDouble();
 
        } while (list.length() == 0);
 
        myList.add(new Producto(list, price));
 
 
    }
 
    public static void showProd(ArrayList<Producto> myList) {
        int index = 0;
        for (Producto p : myList) {
            System.out.println(p.toString());
        }
 
    }
 
    public static void showPrice(ArrayList<Producto> myList) {
        double total = 0;
 
 
        for (Producto p : myList) {
 
            total = total + p.getPrecio();
            System.out.println(p.toString());
        }
 
        System.out.println("Total de los productos: " + total + "€");
    }
 
    public static void PayReturn(ArrayList<Producto> myList){
        FileWriter fichero = null;
        PrintWriter pw = null;
 
 
        try
        {
            fichero = new FileWriter("C:/Users/Juanjo/Desktop/Escritorio/ListaDeCompra/Factura.txt");
            pw = new PrintWriter(fichero);
 
 
                pw.println("Imprimir la accion 4 por completo(esto es lo que me falla)");
 
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                // Nuevamente aprovechamos el finally para
                // asegurarnos que se cierra el fichero.
                if (null != fichero)
                    fichero.close();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }
 
    public static int showMenu(ArrayList<Producto> myList) {
        Scanner input = new Scanner(System.in);
        int option;
 
        System.out.println("*******************************");
        System.out.println("* 1 - Añadir Producto         *");
        System.out.println("* 2 - Eliminar Producto       *");
        System.out.println("* 3 - Mostrar lista artículos *");
        System.out.println("* 4 - Importe total           *");
        System.out.println("* 5 - Imprimir factura        *");
        System.out.println("* 0 - Salir                   *");
        System.out.println("*******************************");
        System.out.println("Opción: ");
 
        option = input.nextInt();
 
        return option;
    }
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder