TBD.my

Full Version: Soalan Java
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Assalamualaikumsalam wbt

Macam mana ye nak code java nie sbb aku dah cuba tapi tak menjadi

1. Write an application that keeps track of
• customers and their purchases
• current inventory.
Allow the user to add, delete or modify the customer information. An inventory will include the purchase and selling price of the cars. Give the user an option to list all customers or specific customer. based on the customer’s identification card number. Define at least four data members for each type of information. For customers, include name, identification card number, address and purchases. For cars, include the type, model, no. of units and price. Please add more data members as you see fit. The data for customers and cars should be stored in two files.
Design appropriate GUI for the application.
Before coding your application construct the use diagram and the activity diagram for the application.

Code:
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;

class CustomerInformation {
JFrame f;
JPanel p1,p2,p3;
JTabbedPane tp;
ImageIcon btnimg1,btnimg2;
JLabel l1, l2, l3, l4,l5,l6,l7,l8,l9,l10;
JTextField tf1,tf2,tf3,tf4,tf5,tf6,tf7,tf8,tf9,tf10;
JScrollPane sp1;
JButton savebtn,resetbtn,editbtn1,editbtn2,deletebtn ;

EmployeeInformation(){
f=new JFrame("Proton and Perodua");
p1=new JPanel(new GridLayout(5,2));
p2=new JPanel(new GridLayout(5,2));
p3=new JPanel(new GridLayout(2,2));
tp=new JTabbedPane();
l1=new JLabel("Customer ID:");
l2=new JLabel("Customer Name:");
l3=new JLabel("Customer Address:");
l4=new JLabel("Purchase");
l5=new JLabel("Enter Customer ID to delete record:");

l7=new JLabel("Customer ID:");
l8=new JLabel("Customer Name:");
l9=new JLabel("Customer Address:");
l10=new JLabel("Purchase");
tf1=new JTextField(12);
tf2=new JTextField(12);
tf3=new JTextField(12);
tf4=new JTextField(12);
tf5=new JTextField(12);
tf6=new JTextField(12);
tf7=new JTextField(12);
tf8=new JTextField(12);
tf9=new JTextField(12);
tf10=new JTextField(12);
savebtn=new JButton(" Add ");
resetbtn=new JButton(" Reset");
editbtn1=new JButton(" Edit ");
editbtn2=new JButton(" Save");
deletebtn=new JButton("Delete");
p1.add(l1);
p1.add(tf1);
p1.add(l2);
p1.add(tf2);
p1.add(l3);
p1.add(tf3);
p1.add(l4);
p1.add(tf4);
p1.add(savebtn);
p1.add(resetbtn);

p2.add(l7);
p2.add(tf7);
p2.add(l8);
p2.add(tf8);
p2.add(l9);
p2.add(tf9);
p2.add(l10);
p2.add(tf10);
p2.add(editbtn1);
p2.add(editbtn2);

p3.add(l5);
p3.add(tf5);
p3.add(deletebtn);
resetbtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
tf1.setText("");
tf2.setText("");
tf3.setText("");
tf4.setText("");
}
});
savebtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
String value1=tf1.getText();
String value2=tf2.getText();
String value3=tf3.getText();
String value4=tf4.getText();
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";
String db = "test";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "root";
System.out.println(value1+value2+value3+value4);
try{
Class.forName(driver);
con = DriverManager.getConnection(url+db, user, pass);
PreparedStatement st=con.prepareStatement("insert into customer(cus_id,cus_name,cus_address,
salary) values(?,?,?,?)");
st.setString(1,value1);
st.setString(2,value2);
st.setString(3,value3);
st.setString(4,value4);
st.executeUpdate();
JOptionPane.showMessageDialog(p1,"Data is successfully inserted into database.");
con.close();
}
catch(Exception e){
JOptionPane.showMessageDialog(p1,"Error in submitting data!");
}
}
});

deletebtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){

String value1=tf5.getText();
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";
String db = "CustomerDetails";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "root";
try{
Class.forName(driver);
con = DriverManager.getConnection(url+db, user, pass);
PreparedStatement st=con.prepareStatement("DELETE FROM customer WHERE cus_id = ?");
st.setString(1,value1);
st.executeUpdate();
JOptionPane.showMessageDialog(p3,"Record is deleted successfully.");
con.close();
}
catch(Exception exp3)
{
JOptionPane.showMessageDialog(p3,"Error in deleting record.");
}
}
});
editbtn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){

String value=tf7.getText();
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";
String db = "CustomerDetails";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "root";
try{
Class.forName(driver);
con = DriverManager.getConnection(url+db, user, pass);
PreparedStatement st=con.prepareStatement("select * from customer where cus_id=?");
st.setString(1,value);
ResultSet res=st.executeQuery();
res.next();
tf7.setText(Integer.toString(res.getInt(1)));
tf8.setText(res.getString(2));
tf9.setText(res.getString(3));
tf10.setText(Integer.toString(res.getInt(4)));
con.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(p2,"Can't edit data");
}
}
});
editbtn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";
String db = "CustomerDetails";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "root";
try
{
int x=JOptionPane.showConfirmDialog(p2,"Confirm edit? All data will be replaced");
if(x==0){
try{
String value1=tf7.getText();
String value2=tf8.getText();
String value3=tf9.getText();
String value4=tf10.getText();

Class.forName(driver);
con = DriverManager.getConnection(url+db, user, pass);;
Statement st=con.createStatement();
st.executeUpdate("update customer set cus_name='"+value2+"', cus_address='"+value3+"',
salary='"+value4+"' where cus_id='"+value1+"'");
JOptionPane.showMessageDialog(p2,"Updated successfully");
con.close();
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(p2,"Error in updating edit fields");
}
}
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(p2,"Error");
}
}
});
}
void dis()
{
f.getContentPane().add(tp);
tp.addTab("Add Record",p1);
tp.addTab("Edit Record",p2);
tp.addTab("Delete Record",p3);

f.setSize(350,180);
f.setVisible(true);
f.setResizable(true);
}
public static void main(String z[]){
CustomerInformation pro=new CustomerInformation();
pro.dis();
}
}

2. Write a program that keeps an appointment calendar in a database. An appointment includes a date, the starting time, the ending time, and a description; for example:

Dentist 2013/9/12 14:30 16:30
Class 2013/9/29 15:30 18:30

Provide a user interface to add appointments, removed cancelled aapointments and print list of appointments for a particular day. Your user interface can either be text- based or graphical.

Code:
/**
Basic constructor for Calender
*/
public Calender(dd HH:mm:ss"))
{
this.HH:mm:ss") = HH:mm:ss");
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMM dd HH:mm:ss");    
    Calendar calendar = new GregorianCalendar(2013,1,28,13,24,56);

    int year       = calendar.get(Calendar.YEAR);
    int month      = calendar.get(Calendar.MONTH); // Jan = 0, dec = 11
    int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
    int dayOfWeek  = calendar.get(Calendar.DAY_OF_WEEK);
    int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);
    int weekOfMonth= calendar.get(Calendar.WEEK_OF_MONTH);

    int hour       = calendar.get(Calendar.HOUR);        // 12 hour clock
    int hourOfDay  = calendar.get(Calendar.HOUR_OF_DAY); // 24 hour clock
    int minute     = calendar.get(Calendar.MINUTE);
    int second     = calendar.get(Calendar.SECOND);
    int millisecond= calendar.get(Calendar.MILLISECOND);

    System.out.println(sdf.format(calendar.getTime()));

    System.out.println("year \t\t: " + year);
    System.out.println("month \t\t: " + month);
    System.out.println("dayOfMonth \t: " + dayOfMonth);
    System.out.println("dayOfWeek \t: " + dayOfWeek);
    System.out.println("weekOfYear \t: " + weekOfYear);
    System.out.println("weekOfMonth \t: " + weekOfMonth);

    System.out.println("hour \t\t: " + hour);
    System.out.println("hourOfDay \t: " + hourOfDay);
    System.out.println("minute \t\t: " + minute);
    System.out.println("second \t\t: " + second);
    System.out.println("millisecond \t: " + millisecond);
Quote:Before coding your application construct the use diagram and the activity diagram for the application.

Dah buat?
dah buat tapi lupa letak mana..

CustomerInformation.java:18: invalid method declaration; return type required
EmployeeInformation(){
^
CustomerInformation.java:98: unclosed string literal
PreparedStatement st=con.prepareStatement("insert into customer(cus_id,cus_name,cus_address,
^
CustomerInformation.java:98: ';' expected
PreparedStatement st=con.prepareStatement("insert into customer(cus_id,cus_name,cus_address,
^
CustomerInformation.java:99: illegal start of expression
salary) values(?,?,?,?)");
^
CustomerInformation.java:99: ';' expected
salary) values(?,?,?,?)");
^
CustomerInformation.java:99: unclosed string literal
salary) values(?,?,?,?)");
^
CustomerInformation.java:189: unclosed string literal
st.executeUpdate("update customer set cus_name='"+value2+"', cus_address='"+value3+"',
^
CustomerInformation.java:189: ';' expected
st.executeUpdate("update customer set cus_name='"+value2+"', cus_address='"+value3+"',
^
CustomerInformation.java:190: illegal start of expression
salary='"+value4+"' where cus_id='"+value1+"'");
^
CustomerInformation.java:190: unclosed character literal
salary='"+value4+"' where cus_id='"+value1+"'");
^
CustomerInformation.java:190: not a statement
salary='"+value4+"' where cus_id='"+value1+"'");
^
CustomerInformation.java:190: ';' expected
salary='"+value4+"' where cus_id='"+value1+"'");
^
12 errors
[Image: lwok.png] Thumbs Up

develop pakai IDE ke notepad ni? kalau guna notepad download ide khas utk java mcm intelliJ... guna ide nampak mana salah...

kalendar tu nape macam tak abes?
(27-11-2013, 12:20 PM)syahmiazhar Wrote: [ -> ][Image: lwok.png] Thumbs Up

develop pakai IDE ke notepad ni? kalau guna notepad download ide khas utk java mcm intelliJ... guna ide nampak mana salah...

Aku compile pakai javac...yeke?

ini code yang aku kasi tuh ke?
la kod sendiri takkan tak kenal kot... yup...

CustomerInformation.java:18: invalid method declaration; return type required <- nama constructor tak sama dengan nama class

CustomerInformation.java:98: unclosed string literal <- ada newline kat string...
tgk line 18, 98, 189...
(27-11-2013, 12:26 PM)syahmiazhar Wrote: [ -> ]la kod sendiri takkan tak kenal kot... yup...

CustomerInformation.java:18: invalid method declaration; return type required <- nama constructor tak sama dengan nama class

CustomerInformation.java:98: unclosed string literal <- ada newline kat string...
tgk line 18, 98, 189...

Thanks kerana membantu...nie syahmi kat KD tu ke?
KD tu ape?
nie bkn ke miexox?
huhu... macam bukan...
baik la...