欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  Java

java中应用程序的详细介绍

程序员文章站 2022-04-02 19:42:02
...
伟大软件的简易三步骤:

1.确认你的软件客户要它做的事。

2.运用基本的OO原则来增加软件的灵活性。

3.努力实现可维护、可重用的设计。

项目架构:

java中应用程序的详细介绍

Guitar类:

 1 package com.headfirst.guitar; 2  3 public class Guitar { 4     private String serialNumber, builder, model, type, backWood, topWood; 5     private double price; 6      7     public Guitar(String serialNumber, double price, String builder, 
 8             String model, String type, String backWood, String topWood){ 9         this.serialNumber = serialNumber;10         this.price = price;11         this.builder = builder;12         this.model = model;13         this.type = type;14         this.backWood = backWood;15         this.topWood = topWood;16     }17     18     public String getSerialNumber(){19         return serialNumber;20     }21     public double getPrice(){22         return price;23     }24     public void setPrice(float newPrice){25         this.price = newPrice;26     }27     public String getBuilder(){28         return builder;29     }30     public String getModel(){31         return model;32     }33     public String getType(){34         return type;35     }36     public String getBackWood(){37         return backWood;38     }39     public String getTopWood(){40         return topWood;41     }42 }

Inventory类:

 1 package com.headfirst.guitar; 2  3 import java.util.Iterator; 4 import java.util.LinkedList; 5 import java.util.List; 6  7 public class Inventory { 8      9     private List guitars;10     11     public Inventory(){12         guitars = new LinkedList();13     }14     15     public void addGuitar(String serialNumber, double price, String builder, String model, String type,16             String backWood, String topWood){17         Guitar guitar = new Guitar(serialNumber, price, builder, 
18                 model, type, backWood, topWood);19         guitars.add(guitar);20     }21     22     public Guitar getGuitar(String serialNumber){23         for(Iterator i = guitars.iterator(); i.hasNext();){24             Guitar guitar = (Guitar) i.next();25             if(guitar.getSerialNumber().equals(serialNumber)){26                 return guitar;27             }28         }29         30         return null;31     }32     33     public Guitar search(Guitar searchGuitar){34         for(Iterator i = guitars.iterator(); i.hasNext();){35             Guitar guitar = (Guitar) i.next();36             String builder = searchGuitar.getBuilder();37             if((builder != null) && (!builder.equals("")) && 
38                     (!builder.equals(guitar.getBuilder())))39                     continue;40             41             String model = searchGuitar.getModel();42             if((model != null) && (!model.equals("")) && 
43                     (!model.equals(guitar.getModel())))44                     continue;45             46             String type = searchGuitar.getType();47             if((type != null) && (!type.equals("")) && 
48                     (!type.equals(guitar.getType())))49                     continue;50             51             String backWood = searchGuitar.getBackWood();52             if((backWood != null) && (!backWood.equals("")) && 
53                     (!backWood.equals(guitar.getBackWood())))54                     continue;55             56             String topWood = searchGuitar.getTopWood();57             if((topWood != null) && (!topWood.equals("")) && 
58                     (!topWood.equals(guitar.getTopWood())))59                     continue;60             return guitar;61         }62         63         return null;64     }65 }

搜寻测试代码:

 1 package com.headfirst.guitar; 2  3 public class FindGuitarTester { 4     public static void main(String[] args){ 5         Inventory inventory = new Inventory(); 6         initializeInventory(inventory); 7          8         Guitar whatErinLikes = new Guitar("", 0, "fender", "Stratocastor", "electric", "Alder", "Alder"); 9         10         Guitar guitar = inventory.search(whatErinLikes);11         if(guitar != null){12             System.out.println("Erin, you might like this " + 
13             guitar.getBuilder() + " " + guitar.getModel() + 
14             " " + guitar.getType() + " guitar:\n " + 
15             guitar.getBackWood() + " back and sides,\n " +16             guitar.getTopWood() + " top.\nYou can have it for only $" + 
17             guitar.getPrice() + "!");18         }else{19             System.out.println("Sorry, Erin, we have nothing for you.");20         }21     }22     23     private static void initializeInventory(Inventory inventory){24         inventory.addGuitar("V95693", 1499.95, "Fender", "Stratocastor", "electric", "Alder", "Alder");25     }26 }

运行结果:

java中应用程序的详细介绍

为什么,明明有,为什么说没有?

检查了代码,可能搜寻中“fender”的f是小写,而我们有的“Fender”是大写。

好,我们对吉他搜索工具进行第一个改善,去除掉所有String比较:

项目修改架构:

java中应用程序的详细介绍

Builder.java:

 1 package com.headfirst.guitar; 2  3 public enum Builder { 4      5     FENDER, MARTIN, GIBSON, COLLINGS, OLSON, RYAN, PRS, ANY; 6      7     public String toString(){ 8         switch(this){ 9         case FENDER:10             return "Fender";11         case MARTIN:12             return "Martin";13         case GIBSON:14             return "Gibson";15         case COLLINGS:16             return "Collings";17         case OLSON:18             return "Olson";19         case RYAN:20             return "Ryan";21         case PRS:22             return "Prs";23         case ANY:24             return "Any";25         default: 
26             return "";27         }28     }29 }

Type.java:

 1 package com.headfirst.guitar; 2  3 public enum Type { 4     ACOUSTIC, ELECTRIC; 5      6     public String toString(){ 7         switch(this){ 8         case ACOUSTIC: 
 9             return "acoustic";10         case ELECTRIC: 
11             return "electric";12         default:13             return "";14         }15     }16 }

Wood.java:

 1 package com.headfirst.guitar; 2  3 public enum Wood { 4     INDIAN_ROSEWOOD, BRAZILIAN_ROSEWOOD, ALDER; 5      6     public String toString(){ 7         switch(this){ 8         case INDIAN_ROSEWOOD: 9             return "Indian Rosewood";10         case BRAZILIAN_ROSEWOOD:11             return "Brazilian Rosewood";12         case ALDER:13             return "Alder";14         default:15             return "";16         }17     }18 }

修改FindGuitarTester.java第8行代码:

1 Guitar whatErinLikes = new Guitar("", 0, "Builder.FENDER", "Stratocastor", "Type.ELECTRIC", "Wood.ALDER", "Wood.ALDER");

修改Inventory.java中search()方法:

 1 public Guitar search(Guitar searchGuitar){ 2         for(Iterator i = guitars.iterator(); i.hasNext();){ 3             Guitar guitar = (Guitar) i.next(); 4              5             if(searchGuitar.getBuilder() != guitar.getBuilder()) 6                     continue; 7              8             String model = searchGuitar.getModel().toLowerCase(); 9             if((model != null) && (!model.equals("")) && 
10                     (!model.equals(guitar.getModel().toLowerCase())))11                     continue;12             13             if(searchGuitar.getType() != guitar.getType())14                     continue;15             16             if(searchGuitar.getBackWood() != guitar.getBackWood())17                     continue;18             19             if(searchGuitar.getTopWood() != guitar.getTopWood())20                     continue;21             22             return guitar;23         }24         25         return null;26     }

现在我们担心的是Model属性,我们没有把它做成enum,并且Model没有办法限制,所以做不出enum。

除此之外:我们修改的地方还有:

java中应用程序的详细介绍

各种变量的类型,用enum的全部替换掉。

以上就是java中应用程序的详细介绍的详细内容,更多请关注其它相关文章!