Many Pro
Many Pro
// Fields
protected String name;
protected int power;
// Default constructor
public Speaker() {
this.name = "";
this.power = 0;
}
// Parameterized constructor
public Speaker(String name, int power) {
this.name = name;
this.power = power;
}
// Default constructor
public SpecSpeaker() {
super();
this.sound = "";
}
// Parameterized constructor
public SpecSpeaker(String name, int power, String sound) {
super(name, power);
this.sound = sound;
}
// Override toString() method to return the string format: name, sound, power
@Override
public String toString() {
return name + ", " + sound + ", " + power;
}
// Method to remove the first letter of the name string
public void setData() {
if (name != null && name.length() > 0) {
name = name.substring(1);
}
}
// Test toString()
System.out.println("1. Test toString()");
System.out.println("OUTPUT:");
System.out.println(specSpeaker.toString());
// Test setData()
System.out.println("2. Test setData()");
specSpeaker.setData();
System.out.println("OUTPUT:");
System.out.println(specSpeaker.toString());
// Test getValue()
System.out.println("3. Test getValue()");
System.out.println("OUTPUT:");
System.out.println(specSpeaker.getValue());
scanner.close();
}
}
-----------------------------------------------------------
class Tofu {
// Fields
private String maker;
private int quantity;
// Default constructor
public Tofu() {
this.maker = "";
this.quantity = 0;
}
// Parameterized constructor
public Tofu(String maker, int quantity) {
this.maker = maker;
this.quantity = quantity;
}
// Method to get maker string with first and last letters lowercase
public String getMaker() {
if (maker.length() < 2) {
return maker.toLowerCase();
}
return maker.substring(0, 1).toLowerCase() + maker.substring(maker.length()
- 1).toLowerCase();
}
// Test getMaker()
System.out.println("1. Test getMaker()");
System.out.println("OUTPUT:");
System.out.println(tofu.getMaker());
// Test setQuantity()
System.out.println("2. Test setQuantity()");
System.out.println("Enter TC (1 or 2): ");
int tc = scanner.nextInt();
if (tc == 2) {
System.out.print("Enter new quantity: ");
int newQuantity = scanner.nextInt();
tofu.setQuantity(newQuantity);
System.out.println("OUTPUT:");
System.out.println(tofu.getQuantity());
} else {
System.out.println("OUTPUT:");
System.out.println(tofu.getQuantity());
}
scanner.close();
}
}
----------------------------------------------------------
// Default constructor
public Medicine() {
}
// Parameterized constructor
public Medicine(String name, String indication, int expirationYear) {
this.name = name;
this.indication = indication;
this.expirationYear = expirationYear;
}
// toString method
@Override
public String toString() {
return "Name: " + name + ", Indication: " + getIndication() + ", Expiration
Year: " + expirationYear;
}
}
// Default constructor
public PrescriptionMedicine() {
super();
}
// Parameterized constructor
public PrescriptionMedicine(String name, String indication, int expirationYear,
String doctorName) {
super(name, indication, expirationYear);
this.doctorName = doctorName;
}
// isExpired method
public String isExpired(int currentYear) {
return currentYear > getExpirationYear() ? "Expired" : "Valid";
}
// toString method
@Override
public String toString() {
return super.toString() + ", Doctor Name: " + doctorName.substring(0,
1).toUpperCase() + doctorName.substring(1).toLowerCase();
}
}
---------------------------------------------------------
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
class Furniture {
private int id;
private String name;
private int quantity;
// Default constructor
public Furniture() {}
// Parameterized constructor
public Furniture(int id, String name, int quantity) {
this.id = id;
this.name = name;
this.quantity = quantity;
}
// Setters
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
// Getters
public int getId() {
return id;
}
furnitureList.addFurniture(chair);
furnitureList.addFurniture(table);
furnitureList.addFurniture(sofa);
----------------------------------------------------------
// Default constructor
public Bike() {
}
// Parameterized constructor
public Bike(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
----------------------------------
// Default constructor
public Bike() {
}
// Parameterized constructor
public Bike(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
// Getter for id
public int getId() {
return id;
}
------------------------------------------------------
import java.util.ArrayList;
import java.util.List;
// Constructor
public BikeManager() {
this.bikes = new ArrayList<>();
}
------------------------------
// Default constructor
public Employee() {
}
// Parameterized constructor
public Employee(int id, String name, double salary) {
this.id = id;
this.name = name;
this.salary = salary;
}
// Getter for id
public int getId() {
return id;
}
-----------------------------------------
import java.util.ArrayList;
import java.util.List;
// Constructor
public EmployeeManager() {
this.employees = new ArrayList<>();
}
--------------------------------
// Constructors
public Product() {}
@Override
public float value() {
return (float)(price * quantity);
}
@Override
public float vat() {
return value() * 0.1f; // assuming VAT is 10%
}
@Override
public float promotion() {
return hasPromotion ? value() * 0.9f : value(); // assuming 10% discount
if on promotion
}
@Override
public String toString() {
return String.format("Product[Code=%s, Name=%s, Supplier=%s, Unit=%s,
Price=%.2f, Quantity=%.2f, Promotion=%s]",
productCode, productName, supplier, unit, price, quantity, hasPromotion
? "Yes" : "No");
}
}
import java.io.*;
import java.util.*;
import java.util.stream.Collectors;
public Products() {
this.productList = new ArrayList<>();
}
import java.util.Scanner;
while (true) {
System.out.println("1 – Add product information");
System.out.println("2 – Show all products");
System.out.println("3 – Delete a product");
System.out.println("4 – Filter products by SUPPLIER");
System.out.println("5 – Sort and display product");
System.out.println("5.1 – Sort by name (ASC)");
System.out.println("5.2 – Sort by value (DES)");
System.out.println("6 – Statistics the products by SUPPLIER");
System.out.println("7 – Save data to file");
System.out.println("8 – Load data from file");
System.out.println("9 – Exit program");
switch (choice) {
case 1:
System.out.println("Enter product code:");
String code = scanner.nextLine();
System.out.println("Enter product name:");
String name = scanner.nextLine();
System.out.println("Enter supplier:");
String supplier = scanner.nextLine();
System.out.println("Enter unit:");
String unit = scanner.nextLine();
System.out.println("Enter price:");
double price = scanner.nextDouble();
System.out.println("Enter quantity:");
float quantity = scanner.nextFloat();
System.out.println("Has promotion? (true/false):");
boolean hasPromotion = scanner.nextBoolean();
products.addProduct(new Product(code, name, supplier, unit,
price, quantity, hasPromotion));
break;
case 2:
products.showAll();
break;
case 3:
System.out.println("Enter product code to delete:");
String deleteCode = scanner.nextLine();
products.deleteProduct(deleteCode);
break;
case 4:
System.out.println("Enter supplier:");
String filterSupplier = scanner.nextLine();
products.filterBySupplier(filterSupplier).forEach(System.out::println);
break;
case 5:
System.out.println("1 – Sort by name (ASC)");
System.out.println("2 – Sort by value (DES)");
int sortChoice = scanner.nextInt();
if (sortChoice == 1) {
products.sortByName();
} else if (sortChoice == 2) {
products.sortByValue();
}
products.showAll();
break;
case 6:
System.out.println("Enter supplier for statistics:");
String statSupplier = scanner.nextLine();
List<Product> filteredProducts =
products.filterBySupplier(statSupplier);
double totalValue =
filteredProducts.stream().mapToDouble(Product::value).sum();
System.out.println("Total value for supplier " + statSupplier +
": " + totalValue);
break;
case 7:
products.save();
break;
case 8:
products.loadData();
break;
case 9:
scanner.close();
System.exit(0);
break;
default:
System.out.println("Invalid choice");
}
}
}
}