Write a JAVA program. Assume that a system is being developed for use in a super
ID: 3838257 • Letter: W
Question
Write a JAVA program.
Assume that a system is being developed for use in a supermarket and you are asked to keep track of items in stock (i.e. available for sale in the supermarket For this problem, first, you should write a simple but complete class that represents an individual product (e.g. "Colgate toothpaste" in the supermarket. The name of the class should be Stockltem, and: a. It should have the following attributes (instance variables): (0 a name, (O a stock code, (iii the price of the product, and (IV the total number of items for this product in stock. b. It should have the following methods: i. setName(String newName): that changes the name of the product. ii. setPrice(double newPrice): that changes the price of the product. iii. incrementstock(int N that adds N number of items for this product in stock. i. getTotalStock0: that returns the current number of items of this type in stock.Explanation / Answer
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
/**
*
*
*
* @author Sam
*
*/
class StockItem {
String name;
String stockCode;
private int price;
int totalStock;
public StockItem() {
price = 0;
totalStock = 0;
name = "";
stockCode = "";
}
public void setName(String name) {
this.name = name;
}
public void setPrice(int price) {
this.price = price;
}
void incrementStock(int N) {
totalStock = totalStock + N;
}
public int getTotalStock() {
return totalStock;
}
public int calculateTotalPrice() {
return totalStock * price;
}
}
public class SupermrketApp {
static StockItem s1, s2;
public static void main(String[] args) throws IOException {
String ch;
Scanner sc = new Scanner(new InputStreamReader(System.in));
s1 = new StockItem();
System.out.print("Enter the first item name: ");
s1.setName(sc.nextLine());
System.out.print("Enter stock code of first item: ");
s1.stockCode = sc.nextLine();
System.out.print("Enter number of first item: ");
s1.incrementStock(sc.nextInt());
System.out.print("Enter price per item: ");
s1.setPrice(sc.nextInt());
System.out.print("Would you like to add more items: ");
ch = sc.next();
while (ch.equalsIgnoreCase("Y")) {
System.out.print("Enter number of new items to add: ");
s1.incrementStock(sc.nextInt());
System.out.print("Would you like to add more items: ");
ch = sc.next();
}
System.out.println("You have " + s1.totalStock + " items of " + s1.name + " (code: " + s1.stockCode + ").");
System.out.println("Total value of the first item: " + s1.calculateTotalPrice());
System.out.println("");
sc.nextLine();
s2 = new StockItem();
System.out.print("Enter the second item name: ");
s2.setName(sc.nextLine());
System.out.print("Enter stock code of second item: ");
s2.stockCode = sc.nextLine();
System.out.print("Enter number of second item: ");
s2.incrementStock(sc.nextInt());
System.out.print("Enter price per item: ");
s2.setPrice(sc.nextInt());
System.out.print("Would you like to add more items: ");
ch = sc.next();
while (ch.equalsIgnoreCase("Y")) {
System.out.print("Enter number of new items to add: ");
s2.incrementStock(sc.nextInt());
System.out.print("Would you like to add more items: ");
ch = sc.next();
}
System.out.println("You have " + s2.totalStock + " items of " + s2.name + " (code: " + s2.stockCode + ").");
System.out.println("Total value of the second item:" + s2.calculateTotalPrice());
}
}
I hope you like the code. The is very simple so I havent commented the code. If you think that you need the code commented, please let me know if you need so. I shall update the code at the earliest. Apart from that if you need any clarification, please let me know. I shall try my best to resolve all your issues.