问题
I have the following program and it's working as I had hoped but now I would like to tidy up the code, putting bits into new methods, so I'm not left with loads of code in the main method. I'm new to Java, so don't know how to go about this.
I have the following methods:
public String getInventoryCode()
{
return inventoryCode;
}
public int getQuantityInStock()
{
return quantityInStock;
}
public int getDailyDemand()
{
return dailyDemand;
}
public int getReOrder()
{
return reOrder;
}
public int getLeadTime()
{
return leadTime;
}
*/ Would like to use these */
*/ Would like to use instead of quantityIn -= item_1.getDailyDemand() in main method */
public void removeDailyDemand(){
quantityInStock -= dailyDemand;
}
*/ Would like to use instead of quantityIn < reOrderThreshold */
public void newDeliveryIn(){
;
}
*/ Would like to use instead of quantityIn += item_1.getReOrder() in main method */
public void isReOrderPoint(){
;
}
The bottom three methods I have made as I want to use them but I'm not exactly sure how, so they aren't currently doing anything, I have got all the rest to function as I had hoped.
Here is the main code:
/* Create new item - Inventory Code, Starting Quantity In Stock, Daily Demand, Re-order Amount, Time in Transit (Lead-time) */
StockItem item_1 = new StockItem("A654Y", 1000, 50, 500, 5);
/* Column titles */
System.out.println("Day" + "\t" + "Stock" + "\t" + "Order Status");
System.out.println("");
/*Variables */
int reOrderThreshold = 600;
int orderTravelTime = 0;
boolean orderInTravel = false;
int quantityIn = item_1.getQuantityInStock();
/* For loop to count 50 days */
for (int n = 1; n <= 50; n++) {
String out = "";
/* Remove daily demand from current stock */
quantityIn -= item_1.getDailyDemand();
out += n;
out += " " + "\t" + quantityIn + "\t";
/* If order has been ordered and has not arrived */
if (orderInTravel && orderTravelTime < item_1.getLeadTime()) {
orderTravelTime++;
out += "In Transit -" + "\t";
/* If ordered and has arrived */
} else if (orderInTravel && orderTravelTime >= item_1.getLeadTime()) {
orderTravelTime = 0;
orderInTravel = false;
quantityIn += item_1.getReOrder();
out += "New Stock Delivered (" + item_1.getReOrder()") and Will be Added to Quantity in Stock the Next Day";
}
/* If stock gets too low, order */
if (quantityIn < reOrderThreshold) {
orderInTravel = true;
out += "Ordered";
}
/* Print out results */
System.out.println(out);
}
Here is part of the output:
Day Stock Order Status
1 950
2 900
3 850
4 800
5 750
6 700
7 650
8 600
9 550 Ordered
10 500 In Transit - Ordered
11 450 In Transit - Ordered
12 400 In Transit - Ordered
13 350 In Transit - Ordered
14 300 In Transit - Ordered
15 250 New Stock Delivered (500) and Will be Added to Quantity in Stock the Next Day
16 700
17 650
18 600
19 550 Ordered
20 500 In Transit - Ordered
21 450 In Transit - Ordered
...
I hope this is clear enough, I haven't included all the code but think what I've included if sufficient.
Thanks in advance.
来源:https://stackoverflow.com/questions/27263398/tidying-up-code-for-simple-java-program