You need to design implement and test a grocery shopping lis
You need to design, implement, and test a grocery shopping list program. The program should maintain and display a list of items.
Item Class:
You will use a class for the Items. The class should have data elements for the following information: item name, unit (i.e. can, box, pounds, or ounces), number to buy, and unit price. Do you need any functions other than the constructor(s)? How do you calculate the extended price for the item (number to by times unit price? How do you print it to the screen?
List Class :
You will also need a List class. The List class will use a dynamic array to store Item objects. The dynamic array wshould start with a cpacity of 4 Item objects. As each item is entered an Item object must created and added to the array. What do you do if the array is full? One List object will have many Item objects. How do you print to the screen?
Operations:
Your program must perform the following activities: create a list, add items, and remove items. To add an item you should prompt the user to enter the name, unit of sale, the number needed, and the unit price.
Displaying the list:
The display should show: for each item in the list, the number of items, the unit of sale, the unit price, the extended price for each item; then the total price for all items
Solution
Code
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
class Items {
String name;
String unit;
int quantity;
double unit_price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public double getUnit_price() {
return unit_price;
}
public void setUnit_price(double unit_price) {
this.unit_price = unit_price;
}
public Items(String name, String unit, int quantity, double unit_price) {
super();
this.name = name;
this.unit = unit;
this.quantity = quantity;
this.unit_price = unit_price;
}
public double getExtendedPrice() {
return this.unit_price * this.quantity;
}
@Override
public String toString() {
return \"Item [name=\" + name + \", unit=\" + unit + \", quantity=\" + quantity + \", unit_price=\" + unit_price
+ \", getExtendedPrice()=\" + getExtendedPrice() + \"]\";
}
public boolean equals_items(Items i) {
return (this.name.equals(i.name) && this.unit_price == i.unit_price && this.quantity == i.quantity);
}
}
class Item_List {
ArrayList<Items> list;
int count;
double total;
public Item_List() {
list = new ArrayList<Items>(4);
count = 0;
total = 0;
}
public void add(Items i) {
if (count >= 4)
System.out.println(\"List is full\");
else {
list.add(i);
count++;
total += i.getExtendedPrice();
}
}
public void print() {
Iterator itr = list.iterator();
while (itr.hasNext()) {
System.out.println(((Items) itr.next()).toString());
}
System.out.println(\"Total price: \" + total);
}
public void remove(Items i) {
if (count == 0)
System.out.println(\"List is empty\");
else {
Iterator itr = list.iterator();
int j = -1;
while (itr.hasNext()) {
if (((Items) itr.next()).equals_items(i))
break;
else
j++;
}
if (j != -1) {
list.remove(j);
this.count--;
this.total -= i.getExtendedPrice();
} else
System.out.println(\"item is not present in the list\");
}
}
}
public class Grocery {
public static void main(String[] args) {
int option = 4;
Item_List list = new Item_List();
Scanner in = new Scanner(System.in);
do {
System.out.println(\"Menu\");
System.out.println(\"1:Add\");
System.out.println(\"2:Remove\");
System.out.println(\"3:Print\");
System.out.println(\"4:quit\");
System.out.println(\"Enter your option\");
option = in.nextInt();
switch (option) {
case 1:
System.out.println(\"Enter name of the item\");
String name = in.next();
System.out.println(\"Enter unit of the item\");
String unit = in.next();
System.out.println(\"Enter quantity of the item\");
int quantity = in.nextInt();
System.out.println(\"Enter unit price of the item\");
double unit_price = in.nextDouble();
Items i = new Items(name, unit, quantity, unit_price);
list.add(i);
// list.print();
break;
case 2:
System.out.println(\"Enter name of the item\");
String name1 = in.next();
System.out.println(\"Enter unit of the item\");
String unit1 = in.next();
System.out.println(\"Enter quantity of the item\");
int quantity1 = in.nextInt();
System.out.println(\"Enter unit price of the item\");
double unit_price1 = in.nextDouble();
Items i1 = new Items(name1, unit1, quantity1, unit_price1);
list.remove(i1);
// list.print();
break;
case 3:
list.print();
break;
case 4:
System.out.println(\"Exiting!\");
break;
default:
System.out.println(\"Invalid option try again!\");
}
} while (option != 4);
}
}
Sample output
Menu
1:Add
2:Remove
3:Print
4:quit
Enter your option
1
Enter name of the item
a
Enter unit of the item
a
Enter quantity of the item
3
Enter unit price of the item
3
Menu
1:Add
2:Remove
3:Print
4:quit
Enter your option
3
Item [name=a, unit=a, quantity=3, unit_price=3.0, getExtendedPrice()=9.0]
Total price: 9.0
Menu
1:Add
2:Remove
3:Print
4:quit
Enter your option
1
Enter name of the item
b
Enter unit of the item
b
Enter quantity of the item
4
Enter unit price of the item
2
Menu
1:Add
2:Remove
3:Print
4:quit
Enter your option
3
Item [name=a, unit=a, quantity=3, unit_price=3.0, getExtendedPrice()=9.0]
Item [name=b, unit=b, quantity=4, unit_price=2.0, getExtendedPrice()=8.0]
Total price: 17.0
Menu
1:Add
2:Remove
3:Print
4:quit
Enter your option
2
Enter name of the item
b
Enter unit of the item
b
Enter quantity of the item
4
Enter unit price of the item
2
Menu
1:Add
2:Remove
3:Print
4:quit
Enter your option
3
Item [name=b, unit=b, quantity=4, unit_price=2.0, getExtendedPrice()=8.0]
Total price: 9.0
Menu
1:Add
2:Remove
3:Print
4:quit
Enter your option
4
Exiting!








