访问"应该"存在于另一个类

时间:2018-02-07 20:29:12

标签: java

我正在创建一个模拟产品分发的程序。 用户输入数量,然后在X个分发站点之间随机分配该数量。

程序的结构是除原始数量之外的主要部分,然后将该数量发送到CPO(中央处理办公室)类。 CPO有一种方法可以实例化所需数量的分发站点,另一种方法可以确定给每个站点的值。

我的问题是我希望main或菜单类显示分发站点的值,但由于它们在另一个类中实例化,因此主要不会看到它们。 因此,像System.out.println(D1)这样的命令;

主档案:

public static void main(String[] args) {
    // TODO code application logic here

    //----------------------------------------------------------------------
    //  Welcome prompt, requests initial quanity of products to be ordered
    //----------------------------------------------------------------------
    System.out.println(
            "\tWelcome to CPO Distribution "
                    + "\n\t  Order Placement Menu \n\n");
    for (int i = 0; i < 55; i++){
        System.out.print("=");
    }
    System.out.println("\nPlease enter the quantity of products to be distributed");
    System.out.print("Product Quantity: ");

    //----------------------------------------------------------------------        
    // Creates Order int to hold the value of the products entered
    //----------------------------------------------------------------------
    order = input.nextInt();
    while (order < 1){
        System.out.println("Please enter a value greater than 0");
        order = input.nextInt();
    }
    for (int i = 0; i < 55; i++){
        System.out.print("=");
    }
    Cpo CPO = new Cpo(order);
    Cpo.dCreation();


    System.out.println(CPO);
    System.out.println(D1);
    System.out.println(D2);
}

CPO课程:

public class Cpo {
    private static int origin, quantity, site, distributionsites = 3;
    private static int D1Total, D2Total, D3Total;
    private static SecureRandom rand = new SecureRandom();

    public Cpo(int origin) {
        this.origin = origin;
        quantity = origin;
        site = distributionsites;
    }
    //----------------------------------------------------------------------------
    // Get'r and Set'r for Order received from CPO
    //----------------------------------------------------------------------------     
    public int getOrigin() {
        return this.origin;
    }

    public void setOrigin(int origin) {
        this.origin = origin;
    }

    //----------------------------------------------------------------------------
    //  instantiating Distributors
    //----------------------------------------------------------------------------
    public static void dCreation() {
        getDamount();
        Distributor D1 = new Distributor("D1",D1Total);
        getDamount();
        Distributor D2 = new Distributor("D2",D2Total);
        getDamount();
        Distributor D3 = new Distributor("D3",D3Total);
    }
    //----------------------------------------------------------------------------
    // Method for distributing order to Distributors
    //----------------------------------------------------------------------------
    public static int getDamount(){
        site = site - 1;
        int remainder = 0;
        if ( site == distributionsites ){
            remainder = rand.nextInt(quantity);
            quantity = quantity - remainder;

        } else if ( site == distributionsites - 1){
            remainder = rand.nextInt(quantity);
            quantity = quantity - remainder;

        } else if (site < 1){
            remainder = quantity;
            quantity = quantity - remainder;

        }
        return remainder;
    }

    public String toString() {
        return "\n The CPO has dispersed an order of " + origin + " products among "
                + distributionsites + " Distribution Sites. \n There are " + quantity
                + " left." + "sites are " + site;
    }
}

0 个答案:

没有答案