当前位置:网站首页>Course design summary

Course design summary

2022-06-11 04:57:00 megaData

index:

package megadata.zfx.test01;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;

public class index {
    private JLabel label3;// Shopping title 
    private JFrame frame;// window 
    private JLabel label1;// Account tag 
    private JTextField field1;// Enter account 
    private JLabel label2;// Password tags 
    private JPasswordField field2;// Password text 
    private JButton b1;// The login button 
    private JButton b2;// Registration button 


    public static void main(String[] args){
        new index();
    }
    public index(){
        setFrame();
        listen();
    }
    public void setFrame(){
        // Interface 
        frame = new JFrame(" Shopping system login ");
        frame.setLayout(new GridLayout(5,1));
        frame.setBounds(100,100,400,400);
        frame.setLocationRelativeTo(null);// Set the screen to the center 

        ImageIcon bg = new ImageIcon("src/resource/pict.jpeg");	// Create a background picture 
        JLabel label=new JLabel(bg);// Add the background picture to the label 
        label.setBounds(100, 100,240, 36);


        // Account and password , Log in to register 
        label3 = new JLabel(" Shopping system login ");
        label3.setFont(new Font(" Song style ",Font.PLAIN,20));
        label1 = new JLabel(" account number ");
        field1 = new JTextField("",30);
        label2 = new JLabel(" password ");
        field2 = new JPasswordField("",30);
        b1 = new JButton(" Sign in ");
        b2 = new JButton(" register ");

        JPanel d=new JPanel();
        frame.add(label);
        d.add(label3);
        frame.add(d);
        JPanel a=new JPanel();
        a.add(label1);
        a.add(field1);
        JPanel b=new JPanel();
        b.add(label2);
        b.add(field2);
        frame.add(a);
        frame.add(b);
        JPanel c=new JPanel();
        c.add(b1);
        c.add(b2);
        frame.add(c);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
    public void listen(){
        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.setVisible(false);
                new Sign(field1.getText(), String.valueOf(field2.getPassword()), frame);
                new shop1().loadFrame();
            }
        });
        b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.setVisible(false);
                new register();
            }
        });
    }
}

Print:

package megadata.zfx.test01;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


public class Print extends Frame {
    // attribute 
    TextArea text1 = new TextArea();



//    public static void main(String[] args) {
//        Print print = new Print();
//        print.loadFrame();
//    }

    public void loadFrame(){
        Label label1 = new Label("          *  Sike small supermarket  *          ");
        Label label2 = new Label(" shopping receipt ");

        // The small ticket text content is preset 
        label1.setFont(new Font(" In black ",Font.BOLD,19));
        label2.setFont(new Font(" In black ",Font.PLAIN,15));
        text1.setEditable(false);// The text box is not editable 

        setBounds(550,250,300,400);
        setLayout(new FlowLayout(FlowLayout.CENTER));
        add(label1);
        add(label2);
        add(text1);

        // Small ticket text output 
        for (int i = 0; i < shop1.v.size() / 4; i++) {
            String name = shop1.v.get(i * 4).toString();
            double price = Double.parseDouble(shop1.v.get(( i + 1) * 4 - 1).toString());
            text1.append("                         "+" Record " + (i + 1) + ":" + name + "," + price + " element " + "\r\n");
            text1.append("---------------------------------------" + "\r\n");
        }
        double sum1 = 0;
        for (int i = 1; i <= shop1.v.size() / 4; i++) {
            sum1 = sum1 + Double.parseDouble(shop1.v.get( 4 * i - 1).toString());
        }
        text1.append("                         "+" total " + sum1 + " element " + "\r\n");


        setVisible(true);
        setResizable(false);

        // Implementation of window closing 
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                setVisible(false);
                text1.setText("");
            }
        });
    }
}

register:

package megadata.zfx.test01;
//package login;

import com.megadata.test1.index;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class register {
    JFrame frame;
    private JPanel contentPanel = new JPanel();
    private JButton ok = new JButton(" Make sure to register ");
    private JButton back =new JButton(" Back to the login screen ");
    private JLabel jLabel1 = new JLabel(" user name ");
    private JLabel jLabel2 = new JLabel(" password ");
    private JTextField atf = new JTextField(" The account name should not exceed 6 A word ",20);
    private JTextField apw =new JTextField(" No more than 6 The number of digits ",20);
    private JTextField idea =new JTextField("",30);
    private static File file = new File(" Account and password .txt");
    public static void main(String[] args){
        new register();
    }
    public register(){
        setFrame();
        addListener();
    }
    public void setFrame(){
        frame = new JFrame(" User registration ");
        frame.setBounds(600,300,600,300);
        frame.setLayout(new GridLayout(4,1));
        frame.setLocationRelativeTo(null);// Set the window in the middle of the screen 
        // account number 
        JPanel panel1=new JPanel();
        panel1.add(jLabel1);
        panel1.add(atf);
        // password 
        JPanel panel2=new JPanel();
        panel2.add(jLabel2);
        panel2.add(apw);
        // Register and return 
        JPanel panel3=new JPanel();
        panel3.add(back);
        panel3.add(ok);
        // Tips 
        JPanel panel4=new JPanel();
        panel4.add(idea);
        frame.add(panel1);
        frame.add(panel2);
        frame.add(panel3);
        frame.add(panel4);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    private void addListener(){
        ok.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(atf.getText().length()>6||apw.getText().length()>6||atf.getText().isEmpty()||apw.getText().isEmpty()){
                    idea.setText(" Input error ");
                }else{
                    setRegister(atf.getText(),apw.getText());
                    idea.setText(" Registered successfully , You can continue to register or return to the login interface ");

                }
            }
        });
        back.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.setVisible(false);
                new index();
            }
        });
    }
    public void setRegister(String atf,String apw){
        try{
            BufferedWriter fileWriter=new BufferedWriter(new FileWriter(file,true));
            fileWriter.write(atf+"="+apw);
            fileWriter.newLine();
            fileWriter.flush();
            fileWriter.close();
        }catch(IOException e){
            System.out.println(" Input error ");
            e.printStackTrace();
        }
    }
}

shop1:

package megadata.zfx.test01;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Vector;

// Master page class 

public class shop1 extends Frame {
    // attribute 
    TextField num1,num2,num3,num4;
    static Vector<Object> v = new Vector<>();
    TextArea text = new TextArea();
    double sum = 0.0;
    int f = 0;


    // Method 
    public static int getVSize(){
        return v.size()/4;
    }
    //    public double getSum(int a){
//        return v.
//    }
    public void loadFrame(){

        num1 = new TextField(10);
        num2 = new TextField(10);
        num4 = new TextField(10);
        num3 = new TextField(5);



        Label label1 = new Label(" Product Name ");
        Label label2 = new Label(" Number ");
        Label label3 = new Label(" The unit price ");
        Label label4 = new Label(" location ");

        Button button = new Button(" add to ");
        Button button1 = new Button(" To delete a single ");
        Button button2 = new Button(" payment ");
        Button button3 = new Button(" detailed list ");
        Button button4 = new Button(" Clear the screen ");
        text.setEditable(false);                                            // Set the text area to be non editable 
        // Add a monitor 
        button.addActionListener(new MyAddListener());
        button1.addActionListener(new MyDeleteListener());
        button2.addActionListener(new MyPayListener());
        button3.addActionListener(new MyPrintListener());
        button4.addActionListener(new MyClearListener());

        // Layout 
        setLayout(new FlowLayout());
        setBounds(200,200,500,350);
        setLocationRelativeTo(null);// Set the screen to the center 
        add(label1);
        add(num1);
        add(label2);
        add(num2);
        add(label3);
        add(num4);
        add(button);
        add(label4);
        add(num3);
        add(button1);
        add(button2);
        add(button3);
        add(button4);
        add(text);


        setVisible(true);
        setResizable(false);          // Fixed window size 
        // Implementation of window closing 
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    // Monitor 
    private class MyAddListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            String n1 = num1.getText();
            int n2 = Integer.parseInt(num2.getText());
            double n3 = Double.parseDouble(num4.getText());
            double n4 = n2 * n3;

            // Add data into vector
            v.add(n1);
            v.add(n2);
            v.add(n3);
            v.add(n4);
            // stay TestArea Area print added information 
            text.append(" add to " + (v.size()/4) +" Record :" + n1 + n2 + " Jin , The unit price " + n3 + " element " + "\r\n");
            // Clear the input in the text box and wait for the next input 
            num1.setText("");
            num2.setText("");
            num4.setText("");
        }
    }
    // Delete a single listener 
    private class MyDeleteListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            int n5 = Integer.parseInt(num3.getText());
            text.append(" Delete " + n5 +" Record :" + v.get((n5 - 1) * 4) + v.get((n5 - 1) * 4 + 1) + " Jin , The unit price " + v.get((n5 - 1) * 4 + 2) + " element " + "\r\n");
            for (int i = 0; i < 4; i++) {
                v.remove((n5 - 1) * 4);
            }
            num3.setText("");
        }
    }
    // Payment listener 
    private class MyPayListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            for (int i = 1; i <= v.size() / 4; i++) {
                sum = sum + Double.parseDouble(v.get( 4 * i - 1).toString());
            }
            text.append(" You bought it " + v.size()/4 +" The total amount of each item is :" + sum + " element , Please pay , thank you ." + "\r\n");
            f = 1;                                                      // Print ticket identifier 
            sum = 0;
        }
    }
    // Empty monitor 
    private class MyClearListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            v.clear();                                      // Empty the container 
            text.setText("");                               // Empty textbox 
            f = 0;
        }
    }
    // Print ticket monitor 
    private class MyPrintListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            if(f==0){
                text.append(" Please check the receipt after confirming the payment " + "\r\n");
            }else{
                new Print().loadFrame();
            }
        }
    }
}





Sign:

package megadata.zfx.test01;
import javax.swing.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Sign {
    public static void main(String[] args){

    }
    public Sign(String atf, String apw, JFrame frame){
        if(login(atf,apw)){
            frame.setVisible(false);
        }else{
            JDialog mistaskeCase=new JDialog(frame," Tips ");
            mistaskeCase.setBounds(600,300,600,300);
            mistaskeCase.setLocationRelativeTo(null);
            JTextField fileOfError=new JTextField(" Wrong account or password ",25);
            mistaskeCase.add(fileOfError);
            mistaskeCase.setVisible(true);
            mistaskeCase.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }
    }
    public boolean login(String username,String password){
        boolean flag = false;
        BufferedReader lbd=null;
        try{
            lbd=new BufferedReader(new FileReader(" Account and password .txt"));
            String line=null;
            while((line=lbd.readLine())!=null){
                String[] datas = line.split("=");
                if(datas[0].equals(username)&& datas[1].equals(password)){
                    flag=true;
                    break;
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            assert lbd !=null;
            try{
                lbd.close();
            }catch(IOException e){
                e.printStackTrace();
            }
        }
        return flag;
    }
}

原网站

版权声明
本文为[megaData]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110451126067.html