简体   繁体   中英

How can i fill an array

I have to display the total sale of the current month, of 6 items in an array (not array list but with the [][] bidimensional array) this is for a vending machine This is my methods class

package proyecto;

import java.util.Scanner;
import java.util.Date;

public class Metodos {

Date d = new Date();
Scanner in = new Scanner(System.in);
String s = new Date().toString();
double price;
double cash = 0;
double change;
int adminpass = 12345;
double funds = 50.00;
int agua = 20, cocacola = 20, sprite = 20, uva = 20, banana = 20, gatorade = 20, total = 120, venta = 0, ventac = 0;
int ventas = 0, ventau = 0, ventab = 0, ventag = 0, fecha;
String mes;

public void dueño() {
    System.out.println("Digite el codigo de seguridad ");
    int admintry = in.nextInt();
    while (!pase(admintry)) {
        System.out.println("Error digite el codigo de nuevo");
        admintry = in.nextInt();
        pase(admintry);
    }
    int opciones1 = 0;
    while (opciones1 != 5) {
        System.out.println("Ingrese que desea hacer: ");
        System.out.println("1. Reaprovisionar");
        System.out.println("2. Sacar las ganancias");
        System.out.println("3. Depositar dinero");
        System.out.println("4. Cierre del dia");
        System.out.println("5. Salir");
        opciones1 = in.nextInt();
        switch (opciones1) {
            case 1:
                reaprovisionar(total);
                break;
            case 2:
                System.out.println("Digite cuanto dinero quiere sacar de la maquina: ");
                double dinero = in.nextDouble();
                fondos(dinero);
                break;
            case 3:
                System.out.println("Digite cuanto dinero depositara");
                double dinero1 = in.nextDouble();
                fondos1(dinero1);
            case 4:
                mes = cortarmes(s);
                fecha = cortarfecha(s);
                int totaldias= totaldias(mes);
                break;
        }
    }
}

public void systema() {
    System.out.println("Digite su compra: ");
    System.out.println("D1. Agua = 1.00");
    System.out.println("D2. coca cola = 1.25");
    System.out.println("D3. Sprite = 1.25");
    System.out.println("D4. uva = 1.25");
    System.out.println("D5. Banana = 1.25");
    System.out.println("D6. Gatorade = 1.75");
    System.out.println("D7. Salir ");
    String opciones = in.next().toUpperCase();
    System.out.println("Ingrese la cantidad de dinero: ");
    cash = in.nextDouble();
    resta(opciones);
    suma(opciones);
    switch (opciones) {
        case "D1":
            System.out.println("El agua cuesta 1.00$");
            price = 1.00;
            while (verificar(cash, price) != true) {
                System.out.println("Ingrese mas dinero");
                cash = in.nextDouble();
                verificar(cash, price);
            }
            System.out.println("Su vuelto es de " + calcular(cash));
            break;
        case "D2":
            System.out.println("La cocacola cuesta 1.25$");
            price = 1.25;
            while (verificar(cash, price) != true) {
                System.out.println("Ingrese mas dinero");
                cash = in.nextDouble();
                verificar(cash, price);
            }
            System.out.println("Su vuelto es de " + calcular(cash));
            break;
        case "D3":
            System.out.println("La sprite cuesta 1.25$");
            price = 1.25;
            while (verificar(cash, price) != true) {
                System.out.println("Ingrese mas dinero");
                cash = in.nextDouble();
                verificar(cash, price);
            }
            System.out.println("Su vuelto es de " + calcular(cash));
            break;
        case "D4":
            System.out.println("La banana cuesta 1.25$");
            price = 1.25;
            while (verificar(cash, price) != true) {
                System.out.println("Ingrese mas dinero");
                cash = in.nextDouble();
                verificar(cash, price);
            }
            System.out.println("Su vuelto es de " + calcular(cash));
            break;
        case "D5":
            System.out.println("La uva cuesta 1.25$");
            price = 1.25;
            while (verificar(cash, price) != true) {
                System.out.println("Ingrese mas dinero");
                cash = in.nextDouble();
                verificar(cash, price);
            }
            System.out.println("Su vuelto es de " + calcular(cash));
            break;
        case "D6":
            System.out.println("El gatorade cuesta 1.75$");
            price = 1.75;
            while (verificar(cash, price) != true) {
                System.out.println("Ingrese mas dinero");
                cash = in.nextDouble();
                verificar(cash, price);
            }
            System.out.println("Su vuelto es de " + calcular(cash));
            break;
    }
}

public boolean pase(int x) {
    if (x == adminpass) {
        return true;

    } else {
        return false;
    }

}

public void fondos(double y) {
    while (y > funds) {
        System.out.println("No puede sacar esta cantidad, digite otra cantidad");
        y = in.nextDouble();

    }
    funds = funds - y;
    System.out.println("Haretirado el dinero exitosamente quedan: $" + funds + " en esta maquina");

}

public String fondos1(double z) {
    funds = funds + z;
    return "Se ha efectuado el deposito correctamente esta maquina contiene" + funds;

}

public int reaprovisionar(int x) {
    int suma = 0;
    if (x == 120) {
        return 120;
    } else {
        return suma + reaprovisionar(x + 1);
    }
}

public boolean verificar(double m, double n) {
    if (m < n) {
        return false;
    } else {
        return true;
    }

}

public void resta(String opciones) {
    switch (opciones) {
        case "D1":
            agua--;
            total--;
            break;
        case "D2":
            cocacola--;
            total--;
            break;
        case "D3":
            sprite--;
            total--;
            break;
        case "D4":
            uva--;
            total--;
            break;
        case "D5":
            banana--;
            total--;
            break;
        case "D6":
            gatorade--;
            total--;
            break;

    }
}

public void suma(String opciones) {
    switch (opciones) {
        case "D1":
            venta++;
            break;
        case "D2":
            ventac++;
            break;
        case "D3":
            ventas++;
            break;
        case "D4":
            ventau++;
            break;
        case "D5":
            ventab++;
            break;
        case "D6":
            ventag++;
            break;

    }
}

public double calcular(double x) {
    System.out.println("Gracias por su compra.");
    funds = funds + cash;
    change = cash - price;
    funds = funds - change;
    return change;

}

public String cortarmes(String mes) {
    String nueva = "";
    for (int i = 4; i < 7; i++) {
        nueva += s.charAt(i);
        nueva.toLowerCase();

    }
    return nueva;
}

public int cortarfecha(String fecha) {
    String nueva2 = "";
    int fecha2;
    for (int i = 8; i < 10; i++) {
        nueva2 += s.charAt(i);
    }
    fecha2 = Integer.parseInt(nueva2);
    return fecha2;
}

public int totaldias(String mes) {
    int totaldias = 0;
    switch (mes) {
        case "jan":
            totaldias = 31;
            break;
        case "feb":
            totaldias = 28;
            break;
        case "mar":
            totaldias = 31;
            break;
        case "apr":
            totaldias = 30;
            break;
        case "may":
            totaldias = 31;
            break;
        case "jun":
            totaldias = 30;
            break;
        case "jul":
            totaldias = 31;
            break;
        case "aug":
            totaldias = 31;
            break;
        case "sep":
            totaldias = 30;
            break;
        case "oct":
            totaldias = 31;
            break;
        case "nov":
            totaldias = 30;
            break;
        case "dec":
            totaldias = 31;
            break;
    }
    return totaldias;
    }
  }

the number of columns should be length 6 cause of the quantity of products and the rows shall be the number of days of each month which i take from Date and make it an int, how can i fill it with the different sellings of the different products in the same daylike for example:

(days)      agua cocacola sprite uva banana gatorade(products)
1
2
3
4
5
6
7
8
9            2     3      4     5      6     7 (total of items sold in the same day)
10

The easiest mode for to fill the array:

int rows = 30;

int[][] itemsSold = new int[rows][6];

for (int i = 0; i < rows; i++) {
    itemsSold[rows][0] = venta;
    itemsSold[rows][1] = ventac;
    itemsSold[rows][2] = ventas;
    itemsSold[rows][3] = ventau;
    itemsSold[rows][4] = ventab;
    itemsSold[rows][5] = ventag;

    //Change the variables
    //venta = ...
    //...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM