In an array, how do i update the value so when one item is chosen, the stock goes down?
I have an array setup and another class in this program that gives me a getInStock to give me the current value in the array. I'm trying to subtract one of the stock once the person using the program enters in a number for the item being used. I want it to then update the number in the array in case it is used again.
public static void main(String args) {
// create the stdin object (to use the keyboard)
Scanner stdin = new Scanner(System.in);
int itemSelected = 0; // Item ID selected by user, 0 for not available
int itemIndex = 0; // selected index into array of items
// display items in the arrays using the toString method
System.out.printf ("%-4.4s %6.6s %-11.11sn", "Item", "Price", "Description");
for (Item b : ITEM_LIST) { System.out.println(b); }
System.out.println ("nSelect an item by its item number. Enter 0 to quit");
do {
try {
System.out.printf ("item #%d: ", shoppingCartCount+1);
itemSelected = stdin.nextInt( ); // read line from keyboard
if (itemSelected == 0)
continue; // exit the loop
// Search ITEM_LIST looking for the user's requested itemID
for (itemIndex=0; itemIndex<ITEM_LIST.length; itemIndex++)
if (itemSelected == ITEM_LIST[itemIndex].getItemID()){
if (ITEM_LIST[itemIndex].getInStock()<1){
System.out.println("Item is not in stock try later");
}
break; // it was found, itemIndex = position in the LIST
}
if (itemIndex == ITEM_LIST.length){ // reached the end and not found
System.out.println("Item is not available");
}
if (shoppingCartCount==9 ) {
shoppingCart[shoppingCartCount] = ITEM_LIST[itemIndex];
shoppingCartCount++; // keep track of items in the cart
System.out.println("Your cart is full.");
break;
}
else { // The item was found
shoppingCart[shoppingCartCount] = ITEM_LIST[itemIndex];
shoppingCartCount++; // keep track of items in the cart
}
} //end of try
catch (InputMismatchException | StringIndexOutOfBoundsException e) {
System.out.println ("Illegal selection. Try again");
}
} while (itemSelected != 0); // loop until a '0' is entered
// display the shopping cart
System.out.println("nnThank you for shopping at Shop-azon");
double total=0;
for (int i=0; i<shoppingCartCount; i++) {
System.out.println(shoppingCart[i]);
total += shoppingCart[i].getPrice();
}
System.out.println(shoppingCartCount + " items in your cart");
System.out.printf("Your total is $%.2fnn", total);
} // end of main()
java
|
show 2 more comments
I have an array setup and another class in this program that gives me a getInStock to give me the current value in the array. I'm trying to subtract one of the stock once the person using the program enters in a number for the item being used. I want it to then update the number in the array in case it is used again.
public static void main(String args) {
// create the stdin object (to use the keyboard)
Scanner stdin = new Scanner(System.in);
int itemSelected = 0; // Item ID selected by user, 0 for not available
int itemIndex = 0; // selected index into array of items
// display items in the arrays using the toString method
System.out.printf ("%-4.4s %6.6s %-11.11sn", "Item", "Price", "Description");
for (Item b : ITEM_LIST) { System.out.println(b); }
System.out.println ("nSelect an item by its item number. Enter 0 to quit");
do {
try {
System.out.printf ("item #%d: ", shoppingCartCount+1);
itemSelected = stdin.nextInt( ); // read line from keyboard
if (itemSelected == 0)
continue; // exit the loop
// Search ITEM_LIST looking for the user's requested itemID
for (itemIndex=0; itemIndex<ITEM_LIST.length; itemIndex++)
if (itemSelected == ITEM_LIST[itemIndex].getItemID()){
if (ITEM_LIST[itemIndex].getInStock()<1){
System.out.println("Item is not in stock try later");
}
break; // it was found, itemIndex = position in the LIST
}
if (itemIndex == ITEM_LIST.length){ // reached the end and not found
System.out.println("Item is not available");
}
if (shoppingCartCount==9 ) {
shoppingCart[shoppingCartCount] = ITEM_LIST[itemIndex];
shoppingCartCount++; // keep track of items in the cart
System.out.println("Your cart is full.");
break;
}
else { // The item was found
shoppingCart[shoppingCartCount] = ITEM_LIST[itemIndex];
shoppingCartCount++; // keep track of items in the cart
}
} //end of try
catch (InputMismatchException | StringIndexOutOfBoundsException e) {
System.out.println ("Illegal selection. Try again");
}
} while (itemSelected != 0); // loop until a '0' is entered
// display the shopping cart
System.out.println("nnThank you for shopping at Shop-azon");
double total=0;
for (int i=0; i<shoppingCartCount; i++) {
System.out.println(shoppingCart[i]);
total += shoppingCart[i].getPrice();
}
System.out.println(shoppingCartCount + " items in your cart");
System.out.printf("Your total is $%.2fnn", total);
} // end of main()
java
Please add a minimal, complete, and verifiable example
– Scary Wombat
Nov 13 '18 at 0:16
What isITEM_LIST
,Item
- what problems are you facing?
– Scary Wombat
Nov 13 '18 at 0:16
Item_ list is the array of everything in the store. Item is another class in the program that send the prices, stock and item ID to the main program I posted. Everything else in the program works fine. The problem I am having, is getting the number of items in stock to decrease when one is chosen by the user
– Moca
Nov 13 '18 at 0:21
Huh? I can not see where in this jumble of code that you are trying to decrease the stock item count
– Scary Wombat
Nov 13 '18 at 0:27
On the try{. On the for loop
– Moca
Nov 13 '18 at 0:32
|
show 2 more comments
I have an array setup and another class in this program that gives me a getInStock to give me the current value in the array. I'm trying to subtract one of the stock once the person using the program enters in a number for the item being used. I want it to then update the number in the array in case it is used again.
public static void main(String args) {
// create the stdin object (to use the keyboard)
Scanner stdin = new Scanner(System.in);
int itemSelected = 0; // Item ID selected by user, 0 for not available
int itemIndex = 0; // selected index into array of items
// display items in the arrays using the toString method
System.out.printf ("%-4.4s %6.6s %-11.11sn", "Item", "Price", "Description");
for (Item b : ITEM_LIST) { System.out.println(b); }
System.out.println ("nSelect an item by its item number. Enter 0 to quit");
do {
try {
System.out.printf ("item #%d: ", shoppingCartCount+1);
itemSelected = stdin.nextInt( ); // read line from keyboard
if (itemSelected == 0)
continue; // exit the loop
// Search ITEM_LIST looking for the user's requested itemID
for (itemIndex=0; itemIndex<ITEM_LIST.length; itemIndex++)
if (itemSelected == ITEM_LIST[itemIndex].getItemID()){
if (ITEM_LIST[itemIndex].getInStock()<1){
System.out.println("Item is not in stock try later");
}
break; // it was found, itemIndex = position in the LIST
}
if (itemIndex == ITEM_LIST.length){ // reached the end and not found
System.out.println("Item is not available");
}
if (shoppingCartCount==9 ) {
shoppingCart[shoppingCartCount] = ITEM_LIST[itemIndex];
shoppingCartCount++; // keep track of items in the cart
System.out.println("Your cart is full.");
break;
}
else { // The item was found
shoppingCart[shoppingCartCount] = ITEM_LIST[itemIndex];
shoppingCartCount++; // keep track of items in the cart
}
} //end of try
catch (InputMismatchException | StringIndexOutOfBoundsException e) {
System.out.println ("Illegal selection. Try again");
}
} while (itemSelected != 0); // loop until a '0' is entered
// display the shopping cart
System.out.println("nnThank you for shopping at Shop-azon");
double total=0;
for (int i=0; i<shoppingCartCount; i++) {
System.out.println(shoppingCart[i]);
total += shoppingCart[i].getPrice();
}
System.out.println(shoppingCartCount + " items in your cart");
System.out.printf("Your total is $%.2fnn", total);
} // end of main()
java
I have an array setup and another class in this program that gives me a getInStock to give me the current value in the array. I'm trying to subtract one of the stock once the person using the program enters in a number for the item being used. I want it to then update the number in the array in case it is used again.
public static void main(String args) {
// create the stdin object (to use the keyboard)
Scanner stdin = new Scanner(System.in);
int itemSelected = 0; // Item ID selected by user, 0 for not available
int itemIndex = 0; // selected index into array of items
// display items in the arrays using the toString method
System.out.printf ("%-4.4s %6.6s %-11.11sn", "Item", "Price", "Description");
for (Item b : ITEM_LIST) { System.out.println(b); }
System.out.println ("nSelect an item by its item number. Enter 0 to quit");
do {
try {
System.out.printf ("item #%d: ", shoppingCartCount+1);
itemSelected = stdin.nextInt( ); // read line from keyboard
if (itemSelected == 0)
continue; // exit the loop
// Search ITEM_LIST looking for the user's requested itemID
for (itemIndex=0; itemIndex<ITEM_LIST.length; itemIndex++)
if (itemSelected == ITEM_LIST[itemIndex].getItemID()){
if (ITEM_LIST[itemIndex].getInStock()<1){
System.out.println("Item is not in stock try later");
}
break; // it was found, itemIndex = position in the LIST
}
if (itemIndex == ITEM_LIST.length){ // reached the end and not found
System.out.println("Item is not available");
}
if (shoppingCartCount==9 ) {
shoppingCart[shoppingCartCount] = ITEM_LIST[itemIndex];
shoppingCartCount++; // keep track of items in the cart
System.out.println("Your cart is full.");
break;
}
else { // The item was found
shoppingCart[shoppingCartCount] = ITEM_LIST[itemIndex];
shoppingCartCount++; // keep track of items in the cart
}
} //end of try
catch (InputMismatchException | StringIndexOutOfBoundsException e) {
System.out.println ("Illegal selection. Try again");
}
} while (itemSelected != 0); // loop until a '0' is entered
// display the shopping cart
System.out.println("nnThank you for shopping at Shop-azon");
double total=0;
for (int i=0; i<shoppingCartCount; i++) {
System.out.println(shoppingCart[i]);
total += shoppingCart[i].getPrice();
}
System.out.println(shoppingCartCount + " items in your cart");
System.out.printf("Your total is $%.2fnn", total);
} // end of main()
java
java
asked Nov 13 '18 at 0:13
MocaMoca
83
83
Please add a minimal, complete, and verifiable example
– Scary Wombat
Nov 13 '18 at 0:16
What isITEM_LIST
,Item
- what problems are you facing?
– Scary Wombat
Nov 13 '18 at 0:16
Item_ list is the array of everything in the store. Item is another class in the program that send the prices, stock and item ID to the main program I posted. Everything else in the program works fine. The problem I am having, is getting the number of items in stock to decrease when one is chosen by the user
– Moca
Nov 13 '18 at 0:21
Huh? I can not see where in this jumble of code that you are trying to decrease the stock item count
– Scary Wombat
Nov 13 '18 at 0:27
On the try{. On the for loop
– Moca
Nov 13 '18 at 0:32
|
show 2 more comments
Please add a minimal, complete, and verifiable example
– Scary Wombat
Nov 13 '18 at 0:16
What isITEM_LIST
,Item
- what problems are you facing?
– Scary Wombat
Nov 13 '18 at 0:16
Item_ list is the array of everything in the store. Item is another class in the program that send the prices, stock and item ID to the main program I posted. Everything else in the program works fine. The problem I am having, is getting the number of items in stock to decrease when one is chosen by the user
– Moca
Nov 13 '18 at 0:21
Huh? I can not see where in this jumble of code that you are trying to decrease the stock item count
– Scary Wombat
Nov 13 '18 at 0:27
On the try{. On the for loop
– Moca
Nov 13 '18 at 0:32
Please add a minimal, complete, and verifiable example
– Scary Wombat
Nov 13 '18 at 0:16
Please add a minimal, complete, and verifiable example
– Scary Wombat
Nov 13 '18 at 0:16
What is
ITEM_LIST
, Item
- what problems are you facing?– Scary Wombat
Nov 13 '18 at 0:16
What is
ITEM_LIST
, Item
- what problems are you facing?– Scary Wombat
Nov 13 '18 at 0:16
Item_ list is the array of everything in the store. Item is another class in the program that send the prices, stock and item ID to the main program I posted. Everything else in the program works fine. The problem I am having, is getting the number of items in stock to decrease when one is chosen by the user
– Moca
Nov 13 '18 at 0:21
Item_ list is the array of everything in the store. Item is another class in the program that send the prices, stock and item ID to the main program I posted. Everything else in the program works fine. The problem I am having, is getting the number of items in stock to decrease when one is chosen by the user
– Moca
Nov 13 '18 at 0:21
Huh? I can not see where in this jumble of code that you are trying to decrease the stock item count
– Scary Wombat
Nov 13 '18 at 0:27
Huh? I can not see where in this jumble of code that you are trying to decrease the stock item count
– Scary Wombat
Nov 13 '18 at 0:27
On the try{. On the for loop
– Moca
Nov 13 '18 at 0:32
On the try{. On the for loop
– Moca
Nov 13 '18 at 0:32
|
show 2 more comments
1 Answer
1
active
oldest
votes
for the use case you have explained , like you want to remove the number when you are using it and later update it better use Queue in Java and use functions like pop() , push() , seek() for help you can see this
Click here!
This makes sense, thank you very much
– Moca
Nov 13 '18 at 0:59
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271958%2fin-an-array-how-do-i-update-the-value-so-when-one-item-is-chosen-the-stock-goe%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
for the use case you have explained , like you want to remove the number when you are using it and later update it better use Queue in Java and use functions like pop() , push() , seek() for help you can see this
Click here!
This makes sense, thank you very much
– Moca
Nov 13 '18 at 0:59
add a comment |
for the use case you have explained , like you want to remove the number when you are using it and later update it better use Queue in Java and use functions like pop() , push() , seek() for help you can see this
Click here!
This makes sense, thank you very much
– Moca
Nov 13 '18 at 0:59
add a comment |
for the use case you have explained , like you want to remove the number when you are using it and later update it better use Queue in Java and use functions like pop() , push() , seek() for help you can see this
Click here!
for the use case you have explained , like you want to remove the number when you are using it and later update it better use Queue in Java and use functions like pop() , push() , seek() for help you can see this
Click here!
answered Nov 13 '18 at 0:56
user3655403user3655403
614
614
This makes sense, thank you very much
– Moca
Nov 13 '18 at 0:59
add a comment |
This makes sense, thank you very much
– Moca
Nov 13 '18 at 0:59
This makes sense, thank you very much
– Moca
Nov 13 '18 at 0:59
This makes sense, thank you very much
– Moca
Nov 13 '18 at 0:59
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271958%2fin-an-array-how-do-i-update-the-value-so-when-one-item-is-chosen-the-stock-goe%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Please add a minimal, complete, and verifiable example
– Scary Wombat
Nov 13 '18 at 0:16
What is
ITEM_LIST
,Item
- what problems are you facing?– Scary Wombat
Nov 13 '18 at 0:16
Item_ list is the array of everything in the store. Item is another class in the program that send the prices, stock and item ID to the main program I posted. Everything else in the program works fine. The problem I am having, is getting the number of items in stock to decrease when one is chosen by the user
– Moca
Nov 13 '18 at 0:21
Huh? I can not see where in this jumble of code that you are trying to decrease the stock item count
– Scary Wombat
Nov 13 '18 at 0:27
On the try{. On the for loop
– Moca
Nov 13 '18 at 0:32