2016-04-14 27 views
0

Dizilimimi kullanıcı girdisiyle doldurmaya çalışıyorum; Ancak, düzgün çalışması için onu alamıyorum. Girmek istedikleri ürün sayısı için kullanıcı için bir int almaya çalışıyorum. Ardından diziyi girmek istediklerini söylediği ürün sayısına göre fiili ürünlerin (Strings) daha fazla kullanıcı girdisiyle doldurmak istiyorum. Bu benim kodudur: ben tam yapmanız girişiminde anlamak olmayabilirArrayList yöntemini kullanıcı girdisi ile doldurmaya çalışmak

import java.util.Scanner; 
import java.util.ArrayList; 

public class Practice { 


    public static void main(String[] args) { 
     bannerPrinter(); 
     getNum(); 
     int num = 0; 
     ArrayList<String> products = productBuilder(num); 
     boolean productGuess = getOrder(products); 
     if (productGuess) { 
      double price = getPrice(); 
      double tax = getTax(price); 
      double total = getTotal(tax, price); 
      printTotal(total); 
     } 
     else { 
      System.out.print("Product not found."); 
     } 
    } 

    public static void bannerPrinter() { 

     System.out.println(); 
     System.out.println("******************************************"); 
     System.out.println("****** Welcome to my eCommerce app! ******"); 
     System.out.println("******************************************"); 
     System.out.println(); 
    } 

    public static int getNum() { 

     Scanner scnr = new Scanner(System.in); 
     int num = 0; 

     System.out.print("Enter the number of products: "); 
     num = scnr.nextInt(); 

     return num; 
    } 

    public static ArrayList<String> productBuilder(int num) { 

     Scanner scnr = new Scanner(System.in); 
     String arrayNames = ""; 
     ArrayList<String> products = new ArrayList(); 

     for (int i = 0; i <= num; i++) { 
      System.out.print("Enter the products: "); 
      products.add(arrayNames); 
     } 
     return products; 
    } 

    public static boolean getOrder(ArrayList<String> products) { 

     Scanner scnr = new Scanner(System.in); 
     String guess = ""; 

     boolean productName = products.contains(guess); 

     System.out.print("Enter a product: "); 
     guess = scnr.nextLine(); 

     if(productName) { 
      System.out.println(); 
      System.out.print("This product has been found."); 
      System.out.println(); 
     } 
     else { 
      System.out.println(); 
     } 

     return productName; 
    } 

    public static double getPrice() { 

     double price = 0.0; 
     price = (int)(Math.random() * 100); 

     return price; 
    } 

    public static double getTax(double price) { 

     double tax = 0.0; 
     tax = price * 0.10; 

     return tax; 
    } 

    public static double getTotal(double price, double tax) { 

     double saleTotal = 0.0; 
     saleTotal = price + tax; 

     return saleTotal; 
    } 

    public static void printTotal(double saleTotal) { 

     System.out.println("You total is $" + saleTotal + "0"); 
    } 

} 
+3

benim Üniversitede ... İlginç *, Şu anda e-ticaret ile ilgili dönem projeleri var. Bu size benziyorsa, birbirimizi tanıyıp tanımadığımızı merak ediyorum. * – Vallentin

+0

Kullanıcının girişlerini aldıktan sonra ne yapmak istediğinizle ilgili net değil. Lütfen bir örnek ver. – VHS

+0

Hata ayıklama yardımını arayan sorular ("neden bu kod çalışmıyor?") Istenen davranışı, belirli bir sorunu veya hatayı ve sorunun kendisinde çoğaltmak için gereken en kısa kodu içermelidir. Açık bir problem bildirimi olmayan sorular diğer okurlar için yararlı değildir. Bakınız: Minimal, Tam ve Doğrulanabilir bir örnek nasıl oluşturulur? – intboolstring

cevap

0

ama açıklamalarım

bkz ... bazı ilginç satırları gördün
public static ArrayList<String> productBuilder(int num) { 

    Scanner scnr = new Scanner(System.in); 
    String arrayNames = ""; 
    ArrayList<String> products = new ArrayList(); 

    for (int i = 0; i <= num; i++) { 
     System.out.print("Enter the products: "); 
     // I assume here scnr is supposed to read something 
     // and assign it to arrayNames? 
     products.add(arrayNames); 
    } 
    return products; 
} 

public static boolean getOrder(ArrayList<String> products) { 

    Scanner scnr = new Scanner(System.in); 
    String guess = ""; 

    // I assume this line is supposed to be after 
    // guess = scnr.nextLine()? 
    boolean productName = products.contains(guess); // very confusing line 

    System.out.print("Enter a product: "); 
    guess = scnr.nextLine(); 

    if(productName) { 
     System.out.println(); 
     System.out.print("This product has been found."); 
     System.out.println(); 
    } 
    else { 
     System.out.println(); 
    } 

    return productName; 
}