For this assignment, you are to create two programs. In the first program you wi
ID: 3688272 • Letter: F
Question
For this assignment, you are to create two programs. In the first program you will enable the user to input ten inventory items. The user will input the name of each item, the price of each individual item and how many of each item are in stock. You will accept the data inside a loop. The dialog with the user when requesting the input should be appropriate and comprehensive. The data will be stored in a file.
The second program will output the contents of the file to the user's screen. The output will include appropriate labels for all of the data. You will output data using a loop. You will identify the file to be accessed using a graphic dialog box.
The two programs will be independent. By that, I mean that one program will not reference the other using objects or classes. If you have any questions about this, make sure you ask.
This is intro class, so the simplest of code would be great. I want to be able to understand it to the best of my ability. Thanks for all your help.
Explanation / Answer
Answer:
First program :
import javax.swing.*;
import java.io.*;
class InvetoryItemsProgram
{
public static void main(String args[])
{
String cost_of_item_as_string;
String number_of_item_as_string;
final int count_items = 10; //given
String name_of_item[] = new String [count_items];
double count_of_items[] = new double [count_items];
double item_price[] = new double [count_items];
try{
FileWriter fw = new FileWriter("inventorylist.txt");
PrintWriter pw = new PrintWriter(fw);
for( int takenIndex = 0; takenIndex < count_items; takenIndex++)
{
name_of_item[takenIndex] = JOptionPane.showInputDialog("Enter name of items: ");
cost_of_item_as_string = JOptionPane.showInputDialog(" Enter cost of items: ");
item_price[takenIndex] = Double.parseDouble(cost_of_item_as_string);
number_of_item_as_string = JOptionPane.showInputDialog(" Enter number of items present in
inventory: ");
count_of_items[takenIndex] = Double.parseDouble(number_of_item_as_string);
}
for( int takenIndex = 0; takenIndex < count_items; takenIndex++)
{
pw.println(name_of_item[takenIndex]+" $"+item_price[takenIndex]+" $"+count_of_items
[takenIndex] );
}
pw.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Second program :
import java.util.*;
import java.io.*;
public class InventoryItemsSecondProgram
{
public static void main(String[] input)
{
String filename;
// Scanner sc = new Scanner(System.in);
filename = "inventorylist.txt";
//System.out.print("Enter File Name to Open (with extension like file.txt) : ");
//fname = scan.nextLine();
String execution_lines = null;
try
{
FileReader fileReader = new FileReader(filename);
BufferedReader bufferedReader = new BufferedReader(fileReader);
while((execution_lines = bufferedReader.readLine()) != null)
{
System.out.println(execution_lines );
}
bufferedReader.close();
}
catch(IOException ex)
{
System.out.println("Error occured in reading file named '" + filename + "'");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.