0% found this document useful (0 votes)
14 views16 pages

Many Pro

Uploaded by

enghieu32
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views16 pages

Many Pro

Uploaded by

enghieu32
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 16

class Speaker {

// 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;
}

// Method to get the name


public String getName() {
return name;
}

// Method to get the power


public int getPower() {
return power;
}

// Method to set the name


public void setName(String name) {
this.name = name;
}

// Method to return the string format: name, power


public String toString() {
return name + ", " + power;
}
}

class SpecSpeaker extends Speaker {


// Field
private String sound;

// 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);
}
}

// Method to return sound+power if power > 10, otherwise return sound


public String getValue() {
if (power > 10) {
return sound + power;
} else {
return sound;
}
}

public static void main(String[] args) {


java.util.Scanner scanner = new java.util.Scanner(System.in);

// Enter name, power and sound for SpecSpeaker


System.out.print("Enter name: ");
String name = scanner.nextLine();
System.out.print("Enter power: ");
int power = scanner.nextInt();
scanner.nextLine(); // consume the newline
System.out.print("Enter sound: ");
String sound = scanner.nextLine();

// Create SpecSpeaker object


SpecSpeaker specSpeaker = new SpecSpeaker(name, power, sound);

// 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();
}

// Method to get quantity


public int getQuantity() {
return quantity;
}

// Method to set quantity


public void setQuantity(int quantity) {
this.quantity = quantity;
}

public static void main(String[] args) {


java.util.Scanner scanner = new java.util.Scanner(System.in);

// Enter maker and quantity for Tofu


System.out.print("Enter maker: ");
String maker = scanner.nextLine();
System.out.print("Enter quantity: ");
int quantity = scanner.nextInt();

// Create Tofu object


Tofu tofu = new Tofu(maker, quantity);

// 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();
}
}

----------------------------------------------------------

public class Medicine {


private String name;
private String indication;
private int expirationYear;

// Default constructor
public Medicine() {
}

// Parameterized constructor
public Medicine(String name, String indication, int expirationYear) {
this.name = name;
this.indication = indication;
this.expirationYear = expirationYear;
}

// Getter for name


public String getName() {
return name;
}

// Getter for indication


public String getIndication() {
return indication.substring(0, 1).toUpperCase() +
indication.substring(1).toLowerCase();
}

// Getter for expirationYear


public int getExpirationYear() {
return expirationYear;
}

// toString method
@Override
public String toString() {
return "Name: " + name + ", Indication: " + getIndication() + ", Expiration
Year: " + expirationYear;
}
}

extends public class PrescriptionMedicine extends Medicine {


private String doctorName;

// Default constructor
public PrescriptionMedicine() {
super();
}

// Parameterized constructor
public PrescriptionMedicine(String name, String indication, int expirationYear,
String doctorName) {
super(name, indication, expirationYear);
this.doctorName = doctorName;
}

// Getter for doctorName


public String getDoctorName() {
return doctorName;
}

// Setter for doctorName


public void setDoctorName(String doctorName) {
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;
}

public void setQuantity(int quantity) {


this.quantity = quantity;
}

// Getters
public int getId() {
return id;
}

public String getName() {


return name.toUpperCase();
}

public int getQuantity() {


return quantity;
}

// Override toString method


@Override
public String toString() {
return "Furniture{" +
"id=" + id +
", name='" + name + '\'' +
", quantity=" + quantity +
'}';
}
}

class FurnitureList extends ArrayList<Furniture> {

// Add a new Furniture to the collection


public void addFurniture(Furniture furniture) {
this.add(furniture);
}

// Return the name in uppercase of the furniture by id


public String getNameById(int id) {
for (Furniture furniture : this) {
if (furniture.getId() == id) {
return furniture.getName();
}
}
return "N/A";
}

// Return the furniture list after sorting in descending order by quantity


public FurnitureList getFurnitureList() {
FurnitureList sortedList = new FurnitureList();
sortedList.addAll(this);
Collections.sort(sortedList, new Comparator<Furniture>() {
@Override
public int compare(Furniture f1, Furniture f2) {
return Integer.compare(f2.getQuantity(), f1.getQuantity());
}
});
return sortedList;
}

// Return total quantity of the furniture list


public int getTotalQuantity() {
int totalQuantity = 0;
for (Furniture furniture : this) {
totalQuantity += furniture.getQuantity();
}
return totalQuantity;
}

// Test the implementation


public static void main(String[] args) {
FurnitureList furnitureList = new FurnitureList();

Furniture chair = new Furniture(1, "Chair", 10);


Furniture table = new Furniture(2, "Table", 5);
Furniture sofa = new Furniture(3, "Sofa", 3);

furnitureList.addFurniture(chair);
furnitureList.addFurniture(table);
furnitureList.addFurniture(sofa);

System.out.println("Name by ID 1: " + furnitureList.getNameById(1)); //


Output: CHAIR
System.out.println("Name by ID 4: " + furnitureList.getNameById(4)); //
Output: N/A

System.out.println("Total Quantity: " +


furnitureList.getTotalQuantity()); // Output: 18

FurnitureList sortedFurnitureList = furnitureList.getFurnitureList();


System.out.println("Sorted Furniture List by Quantity:");
for (Furniture furniture : sortedFurnitureList) {
System.out.println(furniture);
}
}
}

----------------------------------------------------------

public class Bike {


private int id;
private String name;
private double price;

// Default constructor
public Bike() {
}

// Parameterized constructor
public Bike(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}

// Method to return the bike name in uppercase


public String getName() {
return this.name.toUpperCase();
}

// Method to increase the price by 10%


public double getSalePrice() {
return this.price * 1.10;
}

// Overridden toString method to return the bike details


@Override
public String toString() {
return "Bike ID: " + this.id + ", Name: " + this.name + ", Price: " +
String.format("%.2f", this.price);
}

// Main method to test the class


public static void main(String[] args) {
Bike bike1 = new Bike(1, "MountainBike", 200.00);
System.out.println(bike1); // Outputs: Bike ID: 1, Name: MountainBike,
Price: 200.00
System.out.println("Bike Name: " + bike1.getName()); // Outputs: Bike Name:
MOUNTAINBIKE
System.out.println("Sale Price: " + bike1.getSalePrice()); // Outputs: Sale
Price: 220.00
}
}

----------------------------------

public class Bike {


private int id;
private String name;
private double price;

// Default constructor
public Bike() {
}

// Parameterized constructor
public Bike(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}

// Method to return the bike name in uppercase


public String getName() {
return this.name.toUpperCase();
}

// Method to increase the price by 10%


public double getSalePrice() {
return this.price * 1.10;
}

// Overridden toString method to return the bike details


@Override
public String toString() {
return "Bike ID: " + this.id + ", Name: " + this.name + ", Price: " +
String.format("%.2f", this.price);
}

// Getter for id
public int getId() {
return id;
}

// Getter for name


public String getNameLowerCase() {
return name;
}
}

------------------------------------------------------

import java.util.ArrayList;
import java.util.List;

public class BikeManager {


private List<Bike> bikes;

// Constructor
public BikeManager() {
this.bikes = new ArrayList<>();
}

// Method to add a bike to the list


public void addBike(Bike bike) {
this.bikes.add(bike);
}

// Method to get the bike name by id


public String getBikeNameById(int id) {
for (Bike bike : bikes) {
if (bike.getId() == id) {
return bike.getName();
}
}
return "Bike not found";
}

// Main method to test the functionality


public static void main(String[] args) {
BikeManager manager = new BikeManager();
manager.addBike(new Bike(1, "MountainBike", 200.00));
manager.addBike(new Bike(2, "RoadBike", 150.00));

// Testing getBikeNameById method


System.out.println("Name of bike with ID 1: " +
manager.getBikeNameById(1)); // Outputs: MOUNTAINBIKE
System.out.println("Name of bike with ID 2: " +
manager.getBikeNameById(2)); // Outputs: ROADBIKE
System.out.println("Name of bike with ID 3: " +
manager.getBikeNameById(3)); // Outputs: Bike not found
}
}

------------------------------

public class Employee {


private int id;
private String name;
private double salary;

// 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;
}

// Getter for name


public String getName() {
return this.name.toUpperCase();
}

// Setter for name


public void setName(String name) {
this.name = name;
}

// Getter for salary


public double getSalary() {
return salary;
}

// Setter for salary


public void setSalary(double salary) {
this.salary = salary;
}

// Overridden toString method to return employee details


@Override
public String toString() {
return "Employee ID: " + this.id + ", Name: " + this.name.toUpperCase() +
", Salary: " + String.format("%.2f", this.salary);
}
}

-----------------------------------------

import java.util.ArrayList;
import java.util.List;

public class EmployeeManager {


private List<Employee> employees;

// Constructor
public EmployeeManager() {
this.employees = new ArrayList<>();
}

// Method to add an employee to the list


public void addEmployee(Employee employee) {
this.employees.add(employee);
}

// Method to get the employee name by id


public String getNameById(int id) {
for (Employee employee : employees) {
if (employee.getId() == id) {
return employee.getName();
}
}
return "Employee not found";
}

// Method to get the employee with the highest salary


public Employee getEmployeeWithMaxSalary() {
if (employees.isEmpty()) {
return null;
}

Employee maxSalaryEmployee = employees.get(0);


for (Employee employee : employees) {
if (employee.getSalary() > maxSalaryEmployee.getSalary()) {
maxSalaryEmployee = employee;
}
}
return maxSalaryEmployee;
}

// Main method to test the functionality


public static void main(String[] args) {
EmployeeManager manager = new EmployeeManager();
manager.addEmployee(new Employee(1, "Alice", 50000.00));
manager.addEmployee(new Employee(2, "Bob", 60000.00));
manager.addEmployee(new Employee(3, "Charlie", 55000.00));

// Testing getNameById method


System.out.println("Name of employee with ID 1: " +
manager.getNameById(1)); // Outputs: ALICE
System.out.println("Name of employee with ID 2: " +
manager.getNameById(2)); // Outputs: BOB
System.out.println("Name of employee with ID 4: " +
manager.getNameById(4)); // Outputs: Employee not found

// Testing getEmployeeWithMaxSalary method


Employee maxSalaryEmployee = manager.getEmployeeWithMaxSalary();
if (maxSalaryEmployee != null) {
System.out.println("Employee with highest salary: " +
maxSalaryEmployee); // Outputs: Employee with highest salary: Employee ID: 2, Name:
BOB, Salary: 60000.00
} else {
System.out.println("No employees in the list.");
}
}
}

--------------------------------

public interface Valuable {


float value();
float vat();
float promotion();
}

public class Product implements Valuable {


private String productCode;
private String productName;
private String supplier;
private String unit;
private double price;
private float quantity;
private boolean hasPromotion;

// Constructors
public Product() {}

public Product(String productCode, String productName, String supplier, String


unit, double price, float quantity, boolean hasPromotion) {
this.productCode = productCode;
this.productName = productName;
this.supplier = supplier;
this.unit = unit;
this.price = price;
this.quantity = quantity;
this.hasPromotion = hasPromotion;
}

// Getters and Setters


public String getProductCode() {
return productCode;
}

public void setProductCode(String productCode) {


this.productCode = productCode;
}

public String getProductName() {


return productName;
}

public void setProductName(String productName) {


this.productName = productName;
}

public String getSupplier() {


return supplier;
}
public void setSupplier(String supplier) {
this.supplier = supplier;
}

public String getUnit() {


return unit;
}

public void setUnit(String unit) {


this.unit = unit;
}

public double getPrice() {


return price;
}

public void setPrice(double price) {


this.price = price;
}

public float getQuantity() {


return quantity;
}

public void setQuantity(float quantity) {


this.quantity = quantity;
}

public boolean isHasPromotion() {


return hasPromotion;
}

public void setHasPromotion(boolean hasPromotion) {


this.hasPromotion = hasPromotion;
}

@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 class Products {


private List<Product> productList;

public Products() {
this.productList = new ArrayList<>();
}

public void addProduct(Product product) {


productList.add(product);
}

public void showAll() {


productList.forEach(System.out::println);
}

public void deleteProduct(String productCode) {


productList.removeIf(product ->
product.getProductCode().equals(productCode));
}

public List<Product> filterBySupplier(String supplier) {


return productList.stream()
.filter(product -> product.getSupplier().equals(supplier))
.collect(Collectors.toList());
}

public void sortByName() {


productList.sort(Comparator.comparing(Product::getProductName));
}

public void sortByValue() {


productList.sort(Comparator.comparingDouble(Product::value).reversed());
}

public void save() {


try (ObjectOutputStream oos = new ObjectOutputStream(new
FileOutputStream("products.dat"))) {
oos.writeObject(productList);
} catch (IOException e) {
e.printStackTrace();
}
}

public void loadData() {


try (ObjectInputStream ois = new ObjectInputStream(new
FileInputStream("products.dat"))) {
productList = (List<Product>) ois.readObject();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Products products = new Products();
Scanner scanner = new Scanner(System.in);

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");

int choice = scanner.nextInt();


scanner.nextLine(); // consume newline

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");
}
}
}
}

You might also like