I'm new to classes and stuff like that. All I'm trying to do for now is to make the inventory hold one item and then when it works I'll research on how to make multiple Item objects and how to keep the program going so the "edit" and "remove" methods work. So basically this a test for an inventory and items project.
Inventory class: When I run the project and enter a String: (S1) it tells me this:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at inventory.items.Inventory.add(Inventory.java:24)
at inventory.items.Inventory.main(Inventory.java:98)
Keep in mind that I'm a beginner and any thing you'll tell me will help. I can't seem to figure out to get this code working.
public class Inventory extends Item implements Actions{
int num1, num2;
String S1;
Item a = new Item();
Item b = new Item();
Scanner Sc = new Scanner(System.in);
@Override
public void add() {
System.out.println("Please enter the new item specifications as follows: 1. Quantity 2. Name 3. Price");
num1 = Sc.nextInt();
a.setQuantity(num1);
S1 = Sc.nextLine();
a.setName(S1);
num2 = Sc.nextInt();
a.setPrice(num2);
}
/**
*
* @param b
*/
@Override
public void remove(Item b) {
b = null;
System.gc();
}
/**
*
* @param E
*/
@Override
public void edit(String E) {
E = Sc.nextLine();
if(a.getName().equals(b)){
System.out.println("Please edit the item specifications as follows: 1. Quantity 2. Name 3. Price ");
num1 = Sc.nextInt();
S1 = Sc.nextLine();
num2 = Sc.nextInt();
a.setQuantity(num1);
a.setName(S1);
a.setPrice(num2);
}
else{
System.out.println("You can't edit a non existent item. The items are case
sensitive.");
}
}
/**
*
* @param Info
*/
@Override
public void info(String Info) {
if(a.getName().equals(Info)){
System.out.println("Item name : " + a.getName() + " Item price: " +
a.getPrice() + " Item Quantity: " + a.getQuantity());
}
else{
System.out.println("Please enter a an existing item name!");
}
}
public static void main (String [] args){
Inventory inv = new Inventory();
Scanner Sc1 = new Scanner(System.in);
Item a = new Item();
Item b = new Item();
String I = "";
System.out.println("Please enter one of the following commands: 1. add 2.
edit 3. remove 4. info ");
I = Sc1.nextLine();
switch (I){
case "add" :
inv.add();
break;
case "edit" :
System.out.println("Enter the name of the item you want to edit: ");
String Input2 = Sc1.nextLine();
inv.edit(Input2);
break;
case "remove" :
System.out.println("Enter the name of the item you want to remove: ");
inv.remove(a);
break;
case "info" :
System.out.println("Enter the name of the item you want too see its info.");
String Inf = Sc1.nextLine();
inv.info(Inf);
default :
System.out.println("Please enter a valid command! The commands ARE case-
sensitive.");
}
}
}
Item class:
This is the superclass of the Inventory class. I'm using getters and setters to set values of new objects in it.
class Item{
private int q,p;
private String n;
private int quantity = 0;
private String name = " ";
private int price = 0;
public void setQuantity(int q){
this.quantity = q;
}
public int getQuantity(){
return q;
}
public void setName(String n){
this.name = n;
}
public String getName(){
return n;
}
public void setPrice(int p){
this.price = p;
}
public int getPrice(){
return p;
}
}
Actions interface:
I'm using this interface to set the actions that can be made to an Item object; changing it's properties.
interface Actions {
void add();
void remove(Item b);
void edit(String E);
void info(String Info);
}