问题
Java Problem Statement : How to make a computer class having a list of printers available to it.
I want to have a Computer class (something like this) which fullfills the problem statement
public class Computers{
String computername;
//printerlist
public static ArrayList<String> printers=new ArrayList<String>();
// computername-Printerlist
public Map<String,ArrayList<String>> printerDB=newHashMap<String,ArrayList<String>>();
public Computers(String computername){
this.computername=computername;
// now what to put here so that each computer having a
// different computer name will have a list of printers available to it>
}
}
Example:
Computername -- List of Printers
HP -- Hp Printer, Samsung , Canon .
回答1:
First, lets define a Printer class.
package com.ggl.modeltest;
public class Printer {
private String companyName;
private String printerName;
public Printer(String companyName, String printerName) {
this.companyName = companyName;
this.printerName = printerName;
}
public String getCompanyName() {
return companyName;
}
public String getPrinterName() {
return printerName;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((companyName == null) ? 0 : companyName.hashCode());
result = prime * result
+ ((printerName == null) ? 0 : printerName.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Printer other = (Printer) obj;
if (companyName == null) {
if (other.companyName != null)
return false;
} else if (!companyName.equals(other.companyName))
return false;
if (printerName == null) {
if (other.printerName != null)
return false;
} else if (!printerName.equals(other.printerName))
return false;
return true;
}
public Printer copy() {
return new Printer(companyName, printerName);
}
}
This class has a constructor to define the values, and two getter methods to retrieve the values. Once you create an instance of this class, you can't change it.
These types of non modifiable classes make debugging simpler.
The hashCode and equals methods look pretty scary. I had Eclipse generate those methods for me. The reason that we override these methods is that we're going to use them in the Computer class.
So now, we define the Computer class.
package com.ggl.modeltest;
import java.util.ArrayList;
import java.util.List;
public class Computer {
private List<Printer> printerNames;
private String computerName;
public Computer(String computerName) {
this.computerName = computerName;
this.printerNames = new ArrayList<Printer>();
}
public void addPrinter(Printer printerName) {
this.printerNames.add(printerName);
}
public void removePrinter(Printer printerName) {
for (int i = printerNames.size() - 1; i >= 0; i--) {
if (printerNames.get(i).equals(printerName)) {
printerNames.remove(i);
}
}
}
public List<Printer> getPrinterNames() {
return printerNames;
}
public String getComputerName() {
return computerName;
}
}
The constructor takes the name of the computer. Once an instance is constructed, there's no way to change the name of the computer.
Printers are added to and removed from a computer. Two methods have been defined to add a printer to the computer, and remove a printer from a computer.
The remove method removes all instances of the printer from the List, in case more than one was entered.
The method that gets the printer list returns an instance of the List. The calling program can change the contents of this List. If you want to make sure that the calling program can't change the contents of the list, you make a deep copy of the List.
To make a deep copy, you write a copy method for the Printer class.
public Printer copy() {
return new Printer(companyName, printerName);
}
Since strings are unchangeable, we don't have to make a copy of the strings. If we had changeable values, we would have to make a copy of them as well. That's why this is called a deep copy. It can be difficult to get everything copied correctly the first time. That's what testing is for.
Then you call this copy method in a loop in the getPrinterNames method of the Computer class.
public List<Printer> getPrinterNames() {
List<Printer> list = new ArrayList<Printer>();
for (Printer printer : printerNames) {
list.add(printer.copy());
}
return list;
}
回答2:
Create your Computer class and in the Computers class maintain computer objects list. Computer class looks like this .
public class Computer{
private String computername;
private ArrayList<String> printernames;
public Computers(String computername,List<String> printernames){
this.computername=computername;
this.printernames=printernames;
}
//setters //gettters //
}
Usage :
String compname ="Apple";
List<String> printernames = new Arraylist<String>();
printernames.add("AplrPrinter1");
printernames.add("AplrPrinter2");
printernames.add("AplrPrinter3");
Computers comp = new Computers(compname,printernames);
来源:https://stackoverflow.com/questions/17362197/java-program-statement-how-to-make-a-computer-class-having-a-list-of-printers