Question

In java Lets say theres a ArrayList of products that have the variables name, price and...

In java Lets say theres a ArrayList of products that have the variables name, price and stock. The user must be able to select which product he wants to buy and how many times he wants to buy them. Once the purchase process ends, it will update the latest quantity to the Product’s stock count. Create a Java Program with the above requirement

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// Java Code:

import java.util.*;
public class Main
{
   public static void main(String[] args) {
   // Creating 3 ArrayList each store Product_Name , stock and the price.
   ArrayList<String> variable_name=new ArrayList<String>();
   ArrayList<Integer> price=new ArrayList<Integer>();
   ArrayList<Integer> stock=new ArrayList<Integer>();
  
   // Add the detail of Product 1
   variable_name.add("Product1");
   price.add(10);
   stock.add(100);
  
   // Add the detail of Product 2
   variable_name.add("Product2");
   price.add(20);
   stock.add(80);
  
   // Add the detail of Product 3
   variable_name.add("Product3");
   price.add(30);
   stock.add(50);
  
   // Print the Intial stock of each product
   System.out.println("Product_Name price stock");
   for(int i=0;i<3;i++)
   System.out.println(variable_name.get(i)+" "+price.get(i)+" "+stock.get(i));
  
   // User input the purchase Detail which product to buy   
   Scanner in=new Scanner(System.in);
   System.out.print("Which Product you want to buy : ");
   String str=in.nextLine();
  
   // User input the quantity of product to buy.
   System.out.print("How many times you want to buy : ");
   int t=in.nextInt();
  
   // Print the cost of purchase and update the stock of respective product.
   for(int i=0;i<3;i++)
   {
   if(str.equals(variable_name.get(i)))
   {
   System.out.println("The Total price of your purchase : "+(t*price.get(i)));
   stock.set(i,stock.get(i)-t);
   break;
   }
   }
  
   // Print the stock detail after purchase.
   System.out.println("The stock after purchase are:");
   System.out.println("Product_Name price stock");
   for(int i=0;i<3;i++)
   System.out.println(variable_name.get(i)+" "+price.get(i)+" "+stock.get(i));
   }
}


7 8 Main.java 1 import java.util.*; 2 public class Main 3-{ 4. public static void main(String[] args) { 5 // Creating 3 Arrayint t=in.nextInt(); 37 38 39 40 41 42 43- // Print the cost of purchase and update the stock of respective product. for(int i

Add a comment
Know the answer?
Add Answer to:
In java Lets say theres a ArrayList of products that have the variables name, price and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • java Part 1 Create a NetBeans project that asks for a file name. The file should...

    java Part 1 Create a NetBeans project that asks for a file name. The file should contain an unknown quantity of double numeric values with each number on its own line. There should be no empty lines. Open the file and read the numbers. Print the sum, average, and the count of the numbers. Be sure to label the outputs very clearly. Read the file values as Strings and use Double.parseDouble() to convert them. Part 2 Create a NetBeans project...

  • Java I: Create a Java program that will accept the price of an item and the...

    Java I: Create a Java program that will accept the price of an item and the quantity being purchased of that item. After accepting the input, calculate the amount of the purchase (based on the price and quantity), calculate the sales tax on the purchase, then output the product price, quantity, subtotal, sales tax, and the total sale based on the output format shown below. Structure your file name and class name on the following pattern: The first three letters...

  • Java Project In Brief... For this Java project, you will create a Java program for a...

    Java Project In Brief... For this Java project, you will create a Java program for a school. The purpose is to create a report containing one or more classrooms. For each classroom, the report will contain: I need a code that works, runs and the packages are working as well The room number of the classroom. The teacher and the subject assigned to the classroom. A list of students assigned to the classroom including their student id and final grade....

  • Java: student directory GUI You need to implement three classes: Person Student StudentDirectory StudentMain Start by...

    Java: student directory GUI You need to implement three classes: Person Student StudentDirectory StudentMain Start by implementing Person and Student classes. Once you are sure you can serialize and deserialize and ArrayList of Students to and from a file, move on to building the GUI application. Person: The Person class should implement serializable interface. It contains the following: Person's first name (String) Person's last name (String) Person's id number Person's date of birth (Date) public String toString(): This method method...

  • After dinner with Alexander and Elizabeth we notice that Alexander is mixing some herbs as a...

    After dinner with Alexander and Elizabeth we notice that Alexander is mixing some herbs as a medication for Elizabeth which eases her pain and helps her sleep. Once the medicine is administered we begin small talk with Alexander. During the conversation he tells us the story about how his mother and father were hopelessly in love and were lucky enough to die side by side protecting the things they loved most, their children. In the middle of the story we...

  • JAVA Objective : • Learning how to write a simple class. • Learning how to use...

    JAVA Objective : • Learning how to write a simple class. • Learning how to use a prewritten class. • Understanding how object oriented design can aid program design by making it easier to incorporate independently designed pieces of code together into a single application. Instructions: • Create a project called Lab13 that contains three Java file called Book.java , Bookstore.java and TempleBookstore.java • I’ve provided a screenshot of how your project should look like. • Be sure to document...

  • All code will be in Java, and there will be TWO source code. I. Write the...

    All code will be in Java, and there will be TWO source code. I. Write the class  MailOrder to provide the following functions: At this point of the class, since we have not gone through object-oriented programming and method calling in detail yet, the basic requirement in this homework is process-oriented programming with all the code inside method processOrderof this class. Inside method processOrder, we still follow the principles of structured programming.   Set up one one-dimensional array for each field: product...

  • (C Programming language, not C++ and MUST Compile) Companies and people often buy and sell stocks....

    (C Programming language, not C++ and MUST Compile) Companies and people often buy and sell stocks. Often they buy the same stock for different prices at different times. Say one owns 1000 shares a certain stock (such as Checkpoint), one may have bought the stock in amounts of 100 shares over 10 different times with 10 different prices. We will analyze two different methods of accounting, FIFO and LIFO accounting used for determining the “cost” of a stock. This is...

  • Using Java You are helping a corporation create a new system for keeping track of casinos...

    Using Java You are helping a corporation create a new system for keeping track of casinos and customers. The system will be able to record and modify customer and casino information. It will also be able to simulate games in the casino. Customer-specific requirements You can create new customers All new customers have a new customerID assigned to them, starting with 1 for the first, 2 for the second, 3 for the third, ect. New customers have names and monetary...

  • Java 1. Develop a program to emulate a purchase transaction at a retail store. This program...

    Java 1. Develop a program to emulate a purchase transaction at a retail store. This program will have two classes, a LineItem class and a Transaction class. The LineItem class will represent an individual line item of merchandise that a customer is purchasing. The Transaction class will combine several LineItem objects and calculate an overall total price for the line item within the transaction. There will also be two test classes, one for the LineItem class and one for the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT