Writing a java class named inventory class with array list method ( the class ma
ID: 3837383 • Letter: W
Question
Writing a java class named inventory class with array list method
( the class manages the number of cars in the showroom )
Totalcars sold
Totalsales
Newcars
Refil
i dont know how to make the class need help with this
//
public class InventoryItem {
private int NewCars;
private String Refill;
private double Totalsales;
private int TotCarsSold;
public InventoryItem (int NewCars, String Refill, double Totalsales, int TotCarsSold) {
this.NewCars = NewCars;
this.Refill = Refill;
this.Totalsales = Totalsales;
this.TotCarsSold = TotCarsSold;
}
public int getNewCars() {
return NewCars;
}
public String getRefill () {
return Refill;
}
public void setTotalsales (double Totalsales) {
this.Totalsales = Totalsales;
}
public double getTotalsales () {
return Totalsales;
}
public void setTotCarsSold(int TotCarsSold) {
this.TotCarsSold = TotCarsSold;
}
public int getTotCarsSold() {
return TotCarsSold;
}
@Override
public String toString() {
return String.format("[%d ,%s ,%1.2f ,%d]", NewCars, Refill, Totalsales, TotCarsSold);
}
public static void main(String[] args) {
InventoryItem Refill = new InventoryItem(1, "asdf", 2.4, 5);
System.out.println(Refill);
}
}
Warehouse.java-
InventoryItem.java -
public class InventoryItem {
private int NewCars;
private String Refill;
private double Totalsales;
private int TotCarsSold;
public InventoryItem (int NewCars, String Refill, double Totalsales, int TotCarsSold) {
this.NewCars = NewCars;
this.Refill = Refill;
this.Totalsales = Totalsales;
this.TotCarsSold = TotCarsSold;
}
public int getNewCars() {
return NewCars;
}
public String getRefill () {
return Refill;
}
public void setTotalsales (double Totalsales) {
this.Totalsales = Totalsales;
}
public double getTotalsales () {
return Totalsales;
}
public void setTotCarsSold(int TotCarsSold) {
this.TotCarsSold = TotCarsSold;
}
public int getTotCarsSold() {
return TotCarsSold;
}
@Override
public String toString() {
return String.format("[%d ,%s ,%1.2f ,%d]", NewCars, Refill, Totalsales, TotCarsSold);
}
public static void main(String[] args) {
InventoryItem Refill = new InventoryItem(1, "asdf", 2.4, 5);
System.out.println(Refill);
}
}
Warehouse.java
import java.util.ArrayList;
import java.util.List;
public class Warehouse {
private List<InventoryItem> inventory = new ArrayList<InventoryItem>();
public void addItem(InventoryItem i) {
inventory.add(i);
}
public static void main(String [] args) {
// create a Warehouse instance
Warehouse warehouse = new Warehouse();
// create an InventoryItem instance
int NewCars= 111;
String Refill = "Y";
double Totalsales= 120000.0;
int TotCarsSold= 2000;
InventoryItem i = new InventoryItem(sku, itemName, price, stock);
// add the InventoryItem to the Warehouse
warehouse.addItem(i);
}
}
Explanation / Answer
I have re-designed your program for creating a java class named inventory class with array list method. I have added the comments for each part of the code and the final output of the class inventory.
Let me explain you in a very simple step-by-step manner:-
Step-1:
For creating a class, You should know few basic criteria before writing a class.
Firstly, The class name should start with the Upper Case. Here, The public is used because it is accessible to any classes.
Syntax:- public class InventoryItem
Step-2:
Next important part of java class program is the use of Main() method.
Syntax:- public static void main(String args[])
The main() method is the main part of java program and without the main() method you can't execute a single line of code in java.
Step-3:
To make use of ArrayList<> into the inventory class, This can be achieved once we import the files of ArrayList and The List collections from the util library.
Syntax:-
Inventory Class Program:-
// created a class called "Inventory"
public class Inventory {
// Make use of ArrayList as displaying the showroom list items
private final List<TotCarsSold> showroom = new ArrayList<>();
public boolean addCars( TotCarsSold NewCars ) {
showroom .add( NewCars );
return true;
}
// This is the main part of the java program
public static void main( String[] args ) {
final Showroom showroom = new Showroom();
final TotCarsSold v1 = new TotCarsSold( "150", "Large", "2500.00", "241" );
showroom.addVehicle( v1 );
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.