2015-06-26 81 views
3

這是給課程的HW分配,我相信我所有的東西都能正常工作,但我在顯示輸出中需要的所有信息時遇到了問題。如果你看看TestCashier.java,你可以看到我的預期輸出。我需要知道如何在窗格上顯示generateReceipt()average()makeChange()tendered()。任何幫助都將不勝感激。我一直在這個部分停留了很長時間。JOptionPane,JTextArea和JScrollPane,顯示輸出

如果有人想要幫助我在add()上採取額外的步驟,並找出爲什麼在向用戶詢問每件物品價格時無法使用該功能。我需要總共使用add(),但我只能讓我的程序正常工作,當我手動將它們添加到一起並將該值分配給totalSum。感謝您花時間閱讀本文,並感謝所有的輸入/反饋。 (即使批評......我在這裏學習)

Cashier.java

package cashier; 
import java.text.NumberFormat; 

public class Cashier { 
    static int numItems; 
    static double totalSum,averagePrice,price; 
    private int pennies,nickles, dimes, quarters, dollars; 
    private double tendered,change; 

    public void Cashier(){ 
     this.numItems = 0; 
     this.totalSum = 0; 
    } 

    public void average(){ 
     averagePrice = totalSum/numItems; 
     NumberFormat nf1 = NumberFormat.getCurrencyInstance(); 
     System.out.println("The average price per item is "+ nf1.format(averagePrice));//Must format 

    } 
     public void add(String name, Double price) { 
     numItems++; 
     //totalSum =+price; 
    } 


    public void tendered(double t) { 
     this.tendered = t; 
     this.change = tendered - totalSum; 
     NumberFormat df1 = NumberFormat.getCurrencyInstance(); 
     System.out.println("Amount tendered is " + df1.format(tendered)); 
    } 

    void makeChange(Cashier c){ 

     change = (tendered-totalSum); 
     change =change*100; 
     NumberFormat df = NumberFormat.getCurrencyInstance(); 
     System.out.println("The change is: " + df.format(change/100)+"\n");//Must format 


     dollars = (int)(change/100); 
     change %= 100; 


     quarters = (int) (change/25); 
     change %=25; 


     dimes = (int) (change/10); 
     change = change%10; 


     nickles = (int) (change/5); 
     change = change%5; 

     pennies = (int)change; 

     System.out.println("The change includes..."); 
     System.out.println(dollars+" dollars"); 
     System.out.println(quarters+" quarters"); 
     System.out.println(dimes+" dimes"); 
     System.out.println(nickles+" nickles"); 
     System.out.println(pennies+" pennies"); 

    } 






} 

GetData.java

package cashier; 


import javax.swing.JOptionPane; 
import java.text.NumberFormat; 


public class GetData { 

    static double getDouble(String c){ 
     return Double.parseDouble(getWord(c)); 
    } 

    static String getWord(String c){ 
     return JOptionPane.showInputDialog(c); 
    } 


} 

TestCashier.java

package cashier; 
import javax.swing.JOptionPane; 
import javax.swing.JTextArea; 
import javax.swing.JScrollPane; 



import static cashier.Cashier.totalSum; 
import java.text.DateFormat; 
import java.text.NumberFormat; 
import java.util.Date; 

public class TestCashier{ 

    public static void main(String[]arg){ 
     NumberFormat nf = NumberFormat.getCurrencyInstance(); 
     Cashier c = new Cashier(); 

     String name = GetData.getWord(" Enter name of first item"); 
     double price = GetData.getDouble("Enter price of item"); 
     c.add(name,price); 


     String name2 = GetData.getWord(" Enter name of second item"); 
     double price2 = GetData.getDouble("Enter price of item"); 
     c.add(name2,price2); 

     String name3 = GetData.getWord(" Enter name of third item"); 
     Double price3 = GetData.getDouble("Enter price of item"); 
     c.add(name3,price3); 

     String name4 = GetData.getWord(" Enter name of fourth item"); 
     Double price4 = GetData.getDouble("Enter price of item"); 
     c.add(name4,price4); 

     totalSum = price+price2+price3+price4; 



     //make payment 
     double tendered = GetData.getDouble("Enter amount of money for payment"); 



     generateReceipt(c); 
     NumberFormat nf1 = NumberFormat.getCurrencyInstance(); 
     String s = (name+"\t\t"+nf1.format(price)); 
     s = s +("\n"+name2+"\t\t"+nf1.format(price2)); 
     s = s +("\n"+name3+"\t\t"+nf1.format(price3)); 
     s = s +("\n"+name4+"\t\t"+nf1.format(price4)); 
     s = s +("\n"+"_______________________________"); 
     s = s +("\n"+"Total:\t\t"+nf1.format(totalSum)); 


     s = s +("\n\n"+"The number of items purchased is "+Cashier.numItems+" item(s)"); 


     c.tendered(tendered); 
     c.makeChange(c); 

     JTextArea text = new JTextArea(s,30,30); 
     JScrollPane pane = new JScrollPane(text); 
     JOptionPane.showMessageDialog(null, pane,"THE RIP-OFF STORE",JOptionPane.PLAIN_MESSAGE); 



    } 
    static void generateReceipt(Cashier c){ 

     Date current = new Date(); 
     DateFormat df = DateFormat.getDateInstance(DateFormat.FULL); 
     System.out.println("WELCOME TO THE RIP-OFF STORE!"); 
     System.out.println("Home of the 'not so good' deals..."); 
     System.out.println("Thank you for stopping by on "+ df.format(current)); 
     System.out.println(""); 


    } 
} 
+0

謝謝@kRiz的編輯。 – DDNPresident

回答

0

你需要改變你的函數連接到一個字符串並返回字符串。在你的函數generateReceipt(),編輯像這樣:

static String generateReceipt(Cashier c){ 
    String s = ""; 
    Date current = new Date(); 
    DateFormat df = DateFormat.getDateInstance(DateFormat.FULL); 
    s += ("WELCOME TO THE RIP-OFF STORE!\n"); 
    s += ("Home of the 'not so good' deals...\n"); 
    s += ("Thank you for stopping by on "+ df.format(current)) + "\n\n"; 
    return s; 
} 

,並得到該功能的信息,只需調用它像這樣:

String s = generateReceipt(c);

這同樣適用於所有的功能,你需要輸出從你的Cashier.java類。我不會告訴你每個功能,因爲每個功能都是一樣的。記住它會被稱爲像這樣:

s += c.average();

這是所有從TestCashier的字符串連接的,我怎麼會有它:

String s = generateReceipt(c); 
    NumberFormat nf1 = NumberFormat.getCurrencyInstance(); 
    s += (name+"\t\t"+nf1.format(price)); 
    s = s +("\n"+name2+"\t\t"+nf1.format(price2)); 
    s = s +("\n"+name3+"\t\t"+nf1.format(price3)); 
    s = s +("\n"+name4+"\t\t"+nf1.format(price4)); 
    s = s +("\n"+"_______________________________"); 
    s = s +("\n"+"Total:\t\t"+nf1.format(totalSum)); 


    s = s +("\n\n"+"The number of items purchased is "+Cashier.numItems+" item(s)"); 

    s += c.average(); 
    s += c.tendered(tendered); 
    s += c.makeChange(c); 

    JTextArea text = new JTextArea(s,30,30); 
    JScrollPane pane = new JScrollPane(text); 
    JOptionPane.showMessageDialog(null, pane,"THE RIP-OFF STORE",JOptionPane.PLAIN_MESSAGE); 

我沒有看過你的add()函數但我也很高興。與我聊天,如果你需要任何幫助。

**這裏是一個imgur鏈接到輸出的樣子在我的IDE(IntelliJ IDEA的): http://imgur.com/gkG01Oc

編輯:

你幾乎有你添加的功能是正確的。在Cashier.java,你add()曾發表聲明註釋掉:

totalSum =+ price;只需切換運營商totalSum += price;

創建像getTotalSum()或類似的規定吸氣,因爲你總是想使用的,而不是直接訪問類變量的getter和setter (至少在大多數情況下)。在您的TestCashier.java中調用它,如totalSum = c.getTotalSum();,它應該正常工作。請注意您如何將name作爲您的add()函數的參數。這是爲什麼?如果您不需要或使用它,請將其廢棄。