I need to create a Class CustApp that:
Creates a Customer object, a Customer variable named c, and assigns
the object to c.
Set values for Customer properties.
Creates a CustFrame object and passes the Customer variable c.
Creates a CustFrame variable named cf, assigns the CustFrame object
to cf.
So far I have this code, but I can't figure out how to define the properties.
public class CustApp {
public static void main(String[] args) {
Customer c = new Customer();
c.setBounds(62, 65, 176, 23);
c.setBounds(62, 120, 176, 23);
c.setBounds(62, 175, 176, 23);
c.setBounds(62, 230, 176, 23);
c.setText(c.getCustName());
c.setText(c.getShipToStreet());
c.setText(c.getShipToCity() + ", " + c.getShipToState() + " " + c.getShipToZip());
c.setText(c.getContactPerson() + " Ph: " + c.getContactPhone());
CustFrame cf = new CustFrame(c);
}
}
Related
I have an Employee Object, with in it Department Object. I need to sort by Employee Object Fields and then by Department Fields too. Data looks like below.
public static List getEmployeeData() {
Department account = new Department("Account", 75);
Department hr = new Department("HR", 50);
Department ops = new Department("OP", 25);
Department tech = new Department("Tech", 150);
List<Employee> employeeList = Arrays.asList(new Employee("David", 32, "Matara", account),
new Employee("Brayan", 25, "Galle", hr), new Employee("JoAnne", 45, "Negombo", ops),
new Employee("Jake", 65, "Galle", hr), new Employee("Brent", 55, "Matara", hr),
new Employee("Allice", 23, "Matara", ops), new Employee("Austin", 30, "Negombo", tech),
new Employee("Gerry", 29, "Matara", tech), new Employee("Scote", 20, "Negombo", ops),
new Employee("Branden", 32, "Matara", account), new Employee("Iflias", 31, "Galle", hr));
return employeeList;
}
I want to sort by Employee::name, Employee::Age, Department::DepartmentName how it can be sorted?
This should led to the desired result:
List<Employee> employees = getEmployeeData()
.stream()
.sorted(Comparator
.comparing(Employee::getName)
.thenComparing(Employee::getAge)
.thenComparing(e -> e.getDepartment().getName()))
.collect(Collectors.toList());
JComboBox cBox = new JComboBox();
cBox.addItem("Food"); String x = "Food";
cBox.addItem("Shirt");
cBox.addItem("Computer");
cBox.setSelectedItem(null);
cBox.setBounds(56, 127, 336, 27);
contentPane.add(cBox);
tPName = new JTextArea();
tPName.setBounds(38, 227, 130, 26);
contentPane.add(tPName);
tPName.setColumns(10);
tSName = new JTextArea();
tSName.setBounds(262, 227, 130, 26);
contentPane.add(tSName);
tSName.setColumns(10);
JButton btn = new JButton("Further Details");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = (String) cBox.getSelectedItem(); //gets value of combo box
tPName.setText(name); //text area displays value
String x = (String) tPName.getText();
name = "Food"; tSName.setText(F);
name = "Shirt"; tSName.setText(S);
name ="Computer"; tSName.setText(C);
}
});
btn.setBounds(162, 166, 117, 29);
contentPane.add(btn);
how would I make tSName display a text corresponding to a specific value of tPName which is copied from a combobox where F, S and C strings stand for those corresponding values I want to use.
I'm not sure if I understand you correctly. You try to make one textarea display something, when the text of another textarea changes, am I right? If yes, you should look for an Event of the first Textbox (something like tPNameKeyTyped) in this Event you can check the tPName.getText() simply with
if(tPName.getText().equals("whatever")){
tSName.setText("whatever u want");
}
Is that what you are looking for?
I right now dont have pc so I cant check errors but.
this should work
tPName.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tSName.setText(tPName.getText()); // put the value of tPName to tSName.
// i think this is all you want. i actually cant get your question.
// you can put a switch case to put coreesponding values in tSName. like:
switch(tPName.getText()){
case "a" : tSName.setText("1");
break;
//..... so on
default:
tSName.setText("stack overflow is great");
break;
}
}
});
EDIT:- Onece you do this try to refresh and validate the textareas.
tPName.validate();
tSName.validate();
tPName.repaint(); // refresh still not sure as i dont have pc if this not works try <your jframe>.repaint(); this should work
I'm trying to read lines from file into Arraylist. Here is my writer :
private Map<Integer, ArrayList<Integer>> motPage =
new HashMap<Integer, ArrayList<Integer>>();
private void writer() throws UnsupportedEncodingException, FileNotFoundException, IOException{
try (Writer writer = new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream("/home/kdiri/workJuno/motorRecherche/src/kemal.txt"), "utf-8"))) {
for(Map.Entry<Integer, ArrayList<Integer>> entry : motPage.entrySet()){
writer.write(entry.getKey() + " : " + entry.getValue() + "\n");
}
}
}
And this is an exemple result in the file kemal.txt :
0 : [38, 38, 38, 38, 199, 199, 199, 199, 3004, 3004, 3004, 3004, 23, 23]
My question is how can I read it these lines efficiently into Hashmap again ? Because size of file is about 500MB. Thank you in advance.
As JonSkeet said, you should start with something working. Find below one possible way. The snippet is kept quite verbose to show the principle.
String line = "0 : [38, 38, 38, 38, 199, 199, 199, 199, 3004, 3004, 3004, 3004,
23, 23]";
int firstSpace = line.indexOf(" ");
int leftSquareBracket = line.indexOf("[");
int rightSquareBracket = line.indexOf("]");
String keyString = line.substring(0, firstSpace);
String[] valuesString = line.substring(leftSquareBracket + 1, rightSquareBracket)
.split(", ");
int key = new Integer(keyString);
List<Integer> values = new ArrayList<>(valuesString.length);
for (String value : valuesString) {
values.add(new Integer(value));
}
Map<Integer, List<Integer>> motPage = new HashMap<>();
motPage.put(key, values);
Btw. read ... these lines efficiently into Hashmap depends on your requirements. Efficiency could be for example:
read speed of the huge file
convertion speed String to Integer
small size of the bytecode
less object generation
... there could be other as well
When the snippet does not fulfil your efficiency criteria. Start to tune the part which impacts your criteria.
I have the following 4 JChecboxes in my form. If the user clicks all the four, or any choices of JCheckboxes how do I save the values from the checboxes in mysql database in one sigle column? When I click my Add button, it should stored all the values from the selected checkboxes Please help.. Thanks
My codes :
foreign = new JCheckBox("Foreign");
foreign.setFont(new Font("Tahoma", Font.BOLD, 12));
foreign.setForeground(new Color(240, 255, 240));
foreign.setBounds(16, 25, 97, 23);
foreign.setOpaque(false);
panel.add(foreign);
travelling = new JCheckBox("Travelling");
travelling.setFont(new Font("Tahoma", Font.BOLD, 12));
travelling.setForeground(new Color(240, 255, 240));
travelling.setBounds(150, 26, 97, 23);
travelling.setOpaque(false);
panel.add(travelling);
danger = new JCheckBox("Danger Pay");
danger.setFont(new Font("Tahoma", Font.BOLD, 12));
danger.setForeground(new Color(240, 255, 240));
danger.setBounds(16, 68, 97, 23);
danger.setOpaque(false);
panel.add(danger);
medical = new JCheckBox("Medical Scheme");
medical.setFont(new Font("Tahoma", Font.BOLD, 12));
medical.setForeground(new Color(240, 255, 240));
medical.setBounds(150, 69, 121, 23);
medical.setOpaque(false);
panel.add(medical);
add = new JButton("Add");
add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int id1 = Integer.parseInt(id.getText());
String fn = fname.getText();
String ln = lname.getText();
String po = pos.getText();
String sa = sal.getText();
String all = "";
if(foreign.isSelected() && travelling.isSelected() && danger.isSelected() && medical.isSelected()){
all = "Foreign, Travelling, Danger, Medical";
}else if(foreign.isSelected() && travelling.isSelected() && danger.isSelected()){
all = "Foreign, Travelling, Danger";
}else if(foreign.isSelected() && travelling.isSelected()){
all = "Foreign, Travelling";
}else if()
}
});
You could save the data in a separate table. What I mean by this is that suppose you are storing the data in a table temp which is having 2 columns - id and selection. Now my suggestion is that you create 2 tables one for id and the other for selection. the selection table will have 2 columns id and selection. the id column is to be a foreign key to the id table's id column.
Now when the user having id say 01 selects "Foreign" & "Travelling", then you make 2 entries in the selection table that are 01 - Foreign and 01 - Travelling
Now when the user having id say 02 selects all the options then you are to make 4 entries in the selection table against the id of the user:
02-Foreign,
02-Travelling,
02-Danger,
02-Medical
This way you could save each selection separately.
After getting you comment
After your comment I could only suggest that you create 4 columns of boolean type in the table and store the state of each checkBox in each column.
Below is the code I am working on. Basically I need a box to open and get the user to input their data in the corresponding text fields. All is working great. Everything is outputting correctly when I print it from the actionPerformed method, but when I call gui.displayPersonInfo method from main it shows all of the values as null. It is doing the displayPersonInfo method first before the box has even opened, even though I call the method after. Anyone know what is wrong with my code? (output below)
package userInput;
import javax.swing.JFrame;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Person extends JFrame{
String Name;
String Address;
String PhoneNumHome;
String PhoneNumWork;
String email;
JLabel label, label2;
JTextField tf1, tf2, tf3, tf4, tf5, tf6, tf7, tf8;
JButton button;
public Person(){
setLayout(new FlowLayout());
label = new JLabel("Enter your name");
add(label);
tf1 = new JTextField(10);
add(tf1);
label = new JLabel("Enter your Address (street number + street name)");
add(label);
tf2 = new JTextField(10);
add(tf2);
label = new JLabel("Enter your city");
add(label);
tf3 = new JTextField(10);
add(tf3);
label = new JLabel("Enter your province");
add(label);
tf4 = new JTextField(10);
add(tf4);
label = new JLabel("Enter your postal code");
add(label);
tf5 = new JTextField(10);
add(tf5);
label = new JLabel("Enter your home phone number (306-xxx-xxx)");
add(label);
tf6 = new JTextField(10);
add(tf6);
label = new JLabel("Enter your work phone number (306-xxx-xxx)");
add(label);
tf7 = new JTextField(10);
add(tf7);
label = new JLabel("Enter your email (user#emailservice.xxx");
add(label);
tf8 = new JTextField(10);
add(tf8);
button = new JButton("Next");
add(button);
event e = new event();
button.addActionListener(e);
}
public class event implements ActionListener{
public void actionPerformed(ActionEvent e){
String address1, pnum, wnum, a;
try{
String word = tf1.getText();
Name = word;
System.out.println(Name);
Address = tf2.getText();
Address = Address + " " + tf3.getText();
Address = Address + " " + tf4.getText();
Address = Address + " " + tf5.getText();
address1 = Address;
System.out.println(Address);
PhoneNumHome = tf6.getText();
pnum = PhoneNumHome;
PhoneNumWork = tf7.getText();
wnum = PhoneNumWork;
email = tf8.getText();
a = email;
System.out.println(PhoneNumHome);
System.out.println(PhoneNumWork);
System.out.println(email);
saveInfo(word, address1, pnum, wnum, a);
displayPersonInfo();
System.exit(0);
}catch(Exception ex){}
}
}
public void displayPersonInfo(){
System.out.println("Name: " + Name);
System.out.println("Address: " + Address);
System.out.println("Home Phone Number: " + PhoneNumHome);
System.out.println("Work Phone Number: " + PhoneNumWork);
System.out.println("Email: " + email);
}
public void saveInfo(String name, String address, String Hphone, String Wphone, String Email){
Name = name;
Address = address;
PhoneNumHome = Hphone;
PhoneNumWork = Wphone;
email = Email;
}
public static void main(String[] args) {
Person gui = new Person();
gui.displayPersonInfo();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setTitle("Enter Information");
gui.setSize(350,330);
gui.setLocation(500,250);
gui.setVisible(true);
}
}
here is the output: (it acts as if displayPersonInfo occurs first)
run:
Name: null
Address: null
Home Phone Number: null
Work Phone Number: null
Email: null
A Name (now it prints it from within actionPerformed)
An Adress a City a province a postal code
a number
another number
an email
Name: A Name
Address: An Adress a City a province a postal code
Home Phone Number: a number
Work Phone Number: another number
Email: an email
BUILD SUCCESSFUL (total time: 20 seconds)
It seems your saveInfo function is redundant since you already set every value prior to calling it, but I don't know why saveInfo is nulling the values. Try to call displayPersonInfo before saveInfo and see what happens.
In the main method the "displayPersonInfo()" method is being called right on start. You declared the string name as a field up there uninitialized, so by default its value is null. You initialize this variable inside the actionPerformed() method inside the event class.
The problem is:
public void displayPersonInfo() {
System.out.println("Name: " + Name);
}
public static void main(String[] args) {
Person gui = new Person();
gui.displayPersonInfo();
}
You attribute the value that you want for the variable ONLY when the action is performed, so if you try to access that variable before that you will the get the default value up there, which is null. And that's exactly what you're doing. When the main method starts, it calls the displayPersonInfo(), and inside of that you try to access the variable name and it returns to you null, because the variable only receives the value that you want when the action is performed.
So the solution would be:
public void displayPersonInfo() {
String word = tf1.getText();
Name = word;
System.out.println("Name: " + Name);
}
You must give the value that you want before you call the variable. The same applies to the other ones. If you declare something as "String Name;" and try to call it, you will receive null.