Các bạn có thể giúp mình chia cái code này ra thành 6 Classes (Printer, PrinterList, PrinterListUse, Menu, I_List(interface list), I_Menu(interface menu)) được không ạ?
Can you help me separate this code into 6 classes (Printer, PrinterList, PrinterListUse, Menu, I_List(interface list), I_Menu(interface menu)) please?
//Printer.java
public class Printer implements Comparable<Printer> {
private String modelNumber;
private String type;
private boolean isColor;
private double price;
private static int count = 0; // number of objects created used to generate the next model number
// constructor to initialize the object attributes
public Printer(String type, boolean isColor, double price)
{
count++; // increment the count
// generate the next model number
this.modelNumber = "PR"+String.format("%03d", count); // returns the count as a string with width 3left- padded by 0 (if number of digits in count < 3)
// initialize the fields
this.type = type;
this.isColor = isColor;
this.price = price;
}
// setters
public void setType(String type)
{
this.type = type;
}
public void setColored(boolean isColor)
{
this.isColor = isColor;
}
public void setPrice(double price)
{
this.price = price;
}
// getters
public String getModelNumber()
{
return modelNumber;
}
public String getType()
{
return type;
}
public boolean isColored()
{
return isColor;
}
public double getPrice()
{
return price;
}
// method that compares 2 printers based on their price and type and returns
// -1, if this printer < other printer (less price or same price and type alphabetically lower)
// 0 , if both printer are equal (same price and type)
// 1, if this printer > other printer (higher price or same price and type alphabetically higher)
@Override
public int compareTo(Printer other) {
// first compare based on price
if(price < other.price)
return -1;
else if(price > other.price)
return 1;
else
{
// same price then compare based on type
return(type.compareTo(other.type));
}
}
// return string representation of the printer
public String toString()
{
return String.format("%-20s%-30s%-10s%-10.2f",modelNumber,type,isColor,price);
}
}
//end of Printer.java
// PrinterDriver.java
import java.util.ArrayList;
import java.util.Scanner;
public class PrinterDriver {
// method that compares 2 printers based on their price and type and returns
// -1, if this printer < other printer (less price or same price and type alphabetically lower)
// 0 , if both printer are equal (same price and type)
// 1, if this printer > other printer (higher price or same price and type alphabetically higher)
@Override
public int compareTo(Printer other) {
// first compare based on price
if(price < other.price)
return -1;
else if(price > other.price)
return 1;
else
{
// same price then compare based on type
return(type.compareTo(other.type));
}
}
// return string representation of the printer
public String toString()
{
return String.format("%-20s%-30s%-10s%-10.2f",modelNumber,type,isColor,price);
}
// helper method to return the index of printer with modelNumber in the list if found else return -1
private static int getPrinterIndex(ArrayList<Printer> printerList, String modelNumber)
{
// loop over the list
for(int i=0;i<printerList.size();i++)
{
if(printerList.get(i).getModelNumber().equals(modelNumber)) // printer found
return i;
}
return -1; // printer not found
}
// helper method to sort the list of printers in descending oder of price and then descending oder of type and display the list
private static void sortAndDisplay(ArrayList<Printer> printerList)
{
if(printerList.size() == 0) // empty list
System.out.println("No printers exist");
else
{
// sort the list
for(int i=0;i<printerList.size()-1;i++)
{
for(int j=0;j<printerList.size()-1-i;j++)
{
if(printerList.get(j).compareTo(printerList.get(j+1)) < 0)
{
Printer temp = printerList.get(j);
printerList.set(j, printerList.get(j+1));
printerList.set(j+1, temp);
}
}
}
// display the sorted list
System.out.printf("%-20s%-30s%-10s%-10s\n","Model Number","Type","Colored","Price");
for(int i=0;i<printerList.size();i++)
{
System.out.println(printerList.get(i));
}
}
}
public static void main(String[] args) {
// list to store printers
ArrayList<Printer> printerList = new ArrayList<Printer>();
String modelNumber, type, coloredInput;
double price;
int choice;
Scanner scan = new Scanner(System.in);
// loop that continues till the user wants
do
{
// display the menu
System.out.println("1. Add new Printer");
System.out.println("2. Update printer");
System.out.println("3. Print the list");
System.out.println("4. Exit");
System.out.print("Enter your choice(1-4): ");
choice = scan.nextInt(); // input of choice
// validate choice and re-prompt until valid
while(choice < 1 || choice > 4)
{
System.out.println("Invalid choice. Re-enter: ");
choice = scan.nextInt();
}
scan.nextLine();
if(choice == 1) // add a new printer
{
// input the details
System.out.print("Type: ");
type = scan.nextLine();
System.out.print("Is it colored?(yes/no) ");
coloredInput = scan.nextLine();
while(!coloredInput.equalsIgnoreCase("yes") && !coloredInput.equalsIgnoreCase("no"))
{
System.out.print("Invalid input. Please enter yes or no. Re-enter: ");
coloredInput = scan.nextLine();
}
System.out.print("Price: ");
price = scan.nextDouble();
while(price <= 0)
{
System.out.print("Invalid price.Price must be greater than 0. Re-enter");
price = scan.nextDouble();
}
scan.nextLine();
// create and add the printer to the end of the list
if(coloredInput.equalsIgnoreCase("yes"))
printerList.add(new Printer( type, true, price));
else
printerList.add(new Printer( type, false, price));
System.out.println("Printer with model number: "+printerList.get(printerList.size()-1).getModelNumber()+" added successfully");
}
else if(choice == 2) // update printer
{
// input model number
System.out.print("Model Number: ");
modelNumber = scan.nextLine();
int index = getPrinterIndex(printerList, modelNumber);
// validate modelNumber is present in the list
if(index == -1)
System.out.println("No printer with model number: "+modelNumber+" exists");
else
{
// if present, input the updated details
System.out.print("Enter the updated type: ");
type = scan.nextLine();
System.out.print("Is it colored? (yes/no) ");
coloredInput = scan.nextLine();
while(!coloredInput.equalsIgnoreCase("yes") && !coloredInput.equalsIgnoreCase("no"))
{
System.out.print("Invalid input. Please enter yes or no. Re-enter: ");
coloredInput = scan.nextLine();
}
System.out.print("Updated Price: ");
price = scan.nextDouble();
while(price <= 0)
{
System.out.print("Invalid price.Price must be greater than 0. Re-enter");
price = scan.nextDouble();
}
scan.nextLine();
// update the fields of the printer
printerList.get(index).setType(type);
printerList.get(index).setPrice(price);
if(coloredInput.equalsIgnoreCase("yes"))
printerList.get(index).setColored(true);
else
printerList.get(index).setColored(false);
System.out.println("Printer with model number: "+modelNumber+" updated successfully");
}
}
else if(choice == 3) // sort and display
{
sortAndDisplay(printerList);
}
System.out.println();
}while(choice != 4);
}
}
//end of PrinterDriver.java