patternjavaMinor
Stock process program
Viewed 0 times
stockprogramprocess
Problem
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:
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 = item_1.getLeadTime()) {
orderTravelTime = 0;
orderInTravel = false;
quantityIn += item_1.getReOrder();
out += "New Stock Delivered
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 = item_1.getLeadTime()) {
orderTravelTime = 0;
orderInTravel = false;
quantityIn += item_1.getReOrder();
out += "New Stock Delivered
Solution
The first thing I should say is that your code looks very messy and requires "cleaning up". I don't know for sure if this is because of formatting problems when transferring from IDE to Code Review, but if it is not, then formatting needs work.
After formatting, it looks like this:
Note that this is not the final result. There are a lot more to add before it's good.
Now to the actual code...
First of all...
In the main method, there is this:
This could be easily changed to:
This improves efficiency by reducing the time required to concatenate the
Remember that
All the code in the
Then, let's break it away from the
And then draw out the
And there you go, small methods!
As for the extra methods, I think they are unnecessary.
Final code:
```
public class Main {
static StockItem item_1 = new StockItem("A654Y", 1000, 50, 500, 5);
static int reOrderThreshold = 600;
static int orderTravelTime = 0;
st
After formatting, it looks like this:
public class Test {
public static void main(String[] args) {
StockItem item_1 = new StockItem("A654Y", 1000, 50, 500, 5);
System.out.println("Day" + "\t" + "Stock" + "\t" + "Order Status");
System.out.println();
int reOrderThreshold = 600;
int orderTravelTime = 0;
boolean orderInTravel = false;
int quantityIn = item_1.getQuantityInStock();
for (int n = 1; n = 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 (quantityIn < reOrderThreshold) {
orderInTravel = true;
out += "Ordered";
}
System.out.println(out);
}
}
}
class StockItem {
String inventoryCode;
int quantityInStock;
int dailyDemand;
int reOrder;
int leadTime;
public StockItem(String inventoryCode, int quantityInStock,
int dailyDemand, int reOrder, int leadTime) {
this.inventoryCode = inventoryCode;
this.quantityInStock = quantityInStock;
this.dailyDemand = dailyDemand;
this.reOrder = reOrder;
this.leadTime = leadTime;
}
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;
}
}Note that this is not the final result. There are a lot more to add before it's good.
Now to the actual code...
First of all...
In the main method, there is this:
System.out.println("Day" + "\t" + "Stock" + "\t" + "Order Status");
System.out.println();This could be easily changed to:
System.out.println("Day\tStock\tOrder Status\n");This improves efficiency by reducing the time required to concatenate the
Strings, as well as removing the requirement of the empty System.out.println(). In fact, you split the Strings where no split is necessary. " " + "\t" can just simply be " \t".String out = "";Remember that
Strings are immutable. Since out is concatenated multiple times, you should use StringBuilder, and add Strings with out.append(...).All the code in the
for loop can be at least broken down into two methods. But before we do that, the variables should be instantiated outside of main, with static.static StockItem item_1 = new StockItem("A654Y", 1000, 50, 500, 5);
static int reOrderThreshold = 600;
static int orderTravelTime = 0;
static boolean orderInTravel = false;
static int quantityIn = item_1.getQuantityInStock();Then, let's break it away from the
main method:public static void main(String[] args) {
System.out.println("Day\tStock\tOrder Status\n");
for (int n = 1; n = item_1.getLeadTime()) {
orderTravelTime = 0;
orderInTravel = false;
quantityIn += item_1.getReOrder();
out.append("New Stock Delivered (").append(item_1.getReOrder()).append(") and Will be Added to Quantity in Stock the Next Day");
}
if (quantityIn < reOrderThreshold) {
orderInTravel = true;
out.append("Ordered");
}
System.out.println(out);
}And then draw out the
if statements:private static void doDay(int day) {
StringBuilder out = new StringBuilder();
quantityIn -= item_1.getDailyDemand();
out.append(day).append(" \t").append(quantityIn).append("\t");
checkOrder(out);
checkStock(out);
System.out.println(out);
}
private static void checkOrder(StringBuilder output) {
if (orderInTravel && orderTravelTime = item_1.getLeadTime()) {
orderTravelTime = 0;
orderInTravel = false;
quantityIn += item_1.getReOrder();
output.append("New Stock Delivered (").append(item_1.getReOrder()).append(") and Will be Added to Quantity in Stock the Next Day");
}
}
private static void checkStock(StringBuilder output) {
if (quantityIn < reOrderThreshold) {
orderInTravel = true;
output.append("Ordered");
}
}And there you go, small methods!
As for the extra methods, I think they are unnecessary.
Final code:
```
public class Main {
static StockItem item_1 = new StockItem("A654Y", 1000, 50, 500, 5);
static int reOrderThreshold = 600;
static int orderTravelTime = 0;
st
Code Snippets
public class Test {
public static void main(String[] args) {
StockItem item_1 = new StockItem("A654Y", 1000, 50, 500, 5);
System.out.println("Day" + "\t" + "Stock" + "\t" + "Order Status");
System.out.println();
int reOrderThreshold = 600;
int orderTravelTime = 0;
boolean orderInTravel = false;
int quantityIn = item_1.getQuantityInStock();
for (int n = 1; n <= 50; n++) {
String out = "";
quantityIn -= item_1.getDailyDemand();
out += n;
out += " " + "\t" + quantityIn + "\t";
if (orderInTravel && orderTravelTime < item_1.getLeadTime()) {
orderTravelTime++;
out += "In Transit -" + "\t";
} 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 (quantityIn < reOrderThreshold) {
orderInTravel = true;
out += "Ordered";
}
System.out.println(out);
}
}
}
class StockItem {
String inventoryCode;
int quantityInStock;
int dailyDemand;
int reOrder;
int leadTime;
public StockItem(String inventoryCode, int quantityInStock,
int dailyDemand, int reOrder, int leadTime) {
this.inventoryCode = inventoryCode;
this.quantityInStock = quantityInStock;
this.dailyDemand = dailyDemand;
this.reOrder = reOrder;
this.leadTime = leadTime;
}
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;
}
}System.out.println("Day" + "\t" + "Stock" + "\t" + "Order Status");
System.out.println();System.out.println("Day\tStock\tOrder Status\n");String out = "";static StockItem item_1 = new StockItem("A654Y", 1000, 50, 500, 5);
static int reOrderThreshold = 600;
static int orderTravelTime = 0;
static boolean orderInTravel = false;
static int quantityIn = item_1.getQuantityInStock();Context
StackExchange Code Review Q#71467, answer score: 4
Revisions (0)
No revisions yet.