Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Soalan Java
27-11-2013, 10:22 AM, (This post was last modified: 27-11-2013, 12:07 PM by darknite.)
Post: #1
Soalan Java
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);


Attached Files
.doc   Soalan Java 1.doc (Size: 32 KB / Downloads: 5)
.doc   Soalan Java 2.doc (Size: 24 KB / Downloads: 6)
Reply
27-11-2013, 11:14 AM,
Post: #2
RE: Soalan Java
Quote:Before coding your application construct the use diagram and the activity diagram for the application.

Dah buat?

  ▲
▲ ▲
Spoiler:

surah Ali Imran Ayat 31

Reply
27-11-2013, 11:18 AM, (This post was last modified: 27-11-2013, 12:02 PM by darknite.)
Post: #3
RE: Soalan Java
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
Reply
27-11-2013, 12:20 PM, (This post was last modified: 27-11-2013, 12:22 PM by syahmiazhar.)
Post: #4
RE: Soalan Java
[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?
[Image: 1Z3i3.jpg]
Reply
27-11-2013, 12:23 PM,
Post: #5
RE: Soalan Java
(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?
Reply
27-11-2013, 12:26 PM,
Post: #6
RE: Soalan Java
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...
[Image: 1Z3i3.jpg]
Reply
27-11-2013, 12:27 PM,
Post: #7
RE: Soalan Java
(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?
Reply
27-11-2013, 12:36 PM,
Post: #8
RE: Soalan Java
KD tu ape?
[Image: 1Z3i3.jpg]
Reply
27-11-2013, 12:37 PM,
Post: #9
RE: Soalan Java
nie bkn ke miexox?
Reply
27-11-2013, 01:23 PM,
Post: #10
RE: Soalan Java
huhu... macam bukan...
[Image: 1Z3i3.jpg]
Reply
27-11-2013, 01:47 PM,
Post: #11
RE: Soalan Java
baik la...
Reply


Possibly Related Threads...
Thread Author Replies Views Last Post
  soalan java. hard3 4 902 10-05-2013, 10:33 PM
Last Post: JejakaPemalu
  [help] inputfile java please click me to know more Timon 0 387 14-04-2013, 10:02 PM
Last Post: Timon
  [problem]with java tcp client and server chat pron3t 1 548 26-02-2013, 01:21 PM
Last Post: muhaznar
  java API Timon 3 814 09-12-2012, 09:58 PM
Last Post: lamki
  java background music library Timon 0 439 09-12-2012, 05:21 PM
Last Post: Timon
  JAVA Layout... mana 1 feveret :P 127001 7 1,663 03-12-2012, 10:32 AM
Last Post: Wushipher
  Java | MS Excell 127001 5 1,031 29-07-2012, 12:41 AM
Last Post: lamki
  Tutorial: Menghubungkan SQLite Dengan Java s3m00t 4 1,275 25-07-2012, 02:40 AM
Last Post: s3m00t
  [java prob]guna user input dan methode erroe indintifier pron3t 2 655 22-04-2012, 07:56 PM
Last Post: pron3t
  [Tut]Java Array Name Ujang 2 722 19-04-2012, 03:07 PM
Last Post: pron3t

Forum Jump: