Having trouble writing the main class, at the bottom is my RetailItem class. The
ID: 3566445 • Letter: H
Question
Having trouble writing the main class, at the bottom is my RetailItem class.
There is a file that contains 5148 items, on seperate lines in the file are: description, price, quantity in stock and UPC(universal product code) in that order for each item.
Write a JAVA program that reads the content of the file, creates instances of your retail item and stores them in the array. Prompt the user for the items they want to buy by UPC. If an item matching that UPC is found and available, display all data about the item, decrease quantity in stock by one in the list, and add the price to the total cost. If the user enters a UPC that does not exist, display an appropriate message. If the user enters a UPC and the item is out of stock, display an appropriate message. (Do not charge them for items that are not available.)
When the user is finished entering the UPCs for all the items they want to buy, display their total cost for all items selected and available.
Executions should have at least 5 items for each customer.
private String description;
private double price;
private int inStock;
private int upc;
public RetailItem() {
}
public RetailItem(String newDescription, double newPrice, int newInStock, int newUpc) {
description = newDescription;
price = newPrice;
inStock = newInStock;
upc = newUpc;
}
public String getDescription() {
return description;
}
public double getPrice() {
return Math.abs(price);
}
public int getInStock() {
return Math.abs(inStock);
}
public int getUpc() {
return upc;
}
public void setD(String newDescription) {
description = newDescription;
}
public void setP(double newPrice) {
price = newPrice;
}
public void setI(int newInStock) {
inStock = newInStock;
}
public void setU(int newUpc) {
upc = newUpc;
}
public String toString() {
return " " +description + " Price: " + price + " Instock: " +
inStock + " UPC: " + upc;
}
}
Explanation / Answer
// Assuming below since the question didn't mention it clearly
//============================================================
// Its a text file
// File name is passed as command line arguement
// File format is one order per line. Each line has comma seperated list of
// values like this -> "desc,price,stock,puc"
//using do while loop for user to buy items, if the number of times user purchases is fixed, you can use for loop
//I have written a main method, include it in this class or any other class or create a method for the same
public static void main(String args[]) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(args[0]));
String line = null;
RetailItem[] retailItem = new RetailItem[5148];
int i = 0;
StringBuffer desc;
double price;
int stock;
int puc;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
desc = new StringBuffer(parts[0]);
price = Double.parseDouble(parts[1]);
stock = Integer.parseInt(parts[2]);
puc = Integer.parseInt(parts[3]);
retailItem[i] = new RetailItem(desc.toString(), price, stock, puc);
i++;
}
Scanner sc = new Scanner(System.in);
String nextItem;
double totalPrice = 0;
boolean gotUPC = false;
do {
System.out.println("Enter UPC of the item you wish to buy:");
int userUPC = sc.nextInt();
for (RetailItem ri : retailItem) {
if (ri.getUpc() == userUPC) {
gotUPC = true;
if (ri.getInStock() > 1) {
System.out.println("Item found");
System.out.println("UPC:" + ri.getUpc());
System.out.println("Description:" + ri.getDescription());
System.out.println("Price:" + ri.getPrice());
ri.setI(ri.getInStock() - 1);
totalPrice = totalPrice + ri.getPrice();
break;
}else{
System.out.println("Item out of stock");
break;
}
}
}
if (gotUPC == false) {
System.out.println("Item not availlable");
}
System.out.println("Enter Y to buy more items");
nextItem = sc.next();
} while (nextItem.equalsIgnoreCase("Y"));
System.out.println("Total cost is:"+totalPrice);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.