-1

I'm studying java and I need to do a code with this: Create a constructor, and if the date is monday, the variable total will have 10% of discount.

But I'm getting error when I put a date in my main, and have no idea how create this boolean of local date "monday". Can someone help me?

My code:

package pedido;
import java.time.DayOfWeek;
import java.time.LocalDate;

public class pedido {

    private int numero;
    private LocalDate datapedido;
    private double quantidade, valor, total;
    private boolean sunday;

    public pedido(int m, LocalDate j, double p, double k) {
        this.numero = m;
        this.datapedido = j;
        this.quantidade = p;
        this.valor = k;
    }
        
    
    public void totalpedido() {
        if(sunday == true) {
            total = (this.valor * this.quantidade) * 0.9;
            System.out.printf("Por hoje ser domingo o total ficou: R$%.2f", total);
        }
        if(sunday == false) {
            total = (this.valor * this.quantidade);
            System.out.printf("O total ficou: %.2f", total);
        }
    }
    
}

and the main:

package pedido;
import java.time.DayOfWeek;
import java.time.LocalDate;

public class program {

    public static void main(String[] args) {
        
        pedido p1 = new pedido(111, 2021-09-26, 2, 36.50);
        p1.totalpedido();
        
        pedido p2 = new pedido(222, 2021-09-23, 1, 13.25);
        p2.totalpedido();
            
        
    }

}

Errors:

Multiple markers at this line
  - The constructor pedido(int, int, int, double) is 
   undefined
  - The literal 09 of type int is out of range
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 2
    Instead of `if(monday == true) {` and `if(monday == false) {` just use `if(monday) {`… `else {`. – Ole V.V. Sep 29 '21 at 04:48
  • 1
    "I'm getting error" What error, specifically? Read "[How do I ask a good question?](https://stackoverflow.com/help/how-to-ask)" – outis Sep 29 '21 at 08:29

1 Answers1

6

The LocalDate class has a method getDayOfWeek() which returns a DayOfWeek enum. So you can just write

if (datapedido.getDayOfWeek() == DayOfWeek.SUNDAY) {
     // it's Sunday
}

To actually create your LocalDate object, you can write

LocalDate.of(2021, 9, 23)

which is an expression of type LocalDate.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110