在我的Java代码中更好地使用for循环

时间:2017-08-28 12:35:01

标签: java

我的代码也按照我想要的方式工作,但它使用的方式很多。

关于食品订单终端和我向您展示的部分是关于删除 来自购物车的JButtons。

我不想编辑Arraylist中的每一个JButton。我认为循环可以帮到这里,但我不知道该怎么做。

素不相识

public class Bestellterminal { 

private int x = 0;
private double Preis = 0;
private double classicpreis  = 2.5;
private ArrayList<JButton> classiciconlist = new ArrayList<JButton>();

public void addComponentsToPane2(final Container pane) {

for(int i = 0; i <= 50; i++) {

    classiciconlist.get(i).addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            if (x == 0) {
                Preis = Preis - classicpreis;       
                Locale currentlocale = Locale.GERMANY;
                NumberFormat numberFormatter = 
                NumberFormat.getCurrencyInstance(currentlocale);
                String classicpreisx = numberFormatter.format(classicpreis);
                String preisx = numberFormatter.format(Preis);
                labelsumme.setText(String.valueOf("Summe: " + preisx));
                classiciconlist.get(1).setVisible(false);
            x++;
            }           

            else if (x == 1) {
                Preis = Preis - classicpreis;       
                Locale currentlocale = Locale.GERMANY;
                NumberFormat numberFormatter = 
                NumberFormat.getCurrencyInstance(currentlocale);
                String classicpreisx = numberFormatter.format(classicpreis);
                String preisx = numberFormatter.format(Preis);
                labelsumme.setText(String.valueOf("Summe: " + preisx));
                classiciconlist.get(2).setVisible(false);
                x++;
            }           
            else if (x == 2) {
                Preis = Preis - classicpreis;       
                Locale currentlocale = Locale.GERMANY;
                NumberFormat numberFormatter = 
                NumberFormat.getCurrencyInstance(currentlocale);
                String classicpreisx = numberFormatter.format(classicpreis);
                String preisx = numberFormatter.format(Preis);
                labelsumme.setText(String.valueOf("Summe: " + preisx));
                classiciconlist.get(3).setVisible(false);
                x++;
            }     


        }});


    }


}
}    

4 个答案:

答案 0 :(得分:3)

您正在重复您的代码 - 只使用1种方法并将其称为public void myMethod(int i) { Preis = Preis - classicpreis; Locale currentlocale = Locale.GERMANY; NumberFormat numberFormatter = NumberFormat.getCurrencyInstance(currentlocale); String classicpreisx = numberFormatter.format(classicpreis); String preisx = numberFormatter.format(Preis); labelsumme.setText(String.valueOf("Summe: " + preisx)); classiciconlist.get(i + 1).setVisible(false); x++; }

<header class="site-header" role="banner">
   <div class="site-title-bar title-bar" <?php foundationpress_title_bar_responsive_toggle() ?> data-responsive-toggle="example-menu" data-hide-for="large">
      <div class="title-bar-left">
         <button class="menu-icon" type="button" data-toggle="example-menu off-canvas-menu"></button>
      </div>
   </div>
   <div class="mobile-logo">
      <a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home"><img src="<?php echo get_template_directory_uri(); ?>/assets/images/logo.png" alt="Logo" width="HERE" height="HERE" /></a>
   </div>
   <div class="top-bar-container" data-sticky-container>
      <div class="sticky" data-sticky data-options="anchor: page; marginTop: 0; stickyOn: small;" style="width:100%; z-index:2">
         <nav class="site-navigation top-bar topbar-sticky-shrink row columns align-middle" id="example-menu" role="navigation">
            <div class="row align-middle">
               <div class="columns small-12">
                  <?php foundationpress_utility_bar(); ?>
               </div>
            </div>
            <div class="row align-middle main-nav">
               <div class="top-bar-left columns">
                  <div class="site-desktop-title top-bar-title">
                     <a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home"><img src="<?php echo get_template_directory_uri(); ?>/assets/images/logo.png" alt="Logo" width="368" height="61" /></a>
                  </div>
               </div>
               <div class="top-bar-right columns">
                  <?php foundationpress_top_bar_r(); ?>
                  <?php if ( ! get_theme_mod( 'wpt_mobile_menu_layout' ) || get_theme_mod( 'wpt_mobile_menu_layout' ) === 'topbar' ) : ?>
                  <?php get_template_part( 'template-parts/mobile-top-bar' ); ?>
                  <?php endif; ?>
               </div>
            </div>
         </nav>
      </div>
   </div>
   <div class="row align-center cta">
      <div class="columns medium-4 small-12">
         <a href="#" ">
            <div class="button button-primary "><img src="<?php echo get_template_directory_uri(); ?>/assets/images/icon-apply.png" alt="Logo" width="26" height="29" />&nbsp;Apply today
            </div>
         </a>
      </div>
      <div class="columns medium-4 small-12">
         <a href="#" ">
            <div class="button button-secondary "><img src="<?php echo get_template_directory_uri(); ?>/assets/images/icon-tour.png" alt="Logo" width="39" height="29" />&nbsp;Book a tour</div>
         </a>
      </div>
      <div class="columns medium-4 small-12">
         <a href="#" ">
            <div class="button button-tertiary "><img src="<?php echo get_template_directory_uri(); ?>/assets/images/icon-donate.png" alt="Logo" width="35" height="29" />&nbsp;Make a donation</div>
         </a>
      </div>
   </div>
</header>

答案 1 :(得分:1)

如果我没有弄错,if的每个分支之间的唯一区别是classiciconlist.get(x + 1)。如果是这样,这是等效的:

            if (x >= 0 && x <= 2) {
                Preis = Preis - classicpreis;
                Locale currentlocale = Locale.GERMANY;
                NumberFormat numberFormatter = NumberFormat.getCurrencyInstance(currentlocale);
                String classicpreisx = numberFormatter1.format(classicpreis);
                String preisx = numberFormatter.format(Preis);
                labelsumme.setText(String.valueOf("Summe: " + preisx));
                classiciconlist.get(x+1).setVisible(false);
                x++;
            }

答案 2 :(得分:0)

Many lines in the below code is duplicate. Try moving common code out of if/else block and put only conditional code in respective if/else block.

            if (x == 0) {
                Preis = Preis - classicpreis;
                Locale currentlocale = Locale.GERMANY;
                NumberFormat numberFormatter = NumberFormat.getCurrencyInstance(currentlocale);
                String classicpreisx = numberFormatter.format(classicpreis);
                String preisx = numberFormatter.format(Preis);
                labelsumme.setText(String.valueOf("Summe: " + preisx));
                classiciconlist.get(1).setVisible(false);
                x++;
            }

            else if (x == 1) {
                Preis = Preis - classicpreis;
                Locale currentlocale = Locale.GERMANY;
                NumberFormat numberFormatter = NumberFormat.getCurrencyInstance(currentlocale);
                String classicpreisx = numberFormatter.format(classicpreis);
                String preisx = numberFormatter.format(Preis);
                labelsumme.setText(String.valueOf("Summe: " + preisx));
                classiciconlist.get(2).setVisible(false);
                x++;
            } else if (x == 2) {
                Preis = Preis - classicpreis;
                Locale currentlocale = Locale.GERMANY;
                NumberFormat numberFormatter = NumberFormat.getCurrencyInstance(currentlocale);
                String classicpreisx = numberFormatter.format(classicpreis);
                String preisx = numberFormatter.format(Preis);
                labelsumme.setText(String.valueOf("Summe: " + preisx));
                classiciconlist.get(3).setVisible(false);
                x++;
            }

This code should be good

        public void actionPerformed(ActionEvent e) {
            Preis = Preis - classicpreis;
            Locale currentlocale = Locale.GERMANY;
            NumberFormat numberFormatter = NumberFormat.getCurrencyInstance(currentlocale);
            String classicpreisx = numberFormatter.format(classicpreis);
            String preisx = numberFormatter.format(Preis);
            labelsumme.setText(String.valueOf("Summe: " + preisx));
            classiciconlist.get(i+1).setVisible(false);
            x++;
        }

答案 3 :(得分:0)

你有一个巨大的if-else-if_else可以真正缩短为:

if (x == 0) {
    classiciconlist.get(1).setVisible(false);
} else if (x == 1) {
    classiciconlist.get(2).setVisible(false);
} else if (x == 2) {
    classiciconlist.get(3).setVisible(false);
}

其他一切都是一样的,但变量的另一个名称......

外观:

//Preis is calcualted with the same formula no matter the value of x
Preis -= classicpreis;
//Locale uis allways German
Locale currentlocale1 = Locale.GERMANY;
//Number is the same(all for German Locale)
NumberFormat numberFormatter1 = NumberFormat.getCurrencyInstance(currentlocale1);
//same  here
String classicpreisx1 = numberFormatter1.format(classicpreis);
//und here
String preisx1 = numberFormatter1.format(Preis);
//und  here
labelsumme.setText(String.valueOf("Summe: " + preisx1));
//x variable is getting incremented no matther what....
x++;