I need to develop a Java program that lets me search accessories and then add th
ID: 3577196 • Letter: I
Question
I need to develop a Java program that lets me search accessories and then add them to a shopping cart and then check them out. Im not sure on how to develop the cart process. Here is what I have so far
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class WelcomeToiMobileTech {
public static List<Accessories> allDevices = new ArrayList<>();
public static String titleSearch(String devices, List<Accessories> list){
Accessories accessory1 = new Accessories("Ipad" ,"Alina. Inc" , 3);
Accessories accessory2 = new Accessories("Android ", "Masooma. Inc", 7);
Accessories accessory3 = new Accessories("Flat screen TV ", "Andrew. Inc", 5);
allDevices.add(accessory1);
allDevices.add(accessory2);
allDevices.add(accessory3);
for(int p = 0; p<list.size(); p++ ){
if(devices.equals(list.get(p).getDevices() )){
devices = (" is available ");
break;
}
else{
devices = (" is not available");
}
}
return devices;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Accessories> allDevicess = new ArrayList<>();
Accessories accessory1 = new Accessories("Ipad" ,"Alina. Inc" , 3);
Accessories accessory2 = new Accessories("Android ", "Masooma. Inc", 7);
Accessories accessory3 = new Accessories("Flat screen TV ", "Andrew. Inc", 5);
allDevices.add(accessory1);
allDevices.add(accessory2);
allDevices.add(accessory3);
Scanner input = new Scanner(System.in);
int a;
boolean good;
System.out.println("Welcome to I Mobile Tech");
System.out.println("Please enter your name");
String name = input.next();
System.out.print(" ******************");
System.out.println(" Hi " + name+"!" );
System.out.print(" ******************");
do{
System.out.println(" "+ name + ", Please select from the following options: Press 1 to search accessories Press 2 to check services press 3 to exit");
a = input.nextInt();
if( a==1){
good = true;
System.out.println(" What accessories would you like to search? ");
System.out.print(allDevices.toString());
String devices = input.next();
System.out.print( "Accessory" + Accessories.titleSearch(devices, allDevices));
}
else if (a == 2){
good = true;
}
else if(a == 3){
good = true;
System.out.print("See you agian soon");
}
else{
System.out.print("Sorry invalid input, try again ");
good = false;
}
}
while(!(good) || a==1 || a==2 );
}
}
import java.util.ArrayList;
import java.util.List;
public class Accessories {
private String devices;
private String developers;
private int quantity;
private static List<Accessories> allDevices = new ArrayList<>();
public static String titleSearch(String devices, List<Accessories> list){
Accessories accessory1 = new Accessories("Ipad" ,"Alina. Inc" , 3);
Accessories accessory2 = new Accessories("Android ", "Masooma. Inc", 7);
Accessories accessory3 = new Accessories("Flat screen TV ", "Andrew. Inc", 5);
allDevices.add(accessory1);
allDevices.add(accessory2);
allDevices.add(accessory3);
for(int p = 0; p<list.size(); p++ ){
if(devices.equals(list.get(p).getDevices() )){
devices = (" is available ");
break;
}
else{
devices = (" is not available");
}
}
return devices;
}
public Accessories(String devices, String developers, int quantity) {
this.devices = devices;
this.developers = developers;
this.quantity = quantity;
}
public String getDevices() {
return devices;
}
public void setDevices(String devices) {
this.devices = devices;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public String getDevelopers() {
return developers;
}
public void setDevelopers(String developers) {
this.developers = developers;
}
public String toString(){
return " Devices: "+getDevices()+ " | Developers: "+getDevelopers()+ " | Quantity: " + getQuantity()
;
}
}
Explanation / Answer
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class WelcomeToiMobileTech {
public static List<Accessories> allDevices = new ArrayList<>();
public static String titleSearch(String devices, List<Accessories> list){
Accessories accessory1 = new Accessories("Ipad" ,"Alina. Inc" , 3);
Accessories accessory2 = new Accessories("Android ", "Masooma. Inc", 7);
Accessories accessory3 = new Accessories("Flat screen TV ", "Andrew. Inc", 5);
allDevices.add(accessory1);
allDevices.add(accessory2);
allDevices.add(accessory3);
for(int p = 0; p<list.size(); p++ ){
if(devices.equals(list.get(p).getDevices() )){
devices = (" is available ");
break;
}
else{
devices = (" is not available");
}
}
return devices;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Accessories> cart = new ArrayList<>();
Accessories accessory1 = new Accessories("Ipad" ,"Alina. Inc" , 3);
Accessories accessory2 = new Accessories("Android", "Masooma. Inc", 7);
Accessories accessory3 = new Accessories("Flat_screen_TV", "Andrew. Inc", 5);
allDevices.add(accessory1);
allDevices.add(accessory2);
allDevices.add(accessory3);
Scanner input = new Scanner(System.in);
int a;
boolean good;
System.out.println("Welcome to I Mobile Tech");
System.out.println("Please enter your name");
String name = input.next();
System.out.print(" ******************");
System.out.println(" Hi " + name+"!" );
System.out.print(" ******************");
do{
System.out.println(" "+ name +
", Please select from the following options:"
+ " Press 1 to search accessories"
+ " Press 2 to check services"
+ " press 3 to exit"
+ " press 4 to checkout");
a = input.nextInt();
if( a==1){
good = true;
System.out.println(" What accessories would you like to search? ");
System.out.print(allDevices.toString());
String devices = input.next();
System.out.print( "Accessory "+devices + Accessories.titleSearch(devices, allDevices));
if(Accessories.titleSearch(devices, allDevices).equals(" is available ")){
System.out.println("Press 1 to add to cart. Press 2 to skip");
a = input.nextInt();
if(a==1){
for(Accessories a1:allDevices){
if(a1.getDevices().equals(devices)){
if(a1.getQuantity() > 1){
cart.add(a1);
a1.setQuantity(a1.getQuantity() - 1);
System.out.println("Item added successfully");
}
else
System.out.println("Out of stock");
break;
}
}
}
}
}
else if (a == 2){
good = true;
}
else if(a == 3){
good = true;
System.out.print("See you agian soon");
}
else if(a==4){
good = true;
for(Accessories a2:cart){
System.out.println("Item: "+a2.getDevices()+ " Quantity: 1 ");
}
System.out.println("Thank you for checking out. Bye");
}
else{
System.out.print("Sorry invalid input, try again ");
good = false;
}
}
while(!(good) || a==1 || a==2);
input.close();
}
}
//-----------------------------------------------------------------------------------------------------------------
import java.util.ArrayList;
import java.util.List;
public class Accessories {
private String devices;
private String developers;
private int quantity;
private static List<Accessories> allDevices = new ArrayList<>();
public static String titleSearch(String devices2, List<Accessories> list){
Accessories accessory1 = new Accessories("Ipad" ,"Alina. Inc" , 3);
Accessories accessory2 = new Accessories("Android", "Masooma. Inc", 7);
Accessories accessory3 = new Accessories("Flat_screen_TV", "Andrew. Inc", 5);
String devices="";
allDevices.add(accessory1);
allDevices.add(accessory2);
allDevices.add(accessory3);
for(int p = 0; p<list.size(); p++ ){
if(devices2.equals(list.get(p).getDevices() )){
devices = (" is available ");
break;
}
else{
System.out.println(list.get(p).getDevices());
devices = (" is not available");
}
}
return devices;
}
public Accessories(String devices, String developers, int quantity) {
this.devices = devices;
this.developers = developers;
this.quantity = quantity;
}
public String getDevices() {
return devices;
}
public void setDevices(String devices) {
this.devices = devices;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public String getDevelopers() {
return developers;
}
public void setDevelopers(String developers) {
this.developers = developers;
}
public String toString(){
return " Devices: "+getDevices()+ " | Developers: "+getDevelopers()+
" | Quantity: " + getQuantity()
;
}
}
//-----------------------------------------------------------------
Output:
Welcome to I Mobile Tech
Please enter your name
q
******************
Hi q!
******************
q, Please select from the following options:
Press 1 to search accessories
Press 2 to check services
press 3 to exit
press 4 to checkout
1
What accessories would you like to search?
[
Devices: Ipad | Developers: Alina. Inc | Quantity: 3,
Devices: Android | Developers: Masooma. Inc | Quantity: 7,
Devices: Flat_screen_TV | Developers: Andrew. Inc | Quantity: 5]Flat_screen_TV
Ipad
Android
Accessory Flat_screen_TV
is available Ipad
Android
Press 1 to add to cart. Press 2 to skip
1
Item added successfully
q, Please select from the following options:
Press 1 to search accessories
Press 2 to check services
press 3 to exit
press 4 to checkout
1
What accessories would you like to search?
[
Devices: Ipad | Developers: Alina. Inc | Quantity: 3,
Devices: Android | Developers: Masooma. Inc | Quantity: 7,
Devices: Flat_screen_TV | Developers: Andrew. Inc | Quantity: 4]Ipad
Accessory Ipad
is available Press 1 to add to cart. Press 2 to skip
1
Item added successfully
q, Please select from the following options:
Press 1 to search accessories
Press 2 to check services
press 3 to exit
press 4 to checkout
1
What accessories would you like to search?
[
Devices: Ipad | Developers: Alina. Inc | Quantity: 2,
Devices: Android | Developers: Masooma. Inc | Quantity: 7,
Devices: Flat_screen_TV | Developers: Andrew. Inc | Quantity: 4]Android
Ipad
Accessory Android
is available Ipad
Press 1 to add to cart. Press 2 to skip
1
Item added successfully
q, Please select from the following options:
Press 1 to search accessories
Press 2 to check services
press 3 to exit
press 4 to checkout
4
Item: Flat_screen_TV Quantity: 1
Item: Ipad Quantity: 1
Item: Android Quantity: 1
Thank you for checking out. Bye
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.