這是給課程的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("");
}
}
謝謝@kRiz的編輯。 – DDNPresident