Please help with this java assignment that I\'m trying to finish by Monday. The
ID: 3553916 • Letter: P
Question
Please help with this java assignment that I'm trying to finish by Monday. The Assignment is as follows:
In this assignment you will write a program that allows a person to create and manipulate a shopping list for the grocery store. At any given time the list may be partially filled, and your program must keep track of how many values are ACTUALLY in the grocery list, versus the size of your list array. Your program will display a menu that will allow the user to perform the following operations REPEATEDLY until they select quit. You are expected to implement STATIC METHODS for each of the following operations except option 1
Explanation / Answer
Here is the complete code with full explanation...plzzz rate...
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;
public class grocery {
public static int currentSize = 0;
public static int lastPos = 0;
public static Scanner sc = new Scanner(System.in);
public static void main(String[] args) throws IOException {
System.out.println("*********Welcome to Grocery store***********");
System.out
.println("Please enter the size of your purchase list(limit is 20)");
int size = sc.nextInt();
if (size > 20) {
System.out.println("Please restart the program!!!");
} else {
// making the array
String item_array[] = new String[size];
// starting the do while loop infinitely until the user chooses quit
// all the functions are self explanatory
do {
System.out.println("Please choose one of the following");
System.out.println("1.Add a new item to the end of list.");
System.out
.println("2.Add a new item at a specific position in list.");
System.out.println("3.Delete an item from the end of the list");
System.out
.println("4.Delete an item from a specific position in the list");
System.out.println("5.Display a element");
System.out.println("6.Print the list");
System.out.println("7.Sort items in the list");
System.out.println("8.Search the items in the list");
System.out.println("9.Quit");
int ch = sc.nextInt();
switch (ch) {
case 1:
addToEnd(item_array);
System.out
.println("Number of current items:" + currentSize);
break;
case 2:
addToPos(item_array);
System.out
.println("Number of current items:" + currentSize);
break;
case 3:
deleteFromEnd(item_array);
System.out
.println("Number of current items:" + currentSize);
break;
case 4:
deleteFromPos(item_array);
System.out
.println("Number of current items:" + currentSize);
break;
case 5:
displayElement(item_array);
break;
case 6:
print_list(item_array);
System.out
.println("Number of current items:" + currentSize);
break;
case 7:
sort_list(item_array);
break;
case 8:
search_list(item_array);
break;
case 9:
sc.close();
System.exit(0);
break;
}
} while (true);
}
}
// searching in the list
private static void search_list(String[] item_array) throws IOException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter a item to search for:");
int count = 0, i = 0;
String item = br.readLine();
for (i = 0; i < item_array.length; i++) {
if (item_array[i].equals(item)) {
count = 1;
break;
}
}
if (count == 1) {
System.out.println("Item found at index " + i + " of the list");
}
}
// sortig the list
private static void sort_list(String[] item_array) {
// TODO Auto-generated method stub
// simply using the sort method in the java.util package
Arrays.sort(item_array);
}
// printint the list
private static void print_list(String[] item_array) {
// TODO Auto-generated method stub
for (int i = 0; i < item_array.length; i++) {
System.out.println(item_array[i]);
}
}
// displaying the element
private static void displayElement(String[] item_array) {
// TODO Auto-generated method stub
System.out.println("Please enter a position");
int pos = sc.nextInt();
if (pos < 0 || pos >= item_array.length) {
System.out.println("Wrong position entered");
} else {
System.out.println("The list item at this position is:"
+ item_array[pos]);
}
}
// deleting at a position
private static void deleteFromPos(String[] item_array) {
// TODO Auto-generated method stub
System.out.println("Please enter a position");
int pos = sc.nextInt();
if (pos < 0 || pos >= item_array.length) {
System.out.println("Wrong position entered");
} else {
item_array[pos] = null;
System.out.println("Removed from position" + (pos + 1));
currentSize -= 1;
det_pos(item_array);
}
}
// deleting from the end
private static void deleteFromEnd(String[] item_array) {
// TODO Auto-generated method stub
item_array[lastPos] = null;
currentSize -= 1;
det_pos(item_array);
System.out.println("Removed the item");
}
// adding at a index
private static void addToPos(String[] item_array)
throws NumberFormatException, IOException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter a position");
int pos = Integer.parseInt(br.readLine());
if (pos < 0 || pos >= item_array.length) {
System.out.println("Wrong position entered");
} else {
System.out.println("Please enter the item");
String item = br.readLine();
item_array[pos] = item;
System.out.println("Inserted at position" + (pos + 1));
currentSize += 1;
det_pos(item_array);
}
}
// adding to the end
private static void addToEnd(String[] item_array) throws IOException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the list item");
String item = br.readLine();
item_array[currentSize] = item;
System.out.println("Inserted at last");
currentSize += 1;
det_pos(item_array);
}
// function to determine the last non null elemnt in the array
private static void det_pos(String[] item_array) {
lastPos = 0;
for (int i = 0; i < item_array.length; i++) {
if (item_array[i] != null) {
lastPos = i;
}
}
}
}