2016-07-12 2 views
-1

계산원을 만들고 testcashier를 만들고 간단한 계산원 연산을 모방 한 데이터 Java 클래스를 가져 오도록 요청 받았습니다. 모든 수업을 만들고 변경 작업을하고 표시하는 것을 포함하여 모든 항목을 만들었지 만 항목 이름과 가격은 표시되지 않습니다. 이유가 무엇입니까?계산원 클래스를 만들었지 만 표시 할 각 항목의 이름과 가격을 알 수 없습니다.

 public class Cashier { 
     private int numberOfItems; 
     private double totalSum; 
     private double amount; 
     private String names; 
     private String s, name; 
     private double price, tendered, change, dollars, quarters, dimes, 
     nickels, pennies; 

     NumberFormat nf = NumberFormat.getInstance(); 
     DecimalFormat df = (DecimalFormat)nf; 

     public Cashier(){ 
      this.name = ""; 
      this.price = price; 
      price = 0; 
      this.s = ""; 
     } 
     public Cashier(String name, double price, String s) 
     { 
      //this.tendered = 0; 
      this.name= name; 
      this.price = price; 
      //amount = tendered; 
      //price = 0; 
      //this.s = s; 

      } 


      public double average() 
      { 
       return totalSum/numberOfItems; 
      } 
      public void add(String name, double price) 
      { 
      totalSum = totalSum + price; 
      s = s + name + "........" + price + "\n"; 
      numberOfItems++; 
       } 

      public void makeChange() 
      { 

      change = amount - totalSum; 
      change = 100 * change; 
      change = Math.round(change); 
      change = change/100; 
      dollars = (int)(amount - totalSum) * 100/100; 
      pennies = (int)(change * 100) % 100; 
      quarters = (int)pennies/25; 
      pennies = (int)pennies % 25; 
      dimes = (int)pennies/10; 
      pennies = (int)pennies % 10; 
      nickels = (int)pennies/5; 
      pennies = (int)pennies % 5; 
      pennies = (int)pennies; 
       } 
      public String getNames() 
      { 
       return name; 
       } 
        public double getPrices() 
        { 
         return price; 
          } 
       public double getTotal() 
        { 
         return totalSum; 
           } 
          public double getMoney() 
           { 
          return tendered; 
          } 
          public double getChange() 
          { 
         return tendered - totalSum ; 
            } 
          public double getQuantity() 
           { 
       return numberOfItems; 
       } 
        public double getAverage() 
          { 
         return average(); 
          } 
       public double getDollars() 
        { 
        return dollars; 
          } 
         public double getQuarters() 
{ 
return quarters; 
} 
public double getDimes() 
{ 
return dimes; 
} 
public double getNickels() 
{ 
return nickels; 
} 
public double getPennies() 
{ 
return pennies; 
} 

public void tendered(double amount) 
{ 
// double tendered; 
    tendered = amount; 
} 

} 



public class TestCashier { 


public static void main(String[] args) { 

Cashier c = new Cashier(); 

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

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

// Add a two more entries of your own 

// Now average the price of the items 
c.average(); 

// Make payment 
double amount = GetData.getDouble("Enter amount of money for payment"); 
c.tendered(amount); 
//ndered(amount); // Twenty dollars were tendered 

c.makeChange(); 

generateReceipt(c); 

} 
public static void generateReceipt(Cashier c) 
{ 

String s= "ABC Groceries Shop \n"; 

s = s + "Welcome – thanks for stopping, \n"; 

DateFormat df = DateFormat.getDateInstance(); 

Date d = new Date(); 

NumberFormat f = NumberFormat.getCurrencyInstance(); 

s = s + "Today is " + df.format(d) + "\n"; 

s = s + "Item:" +(c.getNames()); 
     //\n"; 


s = s + c.getNames() + "..... " + f.format(c.getPrices()) + "\n" + c.getNames() + 
     "..... " + f.format(c.getPrices()) + "\n" + c.getNames() + "....." + 
     f.format(c.getPrices()) + "\n"; 

s = s + "____________________" + "\n"; 

s = s + "Total " + f.format(c.getTotal()) + "\n\n"; 

s = s + "Amount tendered " + f.format(c.getMoney()) + "\n"; 

s = s + "The change is " + f.format(c.getChange()) + "\n"; 

s = s + "There were " + c.getQuantity() + " items" + "\n"; 
s = s + "The average price is " + f.format(c.getAverage()) + "\n\n"; 

s = s + "The change includes :" + "\n"; 

s = s + c.getDollars() + " dollars" + "\n" + c.getQuarters()+ " quarters" + 
     "\n" + c.getDimes()+ " dimes" + "\n" + c.getNickels()+ " nickels" + 
     "\n" + c.getPennies() + " cents"; 

JTextArea text = new JTextArea(s,15, 25); 

JScrollPane pane = new JScrollPane(text); 

JOptionPane.showMessageDialog(null,pane, "Your bill", 
     JOptionPane.INFORMATION_MESSAGE); 

} 


} 




public class GetData { 
    static String str; 
static double getDouble(String s) 
{ 
str = JOptionPane.showInputDialog(s); 
return Double.parseDouble(str); 
} 
static int getInt(String s) 
{ 
str = JOptionPane.showInputDialog(s); 
return Integer.parseInt(str); 
} 
static String getWord(String s) 
{ 
//str = JOptionPane.showInputDialog(s); 
return JOptionPane.showInputDialog(s); 
} 


} 

답변

1

귀하의 public void add(String name, double price) 방법은 Cashier 클래스의 s 회원의 이름과 가격을 연결,하지만 당신은 빈 남아 멤버를 반환 getNames()getPrices() 방법의 출력을 인쇄 할 수 있습니다.

원하는 출력을 얻는 한 가지 방법은 s을 반환하는 메서드를 호출하여 인쇄하는 것입니다.

+0

그래서 내가 반환해야하지

Cashier c = new Cashier (name, price, ""); 

에 같이 생성자

public Cashier(String name, double price, String s) 

를 사용하여 Cashier를 인스턴스화 할 필요가 이름이나 가격 대신에? @my so confusedddd – hhhh

+0

@mariaandreaerramouspe는'getNamesAndPrices()'를 한 번 호출하여'getNames()'와'getPrices()'에 대한 모든 호출을 바꿉니다. – Eran

+0

여전히 작동하지 않습니다. ugrrrrrrrrrrrrrrrr – hhhh

1

당신은 필드의 값을 설정하지 않습니다

c.add (name, price);