Need to be solved in Java, thank you. C Home Chegg.com x Cou e: COMP 06B x D 95.
ID: 3797455 • Letter: N
Question
Need to be solved in Java, thank you.
C Home Chegg.com x Cou e: COMP 06B x D 95.105/115 Assignme x C file://IC/Users/victor0323/Downloads/COMP1406 A5 W2017-Posted%20X1).pdf (1) The GUI Create a class called ShoppingListApp which will construct the GUI shown below. You should make the window to be non-resizable. You may have to tweak the size a little bit in order to get the bordering margins correct. 10 10 120 15 65 100 10 C Grocery Store Application 10 pping Cart 35 300 10 Return checkout iota Price: Buy 10 a e A 459 1x NG 2017/2123Explanation / Answer
import java.util.ArrayList;
import javax.swing.SwingUtilities;
import javax.swing.table.TableModel;
import javax.swing.event.TableModelListener;
import javax.swing.event.TableModelEvent;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class ShoppingListapp {
public final static String DEFAULT_FILE_NAME = "ShoppingList.txt";
private ArrayList<Item> items = new ArrayList<Item>();
private String fileName = DEFAULT_FILE_NAME;
public ShoppingListapp() {}
public ShoppingListapp(String fileName) {
this.fileName = fileName;
}
public void addItem(Item newItem) {
items.add(newItem);
}
public void delItem(Item remItem) {
items.remove(remItem);
}
public void delItem(int indx) {
items.remove(indx);
}
public void updateItem(Item upItem) {
int size = items.size();
int rowIndx;
Item test;
String upName, testName;
boolean foundItem = false;
upName = upItem.getName();
for (rowIndx=0; rowIndx < size; rowIndx++) {
test = items.get(rowIndx);
testName = test.getName();
if (upName.equals(testName)) {
foundItem = true;
break;
}
}
if (foundItem) {
items.set(rowIndx, upItem);
}
}
public Item getItem(String itemName) {
Item result = null;
Item test;
int size = items.size();
for (int indx=0; indx < size; indx++) {
test = items.get(indx);
if (test.getName().equals(itemName)) {
result = test;
break;
}
}
return result;
}
public Item getItem(int indx) {
return items.get(indx);
}
public void writeShoppingList() throws IOException {
FileWriter fstream = new FileWriter(fileName, false);
BufferedWriter out = new BufferedWriter(fstream);
out.write("Shopping List:");
out.newLine();
for (Item it : items) {
out.write(it.getName() + " " + it.getQuantity());
out.newLine();
}
out.close();
fstream.close();
}
public ArrayList<Item> getItems() {
return items;
}
public static void main(String[] args) {
int argsLen = args.length;
String fileName;
if (argsLen > 0) {
fileName = args[0];
}
else
{
fileName = ShoppingListapp.DEFAULT_FILE_NAME;
}
final ShoppingListapp slm = new ShoppingListapp(fileName);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
final ShoppingListappView view = new ShoppingListappView();
view.addTableModelListener(new TableModelListener() {
public void tableChanged(TableModelEvent tme) {
String itemName, quantity;
Item newItem;
int changeType, rowIndx;
changeType = tme.getType();
rowIndx = tme.getFirstRow();
if (changeType == TableModelEvent.DELETE) {
slm.delItem(rowIndx);
} else {
itemName = view.getValueAt(rowIndx, 0);
quantity = view.getValueAt(rowIndx, 1);
newItem = new Item(itemName, quantity);
slm.addItem(newItem);
}
}
});
view.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent we) {
try {
slm.writeShoppingList();
} catch(IOException e) {
System.out.println("Could not write to file");
}
}
});
view.showDisplay();
}
});
}
}
class Item {
private String name;
private String quantity;
Item (String name, String quantity)
{
this.name = name;
this.quantity = quantity;
}
String getName() {
return name;
}
void setName(String name) {
this.name = name;
}
String getQuantity() {
return quantity;
}
void setQuantity(String quantity) {
this.quantity = quantity;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.