java小程序-----用if for寫會員登陸和商品列表
阿新 • • 發佈:2017-07-01
object span scanner 程序 out 筆記 bre welcome sys
一、父類
1 public class Father{ //父類
2
3 protected static int stock1 = 10; //庫存
4 protected static int stock2 = 12;
5 protected static int stock3 = 15;
6 protected static int stock4 = 21;
7 protected static int stock5 = 9;
8 protected static int stock6 = 15;
9
10 protected static float p1 = 1200; //商品價格
11 protected static float p2 = 89;
12 protected static float p3 = 688;
13 protected static float p4 = 5900;
14 protected static float p5 = 399;
15 protected static float p6 = 2588;
16
17 protected static int integral1=10; //會員積分
18 protected static int integral2=200;
19 protected static int integral3=900;
20 protected static int integral4=3000;
21
22 protected static int id1 = 1; //商品id
23 protected static int id2 = 2;
24 protected static int id3 = 3;
25 protected static int id4 = 4;
26 protected static int id5 = 5;
27 protected static int id6 = 6;
28
29 int vipName; //會員編號
30 String name; //商品名
31 float price; //商品價格
32 int stock; //商品庫存
33 int integral; //會員積分
34 int id; //商品id
35 protected static float sumprice = 0;
36
37
38 public Father(String name,float price,int stock,int id){ //有參構造(商品)
39 this.name = name;
40 this.price = price;
41 this.stock = stock;
42 this.id = id;
43 }
44 public Father(int vipName,int integral){ //有參構造(會員)
45 this.vipName = vipName;
46 this.integral = integral;
47 }
48
49 public Father() {
50 super();
51 }
52
53
54 }
二、會員登陸
import java.util.Scanner;
public class Welcome extends Father{
Scanner land= new Scanner(System.in);
Father vip0 = new Father(0,integral1); //創建對象(會員)
Father vip1 = new Father(1,integral2);
Father vip2 = new Father(2,integral3);
Father vip3 = new Father(3,integral4);
public void vip(Scanner land){
System.out.println("您好 歡迎光臨!\n");
System.out.println("請您先登陸!\n");
System.out.println("請輸入會員編號:");
vipName=land.nextInt();
Father [] vip = new Father[4]; //創建對象數組(會員)
vip[0]=vip0;
vip[1]=vip1;
vip[2]=vip2;
vip[3]=vip3;
//用戶名正確
if(vipName==1||vipName==2||vipName==3||vipName==4||vipName==5||vipName==6){
System.out.println("請輸入密碼:");
String pasW = land.next();
//密碼正確
if(pasW.equals("123")){
System.out.println("登陸成功!\n"+vipName+"號vip,歡迎您!");
System.out.println("您的當前積分為:"+vip[vipName].integral);
}
//密碼錯誤
else{
System.out.println("密碼錯誤0.0,請重新輸入!");
this.vip(land);
}
}
//用戶名錯誤
else{
System.out.println("用戶不存在0.0,請重新輸入!");
this.vip(land);
}
}
public static void main(String[]args){
Scanner land= new Scanner(System.in);
Welcome landVip = new Welcome();
landVip.vip(land);
}
}
三、商品列表及購買
import java.util.Scanner;
public class CommodityList extends Welcome{
Father com1 = new Father("格力空調 ",p1,stock1,id1); //創建對象(商品)
Father com2 = new Father("藍色圓領polo衫(男)",p2,stock2,id2);
Father com3 = new Father("Nice平底運動鞋透氣型",p3,stock3,id3);
Father com4 = new Father("華碩筆記本FL5900 ",p4,stock4,id4);
Father com5 = new Father("adidas黑白戶外雙肩背包",p5,stock5,id5);
Father com6 = new Father("chanel男士香水",p6,stock6,id6);
Father comAll = new Father(); //無參構造對象
Scanner sort= new Scanner(System.in);
public void allCom(){
Object selected=sort.next();
Father [] com = new Father[6]; //商品對象數組
com[0] = com1; //創建對象(商品)
com[1] = com2;
com[2] = com3;
com[3] = com4;
com[4] = com5;
com[5] = com6;
if(selected.equals("all")|selected.equals("0")|selected.equals("所有")){ //所有商品列表
for(int i=0;i<6;i++){ //遍歷打印數組
System.out.print("name:"+com[i].name+" \t");
System.out.print("stock:"+com[i].stock+" \t");
System.out.print("price:"+com[i].price+" \t");
System.out.print("id:"+com[i].id+"\t");
System.out.println();
System.out.println();
}
}
else if(selected.equals("assort")|selected.equals("1")|selected.equals("分類")){//分類商品列表
System.out.println(‘\u2605‘+"數碼電器"+‘\u2605‘);
System.out.print("name:"+com[0].name+" \t");
System.out.print("stock:"+com[0].stock+" \t");
System.out.print("price:"+com[0].price+" \t");
System.out.print("id:"+com[0].id+"\t");
System.out.println();
System.out.println();
System.out.print("name:"+com[3].name+" \t");
System.out.print("stock:"+com[3].stock+" \t");
System.out.print("price:"+com[3].price+" \t");
System.out.print("id:"+com[3].id+"\t");
System.out.println();
System.out.println();
System.out.println(‘\u2605‘+"男士專區"+‘\u2605‘);
System.out.print("name:"+com[1].name+" \t");
System.out.print("stock:"+com[1].stock+" \t");
System.out.print("price:"+com[1].price+" \t");
System.out.print("id:"+com[1].id+"\t");
System.out.println();
System.out.println();
System.out.print("name:"+com[5].name+" \t");
System.out.print("stock:"+com[5].stock+" \t");
System.out.print("price:"+com[5].price+" \t");
System.out.print("id:"+com[5].id+"\t");
System.out.println();
System.out.println();
System.out.println(‘\u2605‘+"戶外運動"+‘\u2605‘);
System.out.print("name:"+com[2].name+" \t");
System.out.print("stock:"+com[2].stock+" \t");
System.out.print("price:"+com[2].price+" \t");
System.out.print("id:"+com[2].id+"\t");
System.out.println();
System.out.println();
System.out.print("name:"+com[4].name+" \t");
System.out.print("stock:"+com[4].stock+" \t");
System.out.print("price:"+com[4].price+" \t");
System.out.print("id:"+com[4].id+"\t");
System.out.println();
System.out.println();
}
else{
System.out.println("輸入錯誤!\n請重新輸入");
this.allCom();
}
}
public void shopping(){
Father [] com = new Father[6]; //商品對象數組
com[0] = com1; //創建對象(商品)
com[1] = com2;
com[2] = com3;
com[3] = com4;
com[4] = com5;
com[5] = com6;
//是否購買判斷
System.out.println("是否購買商品(yes/no/是/否)");
String sure = sort.next();
//購買
if(sure.equals("yes")|sure.equals("是")){
System.out.println("請輸入您選中的商品id");
int shopId = sort.nextInt();
for(int i=0;shopId<=com.length;i++){
comAll.sumprice +=com[shopId-1].price; //商品總價格
--com[shopId-1].stock; //庫存-1
System.out.println("繼續添加?(yes/no)");
String cont = sort.next();
//繼續
if(cont.equals("yes")){
System.out.println("請輸入您選中的商品id");
shopId = sort.nextInt();
}
//不繼續
else if(cont.equals("no")){
System.out.println("謝謝惠顧!您購買的商品總價格為:"+comAll.sumprice+"元!積分增加"+comAll.sumprice/100);
break;
}
//其他回答
else{
System.out.println("不好意思,沒聽清!");
CommodityList Com = new CommodityList(); //創建對象
Com.shopping();
}
}
}
//不購買
else if(sure.equals("no")|sure.equals("否")){
System.out.println("歡迎下次光臨!");
}
//輸入錯誤
else{
System.out.println("不好意思,沒聽清!請您再說一次0.0");
CommodityList Com = new CommodityList(); //創建對象
Com.shopping();
}
}
public static void main(String[]args){
CommodityList Com = new CommodityList(); //創建對象
System.out.println("0 所有商品列表"); //提示信息
System.out.println("1 商品分類列表");
System.out.println("請選擇商品顯示列表(0/1/all/assort/“所有”/“分類”)");//提示輸入信息
Com.allCom();
Com.shopping();
}
}
java小程序-----用if for寫會員登陸和商品列表