Home > Java > javaTutorial > body text

Detailed introduction to applications in java

零下一度
Release: 2017-07-24 16:40:39
Original
1710 people have browsed it

Three easy steps to great software:

1. Confirm what your software customers want it to do.

2. Use basic OO principles to increase software flexibility.

3. Strive to achieve maintainable and reusable designs.

Project structure:

Guitar class:

 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 }
Copy after login

Inventory class:

 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 }
Copy after login

Search test code:

 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 }
Copy after login

Run result:

Why, obviously there is, why does it say no?

Checked the code, maybe the f in "fender" in the search is lowercase, and the "Fender" we have is uppercase.

Okay, let’s make the first improvement to the guitar search tool and remove all String comparisons:

Project modification structure:

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 }
Copy after login

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 }
Copy after login

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 }
Copy after login

Modify the 8th line of code in FindGuitarTester.java:

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

Modify the search() method in Inventory.java:

 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     }
Copy after login

What we are worried about now is the Model attribute. We did not make it an enum, and there is no way to limit the Model, so we cannot make an enum.

In addition: We have modified the following:

Replace all types of various variables with enum.

The above is the detailed content of Detailed introduction to applications in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!