2014-02-20 33 views
0

所以我遇到了问题。我正在设计一个UPC程序,它返回有关产品类型/制造商(例如)的条形码的几个部分,并使用方程来检查最后一位数字。这里是我的问题,但:单独的类中的方法 - UPC值

我有一个Driver类,处理输入和输出,以及一个UPC类,所有我的方法存储在。我刚才弄清楚如何调用方法和toString的工作,我我真的很喜欢它。但现在我的问题是基于分配要求输入用户UPC的方式,我必须将数据作为部分字符串收集,然后使用parseInt运行一段代码以将字符串分隔为多个int数字,以便我可以运行它们通过等式。问题是,我在驱动程序课上做了这个,而且我不确定如何在没有屠杀我的代码的情况下继续进行操作。

我试过把在UPC类中使用parseInt的部分,但它似乎并没有正常工作。有什么办法可以将分离的变量从驱动程序类中移到UPC类,并将它们输入到我要创建的公式中?

这里是Driver类: import java.util.Scanner;

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类:

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() 
    { 

    } */ 



} 

有可能是代码的一些无用位在那里我的测试,但除此之外是所有的作品。在此先感谢,我对文本的墙壁感到抱歉。 :)

+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

啊,我不知道我可以有不止一个构造器。我是否需要用一个不同的名字来构造一个constructer,也许是一些名为public UPC2()的东西?对不起,如果这个问题有点愚蠢,我只是现在搞清楚所有的方法和调用它们是如何工作的 – Alzecha

+0

看看:http://www.javabeginner.com/learn-java/java-constructors – bobbel

+0

我'我会试着通读一下,看看它是否能解决问题,谢谢你们两位。 – Alzecha

相关问题