Write a java console application that allows the user to keep track of various i
ID: 3747834 • Letter: W
Question
Write a java console application that allows the user to keep track of various items in a grocery
shop. The application must allow the user to read, validate, store, display, sort and search the item
name (String), item id (integer number), quantity of the item in-stock (integer number), factory
price (double number) and shop price (double number) for N items in the grocery shop. N should be
declared as a constant and it should be equal to the largest digit of your student id number (e.g. if
your ID number is S0234111 then N should be equal to 4 and you can declare it as final int N=4;).
The item name, item id, quantity of the item in-stock, factory price and shop price must be stored in
five separate single dimension Arrays/ArrayLists (index 0 for item 1 and index N-1 for item N).
The minimum and maximum id numbers which can be stored are 1 and 333. The minimum and
maximum factory prices for each item which can be stored are $1 and $1000. The item id and
factory price must be entered from the keyboard and a validation for minimum and maximum
values must be done.
Your application should display and execute a menu with the following options. A switch statement
must be used to execute the following menu options.1. Read, validate and store all items 2. Calculate and store shop price for all items 3. Display all items 4. Sell an item 5. Display all items with the lowest factory price 6. Sort and display sorted items
7. Exit from the application 1. Read, validate and store all items
This option reads item name, item id, quantity of the item in-stock and factory price for N items
from the keyboard and stores them in four separate single dimension Arrays/ArrayLists. If the id
number is less than 1 and greater than 333 then an appropriate message should be displayed and the
user should be asked to enter a new id number. Similarly if the factory price is less than $1 and
greater than $1000 then an appropriate message should be displayed and the user should be asked to
enter a new price.2. Calculate and store shop price for all items
This option asks user to enter the discount percentage (e.g. 5%), calculates the shop price for each
item and then stores shop price in fifth Array/ArrayList. 3. Display all items
Display all information (item name, item id, quantity of the item in-stock, factory price and shop
price) for each item in the shop. 4. Sell an item
This option asks user to enter item id, searches for that item id and quantity of the item in-stock. If
item is found and the quantity of items in-stock is not 0 then it displays a message “item sold” and
decreases the quantity of items in-stock by one for that item. If item is not found or item found but
the quantity of items in-stock is 0 then it displays a message “item is not in-stock”. A built-in
search algorithm for searching can be used in this assignment. 5. Display all items with the lowest factory price
This option finds and displays the item in Array/ArrayList which has the lowest price. If there is
more than one item with the lowest price then it displays all of them. 6. Sort and display sorted items
This option sorts (by item name) all items stored in Array/ArrayList in descending order and
displays all sorted items (item name, item id, quantity of the item in-stock, factory price, shop
price). You can use any sorting algorithm. A built-in sort algorithm for sorting is not allowed in
this assignment. 7. Exit from the application
The application should display the message an appropriate message with your student id and then
exit from the application.
The application should work in a loop to enable the user to Read, validate and store all items,
Calculate and store shop price for all items, Display all items, Sell an item, Display all items with
the lowest factory price, Sort and display sorted items and Exit from the application.
MUST use the classes and methods.
public class Shop { //fields //get and set methods }
public class ShopTest {
Method to Read, validate and store all items N items
Method to Calculate and store shop price for all items
Method to Display all items
Method to Search for that item id and show quantity of the item in-stock
Method to Display all items with the lowest factory price
Method to Sort and display items
Method to Exit from the application
public static void main(String[] args) { } }
///Code must be easy to read and logically correct///
Explanation / Answer
import java.util.Scanner;
// Defines a class Shop
class Shop
{
// Constant for number of items
final int N = 4;
// Instance variables to store data
String itemName[];
int itemID[];
int quantity[];
double factoryPrice[], shopPrice[];
// Scanner class object declared
Scanner sc;
// Default constructor to allocate memory
Shop()
{
// Dynamically allocates memory to array of size N
itemName = new String[N];
itemID = new int[N];
quantity = new int[N];
factoryPrice = new double[N];
shopPrice = new double[N];
}// End of default constructor
// Method to accept data
void accept()
{
// Scanner class object created
sc = new Scanner(System.in);
// Loops till number of items
for(int x = 0; x < N; x++)
{
// Accepts item name from the user
System.out.print(" Enter item name: ");
itemName[x] = sc.next();
// Loops till valid id entered by the user
do
{
// Accepts item id from the user
System.out.print(" Enter item id: ");
itemID[x] = sc.nextInt();
// Checks if the item id is between 1 and 333 inclusive then come out of the loop
if(itemID[x]>= 1 && itemID[x] <= 333)
break;
// Otherwise invalid id, display error message
else
System.out.print(" ERROR: Invalid item id (Must be between 1 and 333 inclusive): ");
}while(true); // End of do - while loop
// Loops till valid quantity entered by the user
do
{
// Accepts quantity from the user
System.out.print(" Enter quantity: ");
quantity[x] = sc.nextInt();
// Checks if quantity is greater than or equals to 1 (not negative) then come out of the loop
if(quantity[x] >= 1)
break;
// Otherwise invalid quantity, display error message
else
System.out.print(" ERROR: Invalid quantity (Must be greater than zero): ");
}while(true); // End of do - while loop
// Loops till valid factory price entered by the user
do
{
// Accepts factory price from the user
System.out.print(" Enter factory price: ");
factoryPrice[x] = sc.nextDouble();
// Checks if factory price is between 1 and 1000 inclusive then come out of the loop
if(factoryPrice[x]>= 1 && factoryPrice[x] <= 1000)
break;
// Otherwise invalid factory price, display error message
else
System.out.print(" ERROR: Invalid factory price (Must be between 1 and 1000 inclusive): ");
}while(true); // End of do - while loop
}// End of for loop
}// End of method
// Method to calculate shop price
void calculate()
{
// Scanner class object created
sc = new Scanner(System.in);
// Loops till number of items
for(int x = 0; x < N; x++)
{
System.out.print(" Enter discount price: ");
double discount = sc.nextDouble();
shopPrice[x] = factoryPrice[x] - (factoryPrice[x] * discount) / 100.0;
}// End of for loop
}// End of method
// Method to display all items
void displayAll()
{
// Loops till number of items
for(int x = 0; x < N; x++)
System.out.println(" Name: " + itemName[x] + " ID: " + itemID[x] + " Quantity: " + quantity[x]
+ " Factory Price: $" + factoryPrice[x] + " Shop Price: $" + shopPrice[x]);
}// End of method
// Method to search an item id and
// returns -1 for not found, -2 item not in stock and index position for found
int search(int id)
{
int found = -1;
// Loops till number of items
for(int x = 0; x < N; x++)
{
// Checks if current item id is equals to parameter id
if(itemID[x] == id)
{
// Checks if current quantity is greater than zero then quantity available to sell
if(quantity[x] > 0)
{
// Sets the found index position as x to variable found
found = x;
// Returns the found index position
return found;
}// End of if inner condition
// Otherwise returns -2 for insufficient quantity
else
return -2;
}// End of outer if condition
}// End of for loop
// Otherwise returns found value as -1 for not found
return found;
}// End of method
// Method to sell an item
void sellItem()
{
// Scanner class object created
sc = new Scanner(System.in);
// Accepts sell percentage from the user
System.out.print(" Enter the item id to sell: ");
int id = sc.nextInt();
// Calls the function to search the item id and stores the return found status
int found = search(id);
// Checks if found status is greater than zero then item found and having sufficient quantity to sell
if(found > 0)
{
System.out.println("Item sold.");
// Decreases the found index position of quantity by one for sell
quantity[found]--;
}// End of if condition
// Otherwise checks if found status is -1 then item not found
else if(found == -1)
System.out.println("Item not found.");
// Otherwise item found but insufficient quantity to sell
else
System.out.println("Item is not in-stock");
}// End of method
// Method to displays lowest factory price items
void lowestPrice()
{
// Stores the first factory price as lowest
double low = factoryPrice[0];
// Loops till number of items
for(int x = 1; x < N; x++)
// Checks if current factory price is less than earlier low
if(factoryPrice[x] < low)
// Update the low with current factory price
low = factoryPrice[x];
// Loops till number of items
for(int x = 0; x < N; x++)
// Checks if current factory price is equals to low price
if(factoryPrice[x] == low)
// Displays current item information
System.out.print(" Name: " + itemName[x] + " ID: " + itemID[x] + " Quantity: "
+ quantity[x] + " Factory Price: $" + factoryPrice[x] + " Shop Price: $" + shopPrice[x]);
}// End of method
// Method to sort item names in descending order
void sortItem()
{
// Local variables for swapping
int tempI;
String tempN;
double tempD;
// Loops till number of items minus one times
for(int x = 0; x < N-1; x++)
{
// Loops till number of items minus outer loop variable and minus one times
for(int y = 0; y < N - x - 1; y++)
{
// Checks if current item name is less than the next item name
if(itemName[y].compareTo(itemName[y + 1]) < 0)
{
// Swapping item name
tempN = itemName[y];
itemName[y] = itemName[y + 1];
itemName[y + 1] = tempN;
// Swapping item id
tempI = itemID[y];
itemID[y] = itemID[y + 1];
itemID[y + 1] = tempI;
// Swapping item quantity
tempI = quantity[y];
quantity[y] = quantity[y + 1];
quantity[y + 1] = tempI;
// Swapping factory price
tempD = factoryPrice[y];
factoryPrice[y] = factoryPrice[y + 1];
factoryPrice[y + 1] = tempD;
// Swapping shop price
tempD = shopPrice[y];
shopPrice[y] = shopPrice[y + 1];
shopPrice[y + 1] = tempD;
}// End of if condition
}// End of inner for loop
}// End of outer for loop
}// End of method
}// End of class
// Driver class ShopTest definition
public class ShopTest
{
// Scanner class object declared
static Scanner sc;
// Static method to display menu, accept users choice and return user choice
static int menu()
{
// Scanner class object created
sc = new Scanner(System.in);
// To store user choice
int ch;
// Displays menu
System.out.print(" 1. Read");
System.out.print(" 2. Calculate and store shop price for all items ");
System.out.print(" 3. Display all items ");
System.out.print(" 4. Sell an item ");
System.out.print(" 5. Display all items with the lowest factory price ");
System.out.print(" 6. Sort and display sorted items");
System.out.print(" 7. Exit");
// Accepts user choice
System.out.print(" Enter your choice: ");
ch = sc.nextInt();
// Returns user choice
return ch;
}// End of method
// main method definition
public static void main(String[] args)
{
// Creates an object of class Shop using default constructor
Shop sh = new Shop();
// Loops till user choice is not 7
do
{
// Calls the function menu for user choice
// Based on the return user choice calls the appropriate method
switch(menu())
{
case 1:
sh.accept();
break;
case 2:
sh.calculate();
break;
case 3:
sh.displayAll();
break;
case 4:
sh.sellItem();
break;
case 5:
sh.lowestPrice();
break;
case 6:
sh.sortItem();
break;
case 7:
System.exit(0);
default:
System.out.println("Invalid choice.");
}// End of switch - case
}while(true);// End of do - while loop
}// End of main method
}// End of driver class
Sample Output:
1. Read
2. Calculate and store shop price for all items
3. Display all items
4. Sell an item
5. Display all items with the lowest factory price
6. Sort and display sorted items
7. Exit
Enter your choice: 1
Enter item name: Lux
Enter item id: 444
ERROR: Invalid item id (Must be between 1 and 333 inclusive):
Enter item id: -1
ERROR: Invalid item id (Must be between 1 and 333 inclusive):
Enter item id: 111
Enter quantity: -1
ERROR: Invalid quantity (Must be greater than zero):
Enter quantity: 0
ERROR: Invalid quantity (Must be greater than zero):
Enter quantity: 10
Enter factory price: 12000
ERROR: Invalid factory price (Must be between 1 and 1000 inclusive):
Enter factory price: 100.12
Enter item name: Dove
Enter item id: 222
Enter quantity: 45
Enter factory price: 200.23
Enter item name: Cinthol
Enter item id: 333
Enter quantity: 78
Enter factory price: 300.45
Enter item name: Liril
Enter item id: 112
Enter quantity: 1
Enter factory price: 100.12
1. Read
2. Calculate and store shop price for all items
3. Display all items
4. Sell an item
5. Display all items with the lowest factory price
6. Sort and display sorted items
7. Exit
Enter your choice: 2
Enter discount price: 5
Enter discount price: 12
Enter discount price: 3
Enter discount price: 7
1. Read
2. Calculate and store shop price for all items
3. Display all items
4. Sell an item
5. Display all items with the lowest factory price
6. Sort and display sorted items
7. Exit
Enter your choice: 4
Enter the item id to sell: 555
Item not found.
1. Read
2. Calculate and store shop price for all items
3. Display all items
4. Sell an item
5. Display all items with the lowest factory price
6. Sort and display sorted items
7. Exit
Enter your choice: 3
Name: Lux
ID: 111
Quantity: 10
Factory Price: $100.12
Shop Price: $95.114
Name: Dove
ID: 222
Quantity: 45
Factory Price: $200.23
Shop Price: $176.20239999999998
Name: Cinthol
ID: 333
Quantity: 78
Factory Price: $300.45
Shop Price: $291.43649999999997
Name: Liril
ID: 112
Quantity: 1
Factory Price: $100.12
Shop Price: $93.11160000000001
1. Read
2. Calculate and store shop price for all items
3. Display all items
4. Sell an item
5. Display all items with the lowest factory price
6. Sort and display sorted items
7. Exit
Enter your choice: 4
Enter the item id to sell: 112
Item sold.
1. Read
2. Calculate and store shop price for all items
3. Display all items
4. Sell an item
5. Display all items with the lowest factory price
6. Sort and display sorted items
7. Exit
Enter your choice: 3
Name: Lux
ID: 111
Quantity: 10
Factory Price: $100.12
Shop Price: $95.114
Name: Dove
ID: 222
Quantity: 45
Factory Price: $200.23
Shop Price: $176.20239999999998
Name: Cinthol
ID: 333
Quantity: 78
Factory Price: $300.45
Shop Price: $291.43649999999997
Name: Liril
ID: 112
Quantity: 0
Factory Price: $100.12
Shop Price: $93.11160000000001
1. Read
2. Calculate and store shop price for all items
3. Display all items
4. Sell an item
5. Display all items with the lowest factory price
6. Sort and display sorted items
7. Exit
Enter your choice: 4
Enter the item id to sell: 112
Item is not in-stock
1. Read
2. Calculate and store shop price for all items
3. Display all items
4. Sell an item
5. Display all items with the lowest factory price
6. Sort and display sorted items
7. Exit
Enter your choice: 5
Name: Lux
ID: 111
Quantity: 10
Factory Price: $100.12
Shop Price: $95.114
Name: Liril
ID: 112
Quantity: 0
Factory Price: $100.12
Shop Price: $93.11160000000001
1. Read
2. Calculate and store shop price for all items
3. Display all items
4. Sell an item
5. Display all items with the lowest factory price
6. Sort and display sorted items
7. Exit
Enter your choice: 6
1. Read
2. Calculate and store shop price for all items
3. Display all items
4. Sell an item
5. Display all items with the lowest factory price
6. Sort and display sorted items
7. Exit
Enter your choice: 7
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.