Assignment 8.1 (Exceptions) : Myintegers - Java
Assignment 8.1 (Exceptions) : Myintegers - Java
Jens Lauterbach
The method componentReader() reads component data from a file and has the following
functionality:
• It has no return value and receives a file name as String and an array of component objects
as parameters
• Use the following variables for your component objects (Use only String variables for this
assignment)
String component_ID = "0";
String name = "";
String delivery_date = "19000101";
String stock_amount = "0";
String vendor = "";
String comment = "";
String componentMaterial;
• Use a file components.csv (the file needs to be stored in the folder of your Java project e. g.,
C:\...\eclipse-workspace\<YourProjectFolder>) with comma separated values and
the following data.
;1;wheel;20211201;11;GreatWheels;good deal ;3;eor
;3;Handlebar;20211203;13;GreatHandlebars;best deal;1
1
Programming 1 – JAVA
University of Applied Sciences Augsburg Prof. Dr. Jens Lauterbach
• The data of the file corresponds with the component variables above. Values are separated
by “;”. The end of a row that corresponds with the data of one component object is denoted
by “eor”
• Use the class File (package java.io.) to create a file with the file name as constructor
parameter
• Use the class Scanner in the java.util package and its methods to read the data from the file and
store the data values in your component object attributes (you need three component objects in
your array to work with the file)
• Write a test class (with main method) to create an array of components and test the
componentReader() method
• The method throws a FileNotFoundException, handle it in your test class
The method componentDataToString() takes the data stored in your component objects and
transforms it into a comma separated String with the following functionality:
• It has a StringBuffer object (see class StringBuffer) as return value and receives an array of
component objects as parameter
• Use a method of StringBuffer to read the data from your component objects and concatenate it
to a String
• The String should have the same format/data structure as the components.csv file used in the
componentReader() method
• Test the method in your test class and print out the result on the screen
The method writeComponents() writes the data that the method componentDataToString() returns
into a new file componentsOutput.csv
• The method has no return value and receives a StringBuffer object and a Writer object (see class
Writer in the package java.io) as parameters
• Use a method of the StringBuffer class to write the StringBuffer object into a String variable
• Write the content of the String variable into the componentsOutput.csv
• Test your method in the test class. In the test class you need to create a new file object with
componentsOutput.csv as parameter. Pass this file to a new object of the Filewriter class.
• Pass your method writeComponents() the StringBuffer object and the Filewriter object as
parameters
• Check if the file componentsOutput.csv was created and contains the correct data
• The method throws an IOException, handle it in your test class
When starting the program, the data will be read from a file components.csv.
When ending the program, the user will be asked if the changes made to component objects should
be saved or discarded. When the user chooses yes, the data will be saved to components.csv.
2
Programming 1 – JAVA