2014-02-20 3 views
0

그래서 문제가 발생했습니다. 저는 제품 유형/제조업체 (기타)에 관한 바코드 번호의 일부를 반환하는 UPC 프로그램을 설계하고 있으며 마지막 숫자를 확인하는 방정식을 사용합니다. 여기에 내 문제는 :별도의 클래스에있는 메소드 - UPC 값

입력 및 출력, 및 내 모든 메서드가 저장되어있는 UPC 클래스를 처리하는 드라이버 클래스가 있어요. 지금 방금 메서드 및 toString 작업 호출 방법을 알아 냈습니다 및 나는 나는 그것에 관해 정말로 나왔다. 하지만 이제는 사용자의 UPC가 할당 요구 사항을 기반으로 입력되는 방식으로 데이터를 부분 문자열로 수집 한 다음 parseInt를 사용하여 문자열을 여러 int 숫자로 분리하여 실행할 수 있도록 코드 섹션을 실행해야했습니다. 방정식을 통해. 문제는 드라이버 클래스에서이 작업을 수행했기 때문에 코드를 도용하지 않고 어떻게 진행할 수 있는지 잘 모르겠습니다.

parseInt를 사용하는 섹션을 UPC 클래스에 넣으려고했으나 올바르게 작동하지 않는 것 같습니다. 드라이버 클래스에서 UPC 클래스로 분리 된 변수를 가져올 수있는 방법이 있습니까?

다음은 Driver 클래스입니다. import java.util.Scanner;

public class UPC 
{ 

// Instance variables 
    private int itemType;  // digit 1 

    private int manufacturer; // digits 2,3,4,5,6 
    private int product;  // digits 7,8,9,10,11 

    private int checkDigit; // digit 12 

// Constructer method: Initialize instance variables 
    public UPC(int item, int man, int prod, int check) 
    { 
     itemType = item; 
     manufacturer = man; 
     product = prod; 
     checkDigit = check; 
    } 

// Return the UPC code with "-" inserted between the different secions of the barcode 
    public String toString() 
    { 
     return itemType + "-" + manufacturer + "-" + product + "-" + checkDigit; 
    } 

// Return the UPC's item type (First digit) 
    public int getItemType() 
    { 
     return itemType; 
    } 

// Return the manufacturer code (Digits 2-6) 
    public int getManufacturer() 
    { 
     manufacturer = man 
     return manufacturer; 
    } 

// Return the product code (Digits 7-11) 
    public int getProduct() 
    { 
     return product; 
    } 

// Return the check digit (Digit 12) 
    public int getCheckDigit() 
    { 
     return checkDigit; 
    } 

/* Calculate and return the calculated check digit (Equation) 
    public int getCalculatedCheckDigit() 
    { 

    } */ 



} 

내가 테스트했지만, 그 이외의 모든 작품입니다 거기에 코드 몇 쓸모 비트가있을 수 있습니다 : 여기

public class Driver 
{ 

    public static void main (String[] args) 
    { 

    // Create a Scanner object 
     Scanner keyboard = new Scanner(System.in); 



//------------------------------Variables---------------------------------------------- 

    // Declare variables for UPC code (Digits 1-12) 
     int d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12; 


    // Declare character variables for string to individual digit conversion (d2-d11) 
     char c2, c3, c4, c5, c6, c7, c8, c9, c10, c11; 

    // User's entered UPC (Input is d1, manufacturer, product and d12) 
     UPC userUPC; 



//-------------------------------User Input------------------------------------------- 

    // Prompt and get user input for all 12 digits of UPC code 
     System.out.println("Enter all twelve digits of your UPC code."); 
     System.out.println("Please enter in this format: * ***** ***** *."); 

     d1 = keyboard.nextInt(); 

     String manuString = keyboard.next(); 
     String prodString = keyboard.next(); 

     d12 = keyboard.nextInt(); 


//-------------------------------String to Int conversion----------------------------- 

    // Convert user input strings to digits (Int) 
     int manuInt = Integer.parseInt(manuString); 
     int prodInt = Integer.parseInt(prodString); 

     c2 = manuString.charAt(0); 
      d2 = Character.getNumericValue(c2); 
     c3 = manuString.charAt(1); 
      d3 = Character.getNumericValue(c3); 
     c4 = manuString.charAt(2); 
      d4 = Character.getNumericValue(c4); 
     c5 = manuString.charAt(3); 
      d5 = Character.getNumericValue(c5); 
     c6 = manuString.charAt(4); 
      d6 = Character.getNumericValue(c6); 

     c7 = prodString.charAt(0); 
      d7 = Character.getNumericValue(c7); 
     c8 = prodString.charAt(1); 
      d8 = Character.getNumericValue(c8); 
     c9 = prodString.charAt(2); 
      d9 = Character.getNumericValue(c9); 
     c10 = prodString.charAt(3); 
      d10 = Character.getNumericValue(c10); 
     c11 = prodString.charAt(4); 
      d11 = Character.getNumericValue(c11); 



//-----------------------------------UPC---------------------------------------------- 

    // Create user UPC 
     userUPC = new UPC(d1, manuInt, prodInt, d12); 

     resultUPC = new UPC 


//----------------------------Program Output------------------------------------------ 



    // Output data 
     System.out.println("The item's UPC value is: " + userUPC.toString()); 
     System.out.println(); 

     System.out.println("Product Type: " + userUPC.getItemType()); 
     System.out.println("Manufacturer: " + userUPC.getManufacturer()); 
     System.out.println("Product: " + userUPC.getProduct()); 
     System.out.println(); 

     System.out.println("Calculated Check Digit: " /*+ userUPC.getCalculatedCheck()*/); 
     System.out.println("UPC Check Digit: " + userUPC.getCheckDigit()); 


    } 

} 

그리고 내 UPC 클래스입니다. 미리 감사드립니다. 그리고 텍스트의 벽에 유감입니다. :)

+0

어떤 오류가 발생합니까? 당신이하려고 한 것의 진짜 문제는 무엇입니까? – bobbel

+0

그것은 지금은 잘 작동하지만 UPC 클래스 파일에서 d2-d11 정수를 사용해야하고 문자열에서 개별 int 변수로 사용자 입력을 변경하는 코드 섹션을 이동하려고 시도했을 때 UPC 클래스는 모든 문자열을 int로 변환 할 때 작은 오류를 줄 것이라고 말합니다. 지금 내가 묻는 것은 드라이버 클래스에있는 동안 UPC 클래스에서 d2-d11 변수를 사용할 수있는 방법이 있는지 여부입니다. – Alzecha

답변

0

저는 UPC 클래스에 2 개의 생성자가 있어야한다고 생각합니다. 하나는 문자열을 허용하고 다른 하나는 받아 들일 것입니다 (int item, int man, int prod, int check);

문자열을 받아들이는 생성자는 스캐너를 사용하여 숫자를 가져 와서 다른 생성자를 호출합니다. 그렇게하면 모든 것이 UPC 클래스에 캡슐화됩니다.


import java.util.Scanner; 


public class Driver { 

    public static void main(String[] args) { 
     // Create a new scanner to help you get information from the input 
     // stream. 
     Scanner scanner = new Scanner(System.in); 

     // Prompt and get user input for all 12 digits of UPC code 
     System.out.println("Enter all twelve digits of your UPC code."); 
     System.out.println("Please enter in this format: * ***** ***** *."); 

     // get the next input 
     String input = scanner.nextLine(); 

     Upc userUpc = new Upc(input); 

     // Output data 
     System.out.println("The item's UPC value is: " + userUpc.toString()); 
     System.out.println(); 

     System.out.println("Product Type: " + userUpc.getItemType()); 
     System.out.println("Manufacturer: " + userUpc.getManufacturer()); 
     System.out.println("Product: " + userUpc.getProduct()); 
     System.out.println(); 

     System.out.println("Calculated Check Digit: " //+ userUPC.getCalculatedCheck() 
       ); 
     System.out.println("UPC Check Digit: " + userUpc.getCheckDigit()); 

     scanner.close(); 
    } 

} 

class Upc { 

    // Instance variables 
    private int itemType; // digit 1 

    private int manufacturer; // digits 2,3,4,5,6 
    private int product; // digits 7,8,9,10,11 

    private int checkDigit; // digit 12 

    // constructor that accepts String 
    public Upc(String upcString) { 
     //Scanner for the given input that will help us get data from it 
     Scanner scanner = new Scanner(upcString); 

     itemType = scanner.nextInt(); //get item type 
     manufacturer = scanner.nextInt(); //get manufactor 
     product = scanner.nextInt(); //get product 
     checkDigit = scanner.nextInt(); //get chackDigit 

     setVariables(itemType, manufacturer, product, checkDigit); 

     scanner.close(); 
    } 

    public Upc(int item, int man, int prod, int check) 
    { 
     setVariables(item, man, prod, check); 
    } 

    private void setVariables(int item, int man, int prod, int check) 
    { 
     this.itemType = item; 
     this.manufacturer = man; 
     this.product = prod; 
     this.checkDigit = check; 
    } 

    // Return the UPC code with "-" inserted between the different secions of 
    // the barcode 
    public String toString() { 
     return itemType + "-" + manufacturer + "-" + product + "-" + checkDigit; 
    } 

    // Return the UPC's item type (First digit) 
    public int getItemType() { 
     return itemType; 
    } 

    // Return the manufacturer code (Digits 2-6) 
    public int getManufacturer() { 
     return manufacturer; 
    } 

    // Return the product code (Digits 7-11) 
    public int getProduct() { 
     return product; 
    } 

    // Return the check digit (Digit 12) 
    public int getCheckDigit() { 
     return checkDigit; 
    } 

    // Calculate and return the calculated check digit (Equation) 
    // public int getCalculatedCheckDigit() { 
    // 
    // } 

} 



+0

아, 나는 두 명 이상의 컨스트럭터를 가질 수 있다는 것을 몰랐다. 나는 다른 이름을 가진 생성자를 만들 필요가있을뿐, 공개 UPC2() 또는 무언가라는 제목을 붙일 수 있습니까? 미안, 질문이 조금 바보라면, 나는 지금 모든 mthods가 어떻게 작동 하는지를 알아 내고있다. – Alzecha

+0

다음을 보라 : http://www.javabeginner.com/learn-java/java-constructors – bobbel

+0

나는 ' 두 분 모두 덕분에 문제를 해결할 수 있는지 살펴 보겠습니다. – Alzecha

관련 문제