0

オブジェクトを保持する循環キューを作成しようとしています。アイデアは、ユーザーが株を売買することであり、株が購入されるたびに、そのオブジェクトは購入された株の量と価格がキューで購入されたものを保持します。このキューは、サイズが 32 の配列ベースのキューであり、ユーザーが購入を選択するたびに、別のスロットが埋められます。ユーザーが株式を売却することを選択した場合、ユーザーが最初のスロットに含まれるよりも多くの株式を売却しない限り、売却された株式数が最初のオブジェクトから差し引かれます。その場合、それは後続のスロットから差し引かれます。スロットの株式がすべて売却されると、つまり株式がなくなると、そのオブジェクトをデキューすることができ、キューがその位置に来ると、株式を購入するためにスロットが解放されます。

オブジェクト型の処理に苦労しています。特に、適切にデキューできるかどうかを確認するためだけにデキューしようとすると....メソッドが正しいと確信していても、デキューすると最後のスロットが取得されます。クラス/オブジェクトの処理が難しいだけです。 .

注: 配列/キューを手動で作成する必要があるため、Java 組み込み配列を使用できません

StockTran クラス.......Stock をネストされたクラスとして持つ

  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;

}
 }

キュー インターフェイス

    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;
    }

MyQueue クラス

        // 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;

        }
        }

ありがとうございました!

4

0 に答える 0