Option menu that increases and decreases with number of arraylist objects
I've created a for loop that lists all objects in my array-list but i need it to be a dynamic option menu that always ends with exit. I have used switch case option menu in other parts of the program in different methods. But I'm not sure how to create an incrementing case switch in here or if it would have to be put into the for loop?
private static void subCar(Scanner keyboard, CarLot carLot) {
if (carLot.getCar().size() == 0) {
System.out.println("No Cars on the Car Lot to Remove");
}else {
System.out.println("");
System.out.println("Cars Available to Remove: ");
System.out.printf("%-7s%-6s%-35s%-5sn","Option"," ID","Make/Model/Year","Price");
for (int index=0; index < carLot.getCar().size(); index++) {
System.out.printf("%-7s%-6s%-2s%-5sn",carLot.getCar().get(index).getID(),
carLot.getCar().get(index).getMake(),carLot.getCar().get(index).getModel(),
carLot.getCar().get(index).getPrice());
}
}
}
I'm trying to create a menu option so I can select which object to delete from my array-list
I want the output to look like this:
option ID Make/Model/Year Price
1. 2 Chevrolet cavalier 2000 1999.99
2. Exit
enter option:
exit option needs to always come last
java arraylist switch-statement optionmenu
add a comment |
I've created a for loop that lists all objects in my array-list but i need it to be a dynamic option menu that always ends with exit. I have used switch case option menu in other parts of the program in different methods. But I'm not sure how to create an incrementing case switch in here or if it would have to be put into the for loop?
private static void subCar(Scanner keyboard, CarLot carLot) {
if (carLot.getCar().size() == 0) {
System.out.println("No Cars on the Car Lot to Remove");
}else {
System.out.println("");
System.out.println("Cars Available to Remove: ");
System.out.printf("%-7s%-6s%-35s%-5sn","Option"," ID","Make/Model/Year","Price");
for (int index=0; index < carLot.getCar().size(); index++) {
System.out.printf("%-7s%-6s%-2s%-5sn",carLot.getCar().get(index).getID(),
carLot.getCar().get(index).getMake(),carLot.getCar().get(index).getModel(),
carLot.getCar().get(index).getPrice());
}
}
}
I'm trying to create a menu option so I can select which object to delete from my array-list
I want the output to look like this:
option ID Make/Model/Year Price
1. 2 Chevrolet cavalier 2000 1999.99
2. Exit
enter option:
exit option needs to always come last
java arraylist switch-statement optionmenu
putcarLot.getCar().get(index)in a variable, and then access to its properties, don't access the list all time
– azro
Nov 14 '18 at 1:58
have you thought about using a stack or queue made from your arraylist?
– Thatalent
Nov 14 '18 at 2:09
add a comment |
I've created a for loop that lists all objects in my array-list but i need it to be a dynamic option menu that always ends with exit. I have used switch case option menu in other parts of the program in different methods. But I'm not sure how to create an incrementing case switch in here or if it would have to be put into the for loop?
private static void subCar(Scanner keyboard, CarLot carLot) {
if (carLot.getCar().size() == 0) {
System.out.println("No Cars on the Car Lot to Remove");
}else {
System.out.println("");
System.out.println("Cars Available to Remove: ");
System.out.printf("%-7s%-6s%-35s%-5sn","Option"," ID","Make/Model/Year","Price");
for (int index=0; index < carLot.getCar().size(); index++) {
System.out.printf("%-7s%-6s%-2s%-5sn",carLot.getCar().get(index).getID(),
carLot.getCar().get(index).getMake(),carLot.getCar().get(index).getModel(),
carLot.getCar().get(index).getPrice());
}
}
}
I'm trying to create a menu option so I can select which object to delete from my array-list
I want the output to look like this:
option ID Make/Model/Year Price
1. 2 Chevrolet cavalier 2000 1999.99
2. Exit
enter option:
exit option needs to always come last
java arraylist switch-statement optionmenu
I've created a for loop that lists all objects in my array-list but i need it to be a dynamic option menu that always ends with exit. I have used switch case option menu in other parts of the program in different methods. But I'm not sure how to create an incrementing case switch in here or if it would have to be put into the for loop?
private static void subCar(Scanner keyboard, CarLot carLot) {
if (carLot.getCar().size() == 0) {
System.out.println("No Cars on the Car Lot to Remove");
}else {
System.out.println("");
System.out.println("Cars Available to Remove: ");
System.out.printf("%-7s%-6s%-35s%-5sn","Option"," ID","Make/Model/Year","Price");
for (int index=0; index < carLot.getCar().size(); index++) {
System.out.printf("%-7s%-6s%-2s%-5sn",carLot.getCar().get(index).getID(),
carLot.getCar().get(index).getMake(),carLot.getCar().get(index).getModel(),
carLot.getCar().get(index).getPrice());
}
}
}
I'm trying to create a menu option so I can select which object to delete from my array-list
I want the output to look like this:
option ID Make/Model/Year Price
1. 2 Chevrolet cavalier 2000 1999.99
2. Exit
enter option:
exit option needs to always come last
java arraylist switch-statement optionmenu
java arraylist switch-statement optionmenu
edited Nov 14 '18 at 1:58
azro
10.9k41438
10.9k41438
asked Nov 14 '18 at 1:47
S-JayS-Jay
31
31
putcarLot.getCar().get(index)in a variable, and then access to its properties, don't access the list all time
– azro
Nov 14 '18 at 1:58
have you thought about using a stack or queue made from your arraylist?
– Thatalent
Nov 14 '18 at 2:09
add a comment |
putcarLot.getCar().get(index)in a variable, and then access to its properties, don't access the list all time
– azro
Nov 14 '18 at 1:58
have you thought about using a stack or queue made from your arraylist?
– Thatalent
Nov 14 '18 at 2:09
put
carLot.getCar().get(index) in a variable, and then access to its properties, don't access the list all time– azro
Nov 14 '18 at 1:58
put
carLot.getCar().get(index) in a variable, and then access to its properties, don't access the list all time– azro
Nov 14 '18 at 1:58
have you thought about using a stack or queue made from your arraylist?
– Thatalent
Nov 14 '18 at 2:09
have you thought about using a stack or queue made from your arraylist?
– Thatalent
Nov 14 '18 at 2:09
add a comment |
1 Answer
1
active
oldest
votes
Your last option will always be bigger then size of list of cars.
int option = //read it form scanner;
if (option > carLot.getCar().size()) {
// exit
} else {
carLot.getCar().remove(option);
}
To print all options and an exit one. You need to extract index outside the loop, loop and one print after loop. One of the example:
int index = 0;
for (; index < carLot.getCar().size(); index++) {
System.out.printf("%-7s%-6s%-2s%-5sn",
index,
carLot.getCar().get(index).getID(),
carLot.getCar().get(index).getMake() + " " + carLot.getCar().get(index).getModel(),
carLot.getCar().get(index).getPrice());
}
System.out.printf("%-7s%-6s", index, "Exit");
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%2f53292026%2foption-menu-that-increases-and-decreases-with-number-of-arraylist-objects%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
Your last option will always be bigger then size of list of cars.
int option = //read it form scanner;
if (option > carLot.getCar().size()) {
// exit
} else {
carLot.getCar().remove(option);
}
To print all options and an exit one. You need to extract index outside the loop, loop and one print after loop. One of the example:
int index = 0;
for (; index < carLot.getCar().size(); index++) {
System.out.printf("%-7s%-6s%-2s%-5sn",
index,
carLot.getCar().get(index).getID(),
carLot.getCar().get(index).getMake() + " " + carLot.getCar().get(index).getModel(),
carLot.getCar().get(index).getPrice());
}
System.out.printf("%-7s%-6s", index, "Exit");
add a comment |
Your last option will always be bigger then size of list of cars.
int option = //read it form scanner;
if (option > carLot.getCar().size()) {
// exit
} else {
carLot.getCar().remove(option);
}
To print all options and an exit one. You need to extract index outside the loop, loop and one print after loop. One of the example:
int index = 0;
for (; index < carLot.getCar().size(); index++) {
System.out.printf("%-7s%-6s%-2s%-5sn",
index,
carLot.getCar().get(index).getID(),
carLot.getCar().get(index).getMake() + " " + carLot.getCar().get(index).getModel(),
carLot.getCar().get(index).getPrice());
}
System.out.printf("%-7s%-6s", index, "Exit");
add a comment |
Your last option will always be bigger then size of list of cars.
int option = //read it form scanner;
if (option > carLot.getCar().size()) {
// exit
} else {
carLot.getCar().remove(option);
}
To print all options and an exit one. You need to extract index outside the loop, loop and one print after loop. One of the example:
int index = 0;
for (; index < carLot.getCar().size(); index++) {
System.out.printf("%-7s%-6s%-2s%-5sn",
index,
carLot.getCar().get(index).getID(),
carLot.getCar().get(index).getMake() + " " + carLot.getCar().get(index).getModel(),
carLot.getCar().get(index).getPrice());
}
System.out.printf("%-7s%-6s", index, "Exit");
Your last option will always be bigger then size of list of cars.
int option = //read it form scanner;
if (option > carLot.getCar().size()) {
// exit
} else {
carLot.getCar().remove(option);
}
To print all options and an exit one. You need to extract index outside the loop, loop and one print after loop. One of the example:
int index = 0;
for (; index < carLot.getCar().size(); index++) {
System.out.printf("%-7s%-6s%-2s%-5sn",
index,
carLot.getCar().get(index).getID(),
carLot.getCar().get(index).getMake() + " " + carLot.getCar().get(index).getModel(),
carLot.getCar().get(index).getPrice());
}
System.out.printf("%-7s%-6s", index, "Exit");
edited Nov 14 '18 at 2:09
answered Nov 14 '18 at 1:56
uliuli
519313
519313
add a comment |
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%2f53292026%2foption-menu-that-increases-and-decreases-with-number-of-arraylist-objects%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
put
carLot.getCar().get(index)in a variable, and then access to its properties, don't access the list all time– azro
Nov 14 '18 at 1:58
have you thought about using a stack or queue made from your arraylist?
– Thatalent
Nov 14 '18 at 2:09