Uploaded by Endris Yimer

2nd project

advertisement
Advanced Programming project 2 (10%)
Write a full program for the following programs.
cccccc.txt
1. Create two thread classes called GCDPrinterThread andLCDPrinterThread(you can use one
of the methods for creating a Thread Class) which prints GCD and LCD of a given number
entered by a user. The GCDs are printed in 2 second interval whereas the LCDs are printed
in 4 second interval. Then create an instance of both thread classes and invoke the run
method. #public class Main {
2.
3. public static void main(String[] args) {
4.
5.
java.util.Scanner in=new java.util.Scanner(System.in);
6.
System.out.println("Enter The First Number");
7.
int first=in.nextInt();
8.
System.out.println("Enter The Second Number");
9.
int second=in.nextInt();
10.
11.
GCDPrinterWriter g=new GCDPrinterWriter(first, second);
12.
g.start();
13.
14.
LCMPrinterWriter l=new LCMPrinterWriter(first, second);
15.
16.
l.start();
17. }
18.
19. }
20.
21.
22. #Assume you are writing a program for a video rental shop. Know you are supposed to do
only some part of the business logics here. The tables are already designed so you don’t
have to worry.
 Task One: design the user interface like below.
 Task Two: Connect to the database and query the database based on the event
handler’s duty.
i. Register button: the duty of this button is to record new movies’ Title, Genre and
Quantity which are purchased by the store owner in the Movie table. The
Movie_ID will be given automatically by the database.
ii. Rent Page Load :When the rent page is opened, the Customer Idcombobox will be
filled by customer ids available from the Customer table and the Movie
Idcombobox will be filled by movie ids available and rentable from the Movie
table.
iii. Rent button: the duties of this button are to register the rental details (the
Movie_IDandCutomer_ID) in RentalDetail table and to deduct (decrease) the
quantity of the movie available in Movie table.
DB
iv.
v. import java.awt.*;
vi. import java.awt.event.*;
vii. import java.util.*;
viii. import java.util.logging.Level;
ix. import java.util.logging.Logger;
x. import javax.swing.*;
xi. import java.sql.*;
xii. public class GroupSeven extends JFrame {
xiii.
xiv.
Random r;
xv.
JLabel homeLabel=new JLabel("Group Seven Video Store");
xvi.
xvii.
JButton registerPage=new JButton("Rgister Page");
xviii.
JButton rentPage=new JButton("Rent Page");
xix.
JButton registerM=new JButton("Register");
xx.
JButton registerC=new JButton("Register");
xxi.
JButton rent=new JButton("Rent");
xxii.
JButton cancel1=new JButton("Cancel");
xxiii.
JButton cancel2=new JButton("Cancel");
xxiv.
JButton cancel3=new JButton("Cancel");
xxv.
JButton exit=new JButton("Exit");
xxvi.
xxvii.
JLabel mtitle=new JLabel("Movie Title");
xxviii.
JLabel mgenre=new JLabel("Movie Gerene");
xxix.
JLabel mquantity=new JLabel("Movie Quantity");
xxx.
JLabel cid=new JLabel("Customer ID");
xxxi.
xxxii.
xxxiii.
xxxiv.
xxxv.
JLabel cname=new JLabel("Customer Name");
JLabel avmovies=new JLabel("Available Movies");
JLabel customer=new JLabel("Customer ID");
JComboBox mogenre=new JComboBox(new
String[]{"Action","Adventure","Animation","Comedy","Documentary","Drama",
xxxvi.
"Family","Horror","Romantic","Series","Sience
Fiction","Sport","Thriller","Other"});
xxxvii.
JComboBox customerId;
xxxviii.
JComboBox movieId;
xxxix.
xl.
JTextField motitle=new JTextField(15);
xli.
JTextField moquantity=new JTextField(15);
xlii.
JTextField cuid=new JTextField(15);
xliii.
JTextField cuname=new JTextField(15);
xliv.
xlv.
Connection con=null;
xlvi.
Statement stmt=null;
xlvii.
ResultSet rs=null;
xlviii.
PreparedStatement insert=null;
xlix.
PreparedStatement avmov=null;
l.
PreparedStatement avcus=null;
li.
canc a=new canc();
lii.
liii.
GroupSeven(){
liv.
super("Group Seven Video Rent Store");
lv.
setSize(600, 500);
lvi.
setDefaultCloseOperation(EXIT_ON_CLOSE);
lvii.
setVisible(true);
lviii.
lix.
homeLabel.setFont(new Font("Bauhaus 93", Font.PLAIN, 50));
lx.
setLayout(new GridBagLayout());
lxi.
final GridBagConstraints gbc=new GridBagConstraints();
lxii.
constraints(gbc, 0, 0, GridBagConstraints.CENTER); add(forHome(), gbc);
lxiii.
lxiv.
try{
lxv.
Class.forName("org.apache.derby.jdbc.ClientDriver");
lxvi.
con = DriverManager.getConnection
("jdbc:derby://localhost:1527/assignment", "abc", "abc");
lxvii.
stmt = con.createStatement();
lxviii.
lxix.
avmovies();
lxx.
avcustomers();
lxxi.
lxxii.
lxxiii.
}catch(Exception ex){
lxxiv.
JOptionPane.showMessageDialog(null, ex, "Error",
JOptionPane.ERROR_MESSAGE);
lxxv.
}
lxxvi.
lxxvii.
lxxviii.
lxxix.
lxxx.
registerPage.addActionListener(new ActionListener(){
lxxxi.
public void actionPerformed(ActionEvent e){
lxxxii.
forRegisterPage().setVisible(true);
lxxxiii.
forRentPage().setVisible(false);
lxxxiv.
setSize(getWidth()+1, getHeight()+1);
lxxxv.
constraints(gbc, 0, 1, GridBagConstraints.CENTER);
add(forRegisterPage(),gbc, 0);
lxxxvi.
setSize(getWidth()-1, getHeight()-1);
lxxxvii.
}
lxxxviii.
});
lxxxix.
xc.
rentPage.addActionListener(new ActionListener(){
xci.
public void actionPerformed(ActionEvent e){
xcii.
try {
xciii.
avmovies();
xciv.
avcustomers();
xcv.
forRentPage().setVisible(true);
xcvi.
forRegisterPage().setVisible(false);
xcvii.
setSize(getWidth() + 1, getHeight() + 1);
xcviii.
constraints(gbc, 0, 1, GridBagConstraints.CENTER); add(forRentPage(),
gbc, 0);
xcix.
setSize(getWidth() - 1, getHeight() - 1);
c.
ci.
} catch (SQLException ex) {
Logger.getLogger(GroupSeven.class.getName()).log(Level.SEVERE, null,
ex);
cii.
ciii.
civ.
cv.
cvi.
cvii.
cviii.
cix.
cx.
cxi.
cxii.
cxiii.
cxiv.
cxv.
cxvi.
cxvii.
cxviii.
cxix.
cxx.
cxxi.
cxxii.
cxxiii.
cxxiv.
cxxv.
cxxvi.
cxxvii.
cxxviii.
cxxix.
cxxx.
cxxxi.
cxxxii.
cxxxiii.
}
}
});
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int select=JOptionPane.showConfirmDialog(rootPane, "Are you sure to
exit?", "Exit?", JOptionPane.YES_NO_OPTION);
if(select==JOptionPane.YES_OPTION){
System.exit(0);
}
}
});
registerM.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String title=motitle.getText();
String quan=moquantity.getText();
String type=(String) mogenre.getSelectedItem();
if(title.equals("")|| quan.equals("")){
JOptionPane.showMessageDialog(null, "Please Fill All Information",
"Error", JOptionPane.ERROR_MESSAGE);
}
else{
try{
int id=(int) (Math.random() * 1000000);
int no=Integer.parseInt(quan);
int c=no;
insert=con.prepareStatement("INSERT INTO movie VALUES (?, ?, ?, ?,
?)");
insert.setInt(1, id);
cxxxiv.
cxxxv.
cxxxvi.
cxxxvii.
cxxxviii.
cxxxix.
cxl.
cxli.
cxlii.
cxliii.
cxliv.
cxlv.
cxlvi.
cxlvii.
cxlviii.
cxlix.
cl.
cli.
insert.setString(2, title);
insert.setString(3, type);
insert.setInt(4, no);
insert.setInt(5, c);
insert.executeUpdate();
JOptionPane.showMessageDialog(null, "You have Succesfully Register\n" +
"The Movie "+title+".\n\n" +
"ID For "+title+" is\n" +
id, "Success", JOptionPane.INFORMATION_MESSAGE);
motitle.setText("");
moquantity.setText("");
mogenre.setSelectedIndex(0);
}
catch(Exception ex){
Logger.getLogger(GroupSeven.class.getName()).log(Level.SEVERE,
null, ex);
clii.
cliii.
cliv.
clv.
clvi.
clvii.
clviii.
clix.
clx.
clxi.
clxii.
clxiii.
clxiv.
clxv.
clxvi.
clxvii.
clxviii.
clxix.
}
}
}
});
registerC.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String cids=cuid.getText();
String cnames=cuname.getText();
if(cids.equals("")|| cnames.equals("")){
JOptionPane.showMessageDialog(null, "Please Fill All Information",
"Error", JOptionPane.ERROR_MESSAGE);
}
else{
try{
insert=con.prepareStatement("INSERT INTO customer VALUES (?, ?)");
insert.setString(1, cids);
clxx.
clxxi.
clxxii.
clxxiii.
insert.setString(2, cnames);
insert.executeUpdate();
JOptionPane.showMessageDialog(null, "You Have Suucessfully Add A
New Customer", "Sucess", JOptionPane.INFORMATION_MESSAGE);
clxxiv.
cuid.setText("");
clxxv.
cuname.setText("");
clxxvi.
clxxvii.
clxxviii.
}
clxxix.
catch(Exception ex){
clxxx.
JOptionPane.showMessageDialog(null, ex, "Error",
JOptionPane.ERROR_MESSAGE);
clxxxi.
}
clxxxii.
}
clxxxiii.
clxxxiv.
}
clxxxv.
});
clxxxvi.
clxxxvii.
clxxxviii.
rent.addActionListener(new ActionListener(){
clxxxix.
public void actionPerformed(ActionEvent e){
cxc.
String cc=(String) customerId.getSelectedItem();
cxci.
String mm=(String) movieId.getSelectedItem();
cxcii.
int cur=0;
cxciii.
try {
cxciv.
cxcv.
PreparedStatement r = con.prepareStatement("SELECT currentno FROM
movie WHERE mtitle=?");
cxcvi.
r.setString(1, mm);
cxcvii.
cxcviii.
rs=r.executeQuery();
cxcix.
while(rs.next()){
cc.
cur=rs.getInt("currentno");
cci.
}
ccii.
cur=cur-1;
cciii.
cciv.
r=con.prepareStatement("UPDATE movie set currentno=? WHERE
mtitle=?");
ccv.
ccvi.
ccvii.
ccviii.
ccix.
r.setInt(1, cur);
r.setString(2, mm);
r.execute();
JOptionPane.showMessageDialog(null, "You Have Sucessfully Rent The
Film\n" +
ccx.
"Remaining "+cur, "Succes",
JOptionPane.INFORMATION_MESSAGE);
ccxi.
ccxii.
ccxiii.
ccxiv.
ccxv.
avmovies();
} catch (Exception ex) {
Logger.getLogger(GroupSeven.class.getName()).log(Level.SEVERE, null,
ex);
ccxvi.
ccxvii.
ccxviii.
ccxix.
ccxx.
ccxxi.
ccxxii.
ccxxiii.
ccxxiv.
ccxxv.
ccxxvi.
ccxxvii.
ccxxviii.
ccxxix.
ccxxx.
ccxxxi.
ccxxxii.
ccxxxiii.
ccxxxiv.
ccxxxv.
ccxxxvi.
ccxxxvii.
}
}
});
cancel1.addActionListener(a);
cancel1.addActionListener(a);
cancel1.addActionListener(a);
}
public class canc implements ActionListener{
public void actionPerformed(ActionEvent e) {
motitle.setText("");
moquantity.setText("");
cuid.setText("");
cuname.setText("");
mogenre.setSelectedIndex(0);
ccxxxviii.
ccxxxix.
ccxl.
ccxli.
ccxlii.
ccxliii.
ccxliv.
ccxlv.
ccxlvi.
ccxlvii.
ccxlviii.
}
}
public JPanel forButton(){
JPanel forButton=new JPanel(new GridBagLayout());
forButton.setBorder(BorderFactory.createLineBorder(new Color(60, 0, 0), 3));
GridBagConstraints gbc=new GridBagConstraints();
constraints(gbc, 0, 0, GridBagConstraints.CENTER); forButton.add(registerPage,
gbc);
ccxlix.
constraints(gbc, 1, 0, GridBagConstraints.CENTER); forButton.add(rentPage,
gbc);
ccl.
constraints(gbc, 2, 0, GridBagConstraints.CENTER); forButton.add(exit, gbc);
ccli.
cclii.
return forButton;
ccliii.
}
ccliv.
cclv.
cclvi.
public JPanel forHome(){
cclvii.
JPanel forHome=new JPanel(new GridBagLayout());
cclviii.
GridBagConstraints gbc=new GridBagConstraints();
cclix.
constraints(gbc, 0, 0, GridBagConstraints.CENTER); forHome.add(homeLabel,
gbc);
cclx.
constraints(gbc, 0, 1, GridBagConstraints.CENTER); forHome.add(forButton(),
gbc);
cclxi.
cclxii.
return forHome;
cclxiii.
}
cclxiv.
cclxv.
cclxvi.
public JPanel regcusbut(){
cclxvii.
JPanel buttons=new JPanel(new GridBagLayout());
cclxviii.
buttons.add(registerC);
cclxix.
buttons.add(cancel2);
cclxx.
cclxxi.
return buttons;
cclxxii.
cclxxiii.
cclxxiv.
cclxxv.
cclxxvi.
cclxxvii.
cclxxviii.
cclxxix.
cclxxx.
cclxxxi.
cclxxxii.
cclxxxiii.
cclxxxiv.
cclxxxv.
cclxxxvi.
cclxxxvii.
cclxxxviii.
cclxxxix.
ccxc.
ccxci.
ccxcii.
ccxciii.
ccxciv.
ccxcv.
ccxcvi.
ccxcvii.
ccxcviii.
ccxcix.
ccc.
ccci.
cccii.
ccciii.
ccciv.
cccv.
cccvi.
cccvii.
cccviii.
cccix.
}
public JPanel regmovbut(){
JPanel buttons=new JPanel(new GridBagLayout());
buttons.add(registerM);
buttons.add(cancel1);
return buttons;
}
public JPanel rentbut(){
JPanel buttons=new JPanel(new GridBagLayout());
buttons.add(rent);
buttons.add(cancel3);
return buttons;
}
public JPanel movier(){
JPanel mov=new JPanel(new GridBagLayout());
mov.setBorder(BorderFactory.createLineBorder(new Color(60, 0, 0), 5));
GridBagConstraints gbc=new GridBagConstraints();
constraints(gbc, 0, 0, GridBagConstraints.EAST); mov.add(mtitle, gbc);
constraints(gbc, 1, 0, GridBagConstraints.WEST); mov.add(motitle, gbc);
constraints(gbc, 0, 1, GridBagConstraints.EAST); mov.add(mquantity, gbc);
constraints(gbc, 1, 1, GridBagConstraints.WEST); mov.add(moquantity, gbc);
constraints(gbc, 0, 2, GridBagConstraints.EAST); mov.add(mgenre, gbc);
constraints(gbc, 1, 2, GridBagConstraints.WEST); mov.add(mogenre, gbc);
constraints(gbc, 1, 3, GridBagConstraints.EAST); mov.add(regmovbut(), gbc);
return mov;
}
public JPanel customerr(){
JPanel cus=new JPanel(new GridBagLayout());
cccx.
cus.setBorder(BorderFactory.createLineBorder(new Color(60, 0, 0), 5));
cccxi.
GridBagConstraints gbc=new GridBagConstraints();
cccxii.
constraints(gbc, 0, 0, GridBagConstraints.EAST); cus.add(cid, gbc);
cccxiii.
constraints(gbc, 1, 0, GridBagConstraints.WEST); cus.add(cuid, gbc);
cccxiv.
constraints(gbc, 0, 1, GridBagConstraints.EAST); cus.add(cname, gbc);
cccxv.
constraints(gbc, 1, 1, GridBagConstraints.WEST); cus.add(cuname, gbc);
cccxvi.
constraints(gbc, 1, 2, GridBagConstraints.EAST); cus.add(regcusbut(), gbc);
cccxvii.
cccxviii.
return cus;
cccxix.
}
cccxx.
cccxxi.
cccxxii.
public JTabbedPane forRegisterPage(){
cccxxiii.
JTabbedPane forRegister=new JTabbedPane();
cccxxiv.
forRegister.add(movier(), "Register a movie");
cccxxv.
forRegister.add(customerr(), "Register a customer");
cccxxvi.
cccxxvii.
return forRegister;
cccxxviii.
}
cccxxix.
cccxxx.
cccxxxi. public JPanel forRentPage(){
cccxxxii.
JPanel forRent=new JPanel(new GridBagLayout());
cccxxxiii.
forRent.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));
cccxxxiv.
cccxxxv.
GridBagConstraints gbc=new GridBagConstraints();
cccxxxvi.
constraints(gbc,0, 0, GridBagConstraints.EAST); forRent.add(avmovies, gbc);
cccxxxvii.
constraints(gbc,1, 0, GridBagConstraints.WEST); forRent.add(movieId, gbc);
cccxxxviii.
constraints(gbc,0, 1, GridBagConstraints.EAST); forRent.add(customer, gbc);
cccxxxix.
constraints(gbc,1, 1, GridBagConstraints.WEST); forRent.add(customerId, gbc);
cccxl.
constraints(gbc,1, 2, GridBagConstraints.EAST); forRent.add(rentbut(), gbc);
cccxli.
cccxlii.
return forRent;
cccxliii.
}
cccxliv.
cccxlv.
cccxlvi. void avmovies() throws SQLException{
cccxlvii.
cccxlviii.
cccxlix.
cccl.
cccli.
ccclii.
cccliii.
cccliv.
ccclv.
ccclvi.
ccclvii.
ccclviii.
ccclix.
ccclx.
ccclxi.
ccclxii.
ccclxiii.
ccclxiv.
ccclxv.
ccclxvi.
ccclxvii.
ccclxviii.
ccclxix.
ccclxx.
ccclxxi.
ccclxxii.
ccclxxiii.
ccclxxiv.
ccclxxv.
ccclxxvi.
ccclxxvii.
ccclxxviii.
ccclxxix.
ccclxxx.
ccclxxxi.
ccclxxxii.
ccclxxxiii.
ccclxxxiv.
rs = stmt.executeQuery("SELECT mtitle FROM movie WHERE currentno > 0
ORDER BY mtitle");
String[] mov=new String[100];
int i=0;
while (rs.next()) {
mov[i]= String.format("%s", rs.getObject("mtitle"));
i++;
}
String [] correct=new String[i];
for(int j=0; j<i; j++){
correct[j]=mov[j];
}
movieId=new JComboBox(correct);
}
void avcustomers() throws SQLException{
rs = stmt.executeQuery("SELECT cid FROM customer ORDER BY cid");
String[] cus = new String[100];
int i=0;
while (rs.next()) {
cus[i] = String.format("%s", rs.getObject("cid"));
i++;
}
String [] correct=new String[i];
for(int j=0; j<i; j++){
correct[j]=cus[j];
}
customerId=new JComboBox(correct);
}
public void constraints(GridBagConstraints gbc, int x, int y, int anchor){
gbc.gridx=x;
gbc.gridy=y;
gbc.anchor=anchor;
ccclxxxv.
}
ccclxxxvi.
ccclxxxvii.
ccclxxxviii.
public static void main(String[] args) {
ccclxxxix.
GroupSeven g7=new GroupSeven();
cccxc.
}
cccxci. }
DB
create table custumer(
cid not null,
cname varcher(20),
primery key(cid),
creat table movie,
id int not null,
mtitel varchur(20),
mtype varchare(20),
total not null,
current int not null,
primery key(id),
Download