Java 处理由对象组成的循环队列

Java 处理由对象组成的循环队列,java,arrays,class,object,queue,Java,Arrays,Class,Object,Queue,我正在尝试创建一个循环队列来容纳对象。其思想是用户将购买或出售股票,每次购买股票时,该对象将持有购买的股票数量和购买价格。此队列是一个基于数组的队列,大小为32,每次用户选择购买时,它都会填充另一个插槽。 如果用户选择出售股票,则出售的股票数量将从第一个对象中减去,除非用户出售的股票数量超过第一个插槽包含的数量,在这种情况下,它将从后续插槽中减去。一旦该槽的所有股票被卖出…也就是说,它没有更多的股票,该对象可以退出队列,一旦队列到达该位置,它将腾出一个槽来购买股票 我正在努力处理对象类型。特别是

我正在尝试创建一个循环队列来容纳对象。其思想是用户将购买或出售股票,每次购买股票时,该对象将持有购买的股票数量和购买价格。此队列是一个基于数组的队列,大小为32,每次用户选择购买时,它都会填充另一个插槽。 如果用户选择出售股票,则出售的股票数量将从第一个对象中减去,除非用户出售的股票数量超过第一个插槽包含的数量,在这种情况下,它将从后续插槽中减去。一旦该槽的所有股票被卖出…也就是说,它没有更多的股票,该对象可以退出队列,一旦队列到达该位置,它将腾出一个槽来购买股票

我正在努力处理对象类型。特别是当我试图退出队列只是为了测试,看看我是否可以正常退出。。。。当我退出队列时,我将得到最后一个插槽,尽管我非常确定该方法是正确的,但我只是在处理类/对象时遇到了困难

注意:我必须手动创建数组/队列,因此我不能使用Java内置数组

StockTran类……将股票作为嵌套类

  import java.util.Scanner;

    //Once you reach the end of the array, you are going to get an out of bounds exception if you try to enqueue again
    //So if there are any open boxes on the very front of the array, say a[0] is empty, you can place the extra array spot at a[0]
    //Then keep going

    //You can handle the overflow and make the array circular or at least get the size by using (get + put)%size...So if (4+5)%8
    //the answer is 1... meaning the size is 1.

    //Create a total variable thats adds up the total of shares each spot in the array has...This way you can say you don't have
    //that many shares if they try to sell more than what you actually have....Also look at the amount of shares each spot has
    //and only dequeue when the total of shares for that spot is used up..."Only dequeue a truck when it's empty"


    //User is prompted to enter in either b, s, c, or q..... 
    //b = buy shares, s = sell shares, c = capital gain from buying and selling q = quit the program
    //Must be formatted by letter (space) number (space) number........ EX: b 10 10        .... this will buy 10 shares at $10 each      

    //This is the class I am storing in the queue.....Can call methods on it to get out crucial data
        class Stock {
        public int getShares() {
            return shares;
        }

public void setShares(int shares) {
    this.shares = shares;
}

public int getPrice() {
    return price;
}

public void setPrice(int price) {
    this.price = price;
}

private int shares;
private int price;

// Get price Get Shares set price get set price

public void stockTran() {
    // total gain,
}
}

 //Class that handles the user's input, should handle the math for profit, subtracting,       etc....Basically the class that
 //links all the other classes/methods together
 public class StockTran {

// Declaring variables
private MyQueue<Stock> queueArray;  // Size of Array/Queue is 32
static String firstCommand;         // The first token of the scanner...Looks
                                    // whether b,s, or c
static int shares;                  // How many shares that are bought/sold
static int price;                   // The price those shares are bought/sold at
static int netProfit = 0;           // Initializing the profit so it can be
                                    // added/subtracted later
static int put, get = 0;            // Declaring the front and rear portions of the queue

static int total = 0;               // Initial number of elements in the array, this will
                                    // increase/decr


public static void main(String[] args) {
    StockTran eg = new StockTran();

    eg.queueArray = new MyQueue<Stock>(32);
    Stock test = new Stock();

    // Prompts the user to enter an input

    // The if/else if ladder is used to call the correct methods based on
    // the user input..Reads by looking
    // at the tokens
    while (true) {
        Scanner reader = new Scanner(System.in);
        System.out.println("Enter the proper formatted commands ");
        firstCommand = reader.next();
        if (firstCommand.equalsIgnoreCase("b")) {
            shares = reader.nextInt();
            price = reader.nextInt();
            test.setPrice(price);
            test.setShares(shares);
            eg.queueArray.enqueue(test);

            System.out.println("Price: " + test.getPrice());
            System.out.println("Shares: " + test.getShares());
            System.out.println("Size: " + eg.queueArray.size());

            // System.out.println("Buying " + shares + " shares" + ", " +
            // "$" + price + " each");

        } else if (firstCommand.equalsIgnoreCase("s")) {

            System.out.println("dequeued price: " + ((Stock) eg.queueArray.dequeue()).getPrice());
            System.out.println("dequeued shares: " + ((Stock) eg.queueArray.dequeue()).getShares());

            System.out.println("Size: " + eg.queueArray.size());
        }

        else if (firstCommand.equalsIgnoreCase("c")) {
            System.out.println("Capital gain");
        } else if (firstCommand.equalsIgnoreCase("q")) {
            System.out.println("Quitting");
            break;
        } else {
            System.out.println("Command Not Recognized!");
        }
    }
}

// This method will either add or subtract from the total profit depending
// upon the buying and selling....Not finished yet
public int calculate(int netProfit) {
    int i = 0;
    while (i < shares) {

        i++;
    }
    return netProfit;

}
 }
        // Creates the node and allows methods to assign, get the next element, and set elements
        // for the node

        //So you create a node class with quanitity and price.... Has to be an object
        public class MyQueue <E> implements Queue <E>
        {
            //Instance variables
            private Object [] queue;
            private int front, rear = 0;   //Pointers for the queue....Locates where the next enqueue/dequeue spot should be
            private int max = 32;          //Size of my queue
            private int size;

            public MyQueue(int max)
            {
                this.queue = new Object[max];
            }

            @Override
            public int size() {
                // TODO Auto-generated method stub
                size = (rear - front)%32;
                return (rear - front)%32;
            }

            @Override
            public boolean isEmpty() {
                // TODO Auto-generated method stub
                if (front == rear)
                    return true;
                else 
                    return false;
            }

            //Gets the front object of the queue
            @Override
            public Object front() throws EmptyQueueException {
                // TODO Auto-generated method stub
                        return queue[front];
            }

            //Adds an element to the end of the queue
            @Override
            public void enqueue(E element) {
                // TODO Auto-generated method stub
                this.queue[this.rear%this.max] = element;
                if (rear < 32)
                rear++;
                else
                    rear = 0;
                System.out.println("Font is: " + front);
                System.out.println("Rear is: " + rear);

            }

            //Removes an element from the front of the queue
            @Override
            public Object dequeue() throws EmptyQueueException {
                Object temp = null;
                if (isEmpty() == true){
                    throw new EmptyQueueException("Empty Queue!");
                }       
                if(size() > 0 && front != size){
                    temp = queue[front];
                    System.out.println(": " + temp);

                    front = front + 1;
                }
                    else if(front == size())
                     front = 0;

                System.out.println("Font is: " + front);
                System.out.println("Rear is: " + rear);
                return temp;

        }
        }
import java.util.Scanner;
//一旦到达数组的末尾,如果再次尝试排队,将得到一个越界异常
//因此,如果在数组的最前面有任何打开的框,比如[0]为空,则可以将额外的数组点放置在[0]处
//然后继续
//您可以处理溢出并使数组循环,或者至少使用(get+put)%size…获取大小…因此如果(4+5)%8
//答案是1。。。表示大小为1。
//创建一个total变量,它将数组中每个点的共享总数相加…这样你就可以说你没有了
//如果他们试图卖出比你实际拥有的更多的股票,那么就有那么多的股票……同时看看每个现货的股票数量
//只有当该地点的股份总数用完时才出列……”“只有卡车空了才出列”
//提示用户输入b、s、c或q。。。。。
//b=买入股份,s=卖出股份,c=买入和卖出的资本收益q=退出该计划
//必须按字母(空格)编号(空格)编号……格式化。。。。。。。。例:B1010。。。。这将以每股10美元的价格购买10股股票
//这是我存储在队列中的类……可以调用它的方法来获取关键数据
类别股票{
public int getShares(){
返还股份;
}
公共无效集合股份(整数股){
这是股票=股票;
}
public int getPrice(){
退货价格;
}
公共无效设置价格(整数价格){
这个价格=价格;
}
私人股份;
私人int价格;
//获取价格获取股票设定价格获取设定价格
公共证券交易所(){
//总收益,
}
}
//处理用户输入的类,应该处理利润计算、减法等…基本上是
//将所有其他类/方法链接在一起
公共级StockTran{
//声明变量
private MyQueue queueArray;//数组/队列的大小为32
静态字符串firstCommand;//扫描仪的第一个标记…看起来
//无论是b、s还是c
static int shares;//购买/出售的股票数量
静态int price;//这些股票的买入/卖出价格
static int netprofice=0;//初始化利润,以便
//后加/后减
静态int put,get=0;//声明队列的前部和后部
static int total=0;//数组中元素的初始数目,这将
//增加/减少
公共静态void main(字符串[]args){
StockTran eg=新StockTran();
例如,queueArray=newmyqueue(32);
库存测试=新库存();
//提示用户输入一个输入
//if/else if梯形图用于根据调用正确的方法
//用户输入..通过查找进行读取
//在代币处
while(true){
扫描仪阅读器=新扫描仪(System.in);
System.out.println(“输入正确格式的命令”);
firstCommand=reader.next();
if(firstCommand.equalsIgnoreCase(“b”)){
shares=reader.nextInt();
price=reader.nextInt();
测试。设定价格(价格);
测试。固定股份(股份);
例如queueArray.enqueue(测试);
System.out.println(“Price:+test.getPrice());
System.out.println(“Shares:+test.getShares());
System.out.println(“Size:+eg.queueArray.Size());
//System.out.println(“购买”+股份+“股份”+,”+
//“$”+价格+”每个“);
}else if(firstCommand.equalsIgnoreCase(“s”)){
System.out.println(“出列价格:+((股票)如queueArray.dequeue()).getPrice());
System.out.println(“出列共享:+((股票)例如queueArray.dequeue()).getShares());
System.out.println(“Size:+eg.queueArray.Size());
}
else if(firstCommand.equalsIgnoreCase(“c”)){
系统输出打印项次(“资本收益”);
}else if(firstCommand.equalsIgnoreCase(“q”)){
System.out.println(“退出”);
打破
}否则{
System.out.println(“未识别命令!”);
}
}
}
//此方法将根据具体情况从总利润中加或减
//在买卖之后……还没有完成
公共整数计算(整数净利润){
int i=0;
而(i<股份){
i++;
}
返还净利润;
}
}
队列接口

    public interface Queue <E> {

        //Gets the size of array queue
        public int size();

        //Checks if the queue is empty
        public boolean isEmpty();

        //Looks at the first item
        public Object front() throws EmptyQueueException;

        //Puts an element at the end of the array
    //  public void enqueue (Node element);

        public void enqueue (E element);


        //Removes an element from the front of the array
        public Object dequeue() throws EmptyQueueException;
    }
公共接口队列{
//获取数组队列的大小
公共整数大小();
//检查q