Assignment Task(s) The programmer will create an application called “Inventory”.
ID: 3766318 • Letter: A
Question
Assignment Task(s) The programmer will create an application called “Inventory”. This application will read the contents of a binary inventory file (inventory.bin) and store the contents into an array of class type InventoryItems. The variables contained in this class include the Item Name, the amount of items on hand, and the current price. Your application will contain a few user options (sample below): INVENTORY File System A. Inventory File Name [] B. Display Inventory [0] C. Add/Modify Inventory Item D. Delete Inventory Item E. Save Updates Q. Quit User Choice (A-E,Q): The application will also use a local text configuration file (config.txt), which stores the location of the inventory file and the number of items in the inventory list. This information can be stored in any manner as you wish. When initiating the application, it will read the configuration file to find the location of the inventory file. If the inventory file exists, the inventory items will automatically be read. The application will create an ItemNotFoundException, which will be used when searching the database for an existing item. The application will contain a method for searching the database for a file, this method will return a ItemNotFoundException if the item is not in the inventory list. The application will prompt the user for an item. After the item has been entered, the application will search the existing database to determine if the item already exists. If the item exists, the application will prompt the user for the new price and number of items on hand. If the item does not exist, the application will prompt to enter the original price and the number of items on hand. If the user selects to delete an item, the application will search the database for the item. If the item is contained in the inventory, the item will be deleted. If the item is not in the database, the user will receive an “Item Not Found” error. Updates to the inventory file will only be made if the user selects to “Save Updates”Extra Credit (20 points) It is possible for the student to earn 20 extra credit points by sorting the array of inventory items by name. The sorting can be done in one of two different ways: 1. When the user enters a new inventory item, the item is sorted and stored in the array 2. When the user displays the inventory list, the items are sorted when the items are displayed Assignment Task(s) The programmer will create an application called “Inventory”. This application will read the contents of a binary inventory file (inventory.bin) and store the contents into an array of class type InventoryItems. The variables contained in this class include the Item Name, the amount of items on hand, and the current price. Your application will contain a few user options (sample below): INVENTORY File System A. Inventory File Name [] B. Display Inventory [0] C. Add/Modify Inventory Item D. Delete Inventory Item E. Save Updates Q. Quit User Choice (A-E,Q): The application will also use a local text configuration file (config.txt), which stores the location of the inventory file and the number of items in the inventory list. This information can be stored in any manner as you wish. When initiating the application, it will read the configuration file to find the location of the inventory file. If the inventory file exists, the inventory items will automatically be read. The application will create an ItemNotFoundException, which will be used when searching the database for an existing item. The application will contain a method for searching the database for a file, this method will return a ItemNotFoundException if the item is not in the inventory list. The application will prompt the user for an item. After the item has been entered, the application will search the existing database to determine if the item already exists. If the item exists, the application will prompt the user for the new price and number of items on hand. If the item does not exist, the application will prompt to enter the original price and the number of items on hand. If the user selects to delete an item, the application will search the database for the item. If the item is contained in the inventory, the item will be deleted. If the item is not in the database, the user will receive an “Item Not Found” error. Updates to the inventory file will only be made if the user selects to “Save Updates”
Extra Credit (20 points) It is possible for the student to earn 20 extra credit points by sorting the array of inventory items by name. The sorting can be done in one of two different ways: 1. When the user enters a new inventory item, the item is sorted and stored in the array 2. When the user displays the inventory list, the items are sorted when the items are displayed Assignment Task(s) The programmer will create an application called “Inventory”. This application will read the contents of a binary inventory file (inventory.bin) and store the contents into an array of class type InventoryItems. The variables contained in this class include the Item Name, the amount of items on hand, and the current price. Your application will contain a few user options (sample below): INVENTORY File System A. Inventory File Name [] B. Display Inventory [0] C. Add/Modify Inventory Item D. Delete Inventory Item E. Save Updates Q. Quit User Choice (A-E,Q): The application will also use a local text configuration file (config.txt), which stores the location of the inventory file and the number of items in the inventory list. This information can be stored in any manner as you wish. When initiating the application, it will read the configuration file to find the location of the inventory file. If the inventory file exists, the inventory items will automatically be read. The application will create an ItemNotFoundException, which will be used when searching the database for an existing item. The application will contain a method for searching the database for a file, this method will return a ItemNotFoundException if the item is not in the inventory list. The application will prompt the user for an item. After the item has been entered, the application will search the existing database to determine if the item already exists. If the item exists, the application will prompt the user for the new price and number of items on hand. If the item does not exist, the application will prompt to enter the original price and the number of items on hand. If the user selects to delete an item, the application will search the database for the item. If the item is contained in the inventory, the item will be deleted. If the item is not in the database, the user will receive an “Item Not Found” error. Updates to the inventory file will only be made if the user selects to “Save Updates”
Extra Credit (20 points) It is possible for the student to earn 20 extra credit points by sorting the array of inventory items by name. The sorting can be done in one of two different ways: 1. When the user enters a new inventory item, the item is sorted and stored in the array 2. When the user displays the inventory list, the items are sorted when the items are displayed
Explanation / Answer
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author prmsh
*/
public class Inventory {
String item;
double price;
public Inventory(String item, double price) {
this.item = item;
this.price = price;
}
public String getItem() {
return item;
}
public void setItem(String item) {
this.item = item;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
//moodify method
public static void modify(ArrayList<Inventory> stock, String item, double price, String newitem, double newprice) {
boolean found = false;
for (int i = 0; i < stock.size(); i++) {
if (stock.get(i).item.equals(item) && stock.get(i).price == price) {
stock.get(i).setItem(newitem);
stock.get(i).setPrice(newprice);
found = true;
}
}
if (!found) {
System.out.println("Item not found");
}
}
//to String method
@Override
public String toString() {
return "Inventory{" + "item=" + item + ", price=" + price + '}';
}
public static void main(String args[]) throws FileNotFoundException {
ArrayList<Inventory> store = new ArrayList<Inventory>();
String filename = "inventory.bin";
//reading binary file
Scanner sc = new Scanner(new File(filename));
while (sc.hasNext()) {
String line = sc.next();
String[] data = line.split(" ");
Inventory obj = new Inventory(data[0], Double.parseDouble(data[1]));
store.add(obj);
}
//displaying menu
while (true) {
System.out.println("1. Inventory file Name 2.Display Inventory 3.Add/Modify inventory item 4.Delete Inventory item 5.save updates 6.Exit");
Scanner read = new Scanner(System.in);
int choice = read.nextInt();
if (choice == 1) {
System.out.println("File name: " + filename);
} else if (choice == 2) {
for (int i = 0; i < store.size(); i++) {
System.out.println(store.get(i));
}
} //adding or moifiying
else if (choice == 3) {
System.out.println("1.Add 2.Modify");
int temp = sc.nextInt();
if (temp == 1) {//add new item
System.out.println("Enter item name: ");
String item = sc.nextLine();
System.out.println("Enter item price: ");
double price = sc.nextDouble();
Inventory obj = new Inventory(item, price);
store.add(obj);
} else { //modify
System.out.println("Enter item name: ");
String item = sc.nextLine();
System.out.println("Enter item price: ");
double price = sc.nextDouble();
System.out.println("Enter modified item name: ");
String modifieditem = sc.nextLine();
System.out.println("Enter modified item price: ");
double modifiedprice = sc.nextDouble();
modify(store, item, price, modifieditem, modifiedprice);
}
} else if (choice == 4) {
System.out.println("Enter item name: ");
String item = sc.nextLine();
System.out.println("Enter item price: ");
double price = sc.nextDouble();
Inventory obj = null;
for (int i = 0; i < store.size(); i++) {
if (store.get(i).item.equals(item) && store.get(i).price == price) {
obj = store.get(i);
}
}
store.remove(obj);
}
//storing data back to file
else if(choice == 5){
PrintWriter out = new PrintWriter(filename);
for (int i = 0; i < store.size(); i++) {
out.println(store.get(i).getItem()+" "+store.get(i).getPrice());
}
out.close();
}
//exiting from app
else{
System.out.println("Quitting...");
break;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.