mardi 18 juin 2019

How to use random generated values in methods?

I have tried to solve the following problem: I have 3 restaurants, each of them offers 4 items, with their respective prices. I should create a program to simulate the sales of each restaurant in a period of 3 months.

I should be able to run the following results: Random number of items sold by each restaurant every month (for example: first restaurant sold 5 of the first item in the first month, and so on). Based on those random values, show the total of sales (income) of each restaurant every month, and based on that, sort the restaurants from the biggest total of sales to the smallest one. And at last, total of items sold in all restaurants and their respective total income (for example: 100 of the first item were sold and the income of 100 of that item is... ).

As you surely may expect, I have already began the code, but I suspect I have made it wrongly, mainly because the values are manipulated wrongly (for example: the total income based on the sales of the items in a month does not correspond to their individual prices and quantities sold). I am going to show you the code, and I would I appreciate if you could help me with it.

My main questions are:

  1. Is my approach good? Could I solve the problem with it? 2. Why are the values of the outputs of each method different? 3. If there is a better approach, could you show it, please?

I created a class "Month", where there are the variables "restaurant, item1, item2, item3, item4". It has the constructor and a setter and getter method for the restaurant variable. I created four methods, each for I can generate a random number of each item. I also created four methods for I can calculate the total income of each item sold, based of the values of the methods to calculate the number of sold items, and then I created a method to calculate the total income of all items together, using the values of the former four methods. The problem is that as I said, it is creating independent results, as if the methods were not using the values of the other methods, and I did not find out how I could sort the restaurants based on their total incomes every month (even though I know how to implement sorting algorithms, but I could not do it in this case), besides I can calculate the total of each item sold in all restaurants in the period of three months, not their income.

import java.util.Random;

public class Month {
    private String restaurant;
    private double item1;
    private double item2;
    private double item3;

public Month (){
    this.restaurant=restaurant;
    this.item1=item1;
    this.item2=item2;
    this.item3=item3;
}

public double item1Sold (){
    Random item1Quantity = new Random();
    double item1 = item1Quantity.nextInt(30)+1;

    return item1;
}

public double item2Sold (){
    Random item2Quantity = new Random();
    double item2 = item1Quantity.nextInt(30)+1;

    return item2;
}

public double item3Sold (){
    Random item3Quantity = new Random();
    double item3 = item3Quantity.nextInt(30)+1;

    return item3;
}

public double totalIncome (){
    double income = item1Price()+item2Price()+item3Price();

    return income;
}

public double item1Price (){
    double price = item1sales()*50.00;

    return price;
}

public double item2Price (){
    double price = item2sales()*40.00;

    return price;
}

public double item3Price (){
    double price = item3sales()*30.00;

    return price;
}

public String getRestaurant() {
    return restaurant;
}

public void setRestaurant(String restaurant) {
    this.restaurant = restaurant;
}

/**In the main class, I have created only one month ("m1") as an array, 
and each value of the array would be a restaurant, but I am almost sure 
this approach is wrong **/

public class Main {

public static void main(String[] args) {
    Month [] m1 = new Month[2];

    m1 [0] = new Month();
    m1 [0].setRestaurant("rest1");

    m1 [1] = new Month();
    m1 [1].setRestaurant("rest2");

    for (int i=0; i<2; i++)
        System.out.println(m1[i].getRestaurant()+m1[i].item1Sales());

Sorry for the long message, but I had to give details. Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire