문제

So I've run into an issue. I'm designing a UPC program that returns a few parts of a bar-code's number regarding product type/ manufacturer(etc.), and that uses an equation to check the last digit. Here's my problem though:

I've got a Driver class that handles input and output, and a UPC class that all my methods are stored in. I've just now figured out how calling methods and toString work, and I'm really exited about it. But now my issue is with the way the user's UPC is input based on the assignment requirements, I had to gather the data as part string, then run a section of code using parseInt to separate the string into multiple int digits so I can run them through the equation. Problem is, I did this in the driver class, and I'm not sure how I can proceed without butchering my code.

I've tried putting the section that uses parseInt in the UPC class, but it just doesn't seem to work right. Is there any way I can bring the separated variables over from the driver class to the UPC class and input them into the equation I'm going to make?

Here is the Driver class: 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());  


   }  

}

And here is my UPC class:

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

   } */  



}  

There might be a few useless bits of code in there I was testing, but other than that is all works. Thanks in advance, and I'm sorry for the walls of text. :)

도움이 되었습니까?

해결책

I think you should have 2 constructors in the UPC class. One that accepts a string and another that will accept (int item, int man, int prod, int check);

the constructor that accepts the string will use a scanner to get the numbers out of it and call the other constructor with them. That way everything will be encapsulated in the UPC class.


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

}



라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top