At the moment I have to manually add items to my array but I would like to enable users to do this themselves perhaps through JOptionPanes, what would be the best way to go about this? Here is my current code.
public class Main {
public static void main(String[] args){
//Create new Person objects
Address p[] = new Address[3];
p[0] = new Address("27","Abbey View","Hexham","NE46 1EQ");
p[1] = new Address("15", "Chirdon Crescent", "Hexham", "NE46 1LE");
p[2] = new Address("6", "Causey Brae", "Hexham", "NE46 1DB");
Details c[] = new Details[3];
c[0] = new Details ("3", "175,000", "Terraced");
c[1] = new Details ("6", "300,000", "Bungalow");
c[2] = new Details ("4", "250,000", "Detached");
//Send some messages to the objects
c[0].setBeds("3 ");
c[1].setBeds("6");
c[2].setBeds("4");
c[0].setPrice("175,000");
c[1].setPrice("300,000");
c[2].setPrice("250,000");
c[0].setType("Terraced");
c[1].setType("Bungalow");
c[2].setType("Detached");
//Set up the association
p[0].ownsDetails(c[0]);
p[1].ownsDetails(c[1]);
p[2].ownsDetails(c[2]);
//print details
p[1].printDetails();
p[2].printDetails();
p[3].printDetails();
}
System.exit(0);
}
}
You could use the showXXXX methods on JOptionPane and keep prompting the user just as you would on the console.
However, I suggest just creating a simple JFrame that would have controls that allows the user to enter multiple items instead of showing one dialog after another.
Related
Somehow I try to make a user selection of which cook has to prepare the food. I already created the variable whichcook and place that where cook1 used to stay. But I don't know how to carry on. I want to let the user select between either "Jan de Vries" or "SinBad" to prepare. So the methode deliverer.delivered(whichcook, customer); carries on with the selected name. I figure if have to use instanceof I guess, but don't know how to really do that. I know how to make a user-input and cases etc that's not the problem. It's more about how to isolate the right instance!!
Someone a key suggestion???
java
package KebabStore;
public class DamascusKebab
{
public static int cooksnumber;
public static int deliverersnumber;
#SuppressWarnings("unused")
public static void main(String[] args)
{
Cook cook1 = new Cook ("Jan de Vries", "Butcherknife 1", "1212-IS", "Allahmelo", 123456);
Cook cook2 = new Cook ("Sinbad", "Camelhumb 2","2323-IS", "Halal-lem", 654321);
Deliverer deliverer1 = new Deliverer ("Ali Baba", "Helmgras 11", "3434-JH", "Ji-Hattem",456789);
Deliverer deliverer2 = new Deliverer ("Muammar", "Zadeldreef 22", "4545-JH", "Moskemenade", 987654);
Customer customer = new Customer ("Piet Hein", "Klantlaan 25", "5656-KL", "Darmstadt");
cooksnumber = Cook.numberofcooks;
deliverersnumber = Deliverer.numberofdeliverers;
Cook whichcook = cook1;
deliverer.delivered(whichcook, customer);
}
}
How are going to get the user input?
if it's from the Scanner
Cook whichcook;
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt()
if (choice == 1)
whichcook = cook1;
else
whichcook = cook2;
deliverer.delivered(whichcook, customer);
I have List in StudentService
private List<Student> students = new ArrayList<>(Arrays.asList( new Student("1", "Lukasz", "Nowak", "Cicha 3", "1a"), new Student("2", "Tomasz", "Tomczyk", "Krakowska 13a", "1a"), new Student("3", "Grzegorz", "Adamiak", "Podkarpacka 8", "2b"), new Student("4", "Klaudia", "Kurcz", "Warszawska 13", "2b")));
and i want copy some of elements to PresentService and add a present
I tried to do
List<Presents> presents = new ArrayList<Student>()
but I got only errors
List<Presents> presents = new ArrayList<Student>()
This statement will not compile.
You variable definition is expecting objects of type Presents but you gave it a list of Student.
The easiest solution would be
List<Presents> presents = students.stream().map((student) -> new Present(student)).collect(Collectors.toList());
And create a constructor in Presents class
public Presents(Student student) {
//Create your present object as you would want to here
//For example if you wanted name
this.name = student.getName();
}
it might be a simple question or even impossible without any kind of Interface (Arrays, Maps etc.) but I would like to know if there's any possibility of converting an object name to String so I can pass as argument. I have two classes Paciente and Sintomas with multiple objects that I need to pass as argument to a function but I don't want to use arrays (it must be like that) and I can't figure any other way of doing so without manually make an insert for each one.
Paciente Paciente1 = new Paciente("001", "Ana Melo", 33, "");
Paciente Paciente2 = new Paciente("002", "Rui Costa", 13, "");
Paciente Paciente3 = new Paciente("003", "Joana Martins", 85, "");
Paciente Paciente4 = new Paciente("004", "Pedro Torres", 53, "");
Paciente Paciente5 = new Paciente("005", "Ana Gomes", 93, "");
Paciente Paciente6 = new Paciente("006", "Jorge Costa", 56, "");
Sintomas Sintoma1 = new Sintomas("001", "febre");
Sintomas Sintoma2 = new Sintomas("001", "dores");
Sintomas Sintoma3 = new Sintomas("001", "machas");
Sintomas Sintoma4 = new Sintomas("002", "febre");
Sintomas Sintoma5 = new Sintomas("002", "manchas");
Sintomas Sintoma6 = new Sintomas("003", "febre");
Sintomas Sintoma7 = new Sintomas("003", "dores");
Sintomas Sintoma8 = new Sintomas("004", "febre");
Sintomas Sintoma9 = new Sintomas("006", "manchas");
Sintomas Sintoma10 = new Sintomas("006", "dores");
// now I would like to pass to a function as argument something like this:
for(int i = 0 ; i < 6 ; i++)
kSession.insert("Paciente"+(i+1));
// instead of making
kSession.insert(Paciente1);
kSession.insert(Paciente2);
kSession.insert(Paciente3);
// and so on.
Something like this should work(asuming you mean no array because of size constraints), note that there has to be somewhere you add the data, it's also possible to load it from a txt or something, but it has to be defined at some point
List<Paciente> pacientes = new ArrayList<>(); // no size constraints, automatically expands if too small
pacientes.add(new Paciente("", "", ""));
for (Paciente paciente : pacientes) { // loop all Patientes in pacientes
kSession.insert(paciente); // add a paciente to the session, for every entry
}
ofcource same can be done for any class, or object
It all really comes down to, how do you wish to store and access the data, and where do you need to store and access it. Using ArrayList and Map's offer the utility of easily changing the size and content of a list of data, but as any data it must be initially inserted
As a side note if the patients have an ID then using a Map
Map<String, Paciente> pacientes = new HashMap<>();
provides a way to acces the patiens very fast, and the TreeMap structure is sorted on key, should that be needed.
Other options could be
Wrapper classes that manage the data, will work similarily to an ArrayList<> but you can define, rules for adding, deleting, and such from the list.
public class Wrapper{
private List<Paciente> pacientes = new ArrayList<>();
public void addPaciente(Paciente paciente){
if(!this.pacientes.contains(paciente)) // prevent multi entries
paciente.add(paciente);
}
public void addPacientes(List<Paciente> pacientes){
for(Paciente paciente : pacientes) // add all patients using the add method
this.addPaciente(paciente);
}
public List<Paciente> getPacientes(){
return this.pacientes;
}
}
You can then add the patients to the kSession, as earlier described
Finally, there is no reason why Paciente, can have the list of Sintomas, such that
public class Paciente{
private List<Sintomas> sintomas = new ArrayList<>();
public addSintomas(Sintomas sintomas){
if(!this.sintomas.contains(sintomas))
this.sintomas.add(sintomas);
}
// rest is the same principle as above in "Wrapper"
}
This way you can get a Paciente, and add a Sintomas, and then when you wish to check a Pacientes Sintomas you can just get the list of Sintomas from that Paciente
i need to retrive info form a private object but im stuck at this.. its keep say there is a
constructor error
public class Screen{
private movie movieObject;
private Screen(movie movieObject){
this.movieObject = movieObject;
}
private ArrayList<movie> movie = new ArrayList<movie>
public void add(){
String title = keyboard.readString("Enter movie title > ");
String name = keyboard.readString("Enter theatre name > ");
movie add = new movie(title,name) // there is error in this part
Maybe: Line 9 above...
new ArrayList<movie>();
Also if you use standard naming conventions for classes would help us and yourself.
Your last line is quite strange to me. Try instead:
movie.add(new movie(title,name));
Also maybe the movie list instanciation should be done in the constructor ?
Your letter casing is wrong
movie add = new movie(..)
Your class is Movie with capital M, not movie. You should be getting error all over the place, not just the above line
Also, you're missing the (); here new ArrayList<movie>();
And a ; here movie add = new movie(..). <--
But that still doesn't take away from the problems with the letter casing.
It does look like you want something more like this
ArrayList<Movie> movies = new ArrayList<Movie>();
public void add(){
String title = keyboard.readString("Enter movie title > ");
String name = keyboard.readString("Enter theatre name > ");
movies.add(new Movie(title, name));
}
Or maybe you should do something more like this, where your method add a movie, and doesn't concern itself with getting input from keyboard
public void addMovie(Movie movie) {
movies.add(movie);
}
Then in your main you can do something like this
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
Screen screen = new Screen();
System.out.println("Enter a title");
String title = keyboard.nextLine();
System.out.println("Enter a name:);
String name = keyboard.nextLine();
screen.addMovie(new Movie(title, name));
}
The last examples make a lot more sense than the way you are trying to do it.
Backgorund info:
I have a buddy of mine in the Navy, and he wanted to know if I could whip him up a small app that would calcualte when he has his guard duty, because apparently counting on a calendar is hard. I used JOptionPane.showMessageDialog to give him the output of the dates. Here's how I'm doing that.
GregorianCalendar knownDate = new GregorianCalendar(year,month,day);
GregorianCalendar[] futureDates = new GregorianCalendar[10];
for(int i = 0; i < 10; i++) {
futureDates[i] = new GregorianCalendar(year,month,day);
futureDates[i].add(Calendar.DAY_OF_MONTH,10*(i+1)); // duty every 10 days
}
String newline = System.getProperty("line.separator");
StringBuilder sb = new StringBuilder("Jakes duty dates:").append(newline);
for(GregorianCalendar d : futureDates) {
sb.append(months[d.get(Calendar.MONTH)]).append(" ");
sb.append(d.get(Calendar.DAY_OF_MONTH)).append(newline);
}
JOptionPane.showMessageDialog(null,sb.toString());
The 'only problem' is you can't select the text that is displayed. He'd like to select it for IM and email, because what's the point in only being half lazy, right? (Only problem is in quotes because I have a feeling he'll scope creep this to death... haha)
My question:
Is there a "one-line solution" to making a selectable showMessageDialog?
I was able to build on trashgod's answer. While he suggested using a JList, I'm instead using a JTextArea (which gives the kind of selection I need.)
Here's what I'm doing:
JTextArea text = new JTextArea(sb.toString());
JOptionPane.showMessageDialog(null,text);
And it's working like a charm!
================================================
After a little experimentation I did this:
DefaultListModel model = new DefaultListModel();
for(GregorianCalendar g : futureDates) {
String m = months[g.get(Calendar.MONTH)];
String d = String.valueOf(g.get(Calendar.DAY_OF_MONTH));
model.addElement(m + " " + d);
}
JList jlist = new JList(model);
JOptionPane.showMessageDialog(null,jlist);
JOptionPane.showMessageDialog(null,jlist.getSelectedValue());
And the second box displayed what I had selected on the first one. I was really impressed with that. Now granted, this isn't the functionality I was going for (the top section is) but that doesn't make it any less awesome! :-)
Add the dates to a DefaultListModel, create a JList, and pass the list to showMessageDialog(). It's more than one line, but the selection copies to the clipboard using the platform's copy keystroke.
private static final DateFormat df = new SimpleDateFormat("dd-MMM");
private static void createAndShowGUI() {
DefaultListModel dlm = new DefaultListModel();
for (int i = 0; i < 10; i++) {
GregorianCalendar knownDate = new GregorianCalendar();
knownDate.add(Calendar.DAY_OF_MONTH, 10 * i);
dlm.add(i, df.format(knownDate.getTime()));
}
JList list = new JList(dlm);
JOptionPane.showMessageDialog(null, list);
}