Language: Java 1. Create a Product class with the following attributes and metho
ID: 3867416 • Letter: L
Question
Language: Java
1. Create a Product class with the following attributes and methods:
- int: price
- int: count
- String: name
- Constructor, accessors, mutators, and toString.
2. Create an Inventory class as a driver with the following functionalities:
§ A text file named “Products.txt” for storing the Products.
Each line contains information of one product with the following format:
- Name : price : count
The program shows the following menu to the user:
- 1: add items
Ask the user to enter the products. Once the user entered all products, write the Inventory into the “Products.txt” with the given format.
- 2: show Inventory
Read the Inventory information from the “Products.txt” file into an array of Products and print the Inventory information on the screen.
- 0: exit
Explanation / Answer
Note : Could you please check the output .If you required any changes Just intimate.I will modify it.Thank You.
_______________
Product.java
public class Product {
//Declaring instance variables
private int price;
private int count;
private String name;
//Parameterized constructor
public Product(int price, int count, String name) {
super();
this.price = price;
this.count = count;
this.name = name;
}
//Setters and getters
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Product [price=" + price + ", count=" + count + ", name=" + name + "]";
}
}
____________________
Inventory.java
package org.students;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class Inventory {
public static void main(String[] args) {
// Declaring variables
int price;
int count, choice;
String name;
// Creating the reference of File and FileWrite classes
File f;
FileWriter fw = null;
// Scanner object is used to get the inputs entered by the user
Scanner sc = new Scanner(System.in);
while (true) {
// Displaying menu
System.out.println(":: Menu ::");
System.out.println("1. add items ");
System.out.println("2. Show Inventory ");
System.out.println("0. Exit ");
// Getting the choice entered by the user
System.out.println("Enter Choice :");
choice = sc.nextInt();
// Based on the user choice the corresponding case will be executed
switch (choice) {
case 1:
{
// Creating an ArrayList which holds Book class Objects
ArrayList < Product > arl = new ArrayList < Product > ();
while (true) {
sc.nextLine();
System.out.print("Enter Product name (-1 to exit ):");
name = sc.nextLine();
if (name.equals("-1")) {
break;
} else {
System.out.print("Enter the price :$");
price = sc.nextInt();
System.out.print("Enter Count :");
count = sc.nextInt();
Product p = new Product(price, count, name);
arl.add(p);
}
}
// Writing the data to the file
try {
f = new File("Products.txt");
// If file already exists then write data to the existing
// file
if (f.exists()) {
fw = new FileWriter(f, true);
System.out.println("Data Written to File");
} else {
// If no file already exists.Create new File
f.createNewFile();
fw = new FileWriter(f);
System.out.println("Data Written to File");
}
Iterator itr = arl.iterator();
while (itr.hasNext()) {
Product p = (Product) itr.next();
fw.write(p.getName() + ":");
fw.write(p.getPrice() + ":");
fw.write(p.getCount() + " ");
}
fw.close();
} catch (Exception e) {
System.out.println("Exception " + e);
}
arl.clear();
continue;
}
case 2:
{
Scanner sc1 = null;
try {
sc1 = new Scanner(new File("Products.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String str;
String arr[];
System.out.println("** Displaying Inventory **");
// Code which will display the data from the file
while (sc1.hasNext()) {
str = sc1.nextLine();
arr = str.split(":");
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
System.out.println("_________________");
}
continue;
}
case 0:
{
System.out.println("** PROGRAM EXIT **");
break;
}
default:
{
System.out.println("** Invalid Input **");
continue;
}
}
break;
}
}
}
______________________
Output:
:: Menu ::
1. add items
2. Show Inventory
0. Exit
Enter Choice :
1
Enter Product name (-1 to exit ):Pen
Enter the price :$2
Enter Count :50
Enter Product name (-1 to exit ):Pad
Enter the price :$5
Enter Count :50
Enter Product name (-1 to exit ):Book
Enter the price :$7
Enter Count :150
Enter Product name (-1 to exit ):-1
Data Written to File
:: Menu ::
1. add items
2. Show Inventory
0. Exit
Enter Choice :
2
** Displaying Inventory **
Pen
2
50
_________________
Pad
5
50
_________________
Book
7
150
_________________
:: Menu ::
1. add items
2. Show Inventory
0. Exit
Enter Choice :
0
** PROGRAM EXIT **
_______________
Products.txt:
Pen:2:50
Pad:5:50
Book:7:150
_______________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.