2013-04-11 2 views
0

Grocery_shop클래스가 다른 클래스

import java.awt.*; 

import javax.swing.*; 
import java.awt.event.*; 
import java.text.DecimalFormat; 

/** 
    Grocery shop class 

*/ 

public class Grocery_shop extends JFrame 
{ 

    private quantitypanel qty; // A panel for quantity 
    private Grocery_items items;  // A panel for routine charge checkboxes 
    private JPanel buttonPanel; // A panel for the buttons 
    private JButton calcButton;   // Calculates everything 
    private JButton exitButton;   // Exits the application 

    private invoiceClass invoice; 

    /** 
     Constructor 
    */ 

    public Grocery_shop() 
    { 
     // Display a title. 
     setTitle("Victor's Grocery Shop"); 

     // Specify what happens when the close button is clicked. 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 


// Create a NonRoutinePanel object. 
     qty = new quantitypanel(); 
    // qty.setBackground(Color.white); 

     // Create a RoutinePanel object. 
     items = new Grocery_items(qty); 



     // Build the panel that contains the buttons. 
     buildButtonPanel(); 

     // Add the panels to the content pane. 
     add(items, BorderLayout.WEST); 
     add(qty, BorderLayout.EAST); 
     add(buttonPanel, BorderLayout.SOUTH); 

     // Pack and display the window. 
     pack(); 
     setVisible(true); 
    } 

    /** 
     The buildButtonPanel method creates a panel containing 
     buttons. 
    */ 

    private void buildButtonPanel() 
    { 
     // Create a button to calculate the charges. 
     calcButton = new JButton("Add Charges"); 

     // Add an action listener to the button. 
     calcButton.addActionListener(new CalcButtonListener()); 

     // Create a button to exit the application. 
     exitButton = new JButton("Exit"); 

     // Add an action listener to the button. 
     exitButton.addActionListener(new ExitButtonListener()); 

     // Put the buttons in their own panel. 
     buttonPanel = new JPanel(); 
     buttonPanel.add(calcButton); 
     buttonPanel.add(exitButton); 
    } 

    /** 
     CalcButtonListener is an action listener class for the 
     calcButton component. 
    */ 

    private class CalcButtonListener implements ActionListener 
    { 
     /** 
     actionPerformed method 
     @param e An ActionEvent object. 
     */ 

     public void actionPerformed(ActionEvent e) 
     { 
     double totalCharges; // Total charges 

     // Create a DecimalFormat object to format output. 
     DecimalFormat dollar = new DecimalFormat("#,##0.00"); 

     // Calculate the total charges 
     totalCharges = items.getCharges(); 
       //+ nonRoutine.getCharges(); 

     // Display the message. 
     JOptionPane.showMessageDialog(null, "Total Charges: $" + 
              dollar.format(totalCharges)); 
     invoice = new invoiceClass(); 
     invoice.getClass(); 
     } 
    } // End of inner class 

    /** 
     ExitButtonListener is an action listener class for the 
     exitButton component. 
    */ 

    private class ExitButtonListener implements ActionListener 
    { 
     /** 
     actionPerformed method 
     @param e An ActionEvent object. 
     */ 

     public void actionPerformed(ActionEvent e) 
     { 

     System.exit(0); 
     } 
    } // End of inner class 

    /** 
     The main method creates an instance of the JoesAutomotive 
     class, causing it to display its window. 
    */ 

    public static void main(String[] args) 
    { 
     Grocery_shop grocery = new Grocery_shop(); 
    } 
} 

Grocery_items

import java.awt.*; 
import javax.swing.*; 

import java.text.DecimalFormat; 

/** 
    RoutinePanel class 

*/ 

public class Grocery_items extends JPanel 
{ 
    // Named constants for charges 
    private final double Baked_Beans = 0.35; 
    private final double Cornflakes = 1.75; 
    private final double Sugar = 0.75; 
    private final double Tea_Bags = 1.15; 
    private final double Instant_Coffee = 2.50; 
    private final double Bread = 1.25; 
    private final double Sausage = 1.30; 
    private final double Eggs = 0.75; 
    private final double Milk = 0.65; 
    private final double Potatoes = 2.00; 

    private quantitypanel qty; // A panel for quantity 


    private JCheckBox baked_beans_box;  // Check box for baked_beans 
    private JCheckBox CornflakesBox;  // Check box for cornflakes 
    private JCheckBox SugarBox; // Check box for sugar box 
    private JCheckBox Tea_Bags_Box; // Check box for tea bag 
    private JCheckBox Instant_Coffee_Box; // Check box for Instant_Coffee_Box 
    private JCheckBox Bread_Box;  // Check box for bread box 
    private JCheckBox SausageBox; // Check box for sausage box 
    private JCheckBox eggbox; // Check box for egg box 
    private JCheckBox milkbox; // Check box for milk 
    private JCheckBox potatoesbox; // Check box for potatoes 

// private JTextField baked_beans_JT; 
    /** 
     Constructor 
    */ 

    public Grocery_items(quantitypanel qty) 
    { 
    this.qty = qty; 
     DecimalFormat dollar = new DecimalFormat("#,##0.00"); 


     // Create the check boxes. 
     baked_beans_box = new JCheckBox("Baked_Beans ($" + 
           dollar.format(Baked_Beans) + ")"); 



     CornflakesBox = new JCheckBox("Cornflakes ($" + 
           dollar.format(Cornflakes) + ")"); 
     SugarBox = new JCheckBox("Sugar ($" + 
            dollar.format(Sugar) + ")"); 
     Tea_Bags_Box = new JCheckBox("Tea Bags ($" + 
           dollar.format(Tea_Bags) + ")"); 
     Instant_Coffee_Box = new JCheckBox("Instant Coffee_Box ($" + 
           dollar.format(Instant_Coffee) + ")"); 
     Bread_Box = new JCheckBox("Bread Box ($" + 
           dollar.format(Bread) + ")"); 
     SausageBox = new JCheckBox("Suasages ($" + 
            dollar.format(Sausage) + ")"); 
     eggbox = new JCheckBox("Eggs ($" + 
       dollar.format(Eggs) + ")"); 

     milkbox = new JCheckBox("Milk ($" + 
       dollar.format(Milk) + ")"); 
     potatoesbox = new JCheckBox("Potatoes ($" + 
       dollar.format(Potatoes) + ")"); 

     // Create a GridLayout manager. 
     setLayout(new GridLayout(10, 1)); 

     // Create a border. 
     setBorder(BorderFactory.createTitledBorder("Grocery Items")); 

     // Add the check boxes to this panel. 
     add(baked_beans_box); 
     add(CornflakesBox); 
     add(SugarBox); 
     add(Tea_Bags_Box); 
     add(Instant_Coffee_Box); 
     add(Bread_Box); 
     add(SausageBox); 
     add(eggbox); 
     add(milkbox); 
     add(potatoesbox); 

    } 



    /** 
     The getCharges method calculates the routine charges. 
     @return The amount of routine charges. 
    */ 

    public double getCharges() 
    { 
     double charges = 0; 

     if (baked_beans_box.isSelected()) 
     charges += Baked_Beans * qty.getBeanqty(); 
     if (CornflakesBox.isSelected()) 
     charges += Cornflakes; 
     if (SugarBox.isSelected()) 
     charges += Sugar; 
     if (Tea_Bags_Box.isSelected()) 
     charges += Tea_Bags; 
     if (Instant_Coffee_Box.isSelected()) 
     charges += Instant_Coffee; 
     if (Bread_Box.isSelected()) 
     charges += Bread; 
     if (SausageBox.isSelected()) 
     charges += Sausage; 
     if (eggbox.isSelected()) 
      charges += Eggs; 
     if (milkbox.isSelected()) 
      charges += Milk; 
     if (potatoesbox.isSelected()) 
      charges += Potatoes; 



     return charges; 
    } 
} 

quantitypanel 위의 코드와

//import java.awt.LayoutManager; 
    import java.awt.GridLayout; 
    //import javax.swing.JCheckBox; 
    //import javax.swing.JLabel; 
    import javax.swing.BorderFactory; 
    import javax.swing.JOptionPane; 
    import javax.swing.JPanel; 
    import javax.swing.JTextField; 


    public class quantitypanel extends JPanel { 
     private JTextField baked_beans_JT;  // JTextField box for baked_beans 
      private JTextField Cornflakes_JT;  // JTextField box for cornflakes 
      private JTextField Sugar_JT; // JTextField box for sugar box 
      private JTextField Tea_Bags_JT; // JTextField box for tea bag 
      private JTextField Instant_Coffee_JT; // JTextField box for Instant_Coffee_Box 
      private JTextField Bread_JT;  // JTextField box for bread box 
      private JTextField Sausage_JT; // JTextField box for sausage box 
      private JTextField egg_JT; // JTextField box for egg box 
      private JTextField milk_JT; // JTextField box for milk 
      private JTextField potatoes_JT; // JTextField box for potatoes 


      public quantitypanel() 
      { 

       //create JTextField. 
       baked_beans_JT = new JTextField(5); 
       Cornflakes_JT = new JTextField(5); 
       Sugar_JT = new JTextField(5); 
       Tea_Bags_JT = new JTextField(5); 
       Instant_Coffee_JT = new JTextField(5); 
       Bread_JT = new JTextField(5); 
       Sausage_JT = new JTextField(5); 
       egg_JT = new JTextField(5); 
       milk_JT = new JTextField(5); 
       potatoes_JT = new JTextField(5); 


       //initialize text field to 0 
       baked_beans_JT.setText("0"); 
       Cornflakes_JT.setText("0"); 
       Sugar_JT.setText("0"); 
       Tea_Bags_JT.setText("0"); 
       Instant_Coffee_JT.setText("0"); 
       Bread_JT.setText("0"); 
       Sausage_JT.setText("0"); 
       egg_JT.setText("0"); 
       milk_JT.setText("0"); 
       potatoes_JT.setText("0"); 




       //set Layout manager 
       setLayout(new GridLayout(10, 1)); 

       //create border and panel title 

       setBorder(BorderFactory.createTitledBorder("Amount")); 

       //add text fields to the panel. 
       add(baked_beans_JT); 
       add(Cornflakes_JT); 
       add(Sugar_JT); 
       add(Tea_Bags_JT); 
       add(Instant_Coffee_JT); 
       add(Bread_JT); 
       add(Sausage_JT); 
       add(egg_JT); 
       add(milk_JT); 
       add(potatoes_JT); 


      } 

      public double getBeanqty() 
      { 
       try 
      { 
       return Double.parseDouble(baked_beans_JT.getText()); 


      } 

      catch(NumberFormatException ev){ 
       JOptionPane.showMessageDialog(null, "invalid"); 
      } 
       return 0; 

      } 

    } 



invoiceClass 


import java.awt.*; 

import javax.swing.*; 

import java.awt.event.*; 
import java.text.DecimalFormat; 


public class invoiceClass { 
    private Grocery_items items; 
    private quantitypanel qty; // A panel for quantity 



    { 
     double total; // Total charges 

DecimalFormat dollar = new DecimalFormat("#,##0.00"); 


qty = new quantitypanel(); 

items = new Grocery_items(qty); 

     // Calculate the total charges 

     double payment = 0.0; 
     String input; 
    input = JOptionPane.showInputDialog(null, "enter a your payment"); 
    payment = Double.parseDouble(input); 
    total = payment - items.getCharges(); 
     JOptionPane.showMessageDialog(null, "your change is: " + total); 

} 
} 

하십시오에서 정보를 검색하지, 나는 영수증을 만들려고 해요 프로그램.

이 코드 total = payment - items.getCharges();에서 시스템이 총 요금을 받고 사용자가 입력 한 번호에서 빼기를 원합니다. 이 경우 시스템은 지불 만 인식합니다.

도와주세요 ..

감사

+0

시스템에서 items.getCharges()를 인식하지 못한다고 말하면 어떻게됩니까? 컴파일 시간 오류가 발생합니까? 그것은 무엇을 말하는가? –

+0

또한 Java에서 Classes의 명명 규칙은 GroceryItems.java, QuantityPanel.java, GroceryShop.java와 같은 것을 수행해야한다는 점에 유의해야합니다. –

+0

예를 들어 지불 금액이 10 일 경우 시스템은 JOptionpane에 10 개만 인쇄합니다. 청구서를 받고 지불금에서 뺍니다. –

답변

1

items.getCharges() 0을 반환 새로운 invoiceClass 인스턴스가 CalcButtonListenerAction가 발생할 때마다 생성되는 것을 이유. 새로운 Grocery_items 인스턴스가 생성됩니다.

이것은 차례로 확인란을 포함한 새로운 GUI 요소를 만듭니다. 이 확인란은 추가되지 않았으므로 응용 프로그램에서 볼 수있는 확인란이 아닙니다. 당신이 getCharges, 새로 Grocery_items 생성에 JCheckBoxes의 선택 상태를 호출 갈 때 이제 false 그래서 요금이

public double getCharges() { 
    double charges = 0; 

if (baked_beans_box.isSelected()) // now false! 
    charges += Baked_Beans * qty.getBeanqty(); 
if (...) 

솔루션, 따라서 생성되지 않는 추가되지 않습니다 (JCheckBox의 기본 상태)입니다 Grocery_itemsquantitypanel이지만 원본 인스턴스를 사용하십시오.

는 제외 : 사용 자바 명명 규칙 대문자 같은 QuantityPanel로 시작하는 클래스가 있습니다. 밑줄은 일반적으로 사용되지 않습니다. GroceryItems

+0

원본 인스턴스가 무엇을 의미합니까. –

+0

'Grocery_shop '의 생성자에서 생성 된'Grocery_items'의 인스턴스 – Reimeus

관련 문제