我正在尝试创建一个包含对象的循环队列。这个想法是用户将购买或出售股票,并且每次购买股票时,该对象将保存购买的股票数量和在队列中购买的价格。这个队列是一个基于数组的队列,大小为 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;
}
}
谢谢!