I'm trying to use the items of one array within a for loop that is looping through another array. For example, I want to display the item name (an array that already exists) when asking for the price of each of these items. "What price is item[i]". I don't think you can use item[i] in this instance. I've also tried using a get method and using a counter variable, but I must be doing something wrong.
The readPrice portion isn't working - specifically the item[i]. Does anyone have a suggestion how to accomplish the same thing without using item[i]?
I'm new to java, so I'm sorry if this is an obvious answer, but I would really appreciate the help!
Here is my code:
private String[] items;
private int[] priority;
private double[] price;
public void readInput()
{
Scanner keyboard = new Scanner(System.in);
System.out.println("You will need to enter seven shopping items.");
items = new String[7];
for (int i = 0; i < items.length; i++)
{
System.out.println("Enter an item: ");
items[i] = keyboard.nextLine();
}
}
**public void readPrice()**
{
price = new double[7];
Scanner keyboard = new Scanner(System.in);
for(int i = 0; i < price.length; i++)
{
System.out.println("Enter the price for " + **items[i]**);
price[i] = keyboard.nextDouble();
}
}
you can make a class Item.
public class Item {
String name;
double price;
public void setName(String name) {
this.name = name;
}
public void setPrice(double price) {
this.price = price;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
#Override
public String toString() {
return "Item{" + "name=" + name + ", price=" + price + '}';
}
}
After create an object of class Item
public class TestPaper {
public static void main(String[] args) {
Item item;
Item[] itemTable = new Item[7];
ArrayList<Item> itemlist = new ArrayList<>();
for (int i = 0; i < 7; i++) {
item = new Item();
//set value
item.setName("item1");
item.setPrice(200);
//store the object to a table or an arraylist
itemTable[i] = item;
itemlist.add(item);
}
for (Item temp : itemTable) {
System.out.println(temp.toString());
}
}
}
This is better solution, but I will answer you with your code.
You have 7 items, so you can print the items like this:
private String[] items;
private int[] priority;
private double[] price;
public void readInput()
{
Scanner keyboard = new Scanner(System.in);
System.out.println("You will need to enter seven shopping items.");
items = new String[7];
for (int i = 0; i < items.length; i++)
{
System.out.println("Enter an item: ");
items[i] = keyboard.nextLine();
}
}
**public void readPrice()**
{
price = new double[7];
Scanner keyboard = new Scanner(System.in);
for(int i = 0; i < price.length; i++)
{
System.out.println("Enter the price for " + **items[i]**);
price[i] = keyboard.nextDouble();
}
public void printdata(){
for(int I=0; I<7; I++){
System.out.println("item name: " +item[i] +" price: "+price[i])
}
}
Alright, well here's the answer for you. There are two main approaches to this: either pass the object items as a parameter, or make it global.
Global Variable Approach
For the global approach, which you seem to sort of already be doing, first declare your object as a field for your class:
class MyClass
{
private String[] items = new String[7];
//...Rest of class goes here
}
Now you can access it from your two methods as you wish.
public readInput()
{
// Add stuff to items
items[0] = "hi";
}
public readPrice()
{
// Read from items
System.out.println(items[0]);
}
Method Parameter Approach
Now if you want to actually pass an object from one method to another but don't want it to be accessible to any other methods, you can make it a parameter like this:
public readPrice(String[] items)
{
// Read from items
System.out.println(items[0]);
}
And then you can call the method from somewhere else passing specific instances of the object type.
public inputPrice()
{
String[] items = new String[7];
// Add stuff to items
items[0] = "hi";
this.readPrice(items);
}
Passing parameters is especially useful if you want to call a single method several times, but have it act on different inputs.
Please provide a stacktrace of the occurring error to make everyone happy. I personally would create a Map to assign a price to an item.
Code
package test;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
public class Items {
private int itemsLength = 10;
private Map<String, Double> items = new HashMap<>();
public Items() {
System.out.println("Please fill in the details below: ");
for(int i = 0; i < itemsLength; i++) {
Scanner in = new Scanner(System.in);
String itemName;
Double price;
System.out.println("Enter an item name: ");
itemName = in.nextLine();
System.out.println(String.format("Enter %s's price: ", itemName));
price = in.nextDouble();
items.put(itemName, price);
}
System.out.println("Here are your items: ");
System.out.println("+------------------------+");
System.out.println(String.format("|%-10s | %-10s %-6s", "Item", "Price", "|"));
for (Entry<String, Double> entry : items.entrySet()) {
System.out.println(String.format("|%-10s | %-10s %-6s", entry.getKey(), entry.getValue(), "|"));
}
System.out.println("+------------------------+");
}
}
Output
Please fill in the details below:
Enter an item name:
Shoes
Enter Shoes's price:
20
Enter an item name:
Shirt
Enter Shirt's price:
20
Enter an item name:
Necklace
Enter Necklace's price:
50
Enter an item name:
Bracelet
Enter Bracelet's price:
30
Enter an item name:
Socks
Enter Socks's price:
5
Enter an item name:
Flip-flops
Enter Flip-flops's price:
10
Enter an item name:
Soda
Enter Soda's price:
2
Enter an item name:
Diamonds
Enter Diamonds's price:
20000
Enter an item name:
Weird Item
Enter Weird Item's price:
99999
Enter an item name:
Another Weird Item
Enter Another Weird Item's price:
99999
Here are your items:
+------------------------+
|Item | Price |
|Necklace | 50.0 |
|Another Weird Item | 99999.0 |
|Shirt | 20.0 |
|Bracelet | 30.0 |
|Diamonds | 20000.0 |
|Weird Item | 99999.0 |
|Shoes | 20.0 |
|Socks | 5.0 |
|Soda | 2.0 |
|Flip-flops | 10.0 |
+------------------------+
P.S: This is my first response, sorry if its not pretty
Edit: If you want to keep the order when outputting then construct a TreeMap instead of a HashMap.
Related
I am making a program where the person inputs multiple values and the Arraylist stores it as a single string with option 1. Option two allows you to change a specific part of that string from "Complete" to "Incomplete" and vice-versa but only if the status part of the String is one of the two. I am getting the warning "Result of 'String.replace()' is ignored" and the part of the string isn't updating. Any help would be appreciated!
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Array listObj = new Array();
Scanner userinput = new Scanner(System.in);
int arraysize = (listObj.list.size());
int power = 1;
while (power < 2) {
System.out.println("To-Do List / What would you like to do?");
System.out.println("1 = Add Task / 2 = Mark Task as Done / 3 = Remove Task / 4 = Edit Task / 5 = Display Tasks / 6 = Exit");
int selection = userinput.nextInt();
if (selection == 1) {
for (int i = 0; i <= arraysize; i++) {
String title;
String date;
String status;
String description;
String id;
System.out.print("Enter Title: ");
title = userinput.next();
System.out.print("Enter Due Date: ");
date = userinput.next();
System.out.print("Enter Status (Complete or Incomplete): ");
status = userinput.next();
System.out.print("Enter Description: ");
description = userinput.next();
listObj.list.add(title + " " + date + " " + status + " " + description);
System.out.println();
listObj.list.forEach(System.out::println);
System.out.println();
}
}
if (selection == 2) {
int idinput;
System.out.println("Enter Project ID to Toggle Complete/Incomplete: ");
idinput = (userinput.nextInt()-1);
System.out.println();
System.out.println(listObj.list.get(idinput));
System.out.println();
System.out.println("What is the status of this assignment?: ");
String toggleselect = userinput.next();
if (toggleselect.equals("Incomplete")) {
listObj.list.get(idinput).replace("Incomplete", "Complete");
} else if (toggleselect.equals("Complete")) {
listObj.list.get(idinput).replace("Complete", "Incomplete");
} else {
System.out.println("Status is not Complete/Incomplete");
}
System.out.println();
listObj.list.forEach(System.out::println);
System.out.println();
}
}
}
}
import java.util.ArrayList;
public class Array {
ArrayList<String> list = new ArrayList<String>();
public ArrayList<String> getList() {
return list;
}
}`your text`
public String replace(char searchChar, char newChar)
demands a String to output to, as you can see by the return type. In this case use
public E set(int index, E element)
from the ArrayList library,
with E return type being your ArrayList,
and E element being your call
listObj.list.get(idinput).replace("Complete", "Incomplete");
So,
listObj.list.get(idinput).replace("Complete", "Incomplete");
becomes
listObj.list.set(idinput, listObj.list.get(idinput).replace("Complete", "Incomplete"));
Also, why are you creating a class called Array that just encapsulates an ArrayList in it?
To begin my question, here is my question prompt:
Create a program using classes that does the following in the zyLabs developer below. For this lab, you will be working with two different class files. To switch files, look for where it says "Current File" at the top of the developer window. Click the current file name, then select the file you need.
(1) Create two files to submit:
ItemToPurchase.java - Class definition
ShoppingCartPrinter.java - Contains main() method
Build the ItemToPurchase class with the following specifications:
Private fields
String itemName - Initialized in default constructor to "none"
int itemPrice - Initialized in default constructor to 0
int itemQuantity - Initialized in default constructor to 0
Default constructor
Public member methods (mutators & accessors)
setName() & getName() (2 pts)
setPrice() & getPrice() (2 pts)
setQuantity() & getQuantity() (2 pts)
(2) In main(), prompt the user for two items and create two objects of the ItemToPurchase class. Before prompting for the second item, call scnr.nextLine(); to allow the user to input a new string. (2 pts)
Ex:
Item 1
Enter the item name: Chocolate Chips
Enter the item price: 3
Enter the item quantity: 1
Item 2
Enter the item name: Bottled Water
Enter the item price: 1
Enter the item quantity: 10
(3) Add the costs of the two items together and output the total cost. (2 pts)
Ex:
TOTAL COST
Chocolate Chips 1 # $3 = $3
Bottled Water 10 # $1 = $10
Total: $13
My code:
ItemToPurhase.java
public class ItemToPurchase {
//Private fields - itemName, itemPrice, and itemQuanity
private String itemName = "none";
private int itemPrice = 0;
private int itemQuantity = 0;
/*Default Constructor
itemName - Initialized to "none"
itemPrice - Initialized to 0
itemQuantity - Initialized ito 0
*/
public ItemToPurchase () {
this.itemName = "none";
this.itemPrice = 0;
this.itemQuantity = 0;
}
//public member methods (mutators & accessors)
//setName() & getName()
public void setName (String itemName) {
this.itemName = itemName;
}
public String getName() {
return itemName;
}
//setPrice() & getPrice()
public void setPrice (int itemPrice) {
this.itemPrice = itemPrice;
}
public int getPrice(){
return itemPrice;
}
//setQuantity() & getQuantity()
public void setQuantity (int itemQuantity) {
this.itemQuantity = itemQuantity;
}
public int getQuantity() {
return itemQuantity;
}
//print item to purchase
public void printItemPurchase() {
System.out.println(itemQuantity + " " + itemName + " $" + itemPrice +
" = $" + (itemPrice * itemQuantity));
}
}
ShoppingCartPrinter.java
import java.util.Scanner;
public class ShoppingCartPrinter {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int i = 0;
String productName;
int productPrice = 0;
int productQuantity = 0;
int cartTotal = 0;
ItemToPurchase item1 = new ItemToPurchase();
ItemToPurchase item2 = new ItemToPurchase();
// Get item 1 details from user, create itemToPurchase object
System.out.println("Item 1");
System.out.println("Enter the item name: ");
productName = scnr.next();
item1.setName(productName);
System.out.println("Enter the item price: ");
productPrice = scnr.nextInt();
item1.setPrice(productPrice);
System.out.println("Enter the item quantity: ");
productQuantity = scnr.nextInt();
item1.setQuantity(productQuantity);
System.out.println("");
// Get item 2 details from user, create itemToPurchase object
System.out.println("Item 2");
System.out.println("Enter the item name: ");
productName = scnr.next();
item2.setName(productName);
System.out.println("Enter the item price: ");
productPrice = scnr.nextInt();
item2.setPrice(productPrice);
System.out.println("Enter the item quantity: ");
productQuantity = scnr.nextInt();
item2.setQuantity(productQuantity);
System.out.println("");
// Add costs of two items and print total
cartTotal = (item1.getQuantity() * item1.getPrice()) + (item2.getQuantity() * item2.getPrice());
System.out.println("TOTAL COST");
// cartTotal = item one price + item two price
// Totoal Cost
// item one information
int item1Total = item1.getPrice() * item1.getQuantity();
System.out.println(item1.getName() + " " + item1.getQuantity() + " # $" + item1.getPrice() + " = $" + item1Total);
// item two information
int item2Total = item2.getPrice() * item2.getQuantity();
System.out.println(item2.getName() + " " + item2.getQuantity() + " # $" + item2.getPrice() + " = $" + item2Total);
// Total output
System.out.println("");
System.out.print("Total: $" + cartTotal);
return;
}
}
Although I am getting no errors for most of the submissions, there are 2 submissions that raise the "Exception in thread "main" java.util.InputMismatchException" error.
Example:
Exited with return code 1.
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at ShoppingCartPrinter.main(ShoppingCartPrinter.java:23)
Output differs. See highlights below.
Input:
Chocolate Chips
3
1
Bottled Water
1
10
Your output:
Item 1
Enter the item name:
Enter the item price:
Expected output(!):
Item 1
Enter the item name:
Enter the item price:
Enter the item quantity:
Item 2
Enter the item name:
Enter the item price:
Enter the item quantity:
TOTAL COST
Chocolate Chips 1 # $3 = $3
Bottled Water 10 # $1 = $10
Total: $13
My code seems correct for other inputs that are very similar but for some reason, the compiler that my professor uses keeps prompting this error for a very few cases.
If you have not already done so, I suggest that you read the documentation for class java.util.Scanner.
This line of your code is throwing the InputMismatchException.
productPrice = scnr.nextInt();
That means that scnr read a token that was not an int. This is because your product contained two words, namely Chocolate Chips. So this line of your code only read Chocolate
productName = scnr.next();
and hence method nextInt read Chips which is not an int.
You need to change the code and call method nextLine rather than method next. Again, refer to the documentation to understand why.
Also, after calling method nextInt and before calling method nextLine, you need an extra call to method nextLine. Refer to Scanner is skipping nextLine() after using next() or nextFoo()? to understand why.
Only class ShoppingCartPrinter needs to be changed. Here is the changed code. I have added comments to indicate the changes, namely CHANGE HERE and ADDED THIS LINE.
import java.util.Scanner;
public class ShoppingCartPrinter {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int i = 0;
String productName;
int productPrice = 0;
int productQuantity = 0;
int cartTotal = 0;
ItemToPurchase item1 = new ItemToPurchase();
ItemToPurchase item2 = new ItemToPurchase();
// Get item 1 details from user, create itemToPurchase object
System.out.println("Item 1");
System.out.println("Enter the item name: ");
productName = scnr.nextLine(); // CHANGE HERE
item1.setName(productName);
System.out.println("Enter the item price: ");
productPrice = scnr.nextInt();
item1.setPrice(productPrice);
System.out.println("Enter the item quantity: ");
productQuantity = scnr.nextInt();
item1.setQuantity(productQuantity);
scnr.nextLine(); // ADDED THIS LINE
System.out.println("");
// Get item 2 details from user, create itemToPurchase object
System.out.println("Item 2");
System.out.println("Enter the item name: ");
productName = scnr.nextLine(); // CHANGE HERE
item2.setName(productName);
System.out.println("Enter the item price: ");
productPrice = scnr.nextInt();
item2.setPrice(productPrice);
System.out.println("Enter the item quantity: ");
productQuantity = scnr.nextInt();
item2.setQuantity(productQuantity);
System.out.println("");
// Add costs of two items and print total
cartTotal = (item1.getQuantity() * item1.getPrice())
+ (item2.getQuantity() * item2.getPrice());
System.out.println("TOTAL COST");
// cartTotal = item one price + item two price
// Totoal Cost
// item one information
int item1Total = item1.getPrice() * item1.getQuantity();
System.out.println(item1.getName() + " " + item1.getQuantity() + " # $" + item1.getPrice()
+ " = $" + item1Total);
// item two information
int item2Total = item2.getPrice() * item2.getQuantity();
System.out.println(item2.getName() + " " + item2.getQuantity() + " # $" + item2.getPrice()
+ " = $" + item2Total);
// Total output
System.out.println("");
System.out.print("Total: $" + cartTotal);
return;
}
}
I have been trying to create a shopping cart where the user can enter the number of items he/she has purchased and the price for each item.
I am having problems with the array. I am not able to store the price for each product. How can I store the second value (the price) in an array to match each item?
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int[][] itemsCart;
int itemsInTheCart=0;
int itemsPrice = 0;
System.out.print("Please enter the number of items in your cart: ");
itemsInTheCart = scan.nextInt();
for(int i = 0; i < itemsInTheCart; i++){
System.out.print("Enter the price for item " + (i+1) + ": ");
itemsPrice = scan.nextInt();
itemsCart = new int[itemsPrice][itemsPrice];
// System.out.println(itemsCart.length);
}
}
An array needs to be declared with its size before it can be used.
Also in your example you do not need a 2D array, a 1D array is fine.
Also you need to initialize your array outside the loop other wise it will get overridden in each iteration
System.out.print("Please enter the number of items in your cart: ");
itemsInTheCart = scan.nextInt();
int [] itemsCart = new int [itemsInTheCart];
for (....) {
itemsCart[i] = itemsPrice;
}
Instead Use ArrayList
for example create a object class
class ShoppingDetails{
private String itemName;
private Integer price;
public ShoppingDetails(String itemName, Integer price) {
this.itemName = itemName;
this.price = price;
}
//add getter and setter if you want
}
Now you can use the it in
ArrayList<ShoppingDetails> shoppingCart=new ArrayList<>();
shoppingCart.add(new ShoppingDetails("item1",100);
By this way you can map price to items.
As mentioned by #"Scary Wombat", your approach is better suited for a 1D array than a 2D one. However since you are looking to use a 2D array I would like to point your where you code could be better. Also mentioned by him to need to declare the size before using the 2D array
Your line "itemsCart = new int[itemsPrice][itemsPrice];" would be better as itemsCart = new int[i][itemsPrice]; as this will ensure that the new indexes are filled with the appropriate values.
I have made some edits to your main. Feel free to as me if you don't understand
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int[][] itemsCart;
int itemsInTheCart=0;
int itemsPrice = 0;
System.out.print("Please enter the number of items in your cart: ");
itemsInTheCart = scan.nextInt();
itemsCart= new int[itemsInTheCart][2];
for(int i = 0; i < itemsInTheCart; i++){
System.out.print("Enter the price for item " + (i+1) + ": ");
itemsPrice = scan.nextInt();
itemsCart[i][1] = itemsPrice;
}
// for testing
for (int i=0;i<itemsInTheCart;i++){
System.out.println("Item: "+(i+1)+" for price: "+ itemsCart[i][1] );
}
}
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int itemsInTheCart=0;
System.out.print("Please enter the number of items in your cart: ");
itemsInTheCart = scan.nextInt();
int[] itemsCart = new int[itemsInTheCart];
for(int i = 0; i < itemsInTheCart; i++){
System.out.print("Enter the price for item " + (i+1) + ": ");
itemsCart[i] = scan.nextInt();
}
for(int i = 0; i < itemsInTheCart; i++){
System.out.println(itemsCart[i]);
}
}
}
I have a shopping list that contains a set number of items. Through user input, I want to ask the user how many of the same item would they like to add to their shopping bag.
For Example: I have the following ArrayList newProduct = {"apple","orange","banana"};. Now I want to prompt the user to choose which (and how many) of the items in newProduct would they like to add into a second array called bag. If the user chooses a an apple with a quantity of 2, then 2 apples would be added to bag and it would look like this bag = {"apple","apple"};
I tried to useoperands that would duplicate the value. But it didn't work very well. What is wrong with this?
import java.util.*;
import java.util.stream.IntStream;
public class Bag extends Price {
static Scanner input = new Scanner(System.in);
#SuppressWarnings("unused")
private static String newName;
private int choice;
public ArrayList<String> bag = new ArrayList<String>();
public List<Double> bagPrice = new ArrayList<Double>();
public void addProduct() {
Bag item = new Bag();
while (sum(bagPrice) <= 58.00) {
System.out.println("Choose a Priority");
item.choice = input.nextInt();
System.out.println("Choose Quantity");
int quantity = input.nextInt();
if (IntStream.of(newPriority).anyMatch(x -> x == item.choice)) {
item.bag.add(newProduct.get(item.choice));
System.out.print("PRICE $ " + Double.valueOf(formatter.format(price.get(item.choice - 1))));
bagPrice.add(price.get(item.choice - 1));
System.out.println("BAG: " + item.bag);
System.out.println("TOTAL : " + Double.valueOf(formatter.format(sum(bagPrice))));
} else {
System.out.println("Priority Does Not Exist");
}
}
}
public double sum(List<Double> bagPrice) {
double sum = 0.00;
for (double s : bagPrice) {
sum = sum + s;
}
return sum;
}
}
I'm fairly new to programming so if there is any better suggestion I would greatly appreciate it.
You might have to use clone() method to get a copy, hope this would be helpful Clone method for Java arrays
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to keep track of each items cost, and add that to the total cost of all the items. When I run the program the last item cost and total cost is applied to each item. I feel like I should have another loop more dynamically placing the cost of each item and total cost.
Driver Class:
public class GroceryListTester
{
public static void main(String[] args)
{
GroceryList get = new GroceryList();
get.start();
}
}
CheckMail Class:
import chn.util.*;
import java.util.*;
public class GroceryList
{
//Instance variables
private int myNumItems;
private double myItemCost;
private double myTotalCost;
Scanner keyboard = new Scanner(System.in);
//Constructor
public GroceryList()
{
myNumItems = 0;
myTotalCost = 0;
}
//Methods
public void start()
{
System.out.print('\u000C');
getMyNumItems();
getMyItemPrices();
output();
}
public int getMyNumItems()
{
System.out.print("How many items would you like to purchase? => ");
myNumItems = keyboard.nextInt();
return myNumItems;
}
public double getMyItemPrices()
{
int n = 0; //Disp item number
while(n < myNumItems)
{
System.out.print("Please enter price of item #" +(n+1) +": ");
myItemCost = keyboard.nextDouble();
myTotalCost = myTotalCost + myItemCost;
++n;
}
return myTotalCost;
}
public void output()
{
for(int n = 0; n < myNumItems; ++n)
{
System.out.println();
System.out.printf("%8s %8s %8s", "Item #" +(n + 1) , "Cost: $" +myItemCost, "Total: $" +myTotalCost);
}
}
}
Output:
How many items would you like to purchase? => 3
Please enter price of item #1: 1.25
Please enter price of item #2: 1.75
Please enter price of item #3: 1.50
Item #1 Cost: $1.5 Total: $4.5
Item #2 Cost: $1.5 Total: $4.5
Item #3 Cost: $1.5 Total: $4.5
The problem is that you're not storing any reference to the price of any item but the last. This calls for a ArrayList keeping track of each item that has already been ordered.
If instead of using your int myItemCost, you used ArrayList< int> myItemCosts, and each time you added a new item you added the stated price to that List, you could print each of those items at the end.
Here's an example:
ArrayList<int> myItemCosts = new ArrayList<int>();
public double getMyItemPrices()
{
int n = 0;
while (n < myNumItems)
{
System.out.print("Please enter price of item #" + (n+1) + ": ");
myItemCosts.add(keyboard.nextDouble());
myTotalCost += myItemCost;
}
}
And then output() could be structured like so:
public void output()
{
for(int n = 0; n < myNumItems; ++n)
{
System.out.println();
System.out.printf("%8s %8s %8s", "Item #" + (n + 1) , "Cost: $" + myItemCosts[n], "Total: $" + myTotalCost);
}
}
If you decided you needed to add more function to any given grocery item, like a SKU, quantity, discount, etc., you should create your own object to encapsulate these properties:
class SampleGroceryItem
{
public SampleGroceryItem(double price, int quantity, string sku, double discount)
{
this.Price = price;
this.Quantity = quantity;
this.Sku = sku;
this.Discount = discount;
}
public double Price;
public int Quantity;
public string Sku;
public double Discount;
}
and then instantiate a new SampleGroceryItem each time the user inputs all the necessary information, and add that object to a List.