USING JAVA Description: 1.) In this part, we are going to apply the concepts of
ID: 3592321 • Letter: U
Question
USING JAVA
Description:
1.) In this part, we are going to apply the concepts of Bag Interface to Online Shopping Cart. If you examine the Listing 1-2 program, you will find that the program is not complete and the type Item is missing.
2.)In designing interactive application like shopping activities, it is desirable to have the test data stored in a file to speed up the testing time. Therefore, to prepare for the future project, we need to get used to file I/O in Java.
3)There is a tab-delimited text file named “p1artists.txt” which is to be used as an input file. Each record consists of the following two fields:
a) artistID: integer.
b) artistName: String;
Assignment:
A) Refer to the sample program of Listing 1-2 on pages 41 and 42, instead of Item class, implement the Artist class that contains the following:
1.) The 2 fields mentioned above in “p1artists.txt” file.
2.)The Constructor that accepts the 2 fields when the record of an artist is created.
3.) Getters and setters for the 2 fields.
4.) The toString() method.
5. )Test the program with the following data.
ArtistID
Artist Name
1
Acconci
2
Ames
3
Aserty
4
Baron
5
Battenberg
B) Modify the above program according to the following:
1.) Read the input file “p1artists.txt” instead of hardcoding the values.
2.) Use the exception handler to ensure that artist ID is numeric.
3.) If the input is incorrect, print the line number and its contents, but do not stop.
4.) Send the output to a text file named “p1artists_out1.txt”.
C) Enhance the above program for the following:
1.) A tab-delimited text file named “p1arts.txt” contains the following record formats:
ArtID
Title
ArtistID
Appraised Value
1001
Red Rock Mountain
50
18000
1002
Offerings
52
10000
1003
Spring Flowers
12
2400
2.) Assume that this file contains no error. You are going to read “p1artists.txt” into an array, and then process “p1arts.txt” to add the artist name to the output. Name this output file “p1arts_out.txt”. Example follows:
ArtID
Title
Artist
ArtistID
Appraised Value
1001
Red Rock Mountain
Mogan
50
18000
1002
Offerings
Novarre
52
10000
1003
Spring Flowers
Chico
12
2400
3.)Use an exception handler if the ArtistID does not exist in the “p1artists.txt” file. However, you still need to write this record to the output file.
4.)In the end, include the total number of the Art Works and Artists, and the total appraised value.
---------------------------------------------------------------------------------------------------------------------------------------
Listing 1-2 CODE
public class OnlineShopper
{
public static void mian (String[] args)
{
Item[] items = { new Item("Bird feeder", 2050),
new Item("Squirrel guard", 1547),
new Item("Bird bath", 4499),
new Item("Sunflower seeds", 1295)};
BagInterface<Item> shoppingCart = new Bag<>();
int totalCost = 0;
for (int index = 0; index < items.length; index++)
{
Item nextItem = items[index];
shoppingCart.add(nextItem);
totalCost = totalCost + nextItem.getPrice();
}
while (!shoppingCart.isEmpty())
System.out.println(shoppingCart.remove());
System.out.println("Total cost: " + " $" + totalCost / 100 + " . " + totalCost % 100);
}
}
Sample Output
Sunflower seeds $12.95
Bird Bath $44.99
Squirrel guard $15.47
Bird feeder $20.50
Total cost: $93.91
------------------------------------------------------------------------------------------------------------------------------------------------------
p1arts.txt
---------------------------------------------------------------------------------------------------------------------------
p1artist.txt
---------------------------------------------------------------------------------------------------------------------------------------
p1artists-full.txt
ArtistID
Artist Name
1
Acconci
2
Ames
3
Aserty
4
Baron
5
Battenberg
Explanation / Answer
public class OnlineShopper
{
public static void mian (String[] args)
{
Item[] items = { new Item("Bird feeder", 2050),
new Item("Squirrel guard", 1547),
new Item("Bird bath", 4499),
new Item("Sunflower seeds", 1295)};
BagInterface<Item> shoppingCart = new Bag<>();
int totalCost = 0;
for (int index = 0; index < items.length; index++)
{
Item nextItem = items[index];
shoppingCart.add(nextItem);
totalCost = totalCost + nextItem.getPrice();
}
while (!shoppingCart.isEmpty())
System.out.println(shoppingCart.remove());
System.out.println("Total cost: " + " $" + totalCost / 100 + " . " + totalCost % 100);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.