Can you comment on whats happening in the code below? And how would I put this c
ID: 3772869 • Letter: C
Question
Can you comment on whats happening in the code below? And how would I put this code into netbeans? Do I use different packages?
Item.java
class Item
{
String name;
int quantity;
double price;
Item()
{
}
Item(String name,int quantity,double price)
{
this.name=name;
this.quantity=quantity;
this.price = price;
}
void setName(String name)
{
this.name=name;
}
String getName()
{
return name;
}
void setQuantity(int quantity)
{
this.quantity=quantity;
}
int getquantity()
{
return quantity;
}
void setPrice(double price)
{
this.price=price;
}
double getPrice()
{
return price;
}
}
-------------------------------------------------------------------------------------------------------------------------------------------
Inventory.java
import java.io.*;
class Inventory
{
void printInventory(Item[] inventory)
{
int size;
size = inventory.length;
for(int i=0;i<size;i++)
{
System.out.println(inventory[i].getName()+" "+inventory[i].getquantity()
+" "+inventory[i].getPrice());
}
}
void checkLowInventory(Item[] inventory)
{
int size,cnt=0;
size =inventory.length;
for(int i=0;i<size;i++)
{
if(inventory[i].getquantity()<5)
{
System.out.println(inventory[i].getName()+" "+inventory[i].getquantity()
+" "+inventory[i].getPrice());
cnt++;
}
}
if(cnt==0)
{
System.out.println("NO item with quantity less than 5");
}
}
void totalInventoryValue(Item[] inventory)
{
int size,cnt=0;
size =inventory.length;
double totalValue=0;
for(int i=0;i<size;i++)
{
totalValue=totalValue+(inventory[i].getPrice()*inventory[i].getquantity());
}
System.out.println("Total inventory value is : "+totalValue);
}
}
-------------------------------------------------------------------------------------------------------------------------------------------
Compute.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
class Compute
{
public static void main(String[] args) throws FileNotFoundException
{
String line;
String[] temp;
File inpfile = new File("input.txt");
Scanner sc = new Scanner(inpfile);
Scanner sc1 = new Scanner(System.in);
Item[] itms = new Item[10];
Inventory iv = new Inventory();
for(int i=0;i<10;i++)
{
itms[i] = new Item();
itms[i].setName(sc.next());
itms[i].setQuantity(sc.nextInt());
itms[i].setPrice(sc.nextDouble());
}
while(true)
{
System.out.println("Enter choice an option :");
switch(sc1.next().charAt(0))
{
case 'p':iv.printInventory(itms);
break;
case 'c':iv.checkLowInventory(itms);
break;
case 'v':iv.totalInventoryValue(itms);
break;
case 'e':System.exit(0);
}
}
}
}
Explanation / Answer
Item.java
//Class definition
class Item
{
//variable declaration
String name;
int quantity;
double price;
//default constructor
Item()
{
}
//constructor with three parameters
Item(String name,int quantity,double price)
{
//initialize the parameters.
this.name=name;
this.quantity=quantity;
this.price = price;
}
//set the name
void setName(String name)
{
this.name=name;
}
//get the name
String getName()
{
return name;
}
//set the quantity
void setQuantity(int quantity)
{
this.quantity=quantity;
}
//get the quantity
int getquantity()
{
return quantity;
}
//set the price
void setPrice(double price)
{
this.price=price;
}
//get the price
double getPrice()
{
return price;
}
}
//class definition
class Inventory
{
//this method is used to display the output to console
void printInventory(Item[] inventory)
{
int size;
size = inventory.length;
for(int i=0;i<size;i++)
{
System.out.println(inventory[i].getName()+" "+inventory[i].getquantity()
+" "+inventory[i].getPrice());
}
}
//this method is used to check the low inventory
void checkLowInventory(Item[] inventory)
{
int size,cnt=0;
size =inventory.length;
for(int i=0;i<size;i++)
{
if(inventory[i].getquantity()<5)
{
System.out.println(inventory[i].getName()+" "+inventory[i].getquantity()
+" "+inventory[i].getPrice());
cnt++;
}
}
if(cnt==0)
{
System.out.println("NO item with quantity less than 5");
}
}
//this method is used to display the total inventory.
void totalInventoryValue(Item[] inventory)
{
int size;
size =inventory.length;
double totalValue=0;
for(int i=0;i<size;i++)
{
totalValue=totalValue+(inventory[i].getPrice()*inventory[i].getquantity());
}
System.out.println("Total inventory value is : "+totalValue);
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.NoSuchElementException;
import java.util.Scanner;
class Compute
{
public static void main(String[] args) throws FileNotFoundException
{
try
{
File inpfile = new File("input.txt");
Scanner sc = new Scanner(inpfile);
Scanner sc1 = new Scanner(System.in);
Item[] itms = new Item[10];
Inventory iv = new Inventory();
int i=0;
//Item it;
while(sc.hasNextLine())
{
itms[i] = new Item();
itms[i].setName(sc.next());
itms[i].setQuantity(sc.nextInt());
itms[i].setPrice(sc.nextDouble());
i++;
}
while(true)
{
System.out.println("Enter choice an option :");
switch(sc1.next().charAt(0))
{
case 'p':iv.printInventory(itms);
break;
case 'c':iv.checkLowInventory(itms);
break;
case 'v':iv.totalInventoryValue(itms);
break;
case 'e':System.exit(0);
}
}
}catch (NoSuchElementException e) {
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.