In: Computer Science
Please implement Sample string toString()method for each class and return itself a string, not the output. import java.util.ArrayList; public class Customer extends User{ private ArrayList orders; public Customer(String display_name, String password, String email) { super(display_name, password, email); } @Override public String getPermissionLevel() { return "CUSTOMER"; } public void addOrder(Order order){ this.orders.add(order); } public ArrayList listOrders(){ return this.orders; }; }
----------------
public class ElectronicProduct extends Product{ private long SNo; private String warranty_period; public ElectronicProduct(long SNo, String warranty_period, String productId, String productName, String brandName, String Description) { super(productId, productName, brandName, Description); this.SNo = SNo; this.warranty_period = warranty_period; } }
--------------
public class HomeProduct extends Product{ String location; public HomeProduct(String location, String productId, String productName, String brandName, String Description) { super(productId, productName, brandName, Description); this.location = location; } }
----------------------
import java.util.ArrayList; import java.util.Date; public class Order { private int order_no; private ArrayList products; private String order_Status; private Date final_date; public Order(int order_no, String order_Status, Date final_date) { this.order_no = order_no; this.order_Status = order_Status; this.final_date = final_date; this.products = new ArrayList<>(); } public Date getFinal_date() { return final_date; } public String getOrder_Status() { return order_Status; } public int getOrder_no() { return order_no; } public void setFinal_date(Date final_date) { this.final_date = final_date; } public void setOrder_Status(String order_Status) { this.order_Status = order_Status; } public void setOrder_no(int order_no) { this.order_no = order_no; } public ArrayList getProducts(){ return this.products; } public void addProduct(Product product){ this.products.add(product); } }
------------------------------------------------
import java.time.LocalDate; import java.util.ArrayList; import java.util.Date; public class Product { private String productId; private String productName; private String brandName; private String description; private ArrayList categories; private LocalDate dateCatalog; public Product(String productId, String productName, String brandName, String description) { this.productId = productId; this.productName = productName; this.brandName = brandName; this.description = description; this.dateCatalog = LocalDate.now(); this.categories = new ArrayList<>(); } public String getProductId() { return productId; } public String getDescription() { return description; } public String getProductName() { return productName; } public void addCategory(Category category){ this.categories.add(category); } public ArrayList listCategories(){ return this.categories; } }
--------
-----------------------------------------
I have added toString method for each class.
Full Java Code :
import java.util.ArrayList;
public class Customer extends User{
private ArrayList orders;
public Customer(String display_name, String password, String email) {
super(display_name, password, email);
}
//sample toString method for Customer class
public String toString()
{
return "this is toString method of Customer class & " +display_name + " " + password + " " + email;
}
@Override
public String getPermissionLevel() {
return "CUSTOMER";
}
public void addOrder(Order order){
this.orders.add(order);
}
public ArrayList listOrders(){
return this.orders;
};
}
public class ElectronicProduct extends Product{
private long SNo;
private String warranty_period;
public ElectronicProduct(long SNo, String warranty_period, String productId, String productName, String brandName, String Description) {
super(productId, productName, brandName, Description);
this.SNo = SNo;
this.warranty_period = warranty_period;
}
//tostring method for ElectronicProduct Class
public String toString()
{
return productId + " " + productName + " "+ brandName + " " + description + " " +SNo + " " +warranty_period;
}
}
--------------
public class HomeProduct extends Product{
String location;
public HomeProduct(String location, String productId, String productName, String brandName, String Description) {
super(productId, productName, brandName, Description);
this.location = location;
}
//tostring method for HomeProduct Class
public String toString()
{
return productId + " " + productName + " "+ brandName + " " + description + " " +location;
}
}
----------------------
import java.util.ArrayList;
import java.util.Date;
public class Order {
private int order_no;
private ArrayList products;
private String order_Status;
private Date final_date;
public Order(int order_no, String order_Status, Date final_date) {
this.order_no = order_no;
this.order_Status = order_Status;
this.final_date = final_date;
this.products = new ArrayList<>();
}
//sample toString method for Oder class
public String toString()
{
return order_no + " " + products + " " + order_Status + " " + final_date ;
}
public Date getFinal_date() {
return final_date;
}
public String getOrder_Status() {
return order_Status;
}
public int getOrder_no() {
return order_no;
}
public void setFinal_date(Date final_date) {
this.final_date = final_date;
}
public void setOrder_Status(String order_Status) {
this.order_Status = order_Status;
}
public void setOrder_no(int order_no) {
this.order_no = order_no;
}
public ArrayList getProducts(){
return this.products;
}
public void addProduct(Product product){
this.products.add(product);
}
}
------------------------------------------------
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
public class Product {
private String productId;
private String productName;
private String brandName;
private String description;
private ArrayList categories;
private LocalDate dateCatalog;
public Product(String productId, String productName, String brandName, String description) {
this.productId = productId;
this.productName = productName;
this.brandName = brandName;
this.description = description;
this.dateCatalog = LocalDate.now();
this.categories = new ArrayList<>();
}
//tostring method for Product Class
public String toString()
{
return productId + " " + productName + " "+ brandName + " " + description + " " + dateCatalog + " " +categories;
}
public String getProductId() {
return productId;
}
public String getDescription() {
return description;
}
public String getProductName() {
return productName;
}
public void addCategory(Category category){
this.categories.add(category);
}
public ArrayList listCategories(){
return this.categories;
}
}
--------