Java - AssertionFailedError - "," en vez de "."

 
Vista:
sin imagen de perfil

AssertionFailedError - "," en vez de "."

Publicado por Xavi (21 intervenciones) el 04/12/2021 19:38:52
Hola, tengo la siguiente clase :

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
package edu.uoc.pac4;
 
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.*;
import java.util.stream.Collectors;
 
public class ShoppingCart {
 
    private String clientName;
    private double total;
    private List <Product> cart;
 
    public ShoppingCart(String clientName) {
        this.cart = new ArrayList<>();
        setClientName(clientName);
    }
 
    public List<Product> getCart() {
        return this.cart;
    }
 
    public String getClientName() {return clientName;}
 
    public void setClientName(String clientName) {this.clientName = clientName;}
 
    public double getTotal() {return total;}
 
    public void setTotal(double total) {
        this.total = (double) Math.round(total * 100) / 100;
    }
 
    public boolean add(Product product) throws NullPointerException {
        boolean tractat = false;
        if (product != null) {
            if (product instanceof PhysicalProduct) {
                if (((PhysicalProduct) product).getStock() > 0) {
                    cart.add(product);
                    product.incSales();
                    setTotal(getTotal()+product.getPrice());
                    ((PhysicalProduct) product).setStock(((PhysicalProduct) product).getStock() - 1);
                    tractat = true;
                }
            } else if (product instanceof DigitalProduct) {
                cart.add(product);
                DigitalProduct.incSalesTotal();
                product.incSales();
                setTotal(getTotal()+product.getPrice());
                tractat= true;
            }
        } else {
            throw new NullPointerException("[ERROR] Product object cannot be null");
        }
        return tractat;
    }
 
    public boolean remove(Product product) {
        boolean tractat = false;
        if (product != null) {
            if (this.cart.contains(product)){
                cart.remove(product);
                product.decSales();
                if (product instanceof PhysicalProduct) {
                    ((PhysicalProduct) product).setStock(((PhysicalProduct) product).getStock() + 1);
                } else if (product instanceof DigitalProduct) {
                    DigitalProduct.decSalesTotal();
                }
                setTotal(getTotal()-product.getPrice());
                tractat = true;
            }
        } else {
            throw new NullPointerException("[ERROR] Product object cannot be null");
        }
        return tractat;
    }
 
    public void remove() {
        for (Product product : this.cart) {
            product.decSales();
            if (product instanceof PhysicalProduct) {
                ((PhysicalProduct) product).setStock(((PhysicalProduct) product).getStock() + 1);
            } else if (product instanceof DigitalProduct) {
                DigitalProduct.decSalesTotal();
            }
        }
        cart.clear();
        setTotal(0);
    }
 
    public boolean exists(Product product) {return cart.contains(product);}
 
    public boolean isEmpty() {return cart.isEmpty();}
 
    public TreeMap groupAndSort(boolean orderByPriceDesc) {
        TreeMap<Product, Integer> map = new TreeMap<>();
        if (orderByPriceDesc){
            map = new TreeMap<>(new ProductOrderByPrice().reversed());
        }
        for (Product i : this.cart) {
            map.put(i, Collections.frequency(this.cart, i));
        }
 
        return map;
    }
 
    public void printInvoice(boolean orderByPriceDesc){
        TreeMap<Product, Integer> map = groupAndSort(orderByPriceDesc);
        for (Map.Entry<Product, Integer> entry : map.entrySet()) {
            Product key = entry.getKey();
            Integer u = entry.getValue();
            NumberFormat f = DecimalFormat.getInstance();
            f.setMinimumFractionDigits(1);
            f.setMaximumFractionDigits(2);
            System.out.println(key.getReference() + " - Units: " + u + " - (" + f.format(key.getPrice()) + "€) " + key.getPublicationYear());
        }
    }
 
    public String toString(){
        return "Client: " + this.clientName + " - Products: " + cart.size();
    }
 
    /*****************************
     *      STREAM METHODS
     *****************************/
 
    public List<String> getDigitalProductOrderByName() {
 
        return this.cart.stream().filter(product -> product instanceof DigitalProduct).map(Product::getName).sorted().collect(Collectors.toList());
    }
 
    public void getDistinctReferenceByVAT(VAT vat) {
        this.cart.stream().distinct().filter(product -> product.getVAT() == vat).forEach(product -> System.out.println(product.getReference()));
    }
 
    public double averagePriceBooks() {
        return this.cart.stream().filter(product -> product instanceof Book).mapToDouble(Product::getPrice).average().getAsDouble();
    }
 
}

En concreto estoy testeando el siguiente método:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public void printInvoice(boolean orderByPriceDesc){
        TreeMap <Product, Integer> mapa = groupAndSort(orderByPriceDesc);
        // Iterem estructura
        for (Map.Entry<Product, Integer> entry : mapa.entrySet()) {
            // Clau
            Product key = entry.getKey();
            // Valor
            Integer u = entry.getValue();
            // Format
            NumberFormat f = DecimalFormat.getInstance();
            f.setMinimumFractionDigits(1);
            f.setMaximumFractionDigits(2);
            // Mostrem missatge per pantalla
            System.out.println(key.getReference() + " - Units: " + u + " - (" + f.format(key.getPrice()) + "€) " + key.getPublicationYear());
        }
    }

Para testearlo utilizo los siguientes tests:

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
@Test
    @Order(16)
    void testPrintInvoiceByPublicationYear() {
        final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();
        System.setOut(new PrintStream(outputStreamCaptor));
        shoppingCart.printInvoice(false);
        assertEquals("PROD.15000 - Units: 3 - (14.25€) 1997\n" +
                "PROD.15004 - Units: 3 - (18.0€) 1998\n" +
                "PROD.15005 - Units: 1 - (15.9€) 1999\n" +
                "PROD.15002 - Units: 2 - (25.0€) 2003\n" +
                "PROD.15001 - Units: 1 - (12.12€) 2012\n" +
                "PROD.15006 - Units: 3 - (7.89€) 2012\n" +
                "PROD.15003 - Units: 1 - (25.0€) 2020", outputStreamCaptor.toString().trim());
    }
 
    @Test
    @Order(17)
    void testPrintInvoiceByPriceDesc() {
        final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();
        System.setOut(new PrintStream(outputStreamCaptor));
        shoppingCart.printInvoice(true);
        assertEquals("PROD.15003 - Units: 1 - (25.0€) 2020\n" +
                "PROD.15002 - Units: 2 - (25.0€) 2003\n" +
                "PROD.15004 - Units: 3 - (18.0€) 1998\n" +
                "PROD.15005 - Units: 1 - (15.9€) 1999\n" +
                "PROD.15000 - Units: 3 - (14.25€) 1997\n" +
                "PROD.15001 - Units: 1 - (12.12€) 2012\n" +
                "PROD.15006 - Units: 3 - (7.89€) 2012", outputStreamCaptor.toString().trim());
    }

Sin embargo, cuando ejecuto el test obtengo un AssertionFailedError debido a que obtengo el separado del precio en "," en vez de "." . Adjunto imagen:

test

No entiendo porqué me sale el separado en "," en vez de "." . Alguna razón ? Cómo podría solucionarlo?

Gracias
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
sin imagen de perfil

AssertionFailedError - "," en vez de "."

Publicado por Xavi (21 intervenciones) el 04/12/2021 20:21:21
Ya está! ya lo cambié :)
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar