I am printing LinkedList like this:
public class cinema{
public static LinkedList <cinema> cinList = new LinkedList<cinema>();
private int cinemaNum,numSeats;
private String row;
public cinema(int getCinemaNumber,String getRowName,int getNumSeats){
this.cinemaNum = getCinemaNumber;
this.row = getRowName;
this.numSeats = getNumSeats;
//todo
}
public static void addHalls(cinemaValues cin){
cinema Cinema = new cinema(cin.getCinemaNumber(), cin.getRowName(),cin.getNumSeats());
cinList.add(Cinema);
}
public static void print(){
System.out.println(cinList);
}
#Override
public String toString() {
return "cinnum:"+cinemaNum+" row:"+row+" seats:"+numSeats;
}
}
public class session{
public static LinkedList <session> sessList = new LinkedList<session>();
private int CinemaSessionNum;
private String time,movie;
public session(int getCinemaSessionNum, String getTime,String getMovie){
this.CinemaSessionNum = getCinemaSessionNum;
this.time = getTime;
this.movie = getMovie;
}
public static void addSession(CinemaSessions sess){
session ses = new session(sess.getCinemaSessionNum(),sess.getTime(),sess.getMovie());
sessList.add(ses);
}
public static void print(){
System.out.println(sessList);
}
#Override
public String toString() {
return "cinnum:"+CinemaSessionNum+" time:"+time+" movie:"+movie;
}
}
the output:
I want a way to be able search all three parts of an elements using one of the keys
basically,
when the condition is Cinnum:2 I want it to return
Cinnum:1 time:9:00 movie:toy
Cinnum:1 time:14:30 movie:Ratatouille
Any help would be appreciated.
Thanks
Related
I have class-Composite:
public class CompositeText implements ComponentText {
private TypeComponent type;
private String value;
private final List<ComponentText> childComponents;
private CompositeText() {
childComponents = new ArrayList<>();
}
public CompositeText(String value, TypeComponent typeComponent) {
this.value = value;
this.type = typeComponent;
childComponents = new ArrayList<>();
}
#Override
public void add(ComponentText componentText) {
childComponents.add(componentText);
}
#Override
public void remove(ComponentText componentText) {
childComponents.remove(componentText);
}
#Override
public TypeComponent getComponentType() {
return this.type;
}
#Override
public ComponentText getChild(int index) {
return childComponents.get(index);
}
#Override
public int getCountChildElements() {
return childComponents.size();
}
#Override
public int getCountAllElements() {
return childComponents.stream()
.mapToInt(ComponentText::getCountAllElements)
.sum();
}
#Override
public String toString() {
return null;
}
}
I created classes that perform the same action - parsing, parsing text into paragraphs, into sentences, into tokens, into symbols.
public class IntoParagraphParser implements ActionParser {
// call IntoSentenceParser
}
public class IntoSentenceParser implements ActionParser {
// call IntoLexemeParser
}
public class IntoLexemeParser implements ActionParser {
// call IntoSymbolParser
}
public class IntoSymbolParser implements ActionParser {
}
All data is stored in List <ComponentText> childComponents in class-Composite - CompositeText.
How to properly create a method so that it prints all the data that is inside the composite?
I think this will be the method toString() in CompositeText.
Class IntoParagraphParser look:
public class IntoParagraphParser implements ActionParser {
private static final String PARAGRAPH_SPLIT_REGEX = "(?m)(?=^\\s{4})";
private static final IntoParagraphParser paragraphParser = new IntoParagraphParser();
private static final IntoSentenceParser sentenceParser = IntoSentenceParser.getInstance();
private IntoParagraphParser() {
}
public static IntoParagraphParser getInstance() {
return paragraphParser;
}
public ComponentText parse(String text) throws TextException {
ComponentText oneParagraph;
ComponentText componentParagraph = new CompositeText(text, TypeComponent.PARAGRAPH);
String[] arrayParagraph = text.split(PARAGRAPH_SPLIT_REGEX);
for(String element: arrayParagraph) {
oneParagraph = new CompositeText(element, TypeComponent.PARAGRAPH);
oneParagraph.add(sentenceParser.parse(element));
componentParagraph.add(oneParagraph);
}
return componentParagraph;
}
}
Need #Override the method toString() in CompositeText like this:
#Override
public String toString() {
StringBuilder builder = new StringBuilder();
for (ComponentText component : childComponents) {
builder.append(component.toString());
}
return builder.toString();
}
But how to write this code correctly with Stream API?
#Override
public String toString() {
StringBuilder builder = new StringBuilder();
childComponents.stream().map(...????
return builder.toString();
}
In my project, we have to create a system where we create an object Facility and we have to run inspections and maintenance on it to check if it's working. We created a Facility object and then we created a state machine that changes the state of the Facility to resting, working, maintenance, or working.
Here is the SuperFacility class:
import java.util.Map;
public interface SuperFacility {
public void setName(String name);
public void setAddress(String address);
public void setDescription(String description);
public void setRefNumber(int refNumber);
public void setCapacity(int capacity);
public void setCost(double cost);
public void setProblemRate(int problemRate);
public String getName();
public String getAddress();
public String getDescription();
public int getRefNumber();
public int getCapacity();
public double getCost();
public int getProblemRate();
public void oxygenator(boolean oxygenator);
public void nuclearReactor(boolean nuclearReactor);
public void innerAirlocks(boolean innerAirlocks);
public void externalAirlocks(boolean externalAirlocks);
public void comms(boolean comms);
public void waterMaking(boolean waterMaking);
public void startMachines();
public Map getMap();
public void getFacilityStatus();
public void getFacilityStatus(Map<String, Boolean> map);
}
Here is the Facility class:
import java.util.*;
public class Facility extends StateMachine implements SuperFacility {
public String name, address, description;
public int refNumber, capacity, problemRate;
private double cost;
private Map<String, Boolean> map = new HashMap<String, Boolean>();
private boolean[] machines = new boolean[6];
private boolean oxygenator, innerAirlocks, externalAirlocks,
comms, nuclearReactor, waterMaking;
private final int numberOfMachines = 6; // Number of Machines inside Facility
// Setters
public void setName(String name){
this.name = name;
}
public void setAddress(String address){
this.address = address;
}
public void setDescription(String description){
this.description = description;
}
public void setRefNumber(int refNumber){
this.refNumber = refNumber;
}
public void setCapacity(int capacity){
this.capacity = capacity;
}
public void setCost(double cost){
this.cost = cost;
}
public void setProblemRate(int problemRate){
this.problemRate = problemRate;
}
// Getters
public String getName(){
return name;
}
public String getAddress(){
return address;
}
public String getDescription(){
return description;
}
public int getRefNumber(){
return refNumber;
}
public int getCapacity(){
return capacity;
}
public double getCost(){
return cost;
}
public int getProblemRate(){
return problemRate;
}
public void oxygenator(boolean oxygenator){
this.oxygenator = oxygenator;
}
public void nuclearReactor(boolean nuclearReactor){
this.nuclearReactor = nuclearReactor;
}
public void innerAirlocks(boolean innerAirlocks){
this.innerAirlocks = innerAirlocks;
}
public void externalAirlocks(boolean externalAirlocks){
this.externalAirlocks = externalAirlocks;
}
public void comms(boolean comms){
this.comms = comms;
}
public void waterMaking(boolean waterMaking){
this.waterMaking = waterMaking;
}
public boolean[] getMachines(){
machines[0] = oxygenator;
machines[1] = nuclearReactor;
machines[2] = innerAirlocks;
machines[3] = externalAirlocks;
machines[4] = comms;
machines[5] = waterMaking;
return machines;
}
// Set machines to false
public void breakMachines(){
oxygenator(false);
nuclearReactor(false);
innerAirlocks(false);
externalAirlocks(false);
comms(false);
waterMaking(false);
map.clear();
initializeMap(map);
}
public void startMachines(){
// Set all of the booleans from this Facility to true;
// This booleans are what we call "the machines from the Facility"
oxygenator(true);
nuclearReactor(true);
innerAirlocks(true);
externalAirlocks(true);
comms(true);
waterMaking(true);
map.clear();
initializeMap(map);
}
public void initializeMap(Map<String, Boolean> map){
this.map.put("Oxygenator", oxygenator);
this.map.put("Inner Airlocks", innerAirlocks);
this.map.put("External Airlocks", externalAirlocks);
this.map.put("Nuclear Reactor", nuclearReactor);
this.map.put("Comms", comms);
this.map.put("WaterMaking", waterMaking);
}
public Map<String, Boolean> getMap(){
return map;
}
public void getFacilityStatus(){ // The status of the map in this object
for (Map.Entry<String, Boolean> i: map.entrySet()){
System.out.println(i.getKey() + ": " + i.getValue());
}
}
public void getFacilityStatus(Map<String, Boolean> map){ // The status of any Facility map
for (Map.Entry<String, Boolean> i: map.entrySet()){
System.out.println(i.getKey() + ": " + i.getValue());
}
}
}
Here is the StateMachine class:
public class StateMachine {
public State state = State.RESTING;
enum State {
WORKING, RESTING, MAINTENANCE, BROKEN
}
public State getFacilityState(){
return state;
}
public void setStateWorking(Facility fac){
fac.state = State.WORKING;
}
public void setStateResting(Facility fac){
fac.state = State.RESTING;
}
public void setStateMaintenance(Facility fac){
fac.state = State.MAINTENANCE;
}
public void setStateBroken(Facility fac) { fac.state = State.BROKEN;}
public State getState(){
return state;
}
}
In my Inspection class, I have two methods that have to check the state of the Facility to see if it's working, but I am having trouble with my if statement:
import java.util.*;
public class Inspection {
private Facility fac;
public boolean isBroken(){
if (fac.state == State.BROKEN)
return true;
else
return false;
}
public void makeMaintenanceRequest(Control c){
if (fac.state == State.BROKEN){
c.scheduleMaintenance(fac);
}
}
I want the methods to be able to compare the current state of a Facility to the Broken state. How should I compare the states? I keep getting the "cannot find symbol" error for State.BROKEN
Although I cannot understand very well what you're trying to do, I can tell you the Inspection class might already work as you'd like.
I see you commented out the constructor, why? It was okay to inject a Facility instance inside Inspection. However, you should accept a StateMachine instead.
public class Inspection {
private final StateMachine stateMachine;
public Inspection(final StateMachine stateMachine) {
this.stateMachine = stateMachine;
}
...
}
Then, inside your Inspection#isBroken method
public boolean isBroken() {
return this.stateMachine.getFacilityState() == State.BROKEN; // "this" not necessary
}
As Facility extends StateMachine, it exposes a getFacilityState() method.
And because Facility extends StateMachine, Inspection is able to accept it.
final Facility facility = new Facility(...);
final Inspection inspection = new Inspection(facility);
final boolean isBroken = inspection.isBroken();
A simple solution would be to replace State.BROKEN with StateMachine.State.BROKEN
public boolean isBroken(){
if (fac.state == StateMachine.State.BROKEN)
return true;
else
return false;
}
I have a Class A with name and value attributes.
public class A {
private String name;
private String value;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
I have another Class B, such as
public class B {
private String attribute01;
private String attribute01;
private String attribute01;
public String getAttribute01() {
return attribute01;
}
public void setAttribute01(String name) {
this.attribute01 = name;
}
...
}
I would like to return a list with A type, having attribute01 key and where value is getAttribute01() from B, such as ({attribute01, getAttribute01()},{attribute02, getAttribute02()}).
How to implement it?.
Thanks in advance.
Actually I can use a very stupid way, such as
public List<A> keyvalueList(final B objB) {
List<A> list = new ArrayList<>();
A objA = new A();
objA.setName("attribute01");
objA.setValue(objB.getAttribute01());
list.add(objA);
objA = new A();
objA.setName("attribute02");
objA.setValue(objB.getAttribute02());
list.add(objA);
...
return list;
}
Part of them hard coding, obvious it is not a smart way, any proposal.
I wrote sample code for List.Please check my code that is ok to use or not.I added another extra class C.in C,it has two attribute String nameFromA and String attFromB.You should add this C object to list.Following is sample code.
public class A {
private String name;
private String value;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
public class B {
private String att1;
private String att2;
private String att3;
public String getAtt1() {
return att1;
}
public void setAtt1(String att1) {
this.att1 = att1;
}
public String getAtt2() {
return att2;
}
public void setAtt2(String att2) {
this.att2 = att2;
}
public String getAtt3() {
return att3;
}
public void setAtt3(String att3) {
this.att3 = att3;
}
}
public class C {
private String namefromA;
private String attfromB;
public String getNamefromA() {
return namefromA;
}
public void setNamefromA(String namefromA) {
this.namefromA = namefromA;
}
public String getAttfromB() {
return attfromB;
}
public void setAttfromB(String attfromB) {
this.attfromB = attfromB;
}
}
public class Test {
public static void main(String args[]){
C c = new C();
A a = new A();
B b = new B();
a.setName("A1");
b.setAtt1("100");
c.setNamefromA(a.getName());
c.setAttfromB(b.getAtt1());
List list = new ArrayList();
//use generic
list.add(c);
}
}
if you don't want to use class C,then you can use Test class like that
import java.util.ArrayList;
import java.util.List;
public class Test {
private String nameFromA;
private String valueFromB;
public Test(String nameFromA, String valueFromB) {
super();
this.nameFromA = nameFromA;
this.valueFromB = valueFromB;
}
public static void main(String args[]){
A a = new A();
B b = new B();
a.setName("A1");
b.setAtt1("100");
Test test = new Test(a.getName(),b.getAtt1());
List list = new ArrayList();
list.add(test);
}
}
This is my opinion only.Please check it is ok or not.
I am looking to store information in a list or array, and then access the info through a method using a String parameter.
public void blah(String name){
DataStructure[apple].getInfo1; //a float
DataStucture[apple].getInfo2; //an int
Some pseudocode of how the info will be stored:
DataStructure.add(apple);
apple.setFloat(5f);
apple.setInt(1);
My main confusion is how to access this information using a String. Since I will be having a lot of objects in this structure, I figured the easiest way to access the info would be to look up the name directly.
Like this:
public class DataStructure {
private float info1;
private int info2;
public DataStructure(float info1, int info2) {
super();
this.info1 = info1;
this.info2 = info2;
}
public float getInfo1() {
return info1;
}
public void setInfo1(float info1) {
this.info1 = info1;
}
public int getInfo2() {
return info2;
}
public void setInfo2(int info2) {
this.info2 = info2;
}
}
import java.util.HashMap;
public class Test {
public static void main(String[] args) {
final HashMap<String, DataStructure> map = new HashMap<String, DataStructure>();
map.put("apple", new DataStructure(5f, 1));
}
}
I am working in an android application I want to sort a List of Objects with an Object Property. I have sorted it successfully but when I sort it all the List with that object changes the value to same as the sorted value
Please look into ma code :
SortedSet<Caseload> removeDuplicateClientName = new TreeSet<Caseload>(
new Comparator<Caseload>() {
#Override
public int compare(Caseload caseload0, Caseload caseload1) {
return caseload0.ClientName.compareTo(caseload1.ClientName);
}
});
// Getting the list of values from web service
mLISTCaseloadsHeads = parsedXML.getCaseLoadValues("get_Caseload_ClientServiceGroupID", param);
List<Caseload> newBackUp=mLISTCaseloadsHeads ;
Iterator<Caseload> iterator = mCaseloadsHeads.iterator();
while (iterator.hasNext()) {
removeDuplicateClientName.add(iterator.next());
}
mCaseloadsHeads.clear();
mCaseloadsHeads.addAll(removeDuplicateClientName);
The List newBackUp also changes the value to the same as sorted List
Caseload class:
public class Caseload implements Comparable<Caseload> {
public int BusClientLogID;
public int ClientID;
public int ClientStatus;
public int ClientServiceGroup_ClientSiteTherapyID;
public String ClientName;
public String TimeArrive;
public String TimeDepart;
public String SignOutTime;
public String SignInTime;
public String ServiceCompletedCount;
public Boolean ShowFooter = false;
public int getBusClientLogID() {
return BusClientLogID;
}
public void setBusClientLogID(int busClientLogID) {
BusClientLogID = busClientLogID;
}
public int getClientID() {
return ClientID;
}
public void setClientID(int clientID) {
ClientID = clientID;
}
public int getClientStatus() {
return ClientStatus;
}
public void setClientStatus(int clientStatus) {
ClientStatus = clientStatus;
}
public int getClientServiceGroup_ClientSiteTherapyID() {
return ClientServiceGroup_ClientSiteTherapyID;
}
public void setClientServiceGroup_ClientSiteTherapyID(
int clientServiceGroup_ClientSiteTherapyID) {
ClientServiceGroup_ClientSiteTherapyID = clientServiceGroup_ClientSiteTherapyID;
}
public String getClientName() {
return ClientName;
}
public void setClientName(String clientName) {
ClientName = clientName;
}
public String getTimeArrive() {
return TimeArrive;
}
public void setTimeArrive(String timeArrive) {
TimeArrive = timeArrive;
}
public String getTimeDepart() {
return TimeDepart;
}
public void setTimeDepart(String timeDepart) {
TimeDepart = timeDepart;
}
public String getSignOutTime() {
return SignOutTime;
}
public void setSignOutTime(String signOutTime) {
SignOutTime = signOutTime;
}
public String getSignInTime() {
return SignInTime;
}
public void setSignInTime(String signInTime) {
SignInTime = signInTime;
}
public String getServiceCompletedCount() {
return ServiceCompletedCount;
}
public void setServiceCompletedCount(String serviceCompletedCount) {
ServiceCompletedCount = serviceCompletedCount;
}
#Override
public int compareTo(Caseload compareCaseload) {
int busClientLogID = ((Caseload) compareCaseload).getBusClientLogID();
return busClientLogID - this.BusClientLogID;
}
}
Please give me a solution.
I doubt the return statement associated with your compare function in the comparator.
You should go by this approach to get the right ordering :
#Override
public int compare(YourClass lhs, YourClass rhs) {
YourClass p1 = (YourClass) lhs;
YourClass p2 = (YourClass) rhs;
int first = p1.ClientName; //use your getter if you want
int second = p2.ClientName;
if (second < first) {
return 1;
}
else if (second > first) {
return -1;
}
else {
return 0;
}
}
If you go by this approach I guess you will get the required ordering after sort.
Edit:
Now I have got the issue, you are using a reference of the original list in newBackup and its not a new list that is why this is happening, use this and you are good to go.
List<Caseload> newBackUp=new ArrayList<Caseload>(mLISTCaseloadsHeads);