In: Computer Science
Java
Part 2 of 4 - Amusement Park Programming Project
MUST BE COMPATIBLE WITH PART 1
https://www.chegg.com/homework-help/questions-and-answers/java-part-1-4-amusement-park-programming-project-requirements-use-java-selection-construct-q40170145?trackid=ERIFssNL
Requirements:
Class:
Merchandise – models merchandise available in the gift shop such as t-shirts, sweatshirts, and stuffed animals.
Instance Fields:
Constructors and Methods:
If you have any problem with the code feel free to comment.
Program
class Merchandise {
//instance variables of the class
private long id;
private String category, description;
private double price;
private boolean inStock;
//parameterzied constructor
public Merchandise(long id, String category, String
description, double price, boolean inStock) {
this.id = id;
this.category = category;
this.description =
description;
this.inStock = inStock;
//error checking
//price cannot be a negetive
number
if(price <0) {
System.err.println("Price cannot be a negetive, default set it to
zero");
this.price =
0;
}
else
this.price =
price;
}
//setting price
public void setPrice(double price) {
if(price <0) {
System.err.println("Price cannot be a negetive, default set it to
zero");
this.price =
0;
}
else
this.price =
price;
}
//all the getters
public long getId() {
return id;
}
public String getCategory() {
return category;
}
public String getDescription() {
return description;
}
public double getPrice() {
return price;
}
public boolean isInStock() {
return inStock;
}
@Override//detailed description of the object
public String toString() {
String str1, str2, str3, str4,
str5;
str1 = "\nID: "+id+"\n";
str2 = "Category:
"+category+"\n";
str3 = "Description:
"+description+"\n";
str4 = "Price: $"+price+"\n";
str5 = "Is in Stock?
"+inStock+"\n";
return
str1+str2+str3+str4+str5;
}
}
public class Test {//driver class for testing
public static void main(String[] args) {
Merchandise obj = new
Merchandise(12145741, "Toys", "Its the Hotwheels Batmobile", 50.23,
true);
System.out.println(obj);
}
}
Output