Java代码将新对象的先前条目覆盖到队列数组中

问题描述 投票:0回答:1

我正在尝试编写一种算法,该算法允许程序的用户将订单输入到作为数组的队列中。我可以插入订单并插入另一个订单,但是,我下的每个后续订单都会覆盖我之前的订单。阵列中仍然有订单的位置,但是它们都是最新订单的副本。这是我的代码:

//Class to define individual orders for queueOrders
class Order
{

    //Global Variables
    public static String name;
    public static String order;
    public static int orderNum;

    //Constructor
    public Order()
    {

    }

    public Order(int orderNum)
    {
        this.orderNum = orderNum;
    }

    public Order(String name, String order)
    {
        this.name = name;
        this.order = order;
    }

    public Order(String name, String order, int orderNum)
    {
        this.name = name;
        this.order = order;
        this.orderNum = orderNum;
    }

    //Getters and Setters
    public static String getName()
    {
        return name;
    }

    public static void setName(String name)
    {
        Order.name = name;
    }

    public static String getOrder()
    {
        return order;
    }

    public static void setOrder(String order)
    {
        Order.order = order;
    }

    public static int getOrderNum()
    {
        return orderNum;
    }

    public static void setOrderNum(int orderNum)
    {
        Order.orderNum = orderNum;
    }

}


//Class to define queue
class QueueOrders
{

    //Global Variables
//    private int nItems = 0;

    private static int maxSize;
    private static int numOfOrders = 0;

    //Array of Orders objects
    private static Order orders[];

    private static int front;
    private static int back;

    //Constructor
    public QueueOrders(int size)
    {
        maxSize = size;
        orders = new Order[maxSize];
        front = 0;
        back = 0;

    }

 //Insert new order
    public static void insertOrder(String name, String order)
    {
        //Variables
//        int cntr = 1;


        if(isFull())
        {
            System.out.println("There are too many orders."
                    + " Remove some first. ");
        }
        else
        {
//            if(back == maxSize - 1)
//            {
//                back = 0;
//            }


            Order.setName(name);
            Order.setOrder(order);
 ////        Order.name = name;
////        Order.order = order;
//        Order.setOrderNum(cntr);



        Order newOrder = new Order(name, order, (numOfOrders + 1));

        //Add order to orders array
        orders[back] = newOrder;
        //front = numOfOrders - 1;

//        cntr++;
        back++;
        numOfOrders++;

        }
    }

//These functions are in the main class of the program

 public static void main(String[] args)
    {
        QueueOrders queue = new QueueOrders(100);
        menu();
    }

  //Function to add an order to the queue
    public static void addOrder()
    {
        //Variables
        String xName;
        String xOrders;
        Scanner myScan = new Scanner(System.in);

        try
        {
            //Message user
            System.out.println("What is the three letter name for your order? ");

            xName = myScanner.nextLine();

            //Message user
            System.out.println("What is your order? ");

            xOrders = myScan.nextLine();

            QueueOrders.insertOrder(xName, xOrders);

            QueueOrders.displaySingleOrder();

            System.out.println("Your order has been placed. ");

        } catch (Exception e)
        {
            System.out.println("Was unable to insert your order");
        }
    }
java arrays
1个回答
0
投票

问题是您正在使用静态变量作为订单数据(String nameString orderint orderNum)。它们特定于类Order,而不特定于要添加到列表中的对象Order order。从这些变量中删除关键字static

此实现

public class Sample {   
    public String a;
    public static String b; 

    public static void main(String[] args) {
        Sample s1 = new Sample();
        s1.a = "1a";
        s1.b = "1b";
        Sample s2 = new Sample();
        s2.a = "2a";
        s2.b = "2b"; // This one set Sample.b to "2b" because b is 
                     // static and shared among all Sample objects (s1 and s2). 
        System.out.println(s1.a + " " + s1.b + " " + s2.a + " " + s2.b);     
    }
}

将打印1a 2b 2a 2b(不是1a 1b 2a 2b,因为b是静态的并且"1b"的值被s2.b = "2b"覆盖。

请参阅this article以获取更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.