Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem 2: The main program (anotherList) and the linked list skeleton are appen

ID: 3813586 • Letter: P

Question

Problem 2: The main program (anotherList) and the linked list skeleton are appended to this document.

We would like to model a shopping list. The main program (provided) allows the following operations:

1. Quit

2. Output the list

3. Buy an item (Add an element to the list)

4. Discard an item (Remove an element from the list)

5. Check whether an item was bought (Does the list contain the item)

6. How much have I spent (Total)

The skeleton class (LinkedList1 show the major operation to be implemented. That is:

1. public void addToStart (String itemName, int itemCount)

a. itemName: grocery item

b. itemCount: item price

2. public boolean deleteNode (int i)

a. i: position of the item on the list to be deleted

b. returns true if deleted, false otherwise

3. public int size( )

a. returns the number of elements in the list

4. public int total( )

a. returns the sum of all the itemCount in the list

5. public int find (String target)

a. target: itemName to look up

b. returns 0 if item is not on the list, or the position number if found

6. public void outputList ( )

a. outputs the information of all nodes in the list

Testing (sample) – You need to think about other examples to demonstrate that your implementation works.

Test your program to show that it works on the following operations:

1. Output the elements of the list

2. Add an element (use several possibilities)

3. Remove an element (use several possibilities)

4. Output the size of the list

5. Find an element (use several possibilities)

Deliverables

6. Name you program asg1-Problem02-your-id (e.g., asg1-2011300912)

7. Source code: zip file of your project

8. Test runs: copy of your runs

NETBEANS - JAVA

Explanation / Answer

I have used 3 classes in total:

1.AnotherList class:

import java.util.*;
public class AnotherList {

   public static void main(String[] args) {
      
       Scanner sc= new Scanner(System.in);
       LinkedList al=new LinkedList();
       int userOpt = 0;
   while (userOpt != 1) {
   System.out.println("");
   System.out.println("----- Shopping List------");
   System.out.println("(1) Exit the program");
   System.out.println("(2) Display list and total number of items. ");
   System.out.println("(3) Add an element to the list. ");
   System.out.println("(4) Remove an elemnet from the list. ");
   System.out.println("(5) Check whether the list contain the item.. ");
   System.out.println("(6) get the total amount. ");
   userOpt = sc.nextInt();

   if (userOpt == 1) {
   System.exit(0);
   }
   if (userOpt == 2) {
   al.outputList();
   }
   if (userOpt == 3) {
       al.addToStart(null, userOpt, userOpt);
       }
   if (userOpt == 4) {
       al.deleteNode(userOpt);
       }
   if (userOpt == 5) {
       al.find(null);
       }
   if (userOpt == 6) {
       al.total();
       }
   }

   }

}

2. LinkedList class:

import java.util.*;
public class LinkedList {
   ArrayList<ShoppingItem> list = new ArrayList<ShoppingItem>();
   public void addToStart (String ItemName, int ItemPrice, int ItemQty)
   {
       System.out.println("enter in the name of your item");
Scanner keyboard = new Scanner(System.in);
ItemName = keyboard.nextLine();
  
System.out.println("enter in the price of your item");
ItemPrice = keyboard.nextInt();

System.out.println("enter in the Qty of your item");
ItemQty = keyboard.nextInt();

ShoppingItem item=new ShoppingItem(ItemName,ItemPrice, ItemQty);
list.add(item);
System.out.print("item added");
  
   }

   public boolean deleteNode (int i)  
   {
       boolean n=false;
   //in case list is empty then return
   if(i==0) return n;
   else return true;  
   }
   public int size()
   {
       return list.size();
   }
   public int total()
   {   
       int total=0;
       for(ShoppingItem x: list)
       {      
       int itemRate= ShoppingItem.getItemTotalPrice();
       total=total+itemRate;
       }
       return total;
   }
   public int find (String target)
   {
       return 0;
   }
   public void outputList ( )
   {
   System.out.println(list.size()+ "items.");
   for(ShoppingItem x: list)
   {
       System.out.println(x.toString());
   }
   }
  
}

3. ShoppingItem class:


public class ShoppingItem {
   private String ItemName;
private static int ItemPrice;
private static int ItemQty;

public ShoppingItem(String ItemName, int ItemPrice, int ItemQty )
{
this.ItemName = ItemName;
this.ItemPrice = ItemPrice;
this.ItemQty = ItemQty;

}


public String getItemName() {
return ItemName;
}

public int getItemPrice() {
return ItemPrice;
}

public static int getItemTotalPrice() {
return (ItemPrice * ItemQty);
}

public int getItemQty() {
return ItemQty;
}

public void setItemName(String ItemName)
{
this.ItemName = ItemName;
}

public void setItempPrice(int ItemPrice)
{
this.ItemPrice = ItemPrice;
}

public void setItemQty(int ItemQty)
{
this.ItemQty = ItemQty;
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote