Include the following in your program: Create a Class representing the informati
ID: 3671465 • Letter: I
Question
Include the following in your program: Create a Class representing the information that is associated with one item of your database. The information for one item must include a minimum of 3 fields. Within the Class for one item, include the following methods: A constructor that accepts the information about one item and assigns it to the instance data within this Class. A method that prints the information about one item of your collection. Name this method toString. NOTE: It is important that you name this method toString because it's one of Java's built-in methods that allows you print out your Object. Create another Class with an array of Objects of the Class of one item with the following methods: Create an array of the Objects from the Class of one item. Create a constructor to initialize the number of items in the array to zero. Create a method to add one item at a time to the array. Create a method to display the contents of the entire array. Call this method toString. Create a method to increase the size of the array if it is getting full. Create a Driver Class with a Menu with the option to add an item, to print out a list of all items, and exit. When your program is first executed, read the data from an external file and place it in your array. Submit the external file with program. To populate the external file for the first time, hard code into your program sending data to the external file. You will only need to do this once - just when you've created the external file for the first. Save the data back to the external file when the Exit option is chosen from the menu in the Drive. NOTE: You MUST read your data from an external file! You MUST save your data back to that external file when done.Explanation / Answer
//OneItem.java
public class OneItem
{
//declare a variables
private String name;
private double price;
private int quantity;
//default constructor
public OneItem()
{
name="";
price=0;
quantity=0;
}
//parameter constructor
public OneItem(String name, double price, int quantity)
{
this.name=name;
this.price=price;
this.quantity=quantity;
}
//Override the toString
@Override
public String toString() {
return String.format("%-10s%-10.2f%-10d ", name,price,quantity);
}
}//end of the OneItem class
------------------------------------------------------------------------------------------
//ItemsList.java
public class ItemsList
{
//declare variables
private OneItem items[];
private int size;
private int count;
//constructor to set items and size and count to zero
public ItemsList()
{
items=null;
size=0;
count=0;
}
//Parameter constructor
public ItemsList(int size)
{
items=new OneItem[size];
for (int i = 0; i < items.length; i++)
{
items[i]=new OneItem();
}
this.size=size;
count=0;
}
//Add OneItem to the itemlist
public void addItem(OneItem item)
{
if(items.length==count)
{
resize();
items[count]=item;
count++;
}
else
{
items[count]=item;
count++;
}
}
//Resize the array
private void resize()
{
int oldsize=size;
count=oldsize;
int newsize=2*this.size;
size=newsize;
OneItem[] tempList=new OneItem[size];
for (int i = 0; i < oldsize; i++)
tempList[i]=items[i];
items=new OneItem[size];
items=tempList;
}
//Returns the size of the items list
public int getSize()
{
return count;
}
//Override the toString method that returns the string description of the
//items
@Override
public String toString()
{
String description="";
description+=String.format("%-10s%-10s%-10s ", "Item","Price","Quantity");
for (int i = 0; i <count; i++)
{
description+=items[i].toString();
}
return description;
}
}
------------------------------------------------------------------------------------------------------------------------
/**The driver class that reads the text file items.txt
* and display the file data to console and display
* a menu choice and prompt for choice . The add
* items to the itemslist . If user select to display
* then display the list of items in the list.
* The program runs infintely until user selects to
* exit .
* */
//Driver.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Driver
{
public static void main(String[] args)
{
String fileName="items.txt";
Scanner scanner=null;
ItemsList itemsList=new ItemsList(5);
int i=0;
try
{
scanner=new Scanner(new File(fileName));
while(scanner.hasNextLine())
{
String itemName=scanner.next();
double price=scanner.nextDouble();
int qty=scanner.nextInt();
//Add the OneItem object to the itemList
itemsList.addItem(new OneItem(itemName, price, qty));
i++;
}
//close the file objct
scanner.close();
//print the list of the items in itemsList
System.out.println(itemsList.toString());
}
catch (FileNotFoundException e)
{
System.out.println(e.getMessage());
}
boolean repeat=true;
//Create an instance of Scanner class
scanner=new Scanner(System.in);
while(repeat)
{
int choice=menu();
switch (choice)
{
//Add an item to the itemsList
case 1:
System.out.println("Enter item name : ");
String name=scanner.nextLine();
System.out.println("Enter price : ");
double price=Double.parseDouble(scanner.nextLine());
System.out.println("Enter quantity : ");
int quantity=Integer.parseInt(scanner.nextLine());
//Add the OneItem to the itemsList
itemsList.addItem(new OneItem(name, price, quantity));
break;
case 2:
//print the list
System.out.println(itemsList.toString());
break;
case 3:
//Terminate the program
System.out.println("Terminate the program.");
//set repeat to false
repeat=false;
break;
default:
System.out.println("Incorrect option is selected.");
break;
}
}
writeToFile(itemsList);
}
private static void writeToFile(ItemsList itemsList)
{
//Create a file name called items.txt
String filename="items.txt";
//Create a variable of Class PrintWriter
PrintWriter filewriter=null;
try
{
//create an instance of PrintWriter
filewriter=new PrintWriter(new File(filename));
filewriter.println(itemsList.toString());
//close the filewriter
filewriter.close();
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
}
/**The method menu that prints the menu and prompst
* for the menu item to select and return choice.*/
private static int menu()
{
//Create an instance of Scanner class
Scanner keyboard=new Scanner(System.in);
System.out.println("Menu");
System.out.println("1. Add item");
System.out.println("2. Display items");
System.out.println("3. Exit");
System.out.println("Enter your choice");
int choice=Integer.parseInt(keyboard.nextLine());
return choice;
}
}//end of the Driver class
------------------------------------------------------------------------------------------------------------------------------------------------
Input text file naem:
items.txt
Apple 10.00 10
orange 10.00 20
grapes 10.00 20
papaya 10.00 10
coconut 10.00 20
Banana 10.00 20
---------------------------------------------------------------------------------------------------------------------------------------------
Sample output:
Item Price Quantity
Apple 10.00 10
orange 10.00 20
grapes 10.00 20
papaya 10.00 10
coconut 10.00 20
Banana 10.00 20
Menu
1. Add item
2. Display items
3. Exit
Enter your choice
1
Enter item name :
Carrot
Enter price :
10
Enter quantity :
40
Menu
1. Add item
2. Display items
3. Exit
Enter your choice
2
Item Price Quantity
Apple 10.00 10
orange 10.00 20
grapes 10.00 20
papaya 10.00 10
coconut 10.00 20
Banana 10.00 20
Carrot 10.00 40
Menu
1. Add item
2. Display items
3. Exit
Enter your choice
3
Terminate the program.
After the execution of the program, the items.txt file becomes
Item Price Quantity
Apple 10.00 10
orange 10.00 20
grapes 10.00 20
papaya 10.00 10
coconut 10.00 20
Banana 10.00 20
Carrot 10.00 40