Hi! I'm a newbie at java and I need help with an inventory program for a school project. I planned the inventory program for the user to keep track of their inventory in MMORPGs. I want the user to be able to add, delete, update, sort the items, also be able to view the items that's currently in the program, and a log of whenever an item has been added, deleted, or updated. I understand my program is extremely messy, so any tips on making it more efficient would be appreciated. I'm having trouble figuring out how to update the price and amount sold for an item in the arraylist. Also every time I try to add an item, it always skips over adding notes, and when I try to sort, it always stops working, how can I fix these problems? Lastly, how can I log all the actions done to the items? Thank you in advance! If you could refer me to any references I should read up to understand java better, I would really appreciate it. I haven't started on working on the output yet, the teacher has given us a template but I have yet to work with it since I wanted to figure how to log the actions done first.
import java.util.*;
public class Inventory{
public static void main(String[] args){ //main class
ItemList il = new ItemList();
Scanner kb = new Scanner(System.in);
char answer = 'w';
System.out.println("Welcome to the Inventory Program");
do{
System.out.println("Do you wish to add an item, update an item, delete an item");
System.out.println("sort item, view logs, print log history, or quit?");
answer = kb.nextLine().toLowerCase().charAt(0);
if(answer == 'a'){ //add item
il.addItem();
System.out.println();
}
else if(answer == 'u'){ //update item
il.update();
System.out.println();
}
else if(answer == 'd'){ //delete item
il.deleteItem();
System.out.println();
}
else if(answer == 's'){ //sort item
il.sortBy();
System.out.println();
}
else if(answer == 'v'){ //view logs
System.out.println(il);
System.out.println("Total Profit:" + il.calcTotp());
System.out.println();
}
else if(answer == 'p'){ //print log history
//output
System.out.println();
}
}
while (answer != 'q'); //quit
System.out.println("Good bye!");
}
}
import java.util.Scanner;
import java.util.ArrayList;
public class ItemList extends ArrayList<Item>{ //Array
//ArrayList<Item> Output = new ArrayList<Item>();
Scanner kb= new Scanner(System.in);
String sort;
double totP;
public void addItem(){ //creates new item and adds to array
String iname;
int iquantity;
double iprice;
int isold;
String inotes;
String ilocation;
String itype;
System.out.println("Type name:");
iname = kb.nextLine();
System.out.println();
System.out.println("Type quantity:");
iquantity = kb.nextInt();
System.out.println();
System.out.println("Type price:");
iprice = kb.nextInt();
System.out.println();
System.out.println("Type amount sold:");
isold = kb.nextInt();
System.out.println();
System.out.println("Enter notes:");
inotes = kb.nextLine();
System.out.println();
System.out.println("Enter location:");
ilocation = kb.nextLine();
System.out.println();
System.out.println("Enter type:");
itype = kb.nextLine();
System.out.println();
Item i = new Item(iname, iquantity, iprice, isold, inotes, itype, ilocation);
add(i);
Output.add(i);
}
public void deleteItem (){ //delete an item from the arraylist
System.out.println("Type name to delete.");
String deleteName = kb.nextLine();
int input = indexOf(deleteName);
remove(input);
}
public double calcTotp(){ //calculate the total profit from all of the items in list
totP = 0;
for(int i =0; i < size(); i++){
totP= totP + get(i).getProfit();
}
return totP;
}
public void update(){ //user inputs sold and price for a certain item, updates both variables
System.out.println("Which item would you like to update?");
String name = kb.nextLine();
int input = indexOf(name);
//get(i).
/*System.out.println("Please enter the updated price:");
iprice = kb.nextInt();
set(input, iprice);
System.out.println();
System.out.println("Please enter the updated sold amount:");
isold = kb.nextInt();
System.out.println(); */
}
public void sortBy(){ //sort the items in list
System.out.println("How would you like to sort it? (location, price, or type)");
char g = 'g';
g = kb.nextLine().toLowerCase().charAt(0);
if (g == 'l'){ //sort location by alphabetical order
ArrayList<Item> temp = new ArrayList<Item>();
while(size()>0){
int currentIndex= 0;
String first = get(0).getName();
for(int i =0; i < size(); i++){
if (get(i).getName().compareTo(first)<0){
currentIndex = i;
temp.add(get(i));
remove(i);
}
}
}
addAll(temp);
}
else if (g == 'p'){ //sort price lowest to highest
ArrayList<Item> temp = new ArrayList<Item>();
while(size()>0){
int currentIndex= 0;
double lowestPrice = get(0).getPrice();
for(int i =0; i < size(); i++){
if (get(i).getPrice() <lowestPrice){
currentIndex = i;
temp.add(get(i));
remove(i);
}
}
}
addAll(temp);
}
else if (g == 't'){ //sort type by alphabetical order
ArrayList<Item> temp = new ArrayList<Item>();
while(size()>0){
int currentIndex= 0;
String first = get(0).getName();
for(int i =0; i < size(); i++){
if (get(i).getName().compareTo(first)<0){
currentIndex = i;
temp.add(get(i));
remove(i);
}
}
}
addAll(temp);
}
}
}
import java.util.Scanner;
import java.util.ArrayList;
import java.lang.String;
public class Item{
String name;
int quantity;
double price;
int sold;
double profit;
String notes;
String location;
String type;
Scanner kb= new Scanner(System.in);
//creates item using information inputed from ItemList
public Item(String iname, int iquantity, double iprice, int isold, String inotes, String itype, String ilocation){
name = iname;
profit = calcProfit(isold, iquantity, iprice);
price = iprice;
sold = isold;
notes = inotes;
type = itype;
location = ilocation;
}
public String toString(){ //what output looks like when you view list
return name+ ", " +quantity+ ", " +price+ ", " +sold+ ", " +notes+ ", " +type+ ", " +location + "\n";
}
public void setName(String iname){
name = iname;
//iteminfo.add(name);
}
public String getName(){
return name;
}
public void setQuantity(int iquantity){
quantity = iquantity;
//iteminfo.add(quantity);
}
public int getQuantity(){
return quantity;
}
public void setPrice(int iprice){
price = iprice;
//iteminfo.add(price);
}
public double getPrice(){
return price;
}
public void setSold(int isold){
sold = isold;
//iteminfo.add(sold);
}
public int getSold(){
return sold;
}
public void setProfit(int iprofit){
profit = iprofit;
//iteminfo.add(profit);
}
public double getProfit(){
return profit;
}
public void setNotes(String inotes){
notes = inotes;
//iteminfo.add(notes);
}
public String getNotes(){
return notes;
}
public void setLocation(String ilocation){
location = ilocation;
//iteminfo.add(location);
}
public String getLocation(){
return location;
}
public void setType(String itype){
type = itype;
//iteminfo.add(type);
}
public String getType(){
return type;
}
//calculates profit
//if user sells something, price = price sold for, sold = amount sold
//if user buys something, price = -, sold = amount bought
//if user gives something away for free, price = 0, sold = -(amount given away)
//if user finds something, price = 0, sold = amount found
public double calcProfit(int isold, int iquantity, double iprice){
if (iprice < 0){
profit = (iprice*isold);
iquantity = (isold + iquantity);
}
else{
if (iprice ==0){
if (isold < 0){
iquantity = (iquantity + isold);
}
else{
iquantity = (quantity - isold);
}
}
else{
profit = (iprice*isold);
iquantity = (iquantity-isold);
}
}
return profit;
}
}