jeudi 1 décembre 2016

Nested loop pattern return null

I am trying to make id for items. But after A6 B6.., it set to null.

The minimum number of item is 0 and max is 36. The pattern should be

A1-A6 B1-B6 C1-C6 D1-D6 E1-E6 F1-F6

For example if I have 32 items. The id should stop at F2.

My code:

    public void printMenu() {

        System.out.println("\nMenu:");
        System.out.printf("%s%9s%14s%8s\n", "Item#", "Item", "Price", "Qty");

        char letter = 'A';
        for (int i = 0; i < stock.length; ++i) {
            for (int j = 1; j < 7; j++) {
                stock[i].setId(letter + "" + j);
                i++;
            }
            letter++;
        }

  for (int i = 0; i < stock.length; ++i) {
        System.out.printf("%s%15s%13s%8s\n" , stock[i].getId() ,stock[i].getDescription(),
                (stock[i].getPrice()),stock[i].getQuantity());
        }
    }

The output:

Menu:
    Item#     Item         Price     Qty
    A1        Gummies         -1.0      -1
    A2          Chips         -1.0      -1
    A3        Raisins         -1.0      -1
    A4       Pretzels         -1.0      -1
    A5         Cookie         -1.0      -1
    A6        Peanuts         -1.0      -1
    null        Gummies         -1.0      -1
    B1        Gummies         -1.0      -1
    B2          Chips         -1.0      -1
    B3        Raisins         -1.0      -1
    B4       Pretzels         -1.0      -1
    B5         Cookie         -1.0      -1
    B6        Peanuts         -1.0      -1
    null        Gummies         -1.0      -1
    C1        Gummies         -1.0      -1
    C2          Chips         -1.0      -1
    C3        Raisins         -1.0      -1
    C4       Pretzels         -1.0      -1
    C5         Cookie         -1.0      -1
    C6        Peanuts         -1.0      -1
    null        Gummies         -1.0      -1
    D1        Gummies         -1.0      -1
    D2          Chips         -1.0      -1
    D3        Raisins         -1.0      -1
    D4       Pretzels         -1.0      -1
    D5         Cookie         -1.0      -1
    D6        Peanuts         -1.0      -1
    null        Gummies         -1.0      -1
    E1        Gummies         -1.0      -1
    E2          Chips         -1.0      -1
    E3        Raisins         -1.0      -1
    E4       Pretzels         -1.0      -1
    E5         Cookie         -1.0      -1
    E6        Gummies         -1.0      -1

Aucun commentaire:

Enregistrer un commentaire