You need to design, implement, and test a grocery shopping list program. The pro
ID: 3663410 • Letter: Y
Question
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. 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
Explanation / Answer
// Item Class
public class Item {
private String name;
private String unit;
private int no_to_buy;
private double unit_price;
public Item(String name, String unit, int no_to_buy, double unit_price) {
this.name = name;
this.unit = unit;
this.no_to_buy = no_to_buy;
this.unit_price = 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 getNo_to_buy() {
return no_to_buy;
}
public void setNo_to_buy(int no_to_buy) {
this.no_to_buy = no_to_buy;
}
public double getUnit_price() {
return unit_price;
}
public void setUnit_price(double unit_price) {
this.unit_price = unit_price;
}
public double getExtendedPrice(double new_price) {
return Math.abs(new_price-unit_price);
}
@Override
public String toString() {
return "Name of Item: "+name+"Unit :"+unit+
"No of Item to Buy: "+no_to_buy+
"Unit Price: "+unit_price;
}
}
// List Class
/*
* I am using ArrayList, so it will take care of dynamic allocation
*
* */
public class List {
private ArrayList<Item> list;
public List() {
list = new ArrayList<Item>();
}
public void addItem(Item item) {
list.add(item);
}
public Item getItem(int index) {
return list.get(index);
}
public void remove(int index) {
list.remove(index);
}
// Method to print List of Items
public void printList() {
for(Item item: list) {
System.out.println(item.toString());
}
}
@Override
public String toString() {
return list.toString();
}
}
//Shopping Class
import java.util.Scanner;
public class ShoppingClass {
List list;
public ShoppingClass() {
list = new List();
}
public void addItemToList(String name, String unit, int no_to_buy, double unit_price) {
Item item = new Item(name, unit, no_to_buy, unit_price);
list.addItem(item);
}
public void removeItem(int index) {
list.remove(index);
}
public void printList() {
list.printList();
}
public static void main(String[] args) {
String name;
String unit;
int no_to_buy;
double unit_price;
Scanner sc = new Scanner(System.in);
ShoppingClass shoping = new ShoppingClass();
System.out.println("Enter name of Item: ");
name = sc.nextLine();
unit = sc.nextLine();
no_to_buy = sc.nextInt();
unit_price = sc.nextDouble();
shoping.addItemToList(name, unit, no_to_buy, unit_price);
shoping.printList();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.